|
| 1 | +/** |
| 2 | + * ObjectUI |
| 3 | + * Copyright (c) 2024-present ObjectStack Inc. |
| 4 | + * |
| 5 | + * This source code is licensed under the MIT license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | + |
| 9 | +/** |
| 10 | + * "What will publishing change?" — the draft changeset, answered before the |
| 11 | + * user commits. Lists every pending ADR-0033 draft grouped by metadata type, |
| 12 | + * and classifies each as NEW (no published version exists — publishing adds |
| 13 | + * it) or UPDATE (a published version exists — publishing overwrites it). |
| 14 | + * This is the review surface that turns Publish from a leap of faith into an |
| 15 | + * informed click; the per-item designer diff remains the deep-dive. |
| 16 | + * |
| 17 | + * Read-only: fetches `_drafts` + per-item `/published` probes on open, and |
| 18 | + * never writes. Publishing stays with the caller (DraftPreviewBar / chat). |
| 19 | + */ |
| 20 | + |
| 21 | +import { useCallback, useEffect, useState } from 'react'; |
| 22 | +import { FilePlus2, FilePen, Loader2 } from 'lucide-react'; |
| 23 | +import { |
| 24 | + Badge, |
| 25 | + Sheet, |
| 26 | + SheetContent, |
| 27 | + SheetDescription, |
| 28 | + SheetHeader, |
| 29 | + SheetTitle, |
| 30 | +} from '@object-ui/components'; |
| 31 | +import { useObjectTranslation } from '@object-ui/i18n'; |
| 32 | + |
| 33 | +export interface DraftChangeEntry { |
| 34 | + type: string; |
| 35 | + name: string; |
| 36 | + packageId: string | null; |
| 37 | + /** `new` = no published version; `update` = overwrites one; undefined = probing. */ |
| 38 | + kind?: 'new' | 'update'; |
| 39 | +} |
| 40 | + |
| 41 | +/** Pending drafts straight from the ADR-0033 `_drafts` endpoint. */ |
| 42 | +async function listPendingDrafts(): Promise<DraftChangeEntry[]> { |
| 43 | + const res = await fetch('/api/v1/meta/_drafts', { |
| 44 | + credentials: 'include', |
| 45 | + headers: { Accept: 'application/json' }, |
| 46 | + cache: 'no-store', |
| 47 | + }); |
| 48 | + if (!res.ok) throw new Error(`_drafts HTTP ${res.status}`); |
| 49 | + const data = (await res.json()) as |
| 50 | + | Array<Record<string, unknown>> |
| 51 | + | { drafts?: Array<Record<string, unknown>> }; |
| 52 | + const list = Array.isArray(data) ? data : data?.drafts ?? []; |
| 53 | + return list |
| 54 | + .filter((d) => typeof d?.type === 'string' && typeof d?.name === 'string') |
| 55 | + .map((d) => ({ |
| 56 | + type: d.type as string, |
| 57 | + name: d.name as string, |
| 58 | + packageId: typeof d.packageId === 'string' && d.packageId ? (d.packageId as string) : null, |
| 59 | + })); |
| 60 | +} |
| 61 | + |
| 62 | +/** |
| 63 | + * Names that exist in the PUBLISHED world for a type — the plain (no |
| 64 | + * `preview=draft`) list. One request classifies every draft of that type: |
| 65 | + * a draft whose name is absent here is NEW; present means publish UPDATES it. |
| 66 | + * (A per-item `/published` probe would be O(drafts) requests, and the REST |
| 67 | + * tree has no such sub-route — the generic :name handler answers anything.) |
| 68 | + */ |
| 69 | +async function publishedNamesOf(type: string): Promise<Set<string>> { |
| 70 | + const res = await fetch(`/api/v1/meta/${encodeURIComponent(type)}`, { |
| 71 | + credentials: 'include', |
| 72 | + headers: { Accept: 'application/json' }, |
| 73 | + cache: 'no-store', |
| 74 | + }); |
| 75 | + if (!res.ok) throw new Error(`published list HTTP ${res.status}`); |
| 76 | + const data = (await res.json()) as unknown[] | { items?: unknown[] }; |
| 77 | + const list = Array.isArray(data) ? data : data?.items ?? []; |
| 78 | + return new Set( |
| 79 | + (list as Array<{ name?: unknown }>) |
| 80 | + .map((it) => (typeof it?.name === 'string' ? it.name : null)) |
| 81 | + .filter((n): n is string => n !== null), |
| 82 | + ); |
| 83 | +} |
| 84 | + |
| 85 | +export interface DraftChangesPanelProps { |
| 86 | + open: boolean; |
| 87 | + onOpenChange: (open: boolean) => void; |
| 88 | +} |
| 89 | + |
| 90 | +export function DraftChangesPanel({ open, onOpenChange }: DraftChangesPanelProps) { |
| 91 | + const { t } = useObjectTranslation(); |
| 92 | + const [entries, setEntries] = useState<DraftChangeEntry[] | null>(null); |
| 93 | + const [error, setError] = useState<string | null>(null); |
| 94 | + |
| 95 | + const load = useCallback(async () => { |
| 96 | + setEntries(null); |
| 97 | + setError(null); |
| 98 | + try { |
| 99 | + const drafts = await listPendingDrafts(); |
| 100 | + setEntries(drafts); |
| 101 | + // Classify new-vs-update per TYPE: one published-list read covers every |
| 102 | + // draft of that type. A type whose read fails stays unclassified |
| 103 | + // (rendered neutrally) rather than failing the whole panel. |
| 104 | + const types = [...new Set(drafts.map((d) => d.type))]; |
| 105 | + await Promise.all( |
| 106 | + types.map(async (type) => { |
| 107 | + let published: Set<string> | null = null; |
| 108 | + try { |
| 109 | + published = await publishedNamesOf(type); |
| 110 | + } catch { |
| 111 | + return; |
| 112 | + } |
| 113 | + setEntries((prev) => |
| 114 | + prev |
| 115 | + ? prev.map((entry) => |
| 116 | + entry.type === type |
| 117 | + ? { ...entry, kind: published!.has(entry.name) ? 'update' : 'new' } |
| 118 | + : entry, |
| 119 | + ) |
| 120 | + : prev, |
| 121 | + ); |
| 122 | + }), |
| 123 | + ); |
| 124 | + } catch (e) { |
| 125 | + setError((e as Error).message); |
| 126 | + } |
| 127 | + }, []); |
| 128 | + |
| 129 | + useEffect(() => { |
| 130 | + if (open) void load(); |
| 131 | + }, [open, load]); |
| 132 | + |
| 133 | + const byType = new Map<string, DraftChangeEntry[]>(); |
| 134 | + for (const entry of entries ?? []) { |
| 135 | + const bucket = byType.get(entry.type) ?? []; |
| 136 | + bucket.push(entry); |
| 137 | + byType.set(entry.type, bucket); |
| 138 | + } |
| 139 | + |
| 140 | + return ( |
| 141 | + <Sheet open={open} onOpenChange={onOpenChange}> |
| 142 | + <SheetContent side="right" className="w-[420px] sm:max-w-[420px]" data-testid="draft-changes-panel"> |
| 143 | + <SheetHeader> |
| 144 | + <SheetTitle> |
| 145 | + {t('preview.changes.title', { defaultValue: 'Pending changes' })} |
| 146 | + </SheetTitle> |
| 147 | + <SheetDescription> |
| 148 | + {t('preview.changes.description', { |
| 149 | + defaultValue: 'What publishing will change. New items are added; updates overwrite the live version.', |
| 150 | + })} |
| 151 | + </SheetDescription> |
| 152 | + </SheetHeader> |
| 153 | + <div className="mt-4 flex flex-col gap-4 overflow-y-auto px-4 pb-6"> |
| 154 | + {error ? ( |
| 155 | + <p className="text-sm text-destructive"> |
| 156 | + {t('preview.changes.loadFailed', { defaultValue: 'Could not load pending changes:' })}{' '} |
| 157 | + {error} |
| 158 | + </p> |
| 159 | + ) : entries === null ? ( |
| 160 | + <div className="flex items-center gap-2 text-sm text-muted-foreground"> |
| 161 | + <Loader2 className="h-4 w-4 animate-spin" /> |
| 162 | + {t('preview.changes.loading', { defaultValue: 'Loading pending changes…' })} |
| 163 | + </div> |
| 164 | + ) : entries.length === 0 ? ( |
| 165 | + <p className="text-sm text-muted-foreground"> |
| 166 | + {t('preview.changes.empty', { defaultValue: 'Nothing pending — every draft has been published.' })} |
| 167 | + </p> |
| 168 | + ) : ( |
| 169 | + [...byType.entries()].map(([type, items]) => ( |
| 170 | + <div key={type}> |
| 171 | + <h4 className="mb-1.5 text-xs font-semibold uppercase tracking-wide text-muted-foreground"> |
| 172 | + {type} · {items.length} |
| 173 | + </h4> |
| 174 | + <ul className="flex flex-col gap-1"> |
| 175 | + {items.map((entry) => ( |
| 176 | + <li |
| 177 | + key={`${entry.type}:${entry.name}`} |
| 178 | + className="flex items-center gap-2 rounded-md border px-2.5 py-1.5 text-sm" |
| 179 | + > |
| 180 | + {entry.kind === 'new' ? ( |
| 181 | + <FilePlus2 className="h-3.5 w-3.5 shrink-0 text-emerald-600" /> |
| 182 | + ) : entry.kind === 'update' ? ( |
| 183 | + <FilePen className="h-3.5 w-3.5 shrink-0 text-amber-600" /> |
| 184 | + ) : ( |
| 185 | + <Loader2 className="h-3.5 w-3.5 shrink-0 animate-spin text-muted-foreground" /> |
| 186 | + )} |
| 187 | + <span className="min-w-0 flex-1 truncate font-mono text-xs">{entry.name}</span> |
| 188 | + {entry.kind ? ( |
| 189 | + <Badge |
| 190 | + variant="outline" |
| 191 | + className={ |
| 192 | + entry.kind === 'new' |
| 193 | + ? 'border-emerald-200 bg-emerald-50 text-emerald-700' |
| 194 | + : 'border-amber-200 bg-amber-50 text-amber-700' |
| 195 | + } |
| 196 | + > |
| 197 | + {entry.kind === 'new' |
| 198 | + ? t('preview.changes.kindNew', { defaultValue: 'New' }) |
| 199 | + : t('preview.changes.kindUpdate', { defaultValue: 'Update' })} |
| 200 | + </Badge> |
| 201 | + ) : null} |
| 202 | + </li> |
| 203 | + ))} |
| 204 | + </ul> |
| 205 | + </div> |
| 206 | + )) |
| 207 | + )} |
| 208 | + </div> |
| 209 | + </SheetContent> |
| 210 | + </Sheet> |
| 211 | + ); |
| 212 | +} |
0 commit comments