@nandoblanco/opencode-local-backup 1.0.3 → 1.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.
- package/README.md +37 -15
- package/dist/gui-experimental.cjs +1 -0
- package/gui.css +636 -0
- package/gui.tsx +1253 -0
- package/index.js +84 -0
- package/package.json +32 -5
- package/rpc.d.ts +145 -0
- package/rpc.js +237 -0
- package/tui.ts +2 -2
package/gui.tsx
ADDED
|
@@ -0,0 +1,1253 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Command,
|
|
3
|
+
MenuItem,
|
|
4
|
+
Panel,
|
|
5
|
+
Plugin,
|
|
6
|
+
Slot,
|
|
7
|
+
Style,
|
|
8
|
+
type PanelTab,
|
|
9
|
+
type PluginContext,
|
|
10
|
+
type UiMenuEntry,
|
|
11
|
+
} from "@opencode/plugin/gui";
|
|
12
|
+
import { For, Show, createEffect, createMemo, createSignal, onCleanup, onMount } from "solid-js";
|
|
13
|
+
import cssText from "./gui.css?inline";
|
|
14
|
+
import {
|
|
15
|
+
BackupRpc,
|
|
16
|
+
type BackupDiffLineDto,
|
|
17
|
+
type BackupEntryDto,
|
|
18
|
+
type BackupSettingsDto,
|
|
19
|
+
} from "./rpc.js";
|
|
20
|
+
|
|
21
|
+
type ToolFilter = "all" | "edit" | "write" | "patch" | "pre-restore";
|
|
22
|
+
type DateFilter = "all" | "24h" | "7d" | "30d";
|
|
23
|
+
type ScopeMode = "scan-root" | "workspace";
|
|
24
|
+
|
|
25
|
+
const TOOL_CYCLE: ToolFilter[] = ["all", "edit", "write", "patch", "pre-restore"];
|
|
26
|
+
const DATE_CYCLE: DateFilter[] = ["all", "24h", "7d", "30d"];
|
|
27
|
+
const RETENTION_PRESETS: number[] = [30, 14, 7, 5, 0];
|
|
28
|
+
|
|
29
|
+
function formatAge(timestampMs: number): string {
|
|
30
|
+
if (!timestampMs) return "unknown";
|
|
31
|
+
const diffSec = Math.max(0, Math.floor((Date.now() - timestampMs) / 1000));
|
|
32
|
+
if (diffSec < 60) return `${diffSec}s ago`;
|
|
33
|
+
const diffMin = Math.floor(diffSec / 60);
|
|
34
|
+
if (diffMin < 60) return `${diffMin}m ago`;
|
|
35
|
+
const diffHr = Math.floor(diffMin / 60);
|
|
36
|
+
if (diffHr < 24) return `${diffHr}h ago`;
|
|
37
|
+
const diffDay = Math.floor(diffHr / 24);
|
|
38
|
+
if (diffDay < 30) return `${diffDay}d ago`;
|
|
39
|
+
return new Date(timestampMs).toISOString().slice(0, 10);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function formatDateTime(timestampMs: number): string {
|
|
43
|
+
if (!timestampMs) return "unknown";
|
|
44
|
+
const d = new Date(timestampMs);
|
|
45
|
+
const pad = (n: number) => String(n).padStart(2, "0");
|
|
46
|
+
return `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())} ${pad(d.getHours())}:${pad(d.getMinutes())}:${pad(d.getSeconds())}`;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function formatBytes(bytes: number): string {
|
|
50
|
+
if (bytes < 1024) return `${bytes} B`;
|
|
51
|
+
if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`;
|
|
52
|
+
return `${(bytes / (1024 * 1024)).toFixed(2)} MB`;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function getBackupDisplayPath(backupPath: string): string {
|
|
56
|
+
const normalized = String(backupPath || "").replace(/\\/g, "/");
|
|
57
|
+
const idx = normalized.indexOf("/.backups/");
|
|
58
|
+
if (idx <= 0) return normalized;
|
|
59
|
+
const projectRoot = normalized.slice(0, idx);
|
|
60
|
+
const lastSlash = projectRoot.lastIndexOf("/");
|
|
61
|
+
const parentRoot = lastSlash > 0 ? projectRoot.slice(0, lastSlash) : "";
|
|
62
|
+
if (parentRoot && normalized.startsWith(parentRoot + "/")) {
|
|
63
|
+
return normalized.slice(parentRoot.length + 1);
|
|
64
|
+
}
|
|
65
|
+
return normalized;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function splitHighlightSegments(
|
|
69
|
+
text: string,
|
|
70
|
+
terms: string[],
|
|
71
|
+
): Array<{ text: string; match: boolean }> {
|
|
72
|
+
const source = String(text || "");
|
|
73
|
+
if (!source) return [{ text: "", match: false }];
|
|
74
|
+
const cleanTerms = terms
|
|
75
|
+
.map((t) => t.trim().toLowerCase())
|
|
76
|
+
.filter((t) => t.length > 0);
|
|
77
|
+
if (cleanTerms.length === 0) return [{ text: source, match: false }];
|
|
78
|
+
|
|
79
|
+
const lower = source.toLowerCase();
|
|
80
|
+
const intervals: Array<[number, number]> = [];
|
|
81
|
+
for (const term of cleanTerms) {
|
|
82
|
+
let idx = 0;
|
|
83
|
+
while (idx < lower.length) {
|
|
84
|
+
const found = lower.indexOf(term, idx);
|
|
85
|
+
if (found === -1) break;
|
|
86
|
+
intervals.push([found, found + term.length]);
|
|
87
|
+
idx = found + Math.max(1, term.length);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
if (intervals.length === 0) return [{ text: source, match: false }];
|
|
91
|
+
intervals.sort((a, b) => a[0] - b[0] || a[1] - b[1]);
|
|
92
|
+
|
|
93
|
+
const merged: Array<[number, number]> = [intervals[0]!];
|
|
94
|
+
for (let i = 1; i < intervals.length; i++) {
|
|
95
|
+
const prev = merged[merged.length - 1]!;
|
|
96
|
+
const curr = intervals[i]!;
|
|
97
|
+
if (curr[0] <= prev[1]) {
|
|
98
|
+
prev[1] = Math.max(prev[1], curr[1]);
|
|
99
|
+
} else {
|
|
100
|
+
merged.push([curr[0], curr[1]]);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const segments: Array<{ text: string; match: boolean }> = [];
|
|
105
|
+
let cursor = 0;
|
|
106
|
+
for (const [start, end] of merged) {
|
|
107
|
+
if (start > cursor) {
|
|
108
|
+
segments.push({ text: source.slice(cursor, start), match: false });
|
|
109
|
+
}
|
|
110
|
+
segments.push({ text: source.slice(start, end), match: true });
|
|
111
|
+
cursor = end;
|
|
112
|
+
}
|
|
113
|
+
if (cursor < source.length) {
|
|
114
|
+
segments.push({ text: source.slice(cursor), match: false });
|
|
115
|
+
}
|
|
116
|
+
return segments;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function HighlightedText(props: { text: string; terms: string[] }) {
|
|
120
|
+
const segments = createMemo(() => splitHighlightSegments(props.text, props.terms));
|
|
121
|
+
return (
|
|
122
|
+
<For each={segments()}>
|
|
123
|
+
{(seg) => (
|
|
124
|
+
<Show when={seg.match} fallback={<>{seg.text}</>}>
|
|
125
|
+
<mark class="oc-lb-mark">{seg.text}</mark>
|
|
126
|
+
</Show>
|
|
127
|
+
)}
|
|
128
|
+
</For>
|
|
129
|
+
);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
function getRpcClient(ctx: PluginContext) {
|
|
133
|
+
const activeSession = ctx.sessions.current() as any;
|
|
134
|
+
if (activeSession?.server?.client?.rpc) {
|
|
135
|
+
return activeSession.server.client.rpc(BackupRpc);
|
|
136
|
+
}
|
|
137
|
+
const servers = (ctx as any).servers;
|
|
138
|
+
if (servers) {
|
|
139
|
+
const list = typeof servers.list === "function" ? servers.list() : [];
|
|
140
|
+
for (const s of list) {
|
|
141
|
+
if (s?.client?.rpc) return s.client.rpc(BackupRpc);
|
|
142
|
+
if (s?.id && typeof servers.get === "function") {
|
|
143
|
+
const handle = servers.get(s.id);
|
|
144
|
+
if (handle?.client?.rpc) return handle.client.rpc(BackupRpc);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
if (typeof servers.get === "function") {
|
|
148
|
+
const local = servers.get("local") || servers.get("default");
|
|
149
|
+
if (local?.client?.rpc) return local.client.rpc(BackupRpc);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
const directClient = (ctx as any).client;
|
|
153
|
+
if (directClient?.rpc) {
|
|
154
|
+
return directClient.rpc(BackupRpc);
|
|
155
|
+
}
|
|
156
|
+
return null;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
function BackupManagerView(props: {
|
|
160
|
+
ctx: PluginContext;
|
|
161
|
+
surface: "panel" | "modal";
|
|
162
|
+
initialQuery?: string;
|
|
163
|
+
onClose?: () => void;
|
|
164
|
+
}) {
|
|
165
|
+
const { ui } = props.ctx;
|
|
166
|
+
|
|
167
|
+
const [allEntries, setAllEntries] = createSignal<BackupEntryDto[]>([]);
|
|
168
|
+
const [scanRoot, setScanRoot] = createSignal<string>("");
|
|
169
|
+
const [workspaceDir, setWorkspaceDir] = createSignal<string>("");
|
|
170
|
+
const [scopeMode, setScopeMode] = createSignal<ScopeMode>("scan-root");
|
|
171
|
+
const [settings, setSettings] = createSignal<BackupSettingsDto>({
|
|
172
|
+
retentionDays: 30,
|
|
173
|
+
debounceMs: 0,
|
|
174
|
+
rootDir: "",
|
|
175
|
+
dialogWidth: "80%",
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
const [query, setQuery] = createSignal(props.initialQuery || "");
|
|
179
|
+
|
|
180
|
+
// External openers (commands, intercepted clicks) push the query here.
|
|
181
|
+
createEffect(() => {
|
|
182
|
+
const q = ocLbPanelQuery();
|
|
183
|
+
if (q) setQuery(q);
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
const [toolFilter, setToolFilter] = createSignal<ToolFilter>("all");
|
|
187
|
+
const [dateFilter, setDateFilter] = createSignal<DateFilter>("all");
|
|
188
|
+
const [selectedIndex, setSelectedIndex] = createSignal(0);
|
|
189
|
+
const [loading, setLoading] = createSignal(true);
|
|
190
|
+
const [diffLoading, setDiffLoading] = createSignal(false);
|
|
191
|
+
const [diffState, setDiffState] = createSignal<{
|
|
192
|
+
status: string;
|
|
193
|
+
summary: string;
|
|
194
|
+
lines: BackupDiffLineDto[];
|
|
195
|
+
} | null>(null);
|
|
196
|
+
const [confirmRestoreModal, setConfirmRestoreModal] = createSignal(false);
|
|
197
|
+
const [statusMsg, setStatusMsg] = createSignal<{
|
|
198
|
+
text: string;
|
|
199
|
+
kind: "ok" | "warn" | "err";
|
|
200
|
+
} | null>(null);
|
|
201
|
+
|
|
202
|
+
let searchInputRef: HTMLInputElement | undefined;
|
|
203
|
+
const rowRefs = new Map<number, HTMLDivElement>();
|
|
204
|
+
|
|
205
|
+
const colorScheme = createMemo(() => {
|
|
206
|
+
const pref = props.ctx.appearance.mode();
|
|
207
|
+
if (pref === "light" || pref === "dark") return pref;
|
|
208
|
+
if (typeof window !== "undefined" && typeof window.matchMedia === "function") {
|
|
209
|
+
return window.matchMedia("(prefers-color-scheme: light)").matches ? "light" : "dark";
|
|
210
|
+
}
|
|
211
|
+
return "dark";
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
function flashStatus(text: string, kind: "ok" | "warn" | "err" = "ok") {
|
|
215
|
+
setStatusMsg({ text, kind });
|
|
216
|
+
if (typeof window !== "undefined") {
|
|
217
|
+
window.setTimeout(() => {
|
|
218
|
+
setStatusMsg((curr) => (curr?.text === text ? null : curr));
|
|
219
|
+
}, 4500);
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
async function refreshBackups(keepId?: string) {
|
|
224
|
+
const rpc = getRpcClient(props.ctx);
|
|
225
|
+
if (!rpc) {
|
|
226
|
+
setLoading(false);
|
|
227
|
+
flashStatus("No active OpenCode server session found for Backup RPC.", "err");
|
|
228
|
+
return;
|
|
229
|
+
}
|
|
230
|
+
setLoading(true);
|
|
231
|
+
try {
|
|
232
|
+
const useWorkspace = scopeMode() === "workspace" && workspaceDir();
|
|
233
|
+
const res = await rpc.list({
|
|
234
|
+
rootDir: useWorkspace ? workspaceDir() : undefined,
|
|
235
|
+
includeSubprojects: true,
|
|
236
|
+
limit: 500,
|
|
237
|
+
});
|
|
238
|
+
setScanRoot(res.scanRoot || "");
|
|
239
|
+
setWorkspaceDir(res.workspaceDir || "");
|
|
240
|
+
if (res.settings) setSettings(res.settings);
|
|
241
|
+
const nextEntries = Array.isArray(res.entries) ? res.entries : [];
|
|
242
|
+
setAllEntries(nextEntries);
|
|
243
|
+
if (keepId) {
|
|
244
|
+
const idx = filteredEntries().findIndex((e) => e.id === keepId);
|
|
245
|
+
setSelectedIndex(idx >= 0 ? idx : 0);
|
|
246
|
+
} else {
|
|
247
|
+
setSelectedIndex((prev) =>
|
|
248
|
+
Math.min(prev, Math.max(0, filteredEntries().length - 1)),
|
|
249
|
+
);
|
|
250
|
+
}
|
|
251
|
+
} catch (err: any) {
|
|
252
|
+
flashStatus(`Failed to load backups: ${err?.message || String(err)}`, "err");
|
|
253
|
+
} finally {
|
|
254
|
+
setLoading(false);
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
const queryTerms = createMemo(() => {
|
|
259
|
+
const q = query().trim().toLowerCase();
|
|
260
|
+
return q ? q.split(/\s+/).filter(Boolean) : [];
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
const filteredEntries = createMemo(() => {
|
|
264
|
+
const terms = queryTerms();
|
|
265
|
+
const tf = toolFilter();
|
|
266
|
+
const df = dateFilter();
|
|
267
|
+
const now = Date.now();
|
|
268
|
+
const maxAgeMs =
|
|
269
|
+
df === "24h"
|
|
270
|
+
? 24 * 60 * 60 * 1000
|
|
271
|
+
: df === "7d"
|
|
272
|
+
? 7 * 24 * 60 * 60 * 1000
|
|
273
|
+
: df === "30d"
|
|
274
|
+
? 30 * 24 * 60 * 60 * 1000
|
|
275
|
+
: 0;
|
|
276
|
+
|
|
277
|
+
return allEntries().filter((entry) => {
|
|
278
|
+
if (tf !== "all" && entry.tool !== tf) return false;
|
|
279
|
+
if (maxAgeMs > 0 && now - entry.timeMs > maxAgeMs) return false;
|
|
280
|
+
if (terms.length > 0) {
|
|
281
|
+
const hay = `${entry.relativePath} ${entry.backupPath} ${entry.originalPath} ${entry.root} ${entry.snapshotId} ${entry.slug}`.toLowerCase();
|
|
282
|
+
if (!terms.every((t) => hay.includes(t))) return false;
|
|
283
|
+
}
|
|
284
|
+
return true;
|
|
285
|
+
});
|
|
286
|
+
});
|
|
287
|
+
|
|
288
|
+
createEffect(() => {
|
|
289
|
+
const list = filteredEntries();
|
|
290
|
+
if (selectedIndex() >= list.length) {
|
|
291
|
+
setSelectedIndex(Math.max(0, list.length - 1));
|
|
292
|
+
}
|
|
293
|
+
});
|
|
294
|
+
|
|
295
|
+
const selectedEntry = createMemo(() => {
|
|
296
|
+
const list = filteredEntries();
|
|
297
|
+
return list[selectedIndex()] || null;
|
|
298
|
+
});
|
|
299
|
+
|
|
300
|
+
createEffect(() => {
|
|
301
|
+
const entry = selectedEntry();
|
|
302
|
+
const rpc = getRpcClient(props.ctx);
|
|
303
|
+
if (!entry || !rpc) {
|
|
304
|
+
setDiffState(null);
|
|
305
|
+
return;
|
|
306
|
+
}
|
|
307
|
+
let cancelled = false;
|
|
308
|
+
setDiffLoading(true);
|
|
309
|
+
rpc
|
|
310
|
+
.diff({
|
|
311
|
+
backupPath: entry.backupPath,
|
|
312
|
+
originalPath: entry.originalPath,
|
|
313
|
+
maxLines: 220,
|
|
314
|
+
})
|
|
315
|
+
.then((res) => {
|
|
316
|
+
if (!cancelled) {
|
|
317
|
+
setDiffState(res);
|
|
318
|
+
setDiffLoading(false);
|
|
319
|
+
}
|
|
320
|
+
})
|
|
321
|
+
.catch((err) => {
|
|
322
|
+
if (!cancelled) {
|
|
323
|
+
setDiffState({
|
|
324
|
+
status: "error",
|
|
325
|
+
summary: `Diff error: ${err?.message || String(err)}`,
|
|
326
|
+
lines: [],
|
|
327
|
+
});
|
|
328
|
+
setDiffLoading(false);
|
|
329
|
+
}
|
|
330
|
+
});
|
|
331
|
+
onCleanup(() => {
|
|
332
|
+
cancelled = true;
|
|
333
|
+
});
|
|
334
|
+
});
|
|
335
|
+
|
|
336
|
+
function scrollSelectedIntoView(idx: number) {
|
|
337
|
+
const el = rowRefs.get(idx);
|
|
338
|
+
if (el && typeof el.scrollIntoView === "function") {
|
|
339
|
+
el.scrollIntoView({ block: "nearest" });
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
function moveSelection(delta: number) {
|
|
344
|
+
const len = filteredEntries().length;
|
|
345
|
+
if (len === 0) return;
|
|
346
|
+
const next = Math.max(0, Math.min(len - 1, selectedIndex() + delta));
|
|
347
|
+
setSelectedIndex(next);
|
|
348
|
+
scrollSelectedIntoView(next);
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
function cycleToolFilter() {
|
|
352
|
+
const next = TOOL_CYCLE[(TOOL_CYCLE.indexOf(toolFilter()) + 1) % TOOL_CYCLE.length]!;
|
|
353
|
+
setToolFilter(next);
|
|
354
|
+
setSelectedIndex(0);
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
function cycleDateFilter() {
|
|
358
|
+
const next = DATE_CYCLE[(DATE_CYCLE.indexOf(dateFilter()) + 1) % DATE_CYCLE.length]!;
|
|
359
|
+
setDateFilter(next);
|
|
360
|
+
setSelectedIndex(0);
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
async function updateRetentionDays(nextDays: number) {
|
|
364
|
+
const rpc = getRpcClient(props.ctx);
|
|
365
|
+
if (!rpc) return;
|
|
366
|
+
try {
|
|
367
|
+
const res = await rpc.updateSettings({ retentionDays: nextDays });
|
|
368
|
+
if (res?.settings) setSettings(res.settings);
|
|
369
|
+
const label = nextDays === 0 ? "never auto-prune (0d)" : `${nextDays} days`;
|
|
370
|
+
flashStatus(`Updated backup retention to ${label}`, "ok");
|
|
371
|
+
} catch (err: any) {
|
|
372
|
+
flashStatus(`Failed to update retention: ${err?.message || String(err)}`, "err");
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
function cycleRetentionDays() {
|
|
377
|
+
const current = settings().retentionDays;
|
|
378
|
+
const idx = RETENTION_PRESETS.indexOf(current);
|
|
379
|
+
const next =
|
|
380
|
+
idx >= 0
|
|
381
|
+
? RETENTION_PRESETS[(idx + 1) % RETENTION_PRESETS.length]!
|
|
382
|
+
: RETENTION_PRESETS[0]!;
|
|
383
|
+
void updateRetentionDays(next);
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
async function handlePruneNow() {
|
|
387
|
+
const rpc = getRpcClient(props.ctx);
|
|
388
|
+
if (!rpc) return;
|
|
389
|
+
const days = settings().retentionDays;
|
|
390
|
+
if (days <= 0) {
|
|
391
|
+
flashStatus("Retention is set to never (0d); nothing pruned.", "warn");
|
|
392
|
+
return;
|
|
393
|
+
}
|
|
394
|
+
try {
|
|
395
|
+
const useWorkspace = scopeMode() === "workspace" && workspaceDir();
|
|
396
|
+
const res = await rpc.prune({
|
|
397
|
+
rootDir: useWorkspace ? workspaceDir() : scanRoot() || undefined,
|
|
398
|
+
retentionDays: days,
|
|
399
|
+
});
|
|
400
|
+
await refreshBackups(selectedEntry()?.id);
|
|
401
|
+
flashStatus(
|
|
402
|
+
`Pruned ${res.removed} snapshot folder(s) older than ${res.retentionDays}d`,
|
|
403
|
+
"ok",
|
|
404
|
+
);
|
|
405
|
+
} catch (err: any) {
|
|
406
|
+
flashStatus(`Prune failed: ${err?.message || String(err)}`, "err");
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
async function executeRestore() {
|
|
411
|
+
const entry = selectedEntry();
|
|
412
|
+
const rpc = getRpcClient(props.ctx);
|
|
413
|
+
if (!entry || !rpc) return;
|
|
414
|
+
setConfirmRestoreModal(false);
|
|
415
|
+
try {
|
|
416
|
+
const res = await rpc.restore({ entry });
|
|
417
|
+
const safetyNote = res.safetySnapshot ? ` (safety backup: ${res.safetySnapshot})` : "";
|
|
418
|
+
await refreshBackups(entry.id);
|
|
419
|
+
flashStatus(`Restored ${entry.relativePath}${safetyNote}`, "ok");
|
|
420
|
+
} catch (err: any) {
|
|
421
|
+
flashStatus(`Restore failed: ${err?.message || String(err)}`, "err");
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
async function handleOpenFile(filePath: string, label: string) {
|
|
426
|
+
const rpc = getRpcClient(props.ctx);
|
|
427
|
+
if (!rpc || !filePath) return;
|
|
428
|
+
try {
|
|
429
|
+
const res = await rpc.openFile({ filePath });
|
|
430
|
+
flashStatus(`Opened ${label} via ${res.launcher}`, "ok");
|
|
431
|
+
} catch (err: any) {
|
|
432
|
+
flashStatus(`Open failed: ${err?.message || String(err)}`, "err");
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
async function handleOpenDiff() {
|
|
437
|
+
const entry = selectedEntry();
|
|
438
|
+
const rpc = getRpcClient(props.ctx);
|
|
439
|
+
if (!entry || !rpc) return;
|
|
440
|
+
try {
|
|
441
|
+
const res = await rpc.openDiff({
|
|
442
|
+
backupPath: entry.backupPath,
|
|
443
|
+
originalPath: entry.originalPath,
|
|
444
|
+
});
|
|
445
|
+
flashStatus(`Opened diff for ${entry.relativePath} via ${res.launcher}`, "ok");
|
|
446
|
+
} catch (err: any) {
|
|
447
|
+
flashStatus(`Open diff failed: ${err?.message || String(err)}`, "err");
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
const activeFilterCount = createMemo(() => {
|
|
452
|
+
let count = 0;
|
|
453
|
+
if (toolFilter() !== "all") count++;
|
|
454
|
+
if (dateFilter() !== "all") count++;
|
|
455
|
+
return count;
|
|
456
|
+
});
|
|
457
|
+
|
|
458
|
+
const filterMenuItems = createMemo<UiMenuEntry[]>(() => [
|
|
459
|
+
{
|
|
460
|
+
type: "group",
|
|
461
|
+
label: "Tool Filter (^T)",
|
|
462
|
+
items: TOOL_CYCLE.map((tf) => ({
|
|
463
|
+
label: tf === "all" ? "All tools" : `[${tf}]`,
|
|
464
|
+
...(toolFilter() === tf ? { icon: "check-small" as const } : {}),
|
|
465
|
+
onSelect: () => {
|
|
466
|
+
setToolFilter(tf);
|
|
467
|
+
setSelectedIndex(0);
|
|
468
|
+
},
|
|
469
|
+
})),
|
|
470
|
+
},
|
|
471
|
+
{ type: "separator" },
|
|
472
|
+
{
|
|
473
|
+
type: "group",
|
|
474
|
+
label: "Age Filter (^G)",
|
|
475
|
+
items: DATE_CYCLE.map((df) => ({
|
|
476
|
+
label: df === "all" ? "All dates" : `Last ${df}`,
|
|
477
|
+
...(dateFilter() === df ? { icon: "check-small" as const } : {}),
|
|
478
|
+
onSelect: () => {
|
|
479
|
+
setDateFilter(df);
|
|
480
|
+
setSelectedIndex(0);
|
|
481
|
+
},
|
|
482
|
+
})),
|
|
483
|
+
},
|
|
484
|
+
{ type: "separator" },
|
|
485
|
+
{
|
|
486
|
+
type: "group",
|
|
487
|
+
label: "Auto-Prune Retention (^R)",
|
|
488
|
+
items: RETENTION_PRESETS.map((days) => ({
|
|
489
|
+
label: days === 0 ? "Never auto-prune (0d)" : `${days} days`,
|
|
490
|
+
...(settings().retentionDays === days ? { icon: "check-small" as const } : {}),
|
|
491
|
+
onSelect: () => {
|
|
492
|
+
void updateRetentionDays(days);
|
|
493
|
+
},
|
|
494
|
+
})),
|
|
495
|
+
},
|
|
496
|
+
{ type: "separator" },
|
|
497
|
+
{
|
|
498
|
+
label: "Reset tool & date filters",
|
|
499
|
+
icon: "reset",
|
|
500
|
+
disabled: activeFilterCount() === 0,
|
|
501
|
+
onSelect: () => {
|
|
502
|
+
setToolFilter("all");
|
|
503
|
+
setDateFilter("all");
|
|
504
|
+
setSelectedIndex(0);
|
|
505
|
+
},
|
|
506
|
+
},
|
|
507
|
+
]);
|
|
508
|
+
|
|
509
|
+
function handleKeyDown(e: KeyboardEvent) {
|
|
510
|
+
if (confirmRestoreModal()) {
|
|
511
|
+
if (e.key === "Escape") {
|
|
512
|
+
e.preventDefault();
|
|
513
|
+
setConfirmRestoreModal(false);
|
|
514
|
+
} else if (e.key === "Enter") {
|
|
515
|
+
e.preventDefault();
|
|
516
|
+
void executeRestore();
|
|
517
|
+
}
|
|
518
|
+
return;
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
if (e.ctrlKey || e.metaKey) {
|
|
522
|
+
const k = e.key.toLowerCase();
|
|
523
|
+
if (k === "t") {
|
|
524
|
+
e.preventDefault();
|
|
525
|
+
cycleToolFilter();
|
|
526
|
+
return;
|
|
527
|
+
}
|
|
528
|
+
if (k === "g") {
|
|
529
|
+
e.preventDefault();
|
|
530
|
+
cycleDateFilter();
|
|
531
|
+
return;
|
|
532
|
+
}
|
|
533
|
+
if (k === "r") {
|
|
534
|
+
e.preventDefault();
|
|
535
|
+
cycleRetentionDays();
|
|
536
|
+
return;
|
|
537
|
+
}
|
|
538
|
+
if (k === "d") {
|
|
539
|
+
e.preventDefault();
|
|
540
|
+
void handlePruneNow();
|
|
541
|
+
return;
|
|
542
|
+
}
|
|
543
|
+
if (k === "o") {
|
|
544
|
+
e.preventDefault();
|
|
545
|
+
const entry = selectedEntry();
|
|
546
|
+
if (entry) void handleOpenFile(entry.backupPath, entry.relativePath);
|
|
547
|
+
return;
|
|
548
|
+
}
|
|
549
|
+
if (k === "e") {
|
|
550
|
+
e.preventDefault();
|
|
551
|
+
void handleOpenDiff();
|
|
552
|
+
return;
|
|
553
|
+
}
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
if (e.key === "ArrowDown") {
|
|
557
|
+
e.preventDefault();
|
|
558
|
+
moveSelection(1);
|
|
559
|
+
} else if (e.key === "ArrowUp") {
|
|
560
|
+
e.preventDefault();
|
|
561
|
+
moveSelection(-1);
|
|
562
|
+
} else if (e.key === "PageDown") {
|
|
563
|
+
e.preventDefault();
|
|
564
|
+
moveSelection(8);
|
|
565
|
+
} else if (e.key === "PageUp") {
|
|
566
|
+
e.preventDefault();
|
|
567
|
+
moveSelection(-8);
|
|
568
|
+
} else if (e.key === "Enter" && selectedEntry()) {
|
|
569
|
+
e.preventDefault();
|
|
570
|
+
setConfirmRestoreModal(true);
|
|
571
|
+
}
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
onMount(() => {
|
|
575
|
+
void refreshBackups();
|
|
576
|
+
if (typeof window !== "undefined") {
|
|
577
|
+
window.setTimeout(() => searchInputRef?.focus(), 40);
|
|
578
|
+
}
|
|
579
|
+
});
|
|
580
|
+
|
|
581
|
+
const canSwitchScope = createMemo(() => {
|
|
582
|
+
const sr = scanRoot().replace(/\\/g, "/").toLowerCase();
|
|
583
|
+
const wd = workspaceDir().replace(/\\/g, "/").toLowerCase();
|
|
584
|
+
return Boolean(sr && wd && sr !== wd);
|
|
585
|
+
});
|
|
586
|
+
|
|
587
|
+
return (
|
|
588
|
+
<div
|
|
589
|
+
class={`oc-lb-root ${props.surface === "panel" ? "oc-lb-root-panel" : "oc-lb-root-modal"}`}
|
|
590
|
+
data-scheme={colorScheme()}
|
|
591
|
+
onKeyDown={handleKeyDown}
|
|
592
|
+
tabIndex={-1}
|
|
593
|
+
>
|
|
594
|
+
<div class="oc-lb-toolbar">
|
|
595
|
+
<div class="oc-lb-search-row">
|
|
596
|
+
<div class="oc-lb-search-box">
|
|
597
|
+
<span class="oc-lb-search-icon" aria-hidden="true">
|
|
598
|
+
⌕
|
|
599
|
+
</span>
|
|
600
|
+
<input
|
|
601
|
+
ref={searchInputRef}
|
|
602
|
+
type="text"
|
|
603
|
+
class="oc-lb-search-input"
|
|
604
|
+
placeholder="Filter backups by file path, project, snapshot ID, or slug..."
|
|
605
|
+
value={query()}
|
|
606
|
+
onInput={(e) => {
|
|
607
|
+
setQuery(e.currentTarget.value);
|
|
608
|
+
setSelectedIndex(0);
|
|
609
|
+
}}
|
|
610
|
+
/>
|
|
611
|
+
<Show when={query().length > 0}>
|
|
612
|
+
<button
|
|
613
|
+
type="button"
|
|
614
|
+
class="oc-lb-search-clear"
|
|
615
|
+
title="Clear search"
|
|
616
|
+
onClick={() => {
|
|
617
|
+
setQuery("");
|
|
618
|
+
setSelectedIndex(0);
|
|
619
|
+
searchInputRef?.focus();
|
|
620
|
+
}}
|
|
621
|
+
>
|
|
622
|
+
×
|
|
623
|
+
</button>
|
|
624
|
+
</Show>
|
|
625
|
+
</div>
|
|
626
|
+
|
|
627
|
+
<Show when={canSwitchScope()}>
|
|
628
|
+
<div class="oc-lb-scope-seg" role="group" aria-label="Scan scope">
|
|
629
|
+
<button
|
|
630
|
+
type="button"
|
|
631
|
+
class={`oc-lb-scope-btn ${scopeMode() === "scan-root" ? "is-active" : ""}`}
|
|
632
|
+
title={`Scan configured root: ${scanRoot()}`}
|
|
633
|
+
onClick={() => {
|
|
634
|
+
setScopeMode("scan-root");
|
|
635
|
+
void refreshBackups();
|
|
636
|
+
}}
|
|
637
|
+
>
|
|
638
|
+
All Root
|
|
639
|
+
</button>
|
|
640
|
+
<button
|
|
641
|
+
type="button"
|
|
642
|
+
class={`oc-lb-scope-btn ${scopeMode() === "workspace" ? "is-active" : ""}`}
|
|
643
|
+
title={`Scan current workspace only: ${workspaceDir()}`}
|
|
644
|
+
onClick={() => {
|
|
645
|
+
setScopeMode("workspace");
|
|
646
|
+
void refreshBackups();
|
|
647
|
+
}}
|
|
648
|
+
>
|
|
649
|
+
Workspace
|
|
650
|
+
</button>
|
|
651
|
+
</div>
|
|
652
|
+
</Show>
|
|
653
|
+
|
|
654
|
+
<ui.Menu
|
|
655
|
+
label={activeFilterCount() > 0 ? `Filters (${activeFilterCount()})` : "Filters"}
|
|
656
|
+
trigger="button"
|
|
657
|
+
icon="sliders"
|
|
658
|
+
placement="bottom-end"
|
|
659
|
+
items={filterMenuItems()}
|
|
660
|
+
/>
|
|
661
|
+
|
|
662
|
+
<ui.Button
|
|
663
|
+
variant="ghost"
|
|
664
|
+
size="small"
|
|
665
|
+
onClick={() => cycleRetentionDays()}
|
|
666
|
+
>
|
|
667
|
+
Retain: {settings().retentionDays === 0 ? "never" : `${settings().retentionDays}d`}
|
|
668
|
+
</ui.Button>
|
|
669
|
+
|
|
670
|
+
<ui.Button
|
|
671
|
+
variant="ghost"
|
|
672
|
+
size="small"
|
|
673
|
+
onClick={() => void handlePruneNow()}
|
|
674
|
+
>
|
|
675
|
+
Prune
|
|
676
|
+
</ui.Button>
|
|
677
|
+
|
|
678
|
+
<ui.Button
|
|
679
|
+
variant="ghost"
|
|
680
|
+
size="small"
|
|
681
|
+
onClick={() => void refreshBackups(selectedEntry()?.id)}
|
|
682
|
+
>
|
|
683
|
+
Refresh
|
|
684
|
+
</ui.Button>
|
|
685
|
+
</div>
|
|
686
|
+
|
|
687
|
+
<div class="oc-lb-active-chips">
|
|
688
|
+
<Show when={toolFilter() !== "all"}>
|
|
689
|
+
<button
|
|
690
|
+
type="button"
|
|
691
|
+
class="oc-lb-filter-chip"
|
|
692
|
+
onClick={() => setToolFilter("all")}
|
|
693
|
+
title="Click to clear tool filter"
|
|
694
|
+
>
|
|
695
|
+
<span>tool: {toolFilter()}</span>
|
|
696
|
+
<span class="oc-lb-filter-chip-x">×</span>
|
|
697
|
+
</button>
|
|
698
|
+
</Show>
|
|
699
|
+
<Show when={dateFilter() !== "all"}>
|
|
700
|
+
<button
|
|
701
|
+
type="button"
|
|
702
|
+
class="oc-lb-filter-chip"
|
|
703
|
+
onClick={() => setDateFilter("all")}
|
|
704
|
+
title="Click to clear age filter"
|
|
705
|
+
>
|
|
706
|
+
<span>age: {dateFilter()}</span>
|
|
707
|
+
<span class="oc-lb-filter-chip-x">×</span>
|
|
708
|
+
</button>
|
|
709
|
+
</Show>
|
|
710
|
+
<span class="oc-lb-count-summary">
|
|
711
|
+
{filteredEntries().length}/{allEntries().length} snapshots • Root:{" "}
|
|
712
|
+
{scopeMode() === "workspace" && workspaceDir()
|
|
713
|
+
? workspaceDir()
|
|
714
|
+
: scanRoot() || workspaceDir() || "workspace"}
|
|
715
|
+
</span>
|
|
716
|
+
</div>
|
|
717
|
+
</div>
|
|
718
|
+
|
|
719
|
+
<Show when={statusMsg()}>
|
|
720
|
+
{(msg) => (
|
|
721
|
+
<div class={`oc-lb-status oc-lb-status-${msg().kind}`}>{msg().text}</div>
|
|
722
|
+
)}
|
|
723
|
+
</Show>
|
|
724
|
+
|
|
725
|
+
<div class="oc-lb-split">
|
|
726
|
+
<div class="oc-lb-list-pane">
|
|
727
|
+
<div class="oc-lb-pane-header">
|
|
728
|
+
<span>SNAPSHOTS ({filteredEntries().length})</span>
|
|
729
|
+
<span>↑↓ Navigate</span>
|
|
730
|
+
</div>
|
|
731
|
+
<div class="oc-lb-list-scroll">
|
|
732
|
+
<Show
|
|
733
|
+
when={!loading()}
|
|
734
|
+
fallback={<div class="oc-lb-empty">Scanning .backups folders...</div>}
|
|
735
|
+
>
|
|
736
|
+
<Show
|
|
737
|
+
when={filteredEntries().length > 0}
|
|
738
|
+
fallback={
|
|
739
|
+
<div class="oc-lb-empty">
|
|
740
|
+
{allEntries().length === 0
|
|
741
|
+
? "No backups found yet. Automatic snapshots are created before edit, write, patch, apply_patch, and multiedit tool calls."
|
|
742
|
+
: "No backups match your current search or filter criteria."}
|
|
743
|
+
</div>
|
|
744
|
+
}
|
|
745
|
+
>
|
|
746
|
+
<For each={filteredEntries()}>
|
|
747
|
+
{(entry, idx) => {
|
|
748
|
+
const isSelected = () => idx() === selectedIndex();
|
|
749
|
+
const relBackup = () => getBackupDisplayPath(entry.backupPath);
|
|
750
|
+
return (
|
|
751
|
+
<div
|
|
752
|
+
ref={(el) => rowRefs.set(idx(), el)}
|
|
753
|
+
class={`oc-lb-row ${isSelected() ? "is-selected" : ""}`}
|
|
754
|
+
onClick={() => setSelectedIndex(idx())}
|
|
755
|
+
onDblClick={() => {
|
|
756
|
+
setSelectedIndex(idx());
|
|
757
|
+
setConfirmRestoreModal(true);
|
|
758
|
+
}}
|
|
759
|
+
>
|
|
760
|
+
<div class="oc-lb-row-top">
|
|
761
|
+
<span class="oc-lb-row-title" title={entry.relativePath}>
|
|
762
|
+
<HighlightedText
|
|
763
|
+
text={entry.relativePath}
|
|
764
|
+
terms={queryTerms()}
|
|
765
|
+
/>
|
|
766
|
+
</span>
|
|
767
|
+
<span class={`oc-lb-badge oc-lb-badge-${entry.tool}`}>
|
|
768
|
+
{entry.tool}
|
|
769
|
+
</span>
|
|
770
|
+
<span class="oc-lb-row-age">{formatAge(entry.timeMs)}</span>
|
|
771
|
+
</div>
|
|
772
|
+
<div class="oc-lb-row-sub" title={entry.backupPath}>
|
|
773
|
+
↳{" "}
|
|
774
|
+
<HighlightedText
|
|
775
|
+
text={relBackup()}
|
|
776
|
+
terms={queryTerms()}
|
|
777
|
+
/>
|
|
778
|
+
</div>
|
|
779
|
+
</div>
|
|
780
|
+
);
|
|
781
|
+
}}
|
|
782
|
+
</For>
|
|
783
|
+
</Show>
|
|
784
|
+
</Show>
|
|
785
|
+
</div>
|
|
786
|
+
</div>
|
|
787
|
+
|
|
788
|
+
<div class="oc-lb-preview-pane">
|
|
789
|
+
<div class="oc-lb-pane-header">
|
|
790
|
+
<span>SNAPSHOT DETAILS & UNIFIED DIFF</span>
|
|
791
|
+
<Show when={selectedEntry()}>
|
|
792
|
+
{(entry) => <span>{formatBytes(entry().size)}</span>}
|
|
793
|
+
</Show>
|
|
794
|
+
</div>
|
|
795
|
+
|
|
796
|
+
<Show
|
|
797
|
+
when={selectedEntry()}
|
|
798
|
+
fallback={
|
|
799
|
+
<div class="oc-lb-empty">
|
|
800
|
+
Select a backup snapshot on the left to inspect its metadata and diff against the current file on disk.
|
|
801
|
+
</div>
|
|
802
|
+
}
|
|
803
|
+
>
|
|
804
|
+
{(entry) => (
|
|
805
|
+
<>
|
|
806
|
+
<div class="oc-lb-meta-card">
|
|
807
|
+
<div class="oc-lb-meta-title-row">
|
|
808
|
+
<div>
|
|
809
|
+
<div class="oc-lb-meta-filename">
|
|
810
|
+
<HighlightedText
|
|
811
|
+
text={entry().relativePath}
|
|
812
|
+
terms={queryTerms()}
|
|
813
|
+
/>
|
|
814
|
+
</div>
|
|
815
|
+
<div class="oc-lb-meta-sub">
|
|
816
|
+
{formatDateTime(entry().timeMs)} ({formatAge(entry().timeMs)}) •{" "}
|
|
817
|
+
{formatBytes(entry().size)} • [{entry().tool}] •{" "}
|
|
818
|
+
<HighlightedText
|
|
819
|
+
text={entry().snapshotId}
|
|
820
|
+
terms={queryTerms()}
|
|
821
|
+
/>
|
|
822
|
+
</div>
|
|
823
|
+
</div>
|
|
824
|
+
<div class="oc-lb-action-bar">
|
|
825
|
+
<ui.Button
|
|
826
|
+
variant="contrast"
|
|
827
|
+
size="small"
|
|
828
|
+
onClick={() => setConfirmRestoreModal(true)}
|
|
829
|
+
>
|
|
830
|
+
Restore File
|
|
831
|
+
</ui.Button>
|
|
832
|
+
<ui.Button
|
|
833
|
+
variant="neutral"
|
|
834
|
+
size="small"
|
|
835
|
+
onClick={() =>
|
|
836
|
+
void handleOpenFile(entry().backupPath, entry().relativePath)
|
|
837
|
+
}
|
|
838
|
+
>
|
|
839
|
+
Open Backup
|
|
840
|
+
</ui.Button>
|
|
841
|
+
<ui.Button
|
|
842
|
+
variant="neutral"
|
|
843
|
+
size="small"
|
|
844
|
+
onClick={() => void handleOpenDiff()}
|
|
845
|
+
>
|
|
846
|
+
Diff in Editor
|
|
847
|
+
</ui.Button>
|
|
848
|
+
</div>
|
|
849
|
+
</div>
|
|
850
|
+
|
|
851
|
+
<div class="oc-lb-meta-grid">
|
|
852
|
+
<span class="oc-lb-meta-label">Original:</span>
|
|
853
|
+
<span class="oc-lb-meta-val" title={entry().originalPath}>
|
|
854
|
+
<HighlightedText
|
|
855
|
+
text={entry().originalPath}
|
|
856
|
+
terms={queryTerms()}
|
|
857
|
+
/>
|
|
858
|
+
</span>
|
|
859
|
+
<span class="oc-lb-meta-label">Backup:</span>
|
|
860
|
+
<span class="oc-lb-meta-val" title={entry().backupPath}>
|
|
861
|
+
<HighlightedText
|
|
862
|
+
text={entry().backupPath}
|
|
863
|
+
terms={queryTerms()}
|
|
864
|
+
/>
|
|
865
|
+
</span>
|
|
866
|
+
</div>
|
|
867
|
+
</div>
|
|
868
|
+
|
|
869
|
+
<div class="oc-lb-diff-summary">
|
|
870
|
+
<Show
|
|
871
|
+
when={!diffLoading() && diffState()}
|
|
872
|
+
fallback={<span>Computing unified diff...</span>}
|
|
873
|
+
>
|
|
874
|
+
{(d) => (
|
|
875
|
+
<span class={`oc-lb-diff-status-${d().status}`}>
|
|
876
|
+
{d().summary}
|
|
877
|
+
</span>
|
|
878
|
+
)}
|
|
879
|
+
</Show>
|
|
880
|
+
</div>
|
|
881
|
+
|
|
882
|
+
<div class="oc-lb-diff-scroll">
|
|
883
|
+
<Show when={!diffLoading() && diffState()}>
|
|
884
|
+
{(d) => (
|
|
885
|
+
<Show
|
|
886
|
+
when={d().lines.length > 0}
|
|
887
|
+
fallback={
|
|
888
|
+
<div class="oc-lb-empty">
|
|
889
|
+
{d().status === "identical"
|
|
890
|
+
? "No differences: backup snapshot and current file on disk are byte-for-byte identical."
|
|
891
|
+
: d().summary}
|
|
892
|
+
</div>
|
|
893
|
+
}
|
|
894
|
+
>
|
|
895
|
+
<For each={d().lines}>
|
|
896
|
+
{(line) => {
|
|
897
|
+
const prefix =
|
|
898
|
+
line.type === "add"
|
|
899
|
+
? "+ "
|
|
900
|
+
: line.type === "del"
|
|
901
|
+
? "- "
|
|
902
|
+
: line.type === "hunk"
|
|
903
|
+
? ""
|
|
904
|
+
: " ";
|
|
905
|
+
return (
|
|
906
|
+
<div class={`oc-lb-diff-line oc-lb-diff-${line.type}`}>
|
|
907
|
+
{prefix}
|
|
908
|
+
<HighlightedText
|
|
909
|
+
text={line.text}
|
|
910
|
+
terms={queryTerms()}
|
|
911
|
+
/>
|
|
912
|
+
</div>
|
|
913
|
+
);
|
|
914
|
+
}}
|
|
915
|
+
</For>
|
|
916
|
+
</Show>
|
|
917
|
+
)}
|
|
918
|
+
</Show>
|
|
919
|
+
</div>
|
|
920
|
+
</>
|
|
921
|
+
)}
|
|
922
|
+
</Show>
|
|
923
|
+
</div>
|
|
924
|
+
</div>
|
|
925
|
+
|
|
926
|
+
<Show when={confirmRestoreModal() && selectedEntry()}>
|
|
927
|
+
{(entry) => (
|
|
928
|
+
<div
|
|
929
|
+
class="oc-lb-modal-backdrop"
|
|
930
|
+
onClick={() => setConfirmRestoreModal(false)}
|
|
931
|
+
>
|
|
932
|
+
<div
|
|
933
|
+
class="oc-lb-modal-card"
|
|
934
|
+
onClick={(e) => e.stopPropagation()}
|
|
935
|
+
>
|
|
936
|
+
<h3 class="oc-lb-modal-title">Restore Backup Snapshot?</h3>
|
|
937
|
+
<p class="oc-lb-modal-desc">
|
|
938
|
+
Restoring <strong>{entry().relativePath}</strong> from snapshot{" "}
|
|
939
|
+
<code>{entry().snapshotId}</code> will overwrite the current file on disk. A{" "}
|
|
940
|
+
<code>pre-restore</code> safety backup of the current file will be saved automatically first.
|
|
941
|
+
</p>
|
|
942
|
+
<div class="oc-lb-modal-paths">
|
|
943
|
+
<div>
|
|
944
|
+
<strong>From Backup:</strong> {entry().backupPath}
|
|
945
|
+
</div>
|
|
946
|
+
<div>
|
|
947
|
+
<strong>To Original:</strong> {entry().originalPath}
|
|
948
|
+
</div>
|
|
949
|
+
</div>
|
|
950
|
+
<div class="oc-lb-modal-actions">
|
|
951
|
+
<ui.Button
|
|
952
|
+
variant="ghost"
|
|
953
|
+
size="small"
|
|
954
|
+
onClick={() => setConfirmRestoreModal(false)}
|
|
955
|
+
>
|
|
956
|
+
Cancel (Esc)
|
|
957
|
+
</ui.Button>
|
|
958
|
+
<ui.Button
|
|
959
|
+
variant="neutral"
|
|
960
|
+
size="small"
|
|
961
|
+
onClick={() => {
|
|
962
|
+
setConfirmRestoreModal(false);
|
|
963
|
+
void handleOpenDiff();
|
|
964
|
+
}}
|
|
965
|
+
>
|
|
966
|
+
Open Diff in Editor
|
|
967
|
+
</ui.Button>
|
|
968
|
+
<ui.Button
|
|
969
|
+
variant="contrast"
|
|
970
|
+
size="small"
|
|
971
|
+
onClick={() => void executeRestore()}
|
|
972
|
+
>
|
|
973
|
+
Restore File (Enter)
|
|
974
|
+
</ui.Button>
|
|
975
|
+
</div>
|
|
976
|
+
</div>
|
|
977
|
+
</div>
|
|
978
|
+
)}
|
|
979
|
+
</Show>
|
|
980
|
+
</div>
|
|
981
|
+
);
|
|
982
|
+
}
|
|
983
|
+
|
|
984
|
+
// Module-level signals so commands/slots can drive the shared panel's query
|
|
985
|
+
// without re-mounting BackupManagerView (one persistent side panel, no modals).
|
|
986
|
+
const [ocLbPanelQuery, setOcLbPanelQuery] = createSignal("");
|
|
987
|
+
let backupFallbackDialog: { close(): void } | undefined;
|
|
988
|
+
|
|
989
|
+
function openBackupPanel(ctx: PluginContext, initialQuery = "") {
|
|
990
|
+
if (initialQuery) setOcLbPanelQuery(initialQuery);
|
|
991
|
+
const session = ctx.sessions.current();
|
|
992
|
+
if (!session) {
|
|
993
|
+
if (backupFallbackDialog) return;
|
|
994
|
+
const ui = ctx.ui;
|
|
995
|
+
backupFallbackDialog = ctx.dialogs.open((dialog) => {
|
|
996
|
+
onCleanup(() => { backupFallbackDialog = undefined; });
|
|
997
|
+
return (
|
|
998
|
+
<ui.Dialog title="Backup Manager" size="x-large">
|
|
999
|
+
<BackupManagerView ctx={ctx} surface="modal" initialQuery={initialQuery} onClose={dialog.close} />
|
|
1000
|
+
</ui.Dialog>
|
|
1001
|
+
);
|
|
1002
|
+
});
|
|
1003
|
+
return;
|
|
1004
|
+
}
|
|
1005
|
+
backupFallbackDialog?.close();
|
|
1006
|
+
try {
|
|
1007
|
+
// Always select the tab so it becomes the focused panel even if another
|
|
1008
|
+
// panel (usage, search, memory) is already open.
|
|
1009
|
+
ctx.layout.open(`${ctx.id}:backups`, session);
|
|
1010
|
+
} catch {
|
|
1011
|
+
// Layout not ready; retry on next interaction.
|
|
1012
|
+
}
|
|
1013
|
+
}
|
|
1014
|
+
|
|
1015
|
+
function createSpriteIcon(iconId: string, className?: string): SVGSVGElement {
|
|
1016
|
+
const ns = "http://www.w3.org/2000/svg";
|
|
1017
|
+
const svg = document.createElementNS(ns, "svg");
|
|
1018
|
+
svg.setAttribute("data-slot", "icon-svg");
|
|
1019
|
+
if (className) {
|
|
1020
|
+
svg.setAttribute("class", className);
|
|
1021
|
+
} else {
|
|
1022
|
+
svg.setAttribute("width", "16");
|
|
1023
|
+
svg.setAttribute("height", "16");
|
|
1024
|
+
}
|
|
1025
|
+
svg.setAttribute("viewBox", "0 0 14 14");
|
|
1026
|
+
svg.setAttribute("fill", "none");
|
|
1027
|
+
svg.setAttribute("aria-hidden", "true");
|
|
1028
|
+
const useEl = document.createElementNS(ns, "use");
|
|
1029
|
+
useEl.setAttribute("href", `#opencode-v2-icon-${iconId}`);
|
|
1030
|
+
svg.appendChild(useEl);
|
|
1031
|
+
return svg;
|
|
1032
|
+
}
|
|
1033
|
+
|
|
1034
|
+
export default Plugin.define({
|
|
1035
|
+
i18n: {
|
|
1036
|
+
en: {
|
|
1037
|
+
"backups.command.title": "Browse & Restore File Backups",
|
|
1038
|
+
"backups.command.description":
|
|
1039
|
+
"Inspect automatic file snapshots, view unified diffs, and restore files",
|
|
1040
|
+
"backups.panel": "Backups",
|
|
1041
|
+
"backups.panel.toggle": "Toggle Backups Side Panel",
|
|
1042
|
+
},
|
|
1043
|
+
},
|
|
1044
|
+
setup(ctx) {
|
|
1045
|
+
const { ui } = ctx;
|
|
1046
|
+
const panelTabKey = `${ctx.id}:backups`;
|
|
1047
|
+
const backupsTab: PanelTab = {
|
|
1048
|
+
id: "backups",
|
|
1049
|
+
get title() {
|
|
1050
|
+
return ctx.t("backups.panel");
|
|
1051
|
+
},
|
|
1052
|
+
closable: "compact",
|
|
1053
|
+
};
|
|
1054
|
+
|
|
1055
|
+
ctx.add(Style, cssText);
|
|
1056
|
+
|
|
1057
|
+
if (typeof document !== "undefined") {
|
|
1058
|
+
document
|
|
1059
|
+
.querySelectorAll('[data-action="titlebar-backups"], [data-action="vertical-tabs-backups"]')
|
|
1060
|
+
.forEach((el) => el.remove());
|
|
1061
|
+
|
|
1062
|
+
const ensureTitlebarButton = () => {
|
|
1063
|
+
if (document.querySelector('[data-action="titlebar-backups"]')) return;
|
|
1064
|
+
const searchBtn = document.querySelector<HTMLElement>(
|
|
1065
|
+
'header[data-slot="titlebar-v2"] button[aria-label*="Search"]',
|
|
1066
|
+
);
|
|
1067
|
+
const container = searchBtn?.parentElement;
|
|
1068
|
+
if (!container) return;
|
|
1069
|
+
|
|
1070
|
+
const btn = document.createElement("button");
|
|
1071
|
+
btn.type = "button";
|
|
1072
|
+
btn.setAttribute("data-action", "titlebar-backups");
|
|
1073
|
+
btn.setAttribute("aria-label", "Backup Snapshots (Ctrl+Shift+B)");
|
|
1074
|
+
btn.title = "Backup Snapshots (Ctrl+Shift+B)";
|
|
1075
|
+
btn.className = "oc-lb-titlebar-quick-btn";
|
|
1076
|
+
btn.appendChild(createSpriteIcon("archive"));
|
|
1077
|
+
btn.addEventListener("click", (e) => {
|
|
1078
|
+
e.preventDefault();
|
|
1079
|
+
e.stopPropagation();
|
|
1080
|
+
openBackupPanel(ctx, "");
|
|
1081
|
+
});
|
|
1082
|
+
container.appendChild(btn);
|
|
1083
|
+
};
|
|
1084
|
+
|
|
1085
|
+
const ensureVerticalTabsButton = () => {
|
|
1086
|
+
if (document.querySelector('[data-action="vertical-tabs-backups"]')) return;
|
|
1087
|
+
const anchorBtn =
|
|
1088
|
+
document.querySelector<HTMLElement>('button[data-action="vertical-tabs-usage"]') ??
|
|
1089
|
+
document.querySelector<HTMLElement>('button[data-action="vertical-tabs-memories"]') ??
|
|
1090
|
+
document.querySelector<HTMLElement>('button[data-action="vertical-tabs-search"]');
|
|
1091
|
+
const container = anchorBtn?.parentElement;
|
|
1092
|
+
if (!container || !anchorBtn) return;
|
|
1093
|
+
|
|
1094
|
+
const btn = document.createElement("button");
|
|
1095
|
+
btn.type = "button";
|
|
1096
|
+
btn.setAttribute("data-action", "vertical-tabs-backups");
|
|
1097
|
+
btn.setAttribute("data-titlebar-tab-action", "");
|
|
1098
|
+
btn.setAttribute("aria-label", "Backup Snapshots (Ctrl+Shift+B)");
|
|
1099
|
+
btn.title = "Backup Snapshots (Ctrl+Shift+B)";
|
|
1100
|
+
btn.className =
|
|
1101
|
+
"group flex h-7 w-full shrink-0 items-center gap-1.5 rounded-[6px] ps-1.5 pe-2 text-[13px] leading-4 text-v2-text-text-faint hover:text-v2-text-text-base";
|
|
1102
|
+
|
|
1103
|
+
const iconWrap = document.createElement("span");
|
|
1104
|
+
iconWrap.className = "flex size-4 shrink-0 items-center justify-center";
|
|
1105
|
+
iconWrap.appendChild(createSpriteIcon("archive", "size-4 shrink-0"));
|
|
1106
|
+
|
|
1107
|
+
const labelSpan = document.createElement("span");
|
|
1108
|
+
labelSpan.className = "min-w-0 flex-1 truncate text-start font-[460]";
|
|
1109
|
+
labelSpan.textContent = "Backups";
|
|
1110
|
+
|
|
1111
|
+
const kbdSpan = document.createElement("span");
|
|
1112
|
+
kbdSpan.className =
|
|
1113
|
+
"ms-auto shrink-0 text-[11px] leading-none text-v2-text-text-faint opacity-0 transition-opacity group-hover:opacity-100";
|
|
1114
|
+
kbdSpan.textContent = "Ctrl+Shift+B";
|
|
1115
|
+
|
|
1116
|
+
btn.appendChild(iconWrap);
|
|
1117
|
+
btn.appendChild(labelSpan);
|
|
1118
|
+
btn.appendChild(kbdSpan);
|
|
1119
|
+
|
|
1120
|
+
btn.addEventListener("click", (e) => {
|
|
1121
|
+
e.preventDefault();
|
|
1122
|
+
e.stopPropagation();
|
|
1123
|
+
openBackupPanel(ctx, "");
|
|
1124
|
+
});
|
|
1125
|
+
anchorBtn.insertAdjacentElement("afterend", btn);
|
|
1126
|
+
};
|
|
1127
|
+
|
|
1128
|
+
const ensureAllButtons = () => {
|
|
1129
|
+
ensureTitlebarButton();
|
|
1130
|
+
ensureVerticalTabsButton();
|
|
1131
|
+
};
|
|
1132
|
+
|
|
1133
|
+
const handleDocumentClick = (e: MouseEvent) => {
|
|
1134
|
+
const target = e.target as Element | null;
|
|
1135
|
+
if (!target || typeof target.closest !== "function") return;
|
|
1136
|
+
const btn = target.closest(
|
|
1137
|
+
'[data-action="titlebar-backups"], [data-action="vertical-tabs-backups"]',
|
|
1138
|
+
);
|
|
1139
|
+
if (!btn) return;
|
|
1140
|
+
e.preventDefault();
|
|
1141
|
+
e.stopPropagation();
|
|
1142
|
+
e.stopImmediatePropagation();
|
|
1143
|
+
openBackupPanel(ctx, "");
|
|
1144
|
+
};
|
|
1145
|
+
|
|
1146
|
+
const handleGlobalKeydown = (e: KeyboardEvent) => {
|
|
1147
|
+
const mod = e.ctrlKey || e.metaKey;
|
|
1148
|
+
if (!mod) return;
|
|
1149
|
+
const key = e.key.toLowerCase();
|
|
1150
|
+
if (e.shiftKey && !e.altKey && key === "b") {
|
|
1151
|
+
e.preventDefault();
|
|
1152
|
+
e.stopPropagation();
|
|
1153
|
+
openBackupPanel(ctx, "");
|
|
1154
|
+
return;
|
|
1155
|
+
}
|
|
1156
|
+
if (e.altKey && !e.shiftKey && key === "b") {
|
|
1157
|
+
e.preventDefault();
|
|
1158
|
+
e.stopPropagation();
|
|
1159
|
+
const session = ctx.sessions.current();
|
|
1160
|
+
if (session) {
|
|
1161
|
+
ctx.layout.toggle(panelTabKey, session);
|
|
1162
|
+
} else {
|
|
1163
|
+
openBackupPanel(ctx, "");
|
|
1164
|
+
}
|
|
1165
|
+
}
|
|
1166
|
+
};
|
|
1167
|
+
|
|
1168
|
+
ensureAllButtons();
|
|
1169
|
+
const observer = new MutationObserver(() => ensureAllButtons());
|
|
1170
|
+
observer.observe(document.body, { childList: true, subtree: true });
|
|
1171
|
+
document.addEventListener("click", handleDocumentClick, true);
|
|
1172
|
+
window.addEventListener("keydown", handleGlobalKeydown, true);
|
|
1173
|
+
|
|
1174
|
+
onCleanup(() => {
|
|
1175
|
+
observer.disconnect();
|
|
1176
|
+
document.removeEventListener("click", handleDocumentClick, true);
|
|
1177
|
+
window.removeEventListener("keydown", handleGlobalKeydown, true);
|
|
1178
|
+
document
|
|
1179
|
+
.querySelectorAll(
|
|
1180
|
+
'[data-action="titlebar-backups"], [data-action="vertical-tabs-backups"]',
|
|
1181
|
+
)
|
|
1182
|
+
.forEach((el) => el.remove());
|
|
1183
|
+
});
|
|
1184
|
+
}
|
|
1185
|
+
|
|
1186
|
+
ctx.add(Command, () => ({
|
|
1187
|
+
id: "backups",
|
|
1188
|
+
title: ctx.t("backups.command.title"),
|
|
1189
|
+
description: ctx.t("backups.command.description"),
|
|
1190
|
+
keywords: "backups backup restore snapshots diff undo",
|
|
1191
|
+
group: "Session",
|
|
1192
|
+
section: "session",
|
|
1193
|
+
bind: "mod+shift+b",
|
|
1194
|
+
slash: {
|
|
1195
|
+
name: "backups",
|
|
1196
|
+
arguments: true,
|
|
1197
|
+
},
|
|
1198
|
+
featured: true,
|
|
1199
|
+
run(input) {
|
|
1200
|
+
openBackupPanel(ctx, (input ?? "").trim());
|
|
1201
|
+
},
|
|
1202
|
+
}));
|
|
1203
|
+
|
|
1204
|
+
ctx.add(Command, () => {
|
|
1205
|
+
const session = ctx.sessions.current();
|
|
1206
|
+
return {
|
|
1207
|
+
id: "backups-panel",
|
|
1208
|
+
title: ctx.t("backups.panel.toggle"),
|
|
1209
|
+
description: ctx.t("backups.command.description"),
|
|
1210
|
+
keywords: "backups panel sidebar snapshots",
|
|
1211
|
+
group: "Session",
|
|
1212
|
+
section: "session",
|
|
1213
|
+
bind: "mod+alt+b",
|
|
1214
|
+
run() {
|
|
1215
|
+
if (session) {
|
|
1216
|
+
ctx.layout.toggle(panelTabKey, session);
|
|
1217
|
+
} else {
|
|
1218
|
+
openBackupPanel(ctx, "");
|
|
1219
|
+
}
|
|
1220
|
+
},
|
|
1221
|
+
};
|
|
1222
|
+
});
|
|
1223
|
+
|
|
1224
|
+
ctx.add(MenuItem, () => {
|
|
1225
|
+
const session = ctx.sessions.current();
|
|
1226
|
+
const opened = !!session && ctx.layout.state(panelTabKey, session) !== "closed";
|
|
1227
|
+
return {
|
|
1228
|
+
menu: "session.panel",
|
|
1229
|
+
id: "backups",
|
|
1230
|
+
title: ctx.t("backups.panel"),
|
|
1231
|
+
description: ctx.t("backups.command.description"),
|
|
1232
|
+
icon: "archive",
|
|
1233
|
+
keybind: `${ctx.id}.backups-panel`,
|
|
1234
|
+
opened,
|
|
1235
|
+
run() {
|
|
1236
|
+
const current = ctx.sessions.current();
|
|
1237
|
+
if (current) {
|
|
1238
|
+
ctx.layout.toggle(panelTabKey, current);
|
|
1239
|
+
} else {
|
|
1240
|
+
openBackupPanel(ctx, "");
|
|
1241
|
+
}
|
|
1242
|
+
},
|
|
1243
|
+
};
|
|
1244
|
+
});
|
|
1245
|
+
|
|
1246
|
+
ctx.add(Panel, {
|
|
1247
|
+
id: "backups",
|
|
1248
|
+
region: "side",
|
|
1249
|
+
list: (input) => (input.open.includes("backups") ? [backupsTab] : []),
|
|
1250
|
+
render: () => <BackupManagerView ctx={ctx} surface="panel" initialQuery="" />,
|
|
1251
|
+
});
|
|
1252
|
+
},
|
|
1253
|
+
});
|