The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import type { ScopedThreadRef } from "@t3tools/contracts"; |
| 2 | import { createStore } from "zustand"; |
| 3 | import type { ThreadPanelPresentation } from "../../rightPanelLayout"; |
| 4 | import type { DetailsCardMeasurement } from "./ChatCanvasContext"; |
| 5 | import { resolveChatCanvasLayout, type ChatCanvasPreview } from "./chatCanvasLayout"; |
| 6 | import { resolveThreadDetailsCardLayout } from "./threadDetailsCardLayout"; |
| 7 | |
| 8 | interface CanvasEdges { |
| 9 | readonly left: number; |
| 10 | readonly right: number; |
| 11 | readonly bottom: number; |
| 12 | } |
| 13 | |
| 14 | export interface ChatCanvasMeasurements { |
| 15 | readonly width: number; |
| 16 | readonly narrowestWidth?: number; |
| 17 | readonly height: number; |
| 18 | readonly padding: number; |
| 19 | readonly maxChatWidth: number; |
| 20 | readonly minChatWidth: number; |
| 21 | readonly composerHeight: number; |
| 22 | readonly timelineGutter: number; |
| 23 | /** The find bar's box in canvas coordinates while it is shown. */ |
| 24 | readonly findBar: CanvasEdges | null; |
| 25 | } |
| 26 | |
| 27 | interface ChatCanvasStoreState { |
| 28 | readonly measurements: ChatCanvasMeasurements; |
| 29 | readonly detailsCardMeasurement: DetailsCardMeasurement | null; |
| 30 | readonly setMeasurements: (next: ChatCanvasMeasurements) => void; |
| 31 | readonly measureDetailsCard: (next: DetailsCardMeasurement) => void; |
| 32 | } |
| 33 | |
| 34 | const INITIAL_MEASUREMENTS: ChatCanvasMeasurements = { |
| 35 | width: 0, |
| 36 | height: 0, |
| 37 | padding: 48, |
| 38 | maxChatWidth: 768, |
| 39 | minChatWidth: 640, |
| 40 | composerHeight: 0, |
| 41 | timelineGutter: 0, |
| 42 | findBar: null, |
| 43 | }; |
| 44 | |
| 45 | const sameEdges = (a: CanvasEdges | null, b: CanvasEdges | null) => |
| 46 | a === b || |
| 47 | (a !== null && b !== null && a.left === b.left && a.right === b.right && a.bottom === b.bottom); |
| 48 | |
| 49 | const sameMeasurements = (a: ChatCanvasMeasurements, b: ChatCanvasMeasurements) => |
| 50 | a.width === b.width && |
| 51 | a.narrowestWidth === b.narrowestWidth && |
| 52 | a.height === b.height && |
| 53 | a.padding === b.padding && |
| 54 | a.maxChatWidth === b.maxChatWidth && |
| 55 | a.minChatWidth === b.minChatWidth && |
| 56 | a.composerHeight === b.composerHeight && |
| 57 | a.timelineGutter === b.timelineGutter && |
| 58 | sameEdges(a.findBar, b.findBar); |
| 59 | |
| 60 | /** |
| 61 | * One store per chat view: the canvas measures into it, and the chat view's |
| 62 | * header controls derive the details card presentation from the same inputs. |
| 63 | */ |
| 64 | export const createChatCanvasStore = () => |
| 65 | createStore<ChatCanvasStoreState>()((set) => ({ |
| 66 | measurements: INITIAL_MEASUREMENTS, |
| 67 | detailsCardMeasurement: null, |
| 68 | setMeasurements: (next) => |
| 69 | set((state) => (sameMeasurements(state.measurements, next) ? state : { measurements: next })), |
| 70 | measureDetailsCard: (next) => |
| 71 | set((state) => { |
| 72 | const current = state.detailsCardMeasurement; |
| 73 | return current?.threadRef.environmentId === next.threadRef.environmentId && |
| 74 | current.threadRef.threadId === next.threadRef.threadId && |
| 75 | current.width === next.width && |
| 76 | current.fullContentHeight === next.fullContentHeight |
| 77 | ? state |
| 78 | : { detailsCardMeasurement: next }; |
| 79 | }), |
| 80 | })); |
| 81 | |
| 82 | export type ChatCanvasStore = ReturnType<typeof createChatCanvasStore>; |
| 83 | |
| 84 | export function resolveChatCanvasModel({ |
| 85 | measurements, |
| 86 | detailsCardMeasurement, |
| 87 | threadRef, |
| 88 | preview, |
| 89 | detailsCardInlineOpen, |
| 90 | }: { |
| 91 | measurements: ChatCanvasMeasurements; |
| 92 | detailsCardMeasurement: DetailsCardMeasurement | null; |
| 93 | /** The thread whose details card sits on the canvas; other threads' measurements are stale. */ |
| 94 | threadRef: ScopedThreadRef | null; |
| 95 | preview: ChatCanvasPreview | null; |
| 96 | detailsCardInlineOpen: boolean; |
| 97 | }) { |
| 98 | const container = { width: measurements.width, height: measurements.height }; |
| 99 | const { findBar } = measurements; |
| 100 | const detailsCardTopInset = findBar?.bottom ?? 0; |
| 101 | const lane = { padding: measurements.padding, minChatWidth: measurements.minChatWidth }; |
| 102 | // The preview avoids where an open inline card would sit, whether or not a |
| 103 | // preview overlap has pushed the card itself into popover mode. |
| 104 | const preferredDetailsCard = resolveThreadDetailsCardLayout({ |
| 105 | container, |
| 106 | narrowestWidth: measurements.narrowestWidth ?? measurements.width, |
| 107 | lane, |
| 108 | frame: null, |
| 109 | topInset: detailsCardTopInset, |
| 110 | }); |
| 111 | // The canvas outlives thread switches, so a measurement only counts for the |
| 112 | // thread whose card is shown now. |
| 113 | const detailsCardMeasured = |
| 114 | threadRef !== null && |
| 115 | detailsCardMeasurement !== null && |
| 116 | detailsCardMeasurement.threadRef.environmentId === threadRef.environmentId && |
| 117 | detailsCardMeasurement.threadRef.threadId === threadRef.threadId |
| 118 | ? detailsCardMeasurement |
| 119 | : null; |
| 120 | const detailsCard = |
| 121 | detailsCardInlineOpen && |
| 122 | preferredDetailsCard && |
| 123 | detailsCardMeasured?.width === preferredDetailsCard.width && |
| 124 | detailsCardMeasured.fullContentHeight > 0 |
| 125 | ? { |
| 126 | left: preferredDetailsCard.x, |
| 127 | right: preferredDetailsCard.x + preferredDetailsCard.width, |
| 128 | bottom: |
| 129 | preferredDetailsCard.y + |
| 130 | Math.min(detailsCardMeasured.fullContentHeight, preferredDetailsCard.height), |
| 131 | } |
| 132 | : null; |
| 133 | const layout = resolveChatCanvasLayout({ |
| 134 | container, |
| 135 | preview, |
| 136 | padding: measurements.padding, |
| 137 | maxChatWidth: measurements.maxChatWidth, |
| 138 | minChatWidth: measurements.minChatWidth, |
| 139 | composerHeight: measurements.composerHeight, |
| 140 | detailsCard, |
| 141 | findBar, |
| 142 | }); |
| 143 | const detailsCardPlacement = resolveThreadDetailsCardLayout({ |
| 144 | container, |
| 145 | narrowestWidth: measurements.narrowestWidth ?? measurements.width, |
| 146 | lane, |
| 147 | frame: layout.frame, |
| 148 | overlapsDetailsCard: layout.overlapsDetailsCard, |
| 149 | topInset: detailsCardTopInset, |
| 150 | }); |
| 151 | const detailsCardMode: ThreadPanelPresentation = detailsCardPlacement ? "inline" : "popover"; |
| 152 | return { |
| 153 | container, |
| 154 | lane, |
| 155 | layout, |
| 156 | detailsCardTopInset, |
| 157 | preferredDetailsCard, |
| 158 | detailsCardPlacement, |
| 159 | detailsCardMode, |
| 160 | }; |
| 161 | } |