Fix no-op ternary for setup scripts and deduplicate persistence logic
- Add '(setup)' suffix to script names in dropdown when runOnWorktreeCreate is true - Extract shared persistScriptsUpdate helper to eliminate ~50 lines of duplicated persistence/rollback logic between saveProjectScript and updateProjectScript
This commit is contained in:
1 parent
5f94bfd5fe
commit
a84b28420c
2 files changed
+66
-99
No files matched your search
@@ -300,7 +300,9 @@ const ComposerCommandMenu = memo(function ComposerCommandMenu(props: {
|
||||
<Command
|
||||
mode="none"
|
||||
onItemHighlighted={(highlightedValue) => {
|
||||
props.onHighlightedItemChange(typeof highlightedValue === "string" ? highlightedValue : null);
|
||||
props.onHighlightedItemChange(
|
||||
typeof highlightedValue === "string" ? highlightedValue : null,
|
||||
);
|
||||
}}
|
||||
>
|
||||
<div className="relative overflow-hidden rounded-xl border border-border/80 bg-popover/96 shadow-lg/8 backdrop-blur-xs">
|
||||
@@ -670,9 +672,7 @@ export default function ChatView() {
|
||||
});
|
||||
const targetCwd = options?.cwd ?? gitCwd ?? activeProject.cwd;
|
||||
const baseTerminalId =
|
||||
activeThread.activeTerminalId ||
|
||||
activeThread.terminalIds[0] ||
|
||||
DEFAULT_THREAD_TERMINAL_ID;
|
||||
activeThread.activeTerminalId || activeThread.terminalIds[0] || DEFAULT_THREAD_TERMINAL_ID;
|
||||
const isBaseTerminalBusy = activeThread.runningTerminalIds.includes(baseTerminalId);
|
||||
const wantsNewTerminal = Boolean(options?.preferNewTerminal) || isBaseTerminalBusy;
|
||||
const shouldCreateNewTerminal =
|
||||
@@ -739,6 +739,63 @@ export default function ChatView() {
|
||||
},
|
||||
[activeProject, activeThread, activeThreadId, api, dispatch, gitCwd],
|
||||
);
|
||||
const persistScriptsUpdate = useCallback(
|
||||
async (
|
||||
project: NonNullable<typeof activeProject>,
|
||||
nativeApi: NativeApi,
|
||||
nextScripts: ProjectScript[],
|
||||
scriptCommandId: string,
|
||||
input: NewProjectScriptInput,
|
||||
) => {
|
||||
let scriptsPersisted = false;
|
||||
try {
|
||||
const updated = await nativeApi.projects.updateScripts({
|
||||
id: project.id,
|
||||
scripts: nextScripts,
|
||||
});
|
||||
scriptsPersisted = true;
|
||||
|
||||
if (input.keybinding) {
|
||||
const keybindingUpdate = await nativeApi.server.upsertKeybinding({
|
||||
key: input.keybinding,
|
||||
command: commandForProjectScript(scriptCommandId),
|
||||
});
|
||||
queryClient.setQueryData(
|
||||
serverQueryKeys.config(),
|
||||
(current: { cwd: string; keybindings: ResolvedKeybindingsConfig } | undefined) =>
|
||||
current
|
||||
? { ...current, keybindings: keybindingUpdate.keybindings }
|
||||
: {
|
||||
cwd: project.cwd,
|
||||
keybindings: keybindingUpdate.keybindings,
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
dispatch({
|
||||
type: "SET_PROJECT_SCRIPTS",
|
||||
projectId: updated.project.id,
|
||||
scripts: updated.project.scripts,
|
||||
});
|
||||
} catch (error) {
|
||||
if (scriptsPersisted) {
|
||||
await nativeApi.projects
|
||||
.updateScripts({
|
||||
id: project.id,
|
||||
scripts: project.scripts,
|
||||
})
|
||||
.catch(() => undefined);
|
||||
}
|
||||
dispatch({
|
||||
type: "SET_PROJECT_SCRIPTS",
|
||||
projectId: project.id,
|
||||
scripts: project.scripts,
|
||||
});
|
||||
throw error;
|
||||
}
|
||||
},
|
||||
[dispatch, queryClient],
|
||||
);
|
||||
const saveProjectScript = useCallback(
|
||||
async (input: NewProjectScriptInput) => {
|
||||
if (!activeProject) return;
|
||||
@@ -769,54 +826,9 @@ export default function ChatView() {
|
||||
});
|
||||
|
||||
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(nextId),
|
||||
});
|
||||
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;
|
||||
}
|
||||
await persistScriptsUpdate(activeProject, api, nextScripts, nextId, input);
|
||||
},
|
||||
[activeProject, api, dispatch, queryClient],
|
||||
[activeProject, api, dispatch, persistScriptsUpdate],
|
||||
);
|
||||
const updateProjectScript = useCallback(
|
||||
async (scriptId: string, input: NewProjectScriptInput) => {
|
||||
@@ -848,54 +860,9 @@ export default function ChatView() {
|
||||
});
|
||||
|
||||
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;
|
||||
}
|
||||
await persistScriptsUpdate(activeProject, api, nextScripts, scriptId, input);
|
||||
},
|
||||
[activeProject, api, dispatch, queryClient],
|
||||
[activeProject, api, dispatch, persistScriptsUpdate],
|
||||
);
|
||||
|
||||
const handleRuntimeModeChange = async (mode: "approval-required" | "full-access") => {
|
||||
|
||||
@@ -292,7 +292,7 @@ export default function ProjectScriptsControl({
|
||||
>
|
||||
<ScriptIcon icon={script.icon} className="size-4" />
|
||||
<span className="truncate">
|
||||
{script.runOnWorktreeCreate ? `${script.name}` : script.name}
|
||||
{script.runOnWorktreeCreate ? `${script.name} (setup)` : script.name}
|
||||
</span>
|
||||
<span className="relative ms-auto flex h-6 min-w-6 items-center justify-end">
|
||||
{shortcutLabel && (
|
||||
|
||||
Reference in new issue
Block a user