improved a lot of things
This commit is contained in:
@@ -11,12 +11,6 @@ export interface CanvasArtboard {
|
||||
renderUrl?: string;
|
||||
}
|
||||
|
||||
const DEMO_ARTBOARDS: CanvasArtboard[] = [
|
||||
{ id: 'dashboard-card', label: 'DashboardCard', x: 120, y: 100, width: 280, height: 200 },
|
||||
{ id: 'user-profile', label: 'UserProfile', x: 460, y: 100, width: 200, height: 260 },
|
||||
{ id: 'nav-sidebar', label: 'NavSidebar', x: 120, y: 360, width: 200, height: 340 },
|
||||
{ id: 'data-table', label: 'DataTable', x: 380, y: 380, width: 420, height: 280 },
|
||||
];
|
||||
|
||||
function toCanvasArtboard(ab: Artboard): CanvasArtboard | null {
|
||||
const meta = ab.metadata_jsonb;
|
||||
@@ -45,6 +39,23 @@ async function fetchArtboards(workspaceId: string, projectId?: string): Promise<
|
||||
return { rows, canvas: rows.map(toCanvasArtboard).filter((ab): ab is CanvasArtboard => ab !== null) };
|
||||
}
|
||||
|
||||
/** PATCH an artboard (name and/or metadata_jsonb). */
|
||||
export async function patchArtboard(
|
||||
id: string,
|
||||
patch: { name?: string; metadata_jsonb?: Record<string, unknown> },
|
||||
): Promise<Artboard> {
|
||||
const res = await fetch(`/api/artboards/${encodeURIComponent(id)}`, {
|
||||
method: 'PATCH',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(patch),
|
||||
});
|
||||
if (!res.ok) {
|
||||
const err = await res.json().catch(() => ({}));
|
||||
throw new Error((err as { error?: string }).error ?? `Patch failed: ${res.status}`);
|
||||
}
|
||||
return res.json() as Promise<Artboard>;
|
||||
}
|
||||
|
||||
/** Create a new artboard via POST /api/artboards and invalidate the cache. */
|
||||
export async function createArtboardMutation(
|
||||
body: InsertArtboard,
|
||||
@@ -70,8 +81,7 @@ export function useArtboards(workspaceId: string | undefined, projectId?: string
|
||||
});
|
||||
|
||||
const canvasArtboards = query.data?.canvas ?? [];
|
||||
// Show demo artboards while loading or when workspace/project has no artboards yet.
|
||||
const artboards = canvasArtboards.length === 0 ? DEMO_ARTBOARDS : canvasArtboards;
|
||||
const artboards = canvasArtboards;
|
||||
|
||||
return {
|
||||
artboards,
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
|
||||
import type { IntentDiff, InsertIntentDiff, DiffStatus } from '@originmain/origin-graph';
|
||||
|
||||
// ── Fetch ─────────────────────────────────────────────────────────────────────
|
||||
|
||||
async function fetchDiffs(artboardId: string): Promise<IntentDiff[]> {
|
||||
const res = await fetch(`/api/diffs?artboardId=${encodeURIComponent(artboardId)}`);
|
||||
if (!res.ok) throw new Error(`Diffs fetch failed: ${res.status}`);
|
||||
return res.json() as Promise<IntentDiff[]>;
|
||||
}
|
||||
|
||||
async function createDiffRequest(
|
||||
body: Omit<InsertIntentDiff, 'author_id'>,
|
||||
): Promise<IntentDiff> {
|
||||
const res = await fetch('/api/diffs', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(body),
|
||||
});
|
||||
if (!res.ok) {
|
||||
const err = await res.json().catch(() => ({})) as { error?: string };
|
||||
throw new Error(err.error ?? `Create diff failed: ${res.status}`);
|
||||
}
|
||||
return res.json() as Promise<IntentDiff>;
|
||||
}
|
||||
|
||||
async function updateDiffStatusRequest(
|
||||
id: string,
|
||||
status: DiffStatus,
|
||||
notes?: string,
|
||||
): Promise<IntentDiff> {
|
||||
const res = await fetch(`/api/diffs/${id}`, {
|
||||
method: 'PATCH',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ status, notes }),
|
||||
});
|
||||
if (!res.ok) {
|
||||
const err = await res.json().catch(() => ({})) as { error?: string };
|
||||
throw new Error(err.error ?? `Update diff failed: ${res.status}`);
|
||||
}
|
||||
return res.json() as Promise<IntentDiff>;
|
||||
}
|
||||
|
||||
// ── Hook ──────────────────────────────────────────────────────────────────────
|
||||
|
||||
export function useDiffs(artboardId: string | null) {
|
||||
const queryClient = useQueryClient();
|
||||
const queryKey = ['diffs', artboardId];
|
||||
|
||||
const query = useQuery({
|
||||
queryKey,
|
||||
queryFn: () => fetchDiffs(artboardId!),
|
||||
enabled: artboardId !== null,
|
||||
staleTime: 15_000,
|
||||
});
|
||||
|
||||
const createDiff = useMutation({
|
||||
mutationFn: (body: Omit<InsertIntentDiff, 'author_id'>) => createDiffRequest(body),
|
||||
onSuccess: () => queryClient.invalidateQueries({ queryKey }),
|
||||
});
|
||||
|
||||
const updateStatus = useMutation({
|
||||
mutationFn: ({ id, status, notes }: { id: string; status: DiffStatus; notes?: string }) =>
|
||||
updateDiffStatusRequest(id, status, notes),
|
||||
onSuccess: () => queryClient.invalidateQueries({ queryKey }),
|
||||
});
|
||||
|
||||
return {
|
||||
diffs: query.data ?? [],
|
||||
isLoading: query.isLoading,
|
||||
error: query.error,
|
||||
createDiff,
|
||||
updateStatus,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user