Luigit
repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by admin

extensions/canvas/browser/main.tsx

Raw
import {
	CaptureUpdateAction,
	Excalidraw,
	exportToCanvas,
	getCommonBounds,
	MainMenu,
	newElementWith,
	Sidebar,
	serializeAsJSON,
} from "@excalidraw/excalidraw";
import type { ExcalidrawElement } from "@excalidraw/excalidraw/element/types";
import type {
	AppState,
	Collaborator,
	ExcalidrawImperativeAPI,
	ExcalidrawProps,
	SocketId,
} from "@excalidraw/excalidraw/types";
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
import { createRoot } from "react-dom/client";
import { documentFingerprint as fingerprint } from "../src/document.ts";
import type {
	CanvasAction,
	CanvasElement,
	CanvasState,
	CanvasViewport,
} from "../src/store.ts";
import { CanvasIcon } from "./icons.tsx";

declare global {
	interface Window {
		EXCALIDRAW_ASSET_PATH?: string;
	}
}

window.EXCALIDRAW_ASSET_PATH = "/browser/";

const PI_SOCKET = "pi-canvas" as SocketId;
const CLIENT_ID = crypto.randomUUID();
type ActionResponse = { ok: true; state: CanvasState };
type ChangesResponse = {
	currentRevision: number;
	changes: CanvasState[];
	nextRevision: number;
	reset: boolean;
};
type QueuedScene = {
	elements: CanvasElement[];
	revision: number;
	activity: "active" | "settled";
};

function emptyState(): CanvasState {
	return {
		revision: 0,
		elements: [],
	};
}

function accessToken(): string | null {
	const fragment = location.hash.slice(1);
	if (fragment) {
		sessionStorage.setItem("pi-canvas-token", fragment);
		history.replaceState(null, "", location.pathname);
		return fragment;
	}
	return sessionStorage.getItem("pi-canvas-token");
}

class HttpError extends Error {
	constructor(
		readonly status: number,
		message: string,
	) {
		super(message);
	}
}

async function response<T>(value: Response): Promise<T> {
	const body: unknown = await value.json();
	if (!value.ok)
		throw new HttpError(
			value.status,
			typeof body === "object" &&
				body !== null &&
				"error" in body &&
				typeof body.error === "string"
				? body.error
				: `Request failed (${value.status}).`,
		);
	return body as T;
}

function errorText(error: unknown, fallback: string): string {
	return error instanceof Error ? error.message : fallback;
}

async function captureViewport(
	editor: ExcalidrawImperativeAPI,
	elements: CanvasElement[],
): Promise<CanvasViewport> {
	const live = elements.filter((element) => !element.isDeleted);
	if (!live.length)
		throw new Error("Full-scene image is unavailable for an empty drawing.");
	const [left, top, right, bottom] = getCommonBounds(
		live as unknown as ExcalidrawElement[],
	);
	const image = await exportToCanvas({
		elements: live as unknown as ExcalidrawElement[],
		appState: { ...editor.getAppState(), editingTextElement: null },
		files: editor.getFiles(),
		maxWidthOrHeight: 1024,
		exportPadding: 20,
	});
	let url = image.toDataURL("image/png");
	if (url.length > 256 * 1024) url = image.toDataURL("image/jpeg", 0.8);
	const data = url.slice(url.indexOf(",") + 1);
	if (!data.length || data.length > 256 * 1024)
		throw new Error("Full-scene image exceeds size limit.");
	return {
		data,
		mimeType: url.startsWith("data:image/png;") ? "image/png" : "image/jpeg",
		imageWidth: image.width,
		imageHeight: image.height,
		left,
		top,
		width: Math.max(1, right - left),
		height: Math.max(1, bottom - top),
	};
}

function nextPaint(signal: AbortSignal): Promise<void> {
	return new Promise((resolve, reject) => {
		let frame = 0;
		const abort = () => {
			cancelAnimationFrame(frame);
			reject(new DOMException("Aborted", "AbortError"));
		};
		if (signal.aborted) {
			abort();
			return;
		}
		signal.addEventListener("abort", abort, { once: true });
		frame = requestAnimationFrame(() => {
			frame = requestAnimationFrame(() => {
				signal.removeEventListener("abort", abort);
				resolve();
			});
		});
	});
}

function App() {
	const token = useMemo(accessToken, []);
	const api = useRef<ExcalidrawImperativeAPI | null>(null);
	const canonical = useRef<CanvasState>(emptyState());
	const editorFingerprint = useRef("");
	const editorRevision = useRef(0);
	const mounted = useRef(true);
	const initialScene = useRef<CanvasState | undefined>(undefined);
	const suppressedFingerprint = useRef<string | null>(null);
	const queued = useRef<QueuedScene | null>(null);
	const saving = useRef<QueuedScene | null>(null);
	const dirty = useRef(false);
	const conflicted = useRef(false);
	const pointerDown = useRef(false);
	const editingText = useRef(false);
	const localEpoch = useRef(0);
	const activitySequence = useRef(0);
	const syncing = useRef(false);
	const syncAgain = useRef(false);
	const syncController = useRef<AbortController | null>(null);
	const resyncRetry = useRef<number | undefined>(undefined);
	const resyncAttempts = useRef(0);
	const settleTimer = useRef<number | undefined>(undefined);
	const collaborators = useRef(new Map<SocketId, Collaborator>());
	const debounce = useRef<number | undefined>(undefined);
	const [state, setState] = useState<CanvasState | null>(null);
	const [error, setError] = useState<string | null>(
		token ? null : "Canvas link is missing its access token.",
	);
	const [connection, setConnection] = useState("connecting");
	const [connectionAttempt, setConnectionAttempt] = useState(0);
	const [connectionError, setConnectionError] = useState<string | null>(null);
	const [conflict, setConflict] = useState<string | null>(null);
	const retryTimer = useRef<number | undefined>(undefined);
	const retries = useRef(0);
	const [saveStatus, setSaveStatus] = useState<
		"shared" | "saving" | "retrying" | "blocked"
	>("shared");
	const [recoveryChoice, setRecoveryChoice] = useState<{
		kind: "mine" | "shared";
		revision: number;
		epoch: number;
	} | null>(null);
	const [recovering, setRecovering] = useState(false);
	const [seenNoticeId, setSeenNoticeId] = useState<string | null>(null);
	const noticeUnread = Boolean(
		state?.notice && state.notice.id !== seenNoticeId,
	);
	const onSidebarStateChange = useCallback(
		(next: { name: string } | null): void => {
			if (next?.name === "pi-notifications") {
				const notice = canonical.current.notice;
				if (notice) setSeenNoticeId(notice.id);
			}
		},
		[],
	);

	const request = useCallback(
		async (
			action: CanvasAction,
			keepalive = false,
		): Promise<ActionResponse> => {
			if (!token) throw new Error("Canvas access token is unavailable.");
			return response<ActionResponse>(
				await fetch("/api/action", {
					method: "POST",
					headers: {
						Authorization: `Bearer ${token}`,
						"Content-Type": "application/json",
					},
					body: JSON.stringify(action),
					keepalive,
					signal: AbortSignal.timeout(10000),
				}),
			);
		},
		[token],
	);

	const shareSettled = useCallback(async (): Promise<void> => {
		if (
			!api.current ||
			pointerDown.current ||
			editingText.current ||
			dirty.current ||
			queued.current ||
			saving.current ||
			syncing.current ||
			conflicted.current ||
			retryTimer.current !== undefined
		)
			return;
		const revision = canonical.current.revision;
		const epoch = localEpoch.current;
		const sequence = ++activitySequence.current;
		const elements = structuredClone(canonical.current.elements);
		let viewport: CanvasViewport | undefined;
		try {
			viewport = await captureViewport(api.current, elements);
		} catch {
			// Drawing persistence must not depend on image availability.
		}
		if (
			sequence !== activitySequence.current ||
			epoch !== localEpoch.current ||
			revision !== canonical.current.revision ||
			pointerDown.current ||
			editingText.current ||
			dirty.current ||
			saving.current ||
			conflicted.current
		)
			return;
		await request({
			kind: "activity",
			clientId: CLIENT_ID,
			sequence,
			activity: "settled",
			revision,
			...(viewport ? { viewport } : {}),
		}).catch(() => undefined);
	}, [request]);
	const announceActivity = useCallback(
		(activity: "active" | "settled"): void => {
			if (settleTimer.current !== undefined)
				window.clearTimeout(settleTimer.current);
			settleTimer.current = undefined;
			if (activity === "settled") {
				settleTimer.current = window.setTimeout(() => {
					settleTimer.current = undefined;
					void shareSettled();
				}, 150);
				return;
			}
			void request({
				kind: "activity",
				clientId: CLIENT_ID,
				sequence: ++activitySequence.current,
				activity,
			}).catch(() => undefined);
		},
		[request, shareSettled],
	);

	const applyEditor = useCallback(
		(
			elements: CanvasElement[],
			capture: (typeof CaptureUpdateAction)[keyof typeof CaptureUpdateAction],
			revision = canonical.current.revision,
		): void => {
			const next = fingerprint(elements);
			if (!api.current) return;
			editorRevision.current = revision;
			if (next === editorFingerprint.current) return;
			const existing = new Map(
				api.current
					.getSceneElementsIncludingDeleted()
					.map((element) => [element.id, element]),
			);
			const nativeElements = elements.map((element) => {
				const previous = existing.get(element.id);
				if (
					!previous ||
					fingerprint([previous as unknown as CanvasElement]) ===
						fingerprint([element])
				)
					return previous ?? element;
				const updates = Object.fromEntries(
					Object.entries(element).filter(
						([key]) =>
							!["id", "version", "versionNonce", "updated"].includes(key),
					),
				) as Parameters<typeof newElementWith<ExcalidrawElement>>[1];
				return newElementWith<ExcalidrawElement>(previous, updates);
			});
			suppressedFingerprint.current = next;
			editorFingerprint.current = next;
			api.current.updateScene({
				elements: nativeElements as readonly ExcalidrawElement[],
				captureUpdate: capture,
			});
		},
		[],
	);

	const install = useCallback(
		(next: CanvasState, apply = true, forceUndo = false): void => {
			if (!mounted.current) return;
			if (!initialScene.current && !api.current) initialScene.current = next;
			if (next.revision >= canonical.current.revision) {
				canonical.current = next;
				setState(next);
			}
			if (!api.current || next.revision <= editorRevision.current) return;
			if (
				dirty.current ||
				saving.current ||
				pointerDown.current ||
				editingText.current
			) {
				const ownSave =
					saving.current !== null &&
					next.revision === saving.current.revision + 1 &&
					fingerprint(next.elements) === fingerprint(saving.current.elements);
				if (dirty.current && !ownSave) {
					conflicted.current = true;
					setConflict(
						"Remote drawing changed. Your drawing is still in this tab.",
					);
					setSaveStatus("blocked");
					setRecoveryChoice(null);
				}
				return;
			}
			if (!apply) return;
			applyEditor(
				next.elements,
				forceUndo || next.change?.origin === "agent"
					? CaptureUpdateAction.IMMEDIATELY
					: CaptureUpdateAction.NEVER,
				next.revision,
			);
		},
		[applyEditor],
	);

	const reconcile = useCallback(
		async function syncChanges(): Promise<void> {
			if (
				!token ||
				!api.current ||
				!mounted.current ||
				document.visibilityState === "hidden"
			)
				return;
			if (syncing.current) {
				syncAgain.current = true;
				return;
			}
			syncing.current = true;
			const controller = new AbortController();
			syncController.current = controller;
			try {
				for (let page = 0; page < 64; page++) {
					syncAgain.current = false;
					const after = editorRevision.current;
					const result = await response<ChangesResponse>(
						await fetch(`/api/changes?after=${after}`, {
							headers: { Authorization: `Bearer ${token}` },
							signal: AbortSignal.any([
								controller.signal,
								AbortSignal.timeout(10000),
							]),
						}),
					);
					if (!mounted.current || controller.signal.aborted) return;
					for (const next of result.changes) {
						install(next, true, result.reset);
						if (editorRevision.current < next.revision) return;
						if (result.reset || next.change?.origin === "agent")
							await nextPaint(controller.signal);
					}
					if (result.reset)
						setError(
							"Older shared updates were merged into one undoable catch-up step.",
						);
					if (
						editorRevision.current >= result.currentRevision &&
						!syncAgain.current
					)
						break;
					if (editorRevision.current <= after)
						throw new Error("Canvas replay did not advance.");
				}
				resyncAttempts.current = 0;
				if (resyncRetry.current !== undefined)
					window.clearTimeout(resyncRetry.current);
				resyncRetry.current = undefined;
			} catch (reason) {
				if (mounted.current && !controller.signal.aborted) {
					setConnectionError(errorText(reason, "Canvas resync failed."));
					if (resyncAttempts.current++ < 3)
						resyncRetry.current = window.setTimeout(
							() => {
								void syncChanges();
							},
							500 * 2 ** resyncAttempts.current,
						);
					else setConnection("disconnected");
				}
			} finally {
				syncing.current = false;
				if (syncController.current === controller)
					syncController.current = null;
				if (mounted.current) announceActivity("settled");
			}
		},
		[announceActivity, install, token],
	);

	const saveScene = useCallback(async (): Promise<void> => {
		if (
			saving.current ||
			conflicted.current ||
			retryTimer.current !== undefined ||
			!queued.current
		)
			return;
		const draft = queued.current;
		queued.current = null;
		saving.current = draft;
		setSaveStatus("saving");
		try {
			const result = await request({
				kind: "scene",
				revision: draft.revision,
				clientId: CLIENT_ID,
				sequence: ++activitySequence.current,
				activity: "active",
				// Deleted shapes belong to local undo history, not the shared document.
				elements: draft.elements.filter((element) => !element.isDeleted),
			}).catch((reason: unknown) => {
				// A lost HTTP response is not a conflict when the stream already acknowledged this save.
				const latest = canonical.current;
				if (
					latest.revision === draft.revision + 1 &&
					fingerprint(latest.elements) === fingerprint(draft.elements)
				)
					return { ok: true as const, state: latest };
				throw reason;
			});
			if (fingerprint(result.state.elements) !== fingerprint(draft.elements))
				throw new HttpError(
					409,
					"Shared drawing did not acknowledge this save. Local edits remain in this tab.",
				);
			install(result.state, false);
			editorRevision.current = result.state.revision;
			const nextDraft = queued.current as QueuedScene | null;
			if (nextDraft) {
				queued.current = { ...nextDraft, revision: result.state.revision };
				dirty.current = true;
			} else dirty.current = false;
			retries.current = 0;
			if (!conflicted.current) {
				setSaveStatus(nextDraft ? "saving" : "shared");
				setError(null);
			}
		} catch (reason) {
			queued.current ??= draft;
			const message = errorText(
				reason,
				"Drawing update failed. Local edits remain in browser.",
			);
			setError(message);
			const retryable =
				!(reason instanceof HttpError) ||
				reason.status === 408 ||
				reason.status === 429 ||
				reason.status >= 500;
			if (retryable && !conflicted.current && retries.current < 3) {
				retries.current++;
				setSaveStatus("retrying");
				retryTimer.current = window.setTimeout(
					() => {
						retryTimer.current = undefined;
						void saveScene();
					},
					250 * 2 ** retries.current,
				);
			} else {
				conflicted.current = true;
				setSaveStatus("blocked");
				setConflict(
					reason instanceof HttpError && reason.status === 409
						? "Shared drawing changed. Your drawing is still in this tab."
						: reason instanceof HttpError && reason.status === 413
							? "Drawing is too large to share. Save a copy, then remove some shapes or simplify strokes."
							: "Sharing failed. Your drawing is still in this tab.",
				);
			}
		} finally {
			saving.current = null;
			if (
				queued.current &&
				!conflicted.current &&
				retryTimer.current === undefined
			)
				void saveScene();
			else {
				announceActivity("settled");
				void reconcile();
			}
		}
	}, [announceActivity, install, reconcile, request]);

	const scheduleSave = useCallback((): void => {
		if (debounce.current !== undefined) window.clearTimeout(debounce.current);
		debounce.current = window.setTimeout(() => {
			debounce.current = undefined;
			void saveScene();
		}, 200);
	}, [saveScene]);

	const onChange = useCallback(
		(elements: readonly ExcalidrawElement[], appState: AppState): void => {
			const wasEditing = editingText.current;
			editingText.current = appState.editingTextElement !== null;
			if (!wasEditing && editingText.current) announceActivity("active");
			if (wasEditing && !editingText.current)
				announceActivity(pointerDown.current ? "active" : "settled");
			const next = fingerprint(elements as unknown as CanvasElement[]);
			if (suppressedFingerprint.current === next) {
				suppressedFingerprint.current = null;
				return;
			}
			if (next === editorFingerprint.current) {
				if (
					wasEditing &&
					!editingText.current &&
					!dirty.current &&
					!saving.current
				)
					void reconcile();
				return;
			}
			editorFingerprint.current = next;
			dirty.current = true;
			setSaveStatus(
				conflicted.current
					? "blocked"
					: retryTimer.current !== undefined
						? "retrying"
						: "saving",
			);
			setRecoveryChoice(null);
			localEpoch.current++;
			if (!pointerDown.current && !editingText.current)
				announceActivity("active");
			queued.current = {
				elements: structuredClone(elements) as unknown as CanvasElement[],
				revision: editorRevision.current,
				activity:
					editingText.current || pointerDown.current ? "active" : "settled",
			};
			scheduleSave();
		},
		[announceActivity, reconcile, scheduleSave],
	);

	const setApi = useCallback(
		(value: ExcalidrawImperativeAPI): void => {
			api.current = value;
			editorRevision.current =
				initialScene.current?.revision ?? canonical.current.revision;
			editorFingerprint.current = fingerprint(
				value.getSceneElementsIncludingDeleted() as unknown as CanvasElement[],
			);
			announceActivity("settled");
			void reconcile();
		},
		[announceActivity, reconcile],
	);

	useEffect(() => {
		if (!token) return;
		setConnectionError(connectionAttempt > 0 ? "Reconnecting…" : null);
		let closed = false;
		const controller = new AbortController();
		let reconnectTimer: number | undefined;
		let reconnects = 0;
		const connect = async (): Promise<void> => {
			let reader: ReadableStreamDefaultReader<Uint8Array> | undefined;
			try {
				setConnection("connecting");
				install(
					await response<CanvasState>(
						await fetch("/api/state", {
							headers: { Authorization: `Bearer ${token}` },
							signal: AbortSignal.any([
								controller.signal,
								AbortSignal.timeout(10000),
							]),
						}),
					),
					false,
				);
				await reconcile();
				const events = await fetch("/api/events", {
					headers: { Authorization: `Bearer ${token}` },
					signal: controller.signal,
				});
				if (!events.ok || !events.body)
					throw new HttpError(events.status, "Canvas event stream failed.");
				setConnection("connected");
				setConnectionError(null);
				reader = events.body.getReader();
				const decoder = new TextDecoder();
				let buffer = "";
				while (!closed) {
					const chunk = await reader.read();
					if (chunk.done) throw new Error("Canvas event stream disconnected.");
					buffer += decoder.decode(chunk.value, { stream: true });
					const records = buffer.split("\n\n");
					buffer = records.pop() ?? "";
					for (const record of records) {
						const data = record
							.split("\n")
							.find((line) => line.startsWith("data: "))
							?.slice(6);
						if (!data) continue;
						const next = JSON.parse(data) as CanvasState;
						install(next, false);
						if (next.revision > editorRevision.current) await reconcile();
					}
				}
			} catch (reason) {
				if (!closed) {
					setConnection("disconnected");
					setConnectionError(errorText(reason, "Canvas disconnected."));
					if (
						!(
							reason instanceof HttpError &&
							reason.status < 500 &&
							reason.status !== 408 &&
							reason.status !== 429
						) &&
						reconnects < 3
					) {
						reconnects++;
						reconnectTimer = window.setTimeout(
							() => void connect(),
							500 * 2 ** reconnects,
						);
					}
				}
			} finally {
				if (reader) {
					await reader.cancel().catch(() => undefined);
					reader.releaseLock();
				}
			}
		};
		void connect();
		return () => {
			closed = true;
			controller.abort();
			if (reconnectTimer !== undefined) window.clearTimeout(reconnectTimer);
		};
	}, [connectionAttempt, install, reconcile, token]);

	useEffect(() => {
		if (state?.revision !== undefined) announceActivity("settled");
	}, [announceActivity, state?.revision]);
	useEffect(() => {
		const resync = (): void => {
			if (document.visibilityState === "hidden") return;
			void reconcile();
			setConnectionAttempt((value) => value + 1);
		};
		addEventListener("focus", resync);
		addEventListener("pageshow", resync);
		document.addEventListener("visibilitychange", resync);
		return () => {
			removeEventListener("focus", resync);
			removeEventListener("pageshow", resync);
			document.removeEventListener("visibilitychange", resync);
		};
	}, [reconcile]);
	useEffect(() => {
		const heartbeat = window.setInterval(() => {
			if (pointerDown.current || editingText.current)
				announceActivity("active");
		}, 10000);
		return () => {
			window.clearInterval(heartbeat);
			if (settleTimer.current !== undefined)
				window.clearTimeout(settleTimer.current);
		};
	}, [announceActivity]);
	useEffect(() => {
		const onPageHide = (): void => {
			void request(
				{
					kind: "activity",
					clientId: CLIENT_ID,
					sequence: ++activitySequence.current,
					activity: "disconnected",
				},
				true,
			).catch(() => undefined);
		};
		addEventListener("pagehide", onPageHide);
		return () => {
			removeEventListener("pagehide", onPageHide);
		};
	}, [request]);

	useEffect(() => {
		const warn = (event: BeforeUnloadEvent): void => {
			if (!dirty.current && !saving.current) return;
			event.preventDefault();
			event.returnValue = "";
		};
		addEventListener("beforeunload", warn);
		return () => {
			mounted.current = false;
			activitySequence.current++;
			syncController.current?.abort();
			if (resyncRetry.current !== undefined)
				window.clearTimeout(resyncRetry.current);
			removeEventListener("beforeunload", warn);
			if (debounce.current !== undefined) window.clearTimeout(debounce.current);
			if (retryTimer.current !== undefined)
				window.clearTimeout(retryTimer.current);
		};
	}, []);

	const onLinkOpen: NonNullable<ExcalidrawProps["onLinkOpen"]> = (
		_element,
		event,
	) => event.preventDefault();

	const setFocus = useCallback((ids: string[]): void => {
		if (!api.current) return;
		if (ids.length)
			collaborators.current.set(PI_SOCKET, {
				username: "Pi",
				selectedElementIds: Object.fromEntries(ids.map((id) => [id, true])),
				color: { background: "#dbe4ff", stroke: "#4263eb" },
			});
		else collaborators.current.delete(PI_SOCKET);
		api.current.updateScene({ collaborators: new Map(collaborators.current) });
	}, []);

	useEffect(() => {
		setFocus(state?.focusIds ?? []);
	}, [setFocus, state?.focusIds]);

	const download = useCallback((): void => {
		if (!api.current) return;
		const data = serializeAsJSON(
			api.current.getSceneElementsIncludingDeleted(),
			api.current.getAppState(),
			api.current.getFiles(),
			"local",
		);
		const url = URL.createObjectURL(
			new Blob([data], { type: "application/json" }),
		);
		const link = document.createElement("a");
		link.href = url;
		link.download = "canvas-local.excalidraw";
		link.click();
		window.setTimeout(() => URL.revokeObjectURL(url), 1_000);
	}, []);

	const openNotice = useCallback((): void => {
		const notice = canonical.current.notice;
		if (notice) setSeenNoticeId(notice.id);
		api.current?.toggleSidebar({
			name: "pi-notifications",
			force: true,
		});
	}, []);

	const recover = useCallback(async (): Promise<void> => {
		if (!recoveryChoice || recovering || saving.current || !api.current) return;
		setRecovering(true);
		try {
			const latest = await response<CanvasState>(
				await fetch("/api/state", {
					headers: { Authorization: `Bearer ${token}` },
					signal: AbortSignal.timeout(10000),
				}),
			);
			install(latest, false);
			if (
				latest.revision !== recoveryChoice.revision ||
				localEpoch.current !== recoveryChoice.epoch ||
				pointerDown.current ||
				editingText.current
			)
				throw new Error(
					"Drawing changed again or is being edited. Review your choice again.",
				);
			if (retryTimer.current !== undefined)
				window.clearTimeout(retryTimer.current);
			retryTimer.current = undefined;
			retries.current = 0;
			conflicted.current = false;
			setConflict(null);
			setError(null);
			setRecoveryChoice(null);
			if (recoveryChoice.kind === "mine") {
				dirty.current = true;
				queued.current = {
					revision: latest.revision,
					elements: structuredClone(
						api.current.getSceneElementsIncludingDeleted(),
					) as unknown as CanvasElement[],
					activity: "settled",
				};
				await saveScene();
			} else {
				queued.current = null;
				dirty.current = false;
				setSaveStatus("shared");
				applyEditor(latest.elements, CaptureUpdateAction.NEVER);
			}
		} catch (reason) {
			setRecoveryChoice(null);
			setError(
				errorText(
					reason,
					"Sharing recovery failed. Your drawing is still in this tab.",
				),
			);
		} finally {
			setRecovering(false);
		}
	}, [applyEditor, install, recovering, recoveryChoice, saveScene, token]);

	if (!state)
		return (
			<main className="loading">
				<p>{error ?? connectionError ?? "Loading canvas…"}</p>
				{token && connection === "disconnected" ? (
					<button
						type="button"
						onClick={() => setConnectionAttempt((value) => value + 1)}
					>
						Reconnect
					</button>
				) : null}
			</main>
		);
	return (
		<main className="canvas" data-connection={connection}>
			<Excalidraw
				initialData={{
					elements: (initialScene.current?.elements ??
						state.elements) as unknown as readonly ExcalidrawElement[],
				}}
				excalidrawAPI={setApi}
				onChange={onChange}
				onPointerDown={() => {
					pointerDown.current = true;
					announceActivity("active");
				}}
				onPointerUp={() => {
					pointerDown.current = false;
					if (!dirty.current && !saving.current && !editingText.current)
						void reconcile();
					announceActivity(editingText.current ? "active" : "settled");
				}}
				validateEmbeddable={() => false}
				onLinkOpen={onLinkOpen}
				UIOptions={{
					canvasActions: {
						loadScene: false,
						saveToActiveFile: false,
						export: {},
						saveAsImage: true,
						toggleTheme: true,
						changeViewBackgroundColor: true,
						clearCanvas: true,
					},
					tools: { image: false },
				}}
			>
				<MainMenu>
					{state.notice ? (
						<>
							<MainMenu.Group title="Pi">
								<MainMenu.Item
									icon={<CanvasIcon name="notification" />}
									onSelect={openNotice}
								>
									Latest Pi notification
								</MainMenu.Item>
							</MainMenu.Group>
							<MainMenu.Separator />
						</>
					) : null}
					<MainMenu.Group title="Canvas">
						<MainMenu.Item
							icon={<CanvasIcon name="download" />}
							onSelect={download}
						>
							Download drawing
						</MainMenu.Item>
						<MainMenu.DefaultItems.SaveAsImage />
						<MainMenu.DefaultItems.ChangeCanvasBackground />
						<MainMenu.DefaultItems.ClearCanvas />
						<MainMenu.DefaultItems.ToggleTheme />
					</MainMenu.Group>
					<MainMenu.DefaultItems.Help />
				</MainMenu>
				<Sidebar
					name="pi-notifications"
					className="pi-sidebar"
					onStateChange={onSidebarStateChange}
				>
					<Sidebar.Header>Pi notifications</Sidebar.Header>
					<section aria-label="Latest Pi notification" className="pi-notice">
						<button
							type="button"
							className="pi-icon-button"
							aria-label="Close notification"
							onClick={() =>
								api.current?.toggleSidebar({
									name: "pi-notifications",
									force: false,
								})
							}
						>
							<CanvasIcon name="close" />
						</button>
						<pre>{state.notice?.text ?? ""}</pre>
					</section>
				</Sidebar>
			</Excalidraw>
			<div className="pi-footer" role="status" aria-label="Pi sharing status">
				Pi · {connection} ·{" "}
				{saveStatus === "shared"
					? "Shared with Pi"
					: saveStatus === "saving"
						? "Sharing…"
						: "Not shared with Pi"}
				{state.notice ? (
					<button
						type="button"
						className="pi-icon-button pi-notification-trigger"
						aria-label="Latest Pi notification"
						title="Latest Pi notification"
						onClick={openNotice}
					>
						<CanvasIcon name="notification" />
						{noticeUnread ? (
							<span className="notification-unread-dot" aria-hidden="true" />
						) : null}
					</button>
				) : null}
				{error && !conflict ? ` · ${error}` : ""}
				{connectionError ? ` · ${connectionError}` : ""}
				{connection === "disconnected" ? (
					<button
						type="button"
						onClick={() => setConnectionAttempt((value) => value + 1)}
					>
						Reconnect
					</button>
				) : null}
			</div>
			{saveStatus === "blocked" || saveStatus === "retrying" ? (
				<section className="conflict" aria-label="Drawing sharing">
					<strong>Not shared with Pi</strong>
					<p>
						{saveStatus === "retrying"
							? "Connection interrupted. Retrying sharing automatically…"
							: conflict}
					</p>
					{error ? <p>{error}</p> : null}
					<p>Pi cannot see your latest edits until sharing resumes.</p>
					{saveStatus === "blocked" ? (
						<>
							<div className="recovery-actions">
								<button
									type="button"
									disabled={recovering || saving.current !== null}
									onClick={() =>
										setRecoveryChoice({
											kind: "mine",
											revision: canonical.current.revision,
											epoch: localEpoch.current,
										})
									}
								>
									Keep my drawing
								</button>
								<button
									type="button"
									disabled={recovering || saving.current !== null}
									onClick={() =>
										setRecoveryChoice({
											kind: "shared",
											revision: canonical.current.revision,
											epoch: localEpoch.current,
										})
									}
								>
									Use shared drawing
								</button>
								<button type="button" onClick={download}>
									Save copy
								</button>
							</div>
							{recoveryChoice ? (
								<div className="recovery-confirmation">
									<p>
										{recoveryChoice.kind === "mine"
											? "Replace shared drawing with this tab's drawing? Other-tab or Pi edits will be replaced."
											: "Discard this tab's unsaved edits and use shared drawing? Save a copy first if you need them."}
									</p>
									<div className="recovery-actions">
										<button
											type="button"
											disabled={recovering}
											onClick={() => void recover()}
										>
											{recovering
												? "Updating…"
												: recoveryChoice.kind === "mine"
													? "Replace shared drawing"
													: "Discard my edits"}
										</button>
										<button
											type="button"
											disabled={recovering}
											onClick={() => setRecoveryChoice(null)}
										>
											Cancel
										</button>
									</div>
								</div>
							) : null}
						</>
					) : null}
				</section>
			) : null}
		</main>
	);
}

const root = document.getElementById("root");
if (!root) throw new Error("Canvas root is missing.");
createRoot(root).render(<App />);