Skip to content

Open-source Grok Bot alternative for named AI teammates. Local-first desktop, web, and mobile clients with per-bot tools, instructions, and memory. Independent fork of T3 Code.

Refactor project script persistence flow in ChatView

Julius Marminge71d8f741436b
diff --git a/apps/web/src/components/ChatView.tsx b/apps/web/src/components/ChatView.tsx
index b9b06ac1a..b514da8d4 100644
--- a/apps/web/src/components/ChatView.tsx
+++ b/apps/web/src/components/ChatView.tsx
@@ -739,48 +739,35 @@ export default function ChatView() {
},
[activeProject, activeThread, activeThreadId, api, dispatch, gitCwd],
);
- const saveProjectScript = useCallback(
- async (input: NewProjectScriptInput) => {
- if (!activeProject) return;
- const nextId = nextProjectScriptId(
- input.name,
- activeProject.scripts.map((script) => script.id),
- );
- const nextScript: ProjectScript = {
- id: nextId,
- name: input.name,
- command: input.command,
- icon: input.icon,
- runOnWorktreeCreate: input.runOnWorktreeCreate,
- };
- const nextScripts = input.runOnWorktreeCreate
- ? [
- ...activeProject.scripts.map((script) =>
- script.runOnWorktreeCreate ? { ...script, runOnWorktreeCreate: false } : script,
- ),
- nextScript,
- ]
- : [...activeProject.scripts, nextScript];
-
+ const persistProjectScripts = useCallback(
+ async (input: {
+ projectId: string;
+ projectCwd: string;
+ previousScripts: ProjectScript[];
+ nextScripts: ProjectScript[];
+ keybinding?: string | null;
+ keybindingCommand: string;
+ }) => {
dispatch({
type: "SET_PROJECT_SCRIPTS",
- projectId: activeProject.id,
- scripts: nextScripts,
+ projectId: input.projectId,
+ scripts: input.nextScripts,
});
if (!isElectron || !api) return;
+
let scriptsPersisted = false;
try {
const updated = await api.projects.updateScripts({
- id: activeProject.id,
- scripts: nextScripts,
+ id: input.projectId,
+ scripts: input.nextScripts,
});
scriptsPersisted = true;
if (input.keybinding) {
const keybindingUpdate = await api.server.upsertKeybinding({
key: input.keybinding,
- command: commandForProjectScript(nextId),
+ command: input.keybindingCommand,
});
queryClient.setQueryData(
serverQueryKeys.config(),
@@ -788,7 +775,7 @@ export default function ChatView() {
current
? { ...current, keybindings: keybindingUpdate.keybindings }
: {
- cwd: activeProject.cwd,
+ cwd: input.projectCwd,
keybindings: keybindingUpdate.keybindings,
},
);
@@ -803,20 +790,54 @@ export default function ChatView() {
if (scriptsPersisted) {
await api.projects
.updateScripts({
- id: activeProject.id,
- scripts: activeProject.scripts,
+ id: input.projectId,
+ scripts: input.previousScripts,
})
.catch(() => undefined);
}
dispatch({
type: "SET_PROJECT_SCRIPTS",
- projectId: activeProject.id,
- scripts: activeProject.scripts,
+ projectId: input.projectId,
+ scripts: input.previousScripts,
});
throw error;
}
},
- [activeProject, api, dispatch, queryClient],
+ [api, dispatch, queryClient],
+ );
+ const saveProjectScript = useCallback(
+ async (input: NewProjectScriptInput) => {
+ if (!activeProject) return;
+ const nextId = nextProjectScriptId(
+ input.name,
+ activeProject.scripts.map((script) => script.id),
+ );
+ const nextScript: ProjectScript = {
+ id: nextId,
+ name: input.name,
+ command: input.command,
+ icon: input.icon,
+ runOnWorktreeCreate: input.runOnWorktreeCreate,
+ };
+ const nextScripts = input.runOnWorktreeCreate
+ ? [
+ ...activeProject.scripts.map((script) =>
+ script.runOnWorktreeCreate ? { ...script, runOnWorktreeCreate: false } : script,
+ ),
+ nextScript,
+ ]
+ : [...activeProject.scripts, nextScript];
+
+ await persistProjectScripts({
+ projectId: activeProject.id,
+ projectCwd: activeProject.cwd,
+ previousScripts: activeProject.scripts,
+ nextScripts,
+ keybinding: input.keybinding,
+ keybindingCommand: commandForProjectScript(nextId),
+ });
+ },
+ [activeProject, persistProjectScripts],
);
const updateProjectScript = useCallback(
async (scriptId: string, input: NewProjectScriptInput) => {
@@ -841,61 +862,16 @@ export default function ChatView() {
: script,
);
- dispatch({
- type: "SET_PROJECT_SCRIPTS",
+ await persistProjectScripts({
projectId: activeProject.id,
- scripts: nextScripts,
+ projectCwd: activeProject.cwd,
+ previousScripts: activeProject.scripts,
+ nextScripts,
+ keybinding: input.keybinding,
+ keybindingCommand: commandForProjectScript(scriptId),
});
-
- if (!isElectron || !api) return;
- let scriptsPersisted = false;
- try {
- const updated = await api.projects.updateScripts({
- id: activeProject.id,
- scripts: nextScripts,
- });
- scriptsPersisted = true;
-
- if (input.keybinding) {
- const keybindingUpdate = await api.server.upsertKeybinding({
- key: input.keybinding,
- command: commandForProjectScript(scriptId),
- });
- queryClient.setQueryData(
- serverQueryKeys.config(),
- (current: { cwd: string; keybindings: ResolvedKeybindingsConfig } | undefined) =>
- current
- ? { ...current, keybindings: keybindingUpdate.keybindings }
- : {
- cwd: activeProject.cwd,
- keybindings: keybindingUpdate.keybindings,
- },
- );
- }
-
- dispatch({
- type: "SET_PROJECT_SCRIPTS",
- projectId: updated.project.id,
- scripts: updated.project.scripts,
- });
- } catch (error) {
- if (scriptsPersisted) {
- await api.projects
- .updateScripts({
- id: activeProject.id,
- scripts: activeProject.scripts,
- })
- .catch(() => undefined);
- }
- dispatch({
- type: "SET_PROJECT_SCRIPTS",
- projectId: activeProject.id,
- scripts: activeProject.scripts,
- });
- throw error;
- }
},
- [activeProject, api, dispatch, queryClient],
+ [activeProject, persistProjectScripts],
);
const handleRuntimeModeChange = async (mode: "approval-required" | "full-access") => {