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

adaptive-workspace-context.ts

80 lines3.2 KB

1import type { WorkspaceTab } from "../workspace/workspace-tabs-state";
2import { createContext, use, type ReactNode } from "react";
3import type { EnvironmentThreadShell } from "@t3tools/client-runtime/state/shell";
4import {
5 deriveFileInspectorPaneLayout,
6 deriveLayout,
7 deriveWorkspacePaneLayout,
8 type FileInspectorPaneLayout,
9 type Layout,
10 type WorkspaceAuxiliaryPaneRole,
11 type WorkspacePaneLayout,
12} from "../../lib/layout";
13​
14interface AdaptiveWorkspaceContextValue {
15 readonly layout: Layout;
16 readonly panes: WorkspacePaneLayout;
17 readonly fileInspector: FileInspectorPaneLayout;
18 readonly primarySidebarSearchQuery: string;
19 readonly startNewTask: () => void;
20 readonly deferThreadSelectionUntil: (operation: Promise<unknown>) => void;
21 readonly selectThread: (thread: EnvironmentThreadShell) => void;
22 readonly selectThreadRoute: (params: ReactNavigation.RootParamList["Thread"]) => void;
23 readonly activateAuxiliaryPaneRole: (role: WorkspaceAuxiliaryPaneRole) => () => void;
24 readonly registerWorkspaceInspector: (render: () => ReactNode) => () => void;
25 readonly setPrimarySidebarSearchQuery: (query: string) => void;
26 readonly showAuxiliaryPane: (role: WorkspaceAuxiliaryPaneRole) => void;
27 readonly toggleAuxiliaryPane: () => void;
28 readonly togglePrimarySidebar: () => void;
29 readonly setPrimarySidebarVisible: (visible: boolean) => void;
30 /** Opens a workspace tab with its list and, with columns, the page it last showed. */
31 readonly openWorkspaceTab: (
32 tab: WorkspaceTab | "settings",
33 options?: { readonly columns?: boolean },
34 ) => void;
35 readonly setAuxiliaryPaneWidth: (width: number) => void;
36 /**
37 * Android split view while the thread composer has the soft keyboard: the
38 * sidebar and rail step aside and the thread header drops to one line.
39 */
40 readonly composerKeyboardActive: boolean;
41 /** The thread composer reports its focus so the layout can make room for typing. */
42 readonly setComposerEditing: (editing: boolean) => void;
44​
45const compactLayout = deriveLayout({ width: 0, height: 0 });
46const compactPanes = deriveWorkspacePaneLayout({
47 layout: compactLayout,
48 viewportWidth: 0,
49 primarySidebarPreferredVisible: true,
50 auxiliaryPanePreferredVisible: true,
51});
52const compactFileInspector = deriveFileInspectorPaneLayout({
53 layout: compactLayout,
54 viewportWidth: 0,
55});
56export const AdaptiveWorkspaceContext = createContext<AdaptiveWorkspaceContextValue>({
57 layout: compactLayout,
58 panes: compactPanes,
59 fileInspector: compactFileInspector,
60 primarySidebarSearchQuery: "",
61 startNewTask: () => undefined,
62 deferThreadSelectionUntil: () => undefined,
63 selectThread: () => undefined,
64 selectThreadRoute: () => undefined,
65 activateAuxiliaryPaneRole: () => () => undefined,
66 registerWorkspaceInspector: () => () => undefined,
67 setPrimarySidebarSearchQuery: () => undefined,
68 showAuxiliaryPane: () => undefined,
69 toggleAuxiliaryPane: () => undefined,
70 togglePrimarySidebar: () => undefined,
71 setPrimarySidebarVisible: () => undefined,
72 openWorkspaceTab: () => undefined,
73 setAuxiliaryPaneWidth: () => undefined,
74 composerKeyboardActive: false,
75 setComposerEditing: () => undefined,
76});
77​
78export function useAdaptiveWorkspaceLayout(): AdaptiveWorkspaceContextValue {
79 return use(AdaptiveWorkspaceContext);