@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 03:37 UTC

main
ts 168 lines 6.16 KB
Raw
1 import { scopedThreadKey } from "@t3tools/client-runtime/environment";
2 import {
3 ChatFileAttachment,
4 INCOGNITO_BROWSER_PROFILE_ID,
5 PreviewSessionSnapshot,
6 ScopedThreadRef,
7 } from "@t3tools/contracts";
8 import * as Schema from "effect/Schema";
9 import { create } from "zustand";
10 import { createJSONStorage, persist } from "zustand/middleware";
11
12 import { decodeLinkedThreadSurface } from "./linkedThreadSurface";
13 import { resolveStorage } from "./lib/storage";
14 import { randomUUID } from "./lib/utils";
15 import { decodeThreadSurface, type RightPanelSurface } from "./rightPanelStore";
16
17 export type ClosedView =
18 | {
19 kind: "panel-tab";
20 threadRef: ScopedThreadRef;
21 surface: Exclude<RightPanelSurface, { kind: "terminal" }>;
22 }
23 | { kind: "browser"; threadRef: ScopedThreadRef; snapshot: PreviewSessionSnapshot };
24
25 export type ClosedViewEntry = ClosedView & { id: string };
26
27 interface ClosedViewStoreState {
28 entries: ClosedViewEntry[];
29 remember: (view: ClosedView) => string;
30 defer: (id: string) => void;
31 remove: (id: string) => void;
32 }
33
34 const sameTarget = (entry: ClosedViewEntry, view: ClosedView): boolean => {
35 if (
36 entry.kind !== view.kind ||
37 scopedThreadKey(entry.threadRef) !== scopedThreadKey(view.threadRef)
38 ) {
39 return false;
40 }
41 switch (entry.kind) {
42 case "panel-tab":
43 return view.kind === "panel-tab" && entry.surface.id === view.surface.id;
44 case "browser":
45 return view.kind === "browser" && entry.snapshot.tabId === view.snapshot.tabId;
46 }
47 };
48
49 const isPersistentView = (entry: ClosedViewEntry) =>
50 entry.kind !== "browser" || entry.snapshot.profileId !== INCOGNITO_BROWSER_PROFILE_ID;
51
52 const isThreadRef = Schema.is(ScopedThreadRef);
53 const isSnapshot = Schema.is(PreviewSessionSnapshot);
54 const isAttachment = Schema.is(ChatFileAttachment);
55
56 const isClosedViewEntry = (entry: unknown): entry is ClosedViewEntry => {
57 const view = entry as ClosedViewEntry | null;
58 if (!view || typeof view.id !== "string" || !isThreadRef(view.threadRef)) return false;
59 if (view.kind === "browser") return isSnapshot(view.snapshot);
60 if (view.kind !== "panel-tab") return false;
61 const surface = view.surface;
62 if (!surface || typeof surface.id !== "string") return false;
63 switch (surface.kind) {
64 case "diff":
65 case "files":
66 case "pull-requests":
67 return surface.id === surface.kind;
68 case "preview":
69 return surface.resourceId === null || typeof surface.resourceId === "string";
70 case "file":
71 return (
72 typeof surface.relativePath === "string" &&
73 (surface.revealLine === null || Number.isSafeInteger(surface.revealLine)) &&
74 Number.isSafeInteger(surface.revealRequestId) &&
75 (surface.attachment === undefined || isAttachment(surface.attachment))
76 );
77 case "device":
78 return (
79 (surface.title === undefined || typeof surface.title === "string") &&
80 (surface.target === undefined ||
81 (surface.target !== null &&
82 typeof surface.target.hostId === "string" &&
83 typeof surface.target.deviceId === "string" &&
84 typeof surface.target.name === "string" &&
85 (surface.target.platform === "ios" || surface.target.platform === "android")))
86 );
87 case "thread":
88 return typeof surface.threadId === "string" && surface.id === `thread:${surface.threadId}`;
89 case "linked-thread":
90 return (
91 (surface.title === null || typeof surface.title === "string") &&
92 decodeLinkedThreadSurface(surface)?.id === surface.id
93 );
94 case "pull-request":
95 return (
96 typeof surface.projectId === "string" &&
97 typeof surface.repository === "string" &&
98 Number.isSafeInteger(surface.number) &&
99 surface.number > 0 &&
100 (surface.environmentId === undefined || typeof surface.environmentId === "string") &&
101 (surface.host === undefined || typeof surface.host === "string") &&
102 (surface.url === undefined || typeof surface.url === "string")
103 );
104 case "issue":
105 return (
106 typeof surface.projectId === "string" &&
107 typeof surface.tracker === "string" &&
108 typeof surface.scope === "string" &&
109 typeof surface.key === "string" &&
110 (surface.environmentId === undefined || typeof surface.environmentId === "string") &&
111 (surface.host === undefined || typeof surface.host === "string") &&
112 (surface.url === undefined || typeof surface.url === "string")
113 );
114 default:
115 return false;
116 }
117 };
118
119 /** A closed side chat tab saved before its surface became `thread`. */
120 const renamedSideChatTab = (entry: unknown): unknown => {
121 const view = entry as { kind?: unknown; surface?: { kind?: unknown } | null } | null;
122 if (view?.kind !== "panel-tab" || view.surface?.kind !== "side-chat") return entry;
123 return { ...view, surface: decodeThreadSurface(view.surface) };
124 };
125
126 export const useClosedViewStore = create<ClosedViewStoreState>()(
127 persist(
128 (set) => ({
129 entries: [],
130 remember: (view) => {
131 const id = randomUUID();
132 set((state) => ({
133 entries: [
134 { ...view, id },
135 ...state.entries.filter((entry) => !sameTarget(entry, view)),
136 ].slice(0, 20),
137 }));
138 return id;
139 },
140 defer: (id) =>
141 set((state) => {
142 const entry = state.entries.find((entry) => entry.id === id);
143 return entry
144 ? { entries: [...state.entries.filter((entry) => entry.id !== id), entry] }
145 : state;
146 }),
147 remove: (id) =>
148 set((state) => ({ entries: state.entries.filter((entry) => entry.id !== id) })),
149 }),
150 {
151 name: "t3code:closed-views:v2",
152 storage: createJSONStorage(() =>
153 resolveStorage(typeof window !== "undefined" ? window.localStorage : undefined),
154 ),
155 version: 3,
156 migrate: (persisted) => {
157 const entries = (persisted as Partial<Pick<ClosedViewStoreState, "entries">> | null)
158 ?.entries;
159 return {
160 entries: Array.isArray(entries)
161 ? entries.map(renamedSideChatTab).filter(isClosedViewEntry).filter(isPersistentView)
162 : [],
163 };
164 },
165 partialize: ({ entries }) => ({ entries: entries.filter(isPersistentView) }),
166 },
167 ),
168 );