droboticflow/src/context/AnnotationWorkspaceLayoutContext.tsx

42 lines
1.6 KiB
TypeScript

import { createContext, useContext, useMemo, useState } from "react";
type WorkspaceLayout = "focused" | "original";
type LayoutPreference = { layout: WorkspaceLayout; toggleLayout: () => void };
export const AnnotationWorkspaceLayoutContext = createContext<LayoutPreference | null>(null);
function readPreference(_userId: string): WorkspaceLayout {
// Dedicated work pages now have a single focused layout. Old stored original
// preferences must not change the application shell on first render.
return "focused";
}
// Presentation has no task/session identity. Account changes read that account's
// preference synchronously, so even the first render cannot reuse another user's.
export function useAnnotationLayoutPreference(userId: string): LayoutPreference {
const [choice, setChoice] = useState<{ userId: string; layout: WorkspaceLayout } | null>(null);
const layout = choice?.userId === userId ? choice.layout : readPreference(userId);
return useMemo(
() => ({
layout,
toggleLayout: () => {
if (!userId) return;
const next = layout === "focused" ? "original" : "focused";
setChoice({ userId, layout: next });
try {
window.localStorage.setItem(`droboticflow.annotation-layout:${userId}`, next);
} catch {
// Storage restrictions must not prevent an in-place fallback.
}
},
}),
[layout, userId],
);
}
export function useAnnotationWorkspaceLayout() {
const value = useContext(AnnotationWorkspaceLayoutContext);
if (!value) throw new Error("Annotation workspace layout requires the application shell");
return value;
}