mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-05 20:34:18 +01:00
feat(worktree): worktree tabs, server env vars, and multi-directory SSE (#240)
* loop: worktree-tabs-2 completed after 4 iterations * loop: server-env-vars completed after 7 iterations * feat(worktree): add worktree tabs and server env vars settings * refactor(worktree): multi-directory SSE support and workspace chip row * feat(workspace): collapsible workspace manager and sibling filtering * feat(workspace): workspace creation endpoint, refactored workspace manager, and tests * refactor(frontend): unify workspace label and remove dead code - extract shared workspaceLabel helper into api/repos and reuse in WorkspaceManager and RepoDetail directoryLabels - remove unused useSessions hook and subscribeToDirectory subscription - drop redundant frontend workspace dedup now guaranteed by backend - align WorktreeTabs empty-state test with create-workspace button
This commit is contained in:
1 parent
60ed756a0b
commit
b9236f7f20
38 files changed
+2423
-133
No files matched your search
@@ -1,4 +1,4 @@
|
||||
import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query";
|
||||
import { useQueries, useQuery, useMutation, useQueryClient } from "@tanstack/react-query";
|
||||
import { useMemo, useRef, useEffect, useCallback } from "react";
|
||||
import { OpenCodeClient } from "../api/opencode";
|
||||
import { FetchError } from "../api/fetchWrapper";
|
||||
@@ -36,17 +36,33 @@ export const useOpenCodeClient = (opcodeUrl: string | null | undefined, director
|
||||
);
|
||||
};
|
||||
|
||||
export const useSessions = (opcodeUrl: string | null | undefined, directory?: string) => {
|
||||
const client = useOpenCodeClient(opcodeUrl, directory);
|
||||
|
||||
return useQuery({
|
||||
queryKey: ["opencode", "sessions", opcodeUrl, directory],
|
||||
queryFn: () => client!.listSessions(),
|
||||
enabled: !!client,
|
||||
refetchOnWindowFocus: true,
|
||||
refetchOnReconnect: true,
|
||||
staleTime: 10000,
|
||||
export const useSessionsAcrossDirectories = (
|
||||
opcodeUrl: string | null | undefined,
|
||||
directories: string[],
|
||||
) => {
|
||||
const uniqueDirectories = useMemo(
|
||||
() => Array.from(new Set(directories.filter(Boolean))),
|
||||
[directories],
|
||||
);
|
||||
const directoryKey = uniqueDirectories.join('|');
|
||||
const queries = useQueries({
|
||||
queries: uniqueDirectories.map((directory) => ({
|
||||
queryKey: ["opencode", "sessions", opcodeUrl, directory],
|
||||
queryFn: () => new OpenCodeClient(opcodeUrl!, directory).listSessions(),
|
||||
enabled: !!opcodeUrl && !!directory,
|
||||
refetchOnWindowFocus: true,
|
||||
refetchOnReconnect: true,
|
||||
staleTime: 10000,
|
||||
})),
|
||||
});
|
||||
|
||||
return useMemo(() => {
|
||||
const data = queries.flatMap((q) => q.data ?? []);
|
||||
const isLoading = queries.some((q) => q.isLoading);
|
||||
const isError = queries.some((q) => q.isError);
|
||||
return { data, isLoading, isError };
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [directoryKey, queries.map((q) => q.dataUpdatedAt).join('|'), queries.map((q) => q.isLoading).join('|')]);
|
||||
};
|
||||
|
||||
export const useSession = (opcodeUrl: string | null | undefined, sessionID: string | undefined, directory?: string) => {
|
||||
@@ -112,33 +128,114 @@ export const useCreateSession = (
|
||||
});
|
||||
};
|
||||
|
||||
export const useDeleteSession = (opcodeUrl: string | null | undefined, directory?: string) => {
|
||||
export type DeleteSessionTarget = string | { id: string; directory?: string; workspaceID?: string };
|
||||
|
||||
const getDeleteSessionTargetId = (target: DeleteSessionTarget) =>
|
||||
typeof target === 'string' ? target : target.id;
|
||||
|
||||
const getDeleteSessionTargetDirectory = (target: DeleteSessionTarget, fallbackDirectory?: string) =>
|
||||
typeof target === 'string' ? fallbackDirectory : target.directory ?? fallbackDirectory;
|
||||
|
||||
const getDeleteSessionTargetWorkspaceID = (target: DeleteSessionTarget) =>
|
||||
typeof target === 'string' ? undefined : target.workspaceID;
|
||||
|
||||
const getDeleteSessionTargetKey = (target: DeleteSessionTarget, fallbackDirectory?: string) =>
|
||||
`${getDeleteSessionTargetDirectory(target, fallbackDirectory) ?? ''}:${getDeleteSessionTargetId(target)}`;
|
||||
|
||||
const isMissingWorkspaceError = (error: unknown) =>
|
||||
error instanceof Error && error.message.includes('Workspace not found:');
|
||||
|
||||
const shouldDeleteWorkspaceForSessionDeleteError = (error: unknown) =>
|
||||
isMissingWorkspaceError(error) ||
|
||||
(error instanceof FetchError &&
|
||||
error.statusCode === 500 &&
|
||||
(error.message.includes('Unexpected server error') || error.message === 'Request failed'));
|
||||
|
||||
export const useDeleteSession = (opcodeUrl: string | null | undefined, directory?: string | string[]) => {
|
||||
const queryClient = useQueryClient();
|
||||
const client = useOpenCodeClient(opcodeUrl, directory);
|
||||
const directories = useMemo(
|
||||
() => (Array.isArray(directory) ? directory : directory ? [directory] : []),
|
||||
[directory],
|
||||
);
|
||||
const primaryDirectory = directories[0];
|
||||
|
||||
return useMutation({
|
||||
mutationFn: async (sessionIDs: string | string[]) => {
|
||||
if (!client) {
|
||||
mutationFn: async (sessionIDs: DeleteSessionTarget | DeleteSessionTarget[]) => {
|
||||
if (!opcodeUrl) {
|
||||
throw new Error('OpenCode client not available');
|
||||
}
|
||||
|
||||
const ids = Array.isArray(sessionIDs) ? sessionIDs : [sessionIDs]
|
||||
const targets = Array.from(
|
||||
new Map(
|
||||
(Array.isArray(sessionIDs) ? sessionIDs : [sessionIDs]).map((target) => [
|
||||
getDeleteSessionTargetKey(target, primaryDirectory),
|
||||
target,
|
||||
]),
|
||||
).values(),
|
||||
)
|
||||
|
||||
const deletePromises = ids.map(async (sessionID) => {
|
||||
await client.deleteSession(sessionID);
|
||||
})
|
||||
|
||||
const results = await Promise.allSettled(deletePromises)
|
||||
const results: PromiseSettledResult<void>[] = []
|
||||
const removedWorkspaces = new Set<string>()
|
||||
for (const target of targets) {
|
||||
const workspaceID = getDeleteSessionTargetWorkspaceID(target)
|
||||
if (workspaceID && removedWorkspaces.has(workspaceID)) {
|
||||
results.push({ status: 'fulfilled', value: undefined })
|
||||
continue
|
||||
}
|
||||
|
||||
const client = new OpenCodeClient(
|
||||
opcodeUrl,
|
||||
getDeleteSessionTargetDirectory(target, primaryDirectory),
|
||||
)
|
||||
try {
|
||||
await client.deleteSession(getDeleteSessionTargetId(target))
|
||||
results.push({ status: 'fulfilled', value: undefined })
|
||||
} catch (reason) {
|
||||
if (workspaceID && shouldDeleteWorkspaceForSessionDeleteError(reason)) {
|
||||
try {
|
||||
await client.deleteWorkspace(workspaceID)
|
||||
removedWorkspaces.add(workspaceID)
|
||||
results.push({ status: 'fulfilled', value: undefined })
|
||||
continue
|
||||
} catch (workspaceReason) {
|
||||
results.push({ status: 'rejected', reason: workspaceReason })
|
||||
continue
|
||||
}
|
||||
}
|
||||
results.push({ status: 'rejected', reason })
|
||||
}
|
||||
}
|
||||
const failures = results.filter(result => result.status === 'rejected')
|
||||
|
||||
if (failures.length > 0) {
|
||||
throw new Error(`Failed to delete ${failures.length} session(s)`)
|
||||
}
|
||||
|
||||
return results
|
||||
return { deleted: targets.length, results }
|
||||
},
|
||||
onSettled: () => {
|
||||
queryClient.invalidateQueries({ queryKey: ["opencode", "sessions", opcodeUrl, directory] });
|
||||
onSuccess: ({ deleted }) => {
|
||||
showToast.success(deleted === 1 ? 'Session deleted' : `${deleted} sessions deleted`);
|
||||
},
|
||||
onError: () => {
|
||||
showToast.error('Failed to delete sessions');
|
||||
},
|
||||
onSettled: (_data, _error, variables) => {
|
||||
const targets = variables ? (Array.isArray(variables) ? variables : [variables]) : [];
|
||||
const affectedDirectories = new Set([
|
||||
...directories,
|
||||
...targets
|
||||
.map((target) => getDeleteSessionTargetDirectory(target))
|
||||
.filter((value): value is string => !!value),
|
||||
]);
|
||||
|
||||
if (affectedDirectories.size === 0) {
|
||||
queryClient.invalidateQueries({ queryKey: ["opencode", "sessions", opcodeUrl] });
|
||||
return;
|
||||
}
|
||||
|
||||
for (const affectedDirectory of affectedDirectories) {
|
||||
queryClient.invalidateQueries({ queryKey: ["opencode", "sessions", opcodeUrl, affectedDirectory] });
|
||||
}
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
Reference in new issue
Block a user