fix: resolve workspace publish conflicts
This commit is contained in:
@@ -9,6 +9,7 @@ import {
|
||||
type WorkspaceConflict,
|
||||
type WorkspaceRevision,
|
||||
} from "../api/workspaces";
|
||||
import type { WorkspaceDraft } from "../workspaces/drafts";
|
||||
import { Button } from "../components/ui/button";
|
||||
import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle } from "../components/ui/dialog";
|
||||
|
||||
@@ -17,6 +18,7 @@ export interface WorkspacePublishDialogProps {
|
||||
request: PublishWorkspaceRequest;
|
||||
onOpenChange: (open: boolean) => void;
|
||||
onPublished: (revision: WorkspaceRevision | undefined) => void;
|
||||
onResolved: (draft: WorkspaceDraft) => void;
|
||||
onPull: () => Promise<void> | void;
|
||||
onReload: () => void;
|
||||
}
|
||||
@@ -30,6 +32,25 @@ function valueAt(workspace: CanonicalWorkspace, path: string): string {
|
||||
: JSON.stringify(value);
|
||||
}
|
||||
|
||||
function valueAtPath(workspace: CanonicalWorkspace, path: string): unknown {
|
||||
return path.split(".").reduce<unknown>((current, key) => (
|
||||
current && typeof current === "object" ? (current as Record<string, unknown>)[key] : undefined
|
||||
), workspace);
|
||||
}
|
||||
|
||||
function replaceAtPath(value: Record<string, unknown>, path: string[], replacement: unknown): Record<string, unknown> {
|
||||
const [key, ...remaining] = path;
|
||||
const copy = { ...value };
|
||||
if (remaining.length === 0) {
|
||||
if (replacement === undefined) delete copy[key];
|
||||
else copy[key] = replacement;
|
||||
return copy;
|
||||
}
|
||||
const child = copy[key];
|
||||
copy[key] = replaceAtPath(child && typeof child === "object" && !Array.isArray(child) ? child as Record<string, unknown> : {}, remaining, replacement);
|
||||
return copy;
|
||||
}
|
||||
|
||||
function requestWithWorkspace(request: PublishWorkspaceRequest, workspace: CanonicalWorkspace): PublishWorkspaceRequest {
|
||||
return request.action === "delete" ? request : { ...request, workspace };
|
||||
}
|
||||
@@ -39,10 +60,17 @@ function RevisionSummary({ request }: { request: PublishWorkspaceRequest }) {
|
||||
return <p className="rounded-md border border-border/70 bg-muted/40 px-3 py-2 font-mono text-xs text-muted-foreground">{label} · base {request.baseCommit.slice(0, 12)}</p>;
|
||||
}
|
||||
|
||||
function ConflictReview({ conflict, onPull, onReload }: { conflict: WorkspaceConflict; onPull: () => Promise<void> | void; onReload: () => void }) {
|
||||
function ConflictReview({ conflict, onPull, onReload, onResolved }: {
|
||||
conflict: WorkspaceConflict;
|
||||
onPull: () => Promise<void> | void;
|
||||
onReload: () => void;
|
||||
onResolved: (draft: WorkspaceDraft) => void;
|
||||
}) {
|
||||
const [pulling, setPulling] = useState(false);
|
||||
const [pulled, setPulled] = useState(false);
|
||||
const [pullError, setPullError] = useState(false);
|
||||
const [choices, setChoices] = useState<Record<string, "local" | "remote">>({});
|
||||
const canSave = Boolean(conflict.actual.blob) && conflict.fields.every((field) => choices[field]);
|
||||
|
||||
async function pull() {
|
||||
setPulling(true);
|
||||
@@ -57,10 +85,26 @@ function ConflictReview({ conflict, onPull, onReload }: { conflict: WorkspaceCon
|
||||
}
|
||||
}
|
||||
|
||||
function saveRevisedDraft() {
|
||||
if (!conflict.actual.blob || !canSave) return;
|
||||
const workspace = conflict.fields.reduce<CanonicalWorkspace>((current, field) => (
|
||||
choices[field] === "local"
|
||||
? replaceAtPath(current as unknown as Record<string, unknown>, field.split("."), valueAtPath(conflict.local, field)) as unknown as CanonicalWorkspace
|
||||
: current
|
||||
), conflict.remote);
|
||||
onResolved({
|
||||
workspaceId: workspace.workspace.id,
|
||||
baseCommit: conflict.actual.commit,
|
||||
baseBlob: conflict.actual.blob,
|
||||
workspace,
|
||||
updatedAt: new Date().toISOString(),
|
||||
});
|
||||
}
|
||||
|
||||
return <div className="grid gap-4">
|
||||
<div className="rounded-md border border-amber-500/30 bg-amber-500/10 px-3 py-2 text-sm">
|
||||
<p className="font-semibold">The registry changed before publication.</p>
|
||||
<p className="mt-1 text-muted-foreground">Your browser draft is unchanged. Pull or reload before creating a revised draft; this screen never merges or overwrites remote values.</p>
|
||||
<p className="mt-1 text-muted-foreground">Choose a value for every changed field to save a revised browser draft against registry revision {conflict.actual.commit.slice(0, 12)}. Saving never publishes it.</p>
|
||||
</div>
|
||||
<div className="grid gap-3">
|
||||
{conflict.fields.map((field) => <section key={field} className="overflow-hidden rounded-md border border-border/70" aria-label={`Conflict for ${field}`}>
|
||||
@@ -70,18 +114,24 @@ function ConflictReview({ conflict, onPull, onReload }: { conflict: WorkspaceCon
|
||||
<div className="min-w-0 px-3 py-2"><dt className="text-xs text-muted-foreground">Your draft</dt><dd className="mt-1 break-words font-mono text-xs">{valueAt(conflict.local, field)}</dd></div>
|
||||
<div className="min-w-0 px-3 py-2"><dt className="text-xs text-muted-foreground">Registry</dt><dd className="mt-1 break-words font-mono text-xs">{valueAt(conflict.remote, field)}</dd></div>
|
||||
</dl>
|
||||
<fieldset className="flex flex-wrap gap-3 border-t border-border/70 px-3 py-2 text-xs">
|
||||
<legend className="sr-only">Resolve {field}</legend>
|
||||
<label className="flex items-center gap-1.5"><input type="radio" name={`resolution-${field}`} aria-label={`Use your draft for ${field}`} checked={choices[field] === "local"} onChange={() => setChoices((current) => ({ ...current, [field]: "local" }))} />Use your draft</label>
|
||||
<label className="flex items-center gap-1.5"><input type="radio" name={`resolution-${field}`} aria-label={`Use registry value for ${field}`} checked={choices[field] === "remote"} onChange={() => setChoices((current) => ({ ...current, [field]: "remote" }))} />Use registry value</label>
|
||||
</fieldset>
|
||||
</section>)}
|
||||
</div>
|
||||
{pulled && <p role="status" className="text-sm text-muted-foreground">Latest registry state pulled. Reload the workspace before editing or publishing again.</p>}
|
||||
{pullError && <p role="alert" className="text-sm text-destructive">Registry pull could not be completed. Try again or reload the workspace.</p>}
|
||||
<div className="flex flex-wrap justify-end gap-2">
|
||||
<Button onClick={saveRevisedDraft} disabled={!canSave}>Save revised draft</Button>
|
||||
<Button variant="outline" onClick={() => { void pull(); }} disabled={pulling}>{pulling ? "Pulling…" : "Pull latest registry"}</Button>
|
||||
<Button onClick={onReload}>Reload workspace</Button>
|
||||
</div>
|
||||
</div>;
|
||||
}
|
||||
|
||||
export function WorkspacePublishDialog({ open, request, onOpenChange, onPublished, onPull, onReload }: WorkspacePublishDialogProps) {
|
||||
export function WorkspacePublishDialog({ open, request, onOpenChange, onPublished, onResolved, onPull, onReload }: WorkspacePublishDialogProps) {
|
||||
const [validatedRequest, setValidatedRequest] = useState<PublishWorkspaceRequest>();
|
||||
const [confirmationOpen, setConfirmationOpen] = useState(false);
|
||||
const [conflict, setConflict] = useState<WorkspaceConflict>();
|
||||
@@ -141,10 +191,10 @@ export function WorkspacePublishDialog({ open, request, onOpenChange, onPublishe
|
||||
<DialogContent showCloseButton={false} className="max-w-[min(94vw,46rem)] p-0">
|
||||
<DialogHeader className="border-b border-border/70 px-5 pb-4 pt-5 pr-12">
|
||||
<DialogTitle>{conflict ? "Publication conflict" : "Publish workspace"}</DialogTitle>
|
||||
<DialogDescription>{conflict ? "Compare the changed fields, then pull and reload before revising your draft." : "Validation and an explicit confirmation are required before this shared definition is published."}</DialogDescription>
|
||||
<DialogDescription>{conflict ? "Choose each local or registry value, then save a revised draft for normal validation and confirmation." : "Validation and an explicit confirmation are required before this shared definition is published."}</DialogDescription>
|
||||
</DialogHeader>
|
||||
<div className="grid gap-4 px-5 py-5">
|
||||
{conflict ? <ConflictReview conflict={conflict} onPull={onPull} onReload={onReload} /> : <>
|
||||
{conflict ? <ConflictReview conflict={conflict} onPull={onPull} onReload={onReload} onResolved={onResolved} /> : <>
|
||||
<RevisionSummary request={validatedRequest ?? request} />
|
||||
{message && <p role="status" className="rounded-md border border-border/70 bg-muted/40 px-3 py-2 text-sm">{message}</p>}
|
||||
<div className="flex flex-wrap justify-end gap-2">
|
||||
|
||||
Reference in New Issue
Block a user