forked from Dimillian/CodexMonitor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseWorkspaces.ts
More file actions
209 lines (195 loc) · 5.96 KB
/
useWorkspaces.ts
File metadata and controls
209 lines (195 loc) · 5.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import { useCallback, useEffect, useMemo, useState } from "react";
import type { DebugEntry } from "../types";
import type { WorkspaceInfo, WorkspaceSettings } from "../types";
import { ask } from "@tauri-apps/plugin-dialog";
import {
addWorkspace as addWorkspaceService,
connectWorkspace as connectWorkspaceService,
listWorkspaces,
pickWorkspacePath,
removeWorkspace as removeWorkspaceService,
updateWorkspaceSettings as updateWorkspaceSettingsService,
} from "../services/tauri";
type UseWorkspacesOptions = {
onDebug?: (entry: DebugEntry) => void;
};
export function useWorkspaces(options: UseWorkspacesOptions = {}) {
const [workspaces, setWorkspaces] = useState<WorkspaceInfo[]>([]);
const [activeWorkspaceId, setActiveWorkspaceId] = useState<string | null>(null);
const [hasLoaded, setHasLoaded] = useState(false);
const { onDebug } = options;
const refreshWorkspaces = useCallback(async () => {
try {
const entries = await listWorkspaces();
setWorkspaces(entries);
setActiveWorkspaceId((prev) => {
if (!prev) {
return prev;
}
return entries.some((entry) => entry.id === prev) ? prev : null;
});
setHasLoaded(true);
return entries;
} catch (err) {
console.error("Failed to load workspaces", err);
setHasLoaded(true);
throw err;
}
}, []);
useEffect(() => {
void refreshWorkspaces();
}, [refreshWorkspaces]);
const activeWorkspace = useMemo(
() => workspaces.find((entry) => entry.id === activeWorkspaceId) ?? null,
[activeWorkspaceId, workspaces],
);
async function addWorkspace() {
const selection = await pickWorkspacePath();
if (!selection) {
return null;
}
onDebug?.({
id: `${Date.now()}-client-add-workspace`,
timestamp: Date.now(),
source: "client",
label: "workspace/add",
payload: { path: selection },
});
try {
const workspace = await addWorkspaceService(selection, null);
setWorkspaces((prev) => [...prev, workspace]);
setActiveWorkspaceId(workspace.id);
return workspace;
} catch (error) {
onDebug?.({
id: `${Date.now()}-client-add-workspace-error`,
timestamp: Date.now(),
source: "error",
label: "workspace/add error",
payload: error instanceof Error ? error.message : String(error),
});
throw error;
}
}
async function connectWorkspace(entry: WorkspaceInfo) {
onDebug?.({
id: `${Date.now()}-client-connect-workspace`,
timestamp: Date.now(),
source: "client",
label: "workspace/connect",
payload: { workspaceId: entry.id, path: entry.path },
});
try {
await connectWorkspaceService(entry.id);
} catch (error) {
onDebug?.({
id: `${Date.now()}-client-connect-workspace-error`,
timestamp: Date.now(),
source: "error",
label: "workspace/connect error",
payload: error instanceof Error ? error.message : String(error),
});
throw error;
}
}
function markWorkspaceConnected(id: string) {
setWorkspaces((prev) =>
prev.map((entry) => (entry.id === id ? { ...entry, connected: true } : entry)),
);
}
async function updateWorkspaceSettings(
workspaceId: string,
settings: WorkspaceSettings,
) {
onDebug?.({
id: `${Date.now()}-client-update-workspace-settings`,
timestamp: Date.now(),
source: "client",
label: "workspace/settings",
payload: { workspaceId, settings },
});
let previous: WorkspaceInfo | null = null;
setWorkspaces((prev) =>
prev.map((entry) => {
if (entry.id !== workspaceId) {
return entry;
}
previous = entry;
return { ...entry, settings };
}),
);
try {
const updated = await updateWorkspaceSettingsService(workspaceId, settings);
setWorkspaces((prev) =>
prev.map((entry) => (entry.id === workspaceId ? updated : entry)),
);
return updated;
} catch (error) {
if (previous) {
const restore = previous;
setWorkspaces((prev) =>
prev.map((entry) => (entry.id === workspaceId ? restore : entry)),
);
}
onDebug?.({
id: `${Date.now()}-client-update-workspace-settings-error`,
timestamp: Date.now(),
source: "error",
label: "workspace/settings error",
payload: error instanceof Error ? error.message : String(error),
});
throw error;
}
}
async function removeWorkspace(workspaceId: string) {
const workspace = workspaces.find((entry) => entry.id === workspaceId);
const workspaceName = workspace?.name || "this workspace";
const confirmed = await ask(
`Are you sure you want to delete "${workspaceName}"?\n\nThis will remove the workspace from CodexMonitor.`,
{
title: "Delete Workspace",
kind: "warning",
okLabel: "Delete",
cancelLabel: "Cancel",
},
);
if (!confirmed) {
return;
}
onDebug?.({
id: `${Date.now()}-client-remove-workspace`,
timestamp: Date.now(),
source: "client",
label: "workspace/remove",
payload: { workspaceId },
});
try {
await removeWorkspaceService(workspaceId);
setWorkspaces((prev) => prev.filter((entry) => entry.id !== workspaceId));
setActiveWorkspaceId((prev) => (prev === workspaceId ? null : prev));
await refreshWorkspaces();
} catch (error) {
onDebug?.({
id: `${Date.now()}-client-remove-workspace-error`,
timestamp: Date.now(),
source: "error",
label: "workspace/remove error",
payload: error instanceof Error ? error.message : String(error),
});
throw error;
}
}
return {
workspaces,
activeWorkspace,
activeWorkspaceId,
setActiveWorkspaceId,
addWorkspace,
connectWorkspace,
markWorkspaceConnected,
updateWorkspaceSettings,
removeWorkspace,
hasLoaded,
refreshWorkspaces,
};
}