Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

adaptive-workspace-context.ts

87 lines3.5 KB

1import type { WorkspaceTab } from "../workspace/workspace-tabs-state";
2import { createContext, use, type ReactNode } from "react";
3import type {
4 WorkspaceDetail,
5 WorkspaceDetailSheet,
6} from "../workspace/workspace-detail-selection";
7import {
8 deriveFileInspectorPaneLayout,
9 deriveLayout,
10 deriveWorkspacePaneLayout,
11 type FileInspectorPaneLayout,
12 type Layout,
13 type WorkspaceAuxiliaryPaneRole,
14 type WorkspacePaneLayout,
15} from "../../lib/layout";
16​
17interface AdaptiveWorkspaceContextValue {
18 readonly layout: Layout;
19 readonly panes: WorkspacePaneLayout;
20 readonly fileInspector: FileInspectorPaneLayout;
21 readonly primarySidebarSearchQuery: string;
22 readonly startNewTask: () => void;
23 readonly deferThreadSelectionUntil: (operation: Promise<unknown>) => void;
24 /** Use `useSelectWorkspaceDetail`, which also works outside the layout. */
25 readonly selectDetail?: (detail: WorkspaceDetail, sheet?: WorkspaceDetailSheet) => void;
26 readonly activateAuxiliaryPaneRole: (role: WorkspaceAuxiliaryPaneRole) => () => void;
27 readonly registerWorkspaceInspector: (render: () => ReactNode) => () => void;
28 readonly setPrimarySidebarSearchQuery: (query: string) => void;
29 readonly showAuxiliaryPane: (role: WorkspaceAuxiliaryPaneRole) => void;
30 readonly toggleAuxiliaryPane: () => void;
31 readonly togglePrimarySidebar: () => void;
32 readonly setPrimarySidebarVisible: (visible: boolean) => void;
33 /** Opens a workspace tab with its list and, with columns, the page it last showed. */
34 readonly openWorkspaceTab: (
35 tab: WorkspaceTab | "settings",
36 options?: { readonly columns?: boolean },
37 ) => void;
38 readonly setAuxiliaryPaneWidth: (width: number) => void;
39 /**
40 * Android split view while the thread composer has the soft keyboard: the
41 * sidebar and rail step aside and the thread header drops to one line.
42 */
43 readonly composerKeyboardActive: boolean;
44 /**
45 * Whether the sidebar shows in each orientation, where it floats over the content in portrait
46 * (the unfolded Duo). The native split switches between them as a rotation starts.
47 */
48 readonly sidebarByOrientation: { readonly landscape: boolean; readonly portrait: boolean } | null;
49 /** The thread composer reports its focus so the layout can make room for typing. */
50 readonly setComposerEditing: (editing: boolean) => void;
52​
53const compactLayout = deriveLayout({ width: 0, height: 0 });
54const compactPanes = deriveWorkspacePaneLayout({
55 layout: compactLayout,
56 viewportWidth: 0,
57 primarySidebarPreferredVisible: true,
58 auxiliaryPanePreferredVisible: true,
59});
60const compactFileInspector = deriveFileInspectorPaneLayout({
61 layout: compactLayout,
62 viewportWidth: 0,
63});
64export const AdaptiveWorkspaceContext = createContext<AdaptiveWorkspaceContextValue>({
65 layout: compactLayout,
66 panes: compactPanes,
67 fileInspector: compactFileInspector,
68 primarySidebarSearchQuery: "",
69 startNewTask: () => undefined,
70 deferThreadSelectionUntil: () => undefined,
71 activateAuxiliaryPaneRole: () => () => undefined,
72 registerWorkspaceInspector: () => () => undefined,
73 setPrimarySidebarSearchQuery: () => undefined,
74 showAuxiliaryPane: () => undefined,
75 toggleAuxiliaryPane: () => undefined,
76 togglePrimarySidebar: () => undefined,
77 setPrimarySidebarVisible: () => undefined,
78 openWorkspaceTab: () => undefined,
79 setAuxiliaryPaneWidth: () => undefined,
80 composerKeyboardActive: false,
81 sidebarByOrientation: null,
82 setComposerEditing: () => undefined,
83});
84​
85export function useAdaptiveWorkspaceLayout(): AdaptiveWorkspaceContextValue {
86 return use(AdaptiveWorkspaceContext);