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