forked from op7418/CodePilot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseAssistantWorkspace.ts
More file actions
94 lines (83 loc) · 2.41 KB
/
useAssistantWorkspace.ts
File metadata and controls
94 lines (83 loc) · 2.41 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
"use client";
import { useState, useEffect, useCallback } from "react";
import { getLocalDateString } from "@/lib/utils";
interface FileStatus {
exists: boolean;
chars: number;
preview: string;
}
interface WorkspaceState {
onboardingComplete: boolean;
lastCheckInDate: string | null;
schemaVersion: number;
}
interface WorkspaceInfo {
path: string | null;
exists?: boolean;
files: Record<string, FileStatus>;
state: WorkspaceState | null;
}
export function useAssistantWorkspace() {
const [workspace, setWorkspace] = useState<WorkspaceInfo | null>(null);
const [loading, setLoading] = useState(true);
const [error, setError] = useState<string | null>(null);
const refetch = useCallback(async () => {
try {
setError(null);
const res = await fetch("/api/settings/workspace");
if (res.ok) {
const data = await res.json();
setWorkspace(data);
} else {
setError("Failed to fetch workspace info");
}
} catch (e) {
setError(e instanceof Error ? e.message : "Unknown error");
} finally {
setLoading(false);
}
}, []);
useEffect(() => {
refetch();
}, [refetch]);
const setWorkspacePath = useCallback(async (path: string) => {
try {
const res = await fetch("/api/settings/workspace", {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ path }),
});
if (res.ok) await refetch();
} catch (e) {
console.error("Failed to set workspace path:", e);
}
}, [refetch]);
const initializeWorkspace = useCallback(async () => {
if (!workspace?.path) return;
try {
const res = await fetch("/api/settings/workspace", {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ path: workspace.path, initialize: true }),
});
if (res.ok) await refetch();
} catch (e) {
console.error("Failed to initialize workspace:", e);
}
}, [workspace?.path, refetch]);
const today = getLocalDateString();
const needsCheckIn = workspace?.path != null
&& workspace.state != null
&& workspace.state.lastCheckInDate !== today;
return {
workspacePath: workspace?.path ?? null,
fileStatus: workspace?.files ?? {},
state: workspace?.state ?? null,
loading,
error,
needsCheckIn,
setWorkspacePath,
initializeWorkspace,
refetch,
};
}