@morozinc/opencode-project-filesystem-plugin 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,389 @@
1
+ import type { Files, TokenValue, Ui } from "@opencode/plugin/gui";
2
+ import {
3
+ createEffect,
4
+ createMemo,
5
+ createResource,
6
+ createUniqueId,
7
+ For,
8
+ Match,
9
+ onCleanup,
10
+ Show,
11
+ Switch,
12
+ } from "solid-js";
13
+ import { createStore } from "solid-js/store";
14
+ import type { Change, Filesystem, SessionScope } from "./contract";
15
+ import { DiffEditor, FileEditor } from "./editors";
16
+ import { filename, ProjectTree } from "./project-tree";
17
+
18
+ const diffTab = Symbol("diff");
19
+ type EditorTab = string | typeof diffTab;
20
+ type Mode = "Project" | "Commit";
21
+
22
+ export function createWorkspaceState() {
23
+ return createStore({
24
+ mode: "Project" as Mode,
25
+ files: [] as string[],
26
+ diff: undefined as string | undefined,
27
+ active: undefined as EditorTab | undefined,
28
+ });
29
+ }
30
+
31
+ interface WorkspaceProps {
32
+ state: ReturnType<typeof createWorkspaceState>;
33
+ files: Files;
34
+ scope: SessionScope | undefined;
35
+ filesystem: TokenValue<typeof Filesystem>;
36
+ isVisible: boolean;
37
+ ui: Ui;
38
+ }
39
+
40
+ function errorMessage(error: unknown): string {
41
+ return error instanceof Error ? error.message : String(error);
42
+ }
43
+
44
+ export function Workspace(props: WorkspaceProps) {
45
+ const [state, setState] = props.state;
46
+ const tabs = (): readonly EditorTab[] =>
47
+ state.diff === undefined ? state.files : [diffTab, ...state.files];
48
+ const label = (tab: EditorTab) =>
49
+ tab === diffTab ? `Commit: ${filename(state.diff ?? "")}` : filename(tab);
50
+ const fullPath = (tab: EditorTab) => (tab === diffTab ? state.diff : tab);
51
+ const panelId = createUniqueId();
52
+ let tabStrip: HTMLDivElement | undefined;
53
+
54
+ const [changes, { refetch }] = createResource(
55
+ () =>
56
+ props.isVisible &&
57
+ (state.mode === "Commit" || state.active === diffTab) &&
58
+ props.scope,
59
+ (scope) => props.filesystem.changes(scope),
60
+ );
61
+ const rows = createMemo<readonly Change[]>((previous) => {
62
+ if (changes.error || !changes.latest) return [];
63
+ const byPath = new Map(previous.map((file) => [file.path, file]));
64
+ return changes.latest.files.map((file) => {
65
+ const earlier = byPath.get(file.path);
66
+ return earlier?.status === file.status ? earlier : file;
67
+ });
68
+ }, []);
69
+ const change = createMemo(
70
+ () =>
71
+ changes.error
72
+ ? undefined
73
+ : changes.latest?.files.find((file) => file.path === state.diff),
74
+ undefined,
75
+ {
76
+ equals: (left, right) =>
77
+ left?.path === right?.path && left?.version === right?.version,
78
+ },
79
+ );
80
+ const [comparison] = createResource(
81
+ () => {
82
+ const entry = change();
83
+ const scope = props.scope;
84
+ if (state.active !== diffTab || !entry || !scope) return false;
85
+ return { scope, path: entry.path };
86
+ },
87
+ (input) => props.filesystem.compare(input),
88
+ );
89
+
90
+ createEffect(() => {
91
+ if (
92
+ !props.isVisible ||
93
+ !props.scope ||
94
+ (state.mode !== "Commit" && state.active !== diffTab)
95
+ )
96
+ return;
97
+ const interval = setInterval(() => {
98
+ if (!changes.loading && !changes.error) void refetch();
99
+ }, 3_000);
100
+ onCleanup(() => clearInterval(interval));
101
+ });
102
+
103
+ createEffect(() => {
104
+ if (state.active === undefined) return;
105
+ tabStrip
106
+ ?.querySelector('[aria-selected="true"]')
107
+ ?.scrollIntoView({ block: "nearest", inline: "nearest" });
108
+ });
109
+
110
+ function openFile(path: string) {
111
+ if (!state.files.includes(path))
112
+ setState("files", (files) => [...files, path]);
113
+ setState("active", path);
114
+ }
115
+
116
+ function openDiff(path: string) {
117
+ setState({ diff: path, active: diffTab });
118
+ }
119
+
120
+ function closeTab(tab: EditorTab) {
121
+ const order = tabs();
122
+ const index = order.indexOf(tab);
123
+ if (state.active === tab)
124
+ setState("active", order[index + 1] ?? order[index - 1]);
125
+ if (tab === diffTab) setState("diff", undefined);
126
+ else setState("files", (files) => files.filter((file) => file !== tab));
127
+ }
128
+
129
+ function tabShortcut(event: KeyboardEvent, tab: EditorTab) {
130
+ const order = tabs();
131
+ const index = order.indexOf(tab);
132
+ const list = (event.currentTarget as HTMLElement).closest(
133
+ '[role="tablist"]',
134
+ );
135
+ switch (event.key) {
136
+ case "ArrowLeft":
137
+ setState("active", order[(index + order.length - 1) % order.length]);
138
+ break;
139
+ case "ArrowRight":
140
+ setState("active", order[(index + 1) % order.length]);
141
+ break;
142
+ case "Home":
143
+ setState("active", order[0]);
144
+ break;
145
+ case "End":
146
+ setState("active", order.at(-1));
147
+ break;
148
+ case "Delete":
149
+ closeTab(tab);
150
+ break;
151
+ default:
152
+ return;
153
+ }
154
+ event.preventDefault();
155
+ event.stopPropagation();
156
+ list?.querySelector<HTMLElement>('[aria-selected="true"]')?.focus();
157
+ }
158
+
159
+ return (
160
+ <section class="opf" aria-label="Project filesystem">
161
+ <nav class="opf-rail" aria-label="Filesystem mode">
162
+ <For each={["Project", "Commit"] as const}>
163
+ {(mode) => (
164
+ <button
165
+ type="button"
166
+ class="opf-icon-button"
167
+ aria-label={mode}
168
+ title={mode}
169
+ aria-pressed={state.mode === mode}
170
+ onClick={() => setState("mode", mode)}
171
+ >
172
+ <props.ui.Icon name={mode === "Project" ? "folder" : "branch"} />
173
+ </button>
174
+ )}
175
+ </For>
176
+ </nav>
177
+ <aside class="opf-sidebar" aria-label={`${state.mode} list`}>
178
+ <header class="opf-heading">
179
+ <span>{state.mode}</span>
180
+ <Show
181
+ when={
182
+ state.mode === "Commit" &&
183
+ props.scope &&
184
+ !changes.error &&
185
+ changes.latest
186
+ }
187
+ >
188
+ {(data) => (
189
+ <span
190
+ class="opf-count"
191
+ title={`Tracking since ${new Date(data().since).toLocaleString()}`}
192
+ >
193
+ {data().files.length}
194
+ </span>
195
+ )}
196
+ </Show>
197
+ <Show when={state.mode === "Commit" && props.scope}>
198
+ <button
199
+ type="button"
200
+ class="opf-icon-button opf-refresh"
201
+ aria-label="Refresh changes"
202
+ title="Refresh changes"
203
+ onClick={() => void refetch()}
204
+ >
205
+ <props.ui.Icon name="outline-rotate-clockwise" size="small" />
206
+ </button>
207
+ </Show>
208
+ </header>
209
+ <div class="opf-sidebar-scroll">
210
+ <Switch>
211
+ <Match when={state.mode === "Project"}>
212
+ <ProjectTree
213
+ files={props.files}
214
+ ui={props.ui}
215
+ active={
216
+ typeof state.active === "string" ? state.active : undefined
217
+ }
218
+ onFile={openFile}
219
+ />
220
+ </Match>
221
+ <Match when={!props.scope}>
222
+ <div class="opf-note">
223
+ Change tracking is available for local desktop sessions.
224
+ </div>
225
+ </Match>
226
+ <Match when={changes.error}>
227
+ {(error) => (
228
+ <div class="opf-note" role="alert">
229
+ {errorMessage(error())}
230
+ </div>
231
+ )}
232
+ </Match>
233
+ <Match when={changes.latest}>
234
+ <Show
235
+ when={rows().length}
236
+ fallback={
237
+ <div class="opf-note">No changes in this session.</div>
238
+ }
239
+ >
240
+ <ul class="opf-changes" aria-label="Changed files">
241
+ <For each={rows()}>
242
+ {(file) => (
243
+ <li>
244
+ <button
245
+ type="button"
246
+ class="opf-row"
247
+ aria-current={
248
+ state.active === diffTab && state.diff === file.path
249
+ ? "true"
250
+ : undefined
251
+ }
252
+ aria-label={`${file.status}: ${file.path}`}
253
+ title={`${file.status}: ${file.path}`}
254
+ onClick={() => openDiff(file.path)}
255
+ >
256
+ <span
257
+ class={`opf-status ${file.status}`}
258
+ aria-hidden="true"
259
+ >
260
+ {file.status[0].toUpperCase()}
261
+ </span>
262
+ <span class="opf-change-name opf-truncate">
263
+ {filename(file.path)}
264
+ </span>
265
+ <span class="opf-directory opf-truncate">
266
+ {file.path.slice(0, file.path.lastIndexOf("/") + 1)}
267
+ </span>
268
+ </button>
269
+ </li>
270
+ )}
271
+ </For>
272
+ </ul>
273
+ </Show>
274
+ </Match>
275
+ <Match when={true}>
276
+ <div class="opf-note" role="status">
277
+ Reading workspace changes…
278
+ </div>
279
+ </Match>
280
+ </Switch>
281
+ </div>
282
+ </aside>
283
+ <div class="opf-editors">
284
+ <div
285
+ ref={tabStrip}
286
+ role="tablist"
287
+ class="opf-tabs"
288
+ aria-label="Open filesystem tabs"
289
+ >
290
+ <For each={tabs()}>
291
+ {(tab) => (
292
+ <div
293
+ class="opf-tab"
294
+ classList={{ "opf-selected": state.active === tab }}
295
+ role="presentation"
296
+ >
297
+ <button
298
+ type="button"
299
+ role="tab"
300
+ class="opf-tab-select"
301
+ aria-selected={state.active === tab}
302
+ aria-controls={panelId}
303
+ title={fullPath(tab)}
304
+ tabindex={state.active === tab ? 0 : -1}
305
+ onKeyDown={(event) => tabShortcut(event, tab)}
306
+ onClick={() => setState("active", tab)}
307
+ >
308
+ <props.ui.Icon
309
+ name={tab === diffTab ? "branch" : "file-document"}
310
+ size="small"
311
+ />
312
+ <span class="opf-truncate">{label(tab)}</span>
313
+ </button>
314
+ <button
315
+ type="button"
316
+ class="opf-icon-button opf-close"
317
+ aria-label={`Close ${label(tab)}`}
318
+ title={`Close ${label(tab)}`}
319
+ tabindex={-1}
320
+ onClick={() => closeTab(tab)}
321
+ >
322
+ <props.ui.Icon name="close-small" size="small" />
323
+ </button>
324
+ </div>
325
+ )}
326
+ </For>
327
+ </div>
328
+ <div
329
+ class="opf-editor"
330
+ id={panelId}
331
+ role="tabpanel"
332
+ aria-label={
333
+ state.active === undefined ? "File preview" : label(state.active)
334
+ }
335
+ >
336
+ <Switch
337
+ fallback={<div class="opf-empty">Select a file to open it.</div>}
338
+ >
339
+ <Match when={state.active === diffTab}>
340
+ <Switch
341
+ fallback={
342
+ <div class="opf-note" role="status">
343
+ Loading comparison…
344
+ </div>
345
+ }
346
+ >
347
+ <Match when={changes.error || comparison.error}>
348
+ <div class="opf-note" role="alert">
349
+ {errorMessage(changes.error || comparison.error)}
350
+ </div>
351
+ </Match>
352
+ <Match when={!changes.loading && !change()}>
353
+ <div class="opf-note">
354
+ This file has no remaining session changes.
355
+ </div>
356
+ </Match>
357
+ <Match when={!comparison.loading && comparison()}>
358
+ {(data) => (
359
+ <DiffEditor before={data().before} after={data().after} />
360
+ )}
361
+ </Match>
362
+ </Switch>
363
+ </Match>
364
+ <Match
365
+ when={typeof state.active === "string" && state.active}
366
+ keyed
367
+ >
368
+ {(path) => (
369
+ <FileEditor files={props.files} ui={props.ui} path={path} />
370
+ )}
371
+ </Match>
372
+ </Switch>
373
+ <Show when={state.active !== undefined && fullPath(state.active)}>
374
+ {(path) => (
375
+ <footer
376
+ class="opf-path"
377
+ tabindex={0}
378
+ aria-label="File path"
379
+ title={path()}
380
+ >
381
+ {path()}
382
+ </footer>
383
+ )}
384
+ </Show>
385
+ </div>
386
+ </div>
387
+ </section>
388
+ );
389
+ }