|
| 1 | +import { useRef, useState } from "react"; |
| 2 | +import type { |
| 3 | + ExternalFileChangeBlockedState, |
| 4 | + ExternalFileChangeCoordinatorHandle, |
| 5 | +} from "../hooks/useExternalFileChangeCoordinator"; |
| 6 | +import { useDialogBehavior } from "./ui/useDialogBehavior"; |
| 7 | + |
| 8 | +function errorMessage(error: unknown): string { |
| 9 | + return error instanceof Error ? error.message : String(error); |
| 10 | +} |
| 11 | + |
| 12 | +function downloadText(filename: string, content: string): void { |
| 13 | + const url = URL.createObjectURL(new Blob([content], { type: "text/html;charset=utf-8" })); |
| 14 | + const anchor = document.createElement("a"); |
| 15 | + anchor.href = url; |
| 16 | + anchor.download = filename; |
| 17 | + anchor.click(); |
| 18 | + URL.revokeObjectURL(url); |
| 19 | +} |
| 20 | + |
| 21 | +function ConflictReview({ |
| 22 | + conflict, |
| 23 | + onClose, |
| 24 | +}: { |
| 25 | + conflict: Extract<ExternalFileChangeBlockedState, { status: "conflict" }>; |
| 26 | + onClose: () => void; |
| 27 | +}) { |
| 28 | + const containerRef = useRef<HTMLDivElement>(null); |
| 29 | + useDialogBehavior({ open: true, onClose, containerRef }); |
| 30 | + const external = conflict.error.currentContent ?? "(The server did not return file contents.)"; |
| 31 | + const studio = conflict.error.attemptedContent; |
| 32 | + |
| 33 | + return ( |
| 34 | + <div |
| 35 | + className="fixed inset-0 z-[110] flex items-center justify-center bg-black/70 px-5 backdrop-blur-sm" |
| 36 | + onClick={onClose} |
| 37 | + > |
| 38 | + <div |
| 39 | + ref={containerRef} |
| 40 | + role="dialog" |
| 41 | + aria-modal="true" |
| 42 | + aria-labelledby="external-conflict-title" |
| 43 | + tabIndex={-1} |
| 44 | + className="max-h-[88vh] w-full max-w-5xl overflow-auto rounded-xl border border-amber-400/30 bg-neutral-950 p-5 text-neutral-100 shadow-2xl outline-none" |
| 45 | + onClick={(event) => event.stopPropagation()} |
| 46 | + > |
| 47 | + <div className="flex items-start justify-between gap-4"> |
| 48 | + <div> |
| 49 | + <h2 id="external-conflict-title" className="text-base font-semibold"> |
| 50 | + Review both versions of {conflict.error.filePath} |
| 51 | + </h2> |
| 52 | + <p className="mt-1 text-xs text-neutral-400"> |
| 53 | + Reviewing or exporting does not change either version. |
| 54 | + </p> |
| 55 | + </div> |
| 56 | + <button type="button" onClick={onClose} className="rounded px-2 py-1 text-neutral-400"> |
| 57 | + Close |
| 58 | + </button> |
| 59 | + </div> |
| 60 | + <div className="mt-4 grid gap-4 md:grid-cols-2"> |
| 61 | + {[ |
| 62 | + { title: "File on disk", content: external, suffix: "external" }, |
| 63 | + { title: "Unsaved Studio version", content: studio, suffix: "studio" }, |
| 64 | + ].map((side) => ( |
| 65 | + <section key={side.suffix} aria-label={side.title}> |
| 66 | + <div className="mb-2 flex items-center justify-between gap-2"> |
| 67 | + <h3 className="text-sm font-medium">{side.title}</h3> |
| 68 | + <div className="flex gap-2"> |
| 69 | + <button |
| 70 | + type="button" |
| 71 | + className="rounded border border-neutral-700 px-2 py-1 text-[11px]" |
| 72 | + onClick={() => void navigator.clipboard.writeText(side.content)} |
| 73 | + > |
| 74 | + Copy |
| 75 | + </button> |
| 76 | + <button |
| 77 | + type="button" |
| 78 | + className="rounded border border-neutral-700 px-2 py-1 text-[11px]" |
| 79 | + onClick={() => |
| 80 | + downloadText(`${conflict.error.filePath}.${side.suffix}.html`, side.content) |
| 81 | + } |
| 82 | + > |
| 83 | + Download |
| 84 | + </button> |
| 85 | + </div> |
| 86 | + </div> |
| 87 | + <textarea |
| 88 | + readOnly |
| 89 | + value={side.content} |
| 90 | + className="h-80 w-full resize-y rounded border border-neutral-800 bg-neutral-900 p-3 font-mono text-[11px] leading-relaxed text-neutral-300" |
| 91 | + /> |
| 92 | + </section> |
| 93 | + ))} |
| 94 | + </div> |
| 95 | + </div> |
| 96 | + </div> |
| 97 | + ); |
| 98 | +} |
| 99 | + |
| 100 | +function FailedDraftReview({ |
| 101 | + path, |
| 102 | + content, |
| 103 | + onClose, |
| 104 | +}: { |
| 105 | + path: string; |
| 106 | + content: string; |
| 107 | + onClose: () => void; |
| 108 | +}) { |
| 109 | + const containerRef = useRef<HTMLDivElement>(null); |
| 110 | + useDialogBehavior({ open: true, onClose, containerRef }); |
| 111 | + return ( |
| 112 | + <div |
| 113 | + className="fixed inset-0 z-[110] flex items-center justify-center bg-black/70 px-5 backdrop-blur-sm" |
| 114 | + onClick={onClose} |
| 115 | + > |
| 116 | + <div |
| 117 | + ref={containerRef} |
| 118 | + role="dialog" |
| 119 | + aria-modal="true" |
| 120 | + aria-labelledby="failed-draft-title" |
| 121 | + tabIndex={-1} |
| 122 | + className="max-h-[88vh] w-full max-w-3xl overflow-auto rounded-xl border border-amber-400/30 bg-neutral-950 p-5 text-neutral-100 shadow-2xl outline-none" |
| 123 | + onClick={(event) => event.stopPropagation()} |
| 124 | + > |
| 125 | + <div className="flex items-start justify-between gap-4"> |
| 126 | + <div> |
| 127 | + <h2 id="failed-draft-title" className="text-base font-semibold"> |
| 128 | + Recover unsaved Studio draft for {path} |
| 129 | + </h2> |
| 130 | + <p className="mt-1 text-xs text-neutral-400"> |
| 131 | + Copy or download this draft before choosing to discard it. |
| 132 | + </p> |
| 133 | + </div> |
| 134 | + <button type="button" onClick={onClose} className="rounded px-2 py-1 text-neutral-400"> |
| 135 | + Close |
| 136 | + </button> |
| 137 | + </div> |
| 138 | + <div className="mt-4 flex justify-end gap-2"> |
| 139 | + <button |
| 140 | + type="button" |
| 141 | + className="rounded border border-neutral-700 px-2 py-1 text-[11px]" |
| 142 | + onClick={() => void navigator.clipboard.writeText(content)} |
| 143 | + > |
| 144 | + Copy |
| 145 | + </button> |
| 146 | + <button |
| 147 | + type="button" |
| 148 | + className="rounded border border-neutral-700 px-2 py-1 text-[11px]" |
| 149 | + onClick={() => downloadText(`${path}.studio.html`, content)} |
| 150 | + > |
| 151 | + Download |
| 152 | + </button> |
| 153 | + </div> |
| 154 | + <textarea |
| 155 | + readOnly |
| 156 | + value={content} |
| 157 | + className="mt-2 h-80 w-full resize-y rounded border border-neutral-800 bg-neutral-900 p-3 font-mono text-[11px] leading-relaxed text-neutral-300" |
| 158 | + /> |
| 159 | + </div> |
| 160 | + </div> |
| 161 | + ); |
| 162 | +} |
| 163 | + |
| 164 | +export function ExternalFileConflictBanner({ |
| 165 | + coordinator, |
| 166 | +}: { |
| 167 | + coordinator: ExternalFileChangeCoordinatorHandle; |
| 168 | +}) { |
| 169 | + const [reviewing, setReviewing] = useState(false); |
| 170 | + const blocked = coordinator.blocked; |
| 171 | + if (!blocked) return null; |
| 172 | + |
| 173 | + const conflict = blocked.status === "conflict" ? blocked : null; |
| 174 | + const failure = blocked.status === "failed" ? blocked : null; |
| 175 | + return ( |
| 176 | + <> |
| 177 | + <div |
| 178 | + role="alert" |
| 179 | + className="absolute left-1/2 top-14 z-[94] flex max-w-[calc(100vw-32px)] -translate-x-1/2 flex-wrap items-center gap-3 rounded-md border border-amber-400/30 bg-amber-950/95 px-4 py-2 text-[12px] font-medium text-amber-50 shadow-lg" |
| 180 | + > |
| 181 | + <span> |
| 182 | + {conflict |
| 183 | + ? `${conflict.error.filePath} changed outside Studio. Preview is paused so neither version is lost.` |
| 184 | + : `Studio could not safely finish local saves: ${errorMessage(blocked.error)}. Preview is paused.`} |
| 185 | + </span> |
| 186 | + {conflict && ( |
| 187 | + <button type="button" onClick={() => setReviewing(true)} className="underline"> |
| 188 | + Review or export both |
| 189 | + </button> |
| 190 | + )} |
| 191 | + {failure?.studioContent != null && ( |
| 192 | + <button type="button" onClick={() => setReviewing(true)} className="underline"> |
| 193 | + Review or export Studio draft |
| 194 | + </button> |
| 195 | + )} |
| 196 | + {failure && !failure.recovered && ( |
| 197 | + <button type="button" onClick={() => void coordinator.retry()} className="underline"> |
| 198 | + Retry save |
| 199 | + </button> |
| 200 | + )} |
| 201 | + <button |
| 202 | + type="button" |
| 203 | + onClick={() => void coordinator.useExternalFile()} |
| 204 | + className="rounded border border-amber-200/30 px-2 py-1" |
| 205 | + > |
| 206 | + Discard Studio edits and reload file |
| 207 | + </button> |
| 208 | + {conflict && ( |
| 209 | + <button |
| 210 | + type="button" |
| 211 | + onClick={() => { |
| 212 | + if ( |
| 213 | + window.confirm( |
| 214 | + "Overwrite the externally changed file with the Studio version? The server will preserve its normal backup before writing.", |
| 215 | + ) |
| 216 | + ) { |
| 217 | + void coordinator.keepStudioFile(); |
| 218 | + } |
| 219 | + }} |
| 220 | + className="rounded border border-red-300/40 px-2 py-1 text-red-100" |
| 221 | + > |
| 222 | + Overwrite file with Studio version |
| 223 | + </button> |
| 224 | + )} |
| 225 | + {failure?.recovered && failure.studioContent != null && ( |
| 226 | + <button |
| 227 | + type="button" |
| 228 | + onClick={() => { |
| 229 | + if ( |
| 230 | + window.confirm( |
| 231 | + "Overwrite the file with the recovered Studio draft? The current file will be preserved by the server's normal backup before writing.", |
| 232 | + ) |
| 233 | + ) { |
| 234 | + void coordinator.keepStudioFile(); |
| 235 | + } |
| 236 | + }} |
| 237 | + className="rounded border border-red-300/40 px-2 py-1 text-red-100" |
| 238 | + > |
| 239 | + Overwrite file with recovered Studio draft |
| 240 | + </button> |
| 241 | + )} |
| 242 | + </div> |
| 243 | + {reviewing && conflict && ( |
| 244 | + <ConflictReview conflict={conflict} onClose={() => setReviewing(false)} /> |
| 245 | + )} |
| 246 | + {reviewing && failure?.studioContent != null && ( |
| 247 | + <FailedDraftReview |
| 248 | + path={failure.path} |
| 249 | + content={failure.studioContent} |
| 250 | + onClose={() => setReviewing(false)} |
| 251 | + /> |
| 252 | + )} |
| 253 | + </> |
| 254 | + ); |
| 255 | +} |
0 commit comments