refactor(command-palette): consolidate fetch source hooks behind useApiSource

This commit is contained in:
simosmik
2026-04-30 07:47:46 +00:00
parent 95ad7272e9
commit f1d7df2b1e
5 changed files with 130 additions and 153 deletions

View File

@@ -0,0 +1,53 @@
import { useEffect, useState } from 'react';
export type ApiSourceState<T> = {
items: T[];
isLoading: boolean;
error: Error | null;
};
export function useApiSource<T, R = unknown>(opts: {
enabled: boolean;
deps: React.DependencyList;
fetcher: (signal: AbortSignal) => Promise<Response>;
parse: (raw: R) => T[];
}): ApiSourceState<T> {
const [items, setItems] = useState<T[]>([]);
const [isLoading, setIsLoading] = useState(false);
const [error, setError] = useState<Error | null>(null);
const { enabled, deps, fetcher, parse } = opts;
useEffect(() => {
if (!enabled) {
setItems([]);
setError(null);
return;
}
const controller = new AbortController();
setIsLoading(true);
setError(null);
fetcher(controller.signal)
.then((r) => r.json() as Promise<R>)
.then((data) => {
if (controller.signal.aborted) return;
setItems(parse(data));
})
.catch((err: unknown) => {
if (controller.signal.aborted) return;
if (err instanceof DOMException && err.name === 'AbortError') return;
setItems([]);
setError(err instanceof Error ? err : new Error(String(err)));
})
.finally(() => {
if (!controller.signal.aborted) setIsLoading(false);
});
return () => controller.abort();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [enabled, ...deps]);
return { items, isLoading, error };
}

View File

@@ -1,7 +1,7 @@
import { useEffect, useState } from 'react';
import { authenticatedFetch } from '../../../utils/api';
import { useApiSource } from './useApiSource';
export type BranchResult = {
name: string;
isCurrent: boolean;
@@ -13,35 +13,20 @@ interface BranchesResponse {
}
export function useBranchesSource(projectId: string | undefined, enabled: boolean) {
const [items, setItems] = useState<BranchResult[]>([]);
useEffect(() => {
if (!enabled || !projectId) {
setItems([]);
return;
}
let cancelled = false;
const params = new URLSearchParams({ project: projectId });
authenticatedFetch(`/api/git/branches?${params.toString()}`)
.then((r) => r.json() as Promise<BranchesResponse>)
.then((data) => {
if (cancelled) return;
const list = data.branches ?? [];
setItems(
list.map<BranchResult>((b) => ({
name: b.name,
isCurrent: Boolean(b.current),
isRemote: Boolean(b.isRemote),
})),
);
})
.catch(() => {
if (!cancelled) setItems([]);
});
return () => {
cancelled = true;
};
}, [projectId, enabled]);
return { items };
return useApiSource<BranchResult, BranchesResponse>({
enabled: enabled && !!projectId,
deps: [projectId],
fetcher: (signal) => {
const params = new URLSearchParams({ project: projectId! });
return authenticatedFetch(`/api/git/branches?${params.toString()}`, { signal });
},
parse: (data) => {
const list = data.branches ?? [];
return list.map<BranchResult>((b) => ({
name: b.name,
isCurrent: Boolean(b.current),
isRemote: Boolean(b.isRemote),
}));
},
});
}

View File

@@ -1,7 +1,7 @@
import { useEffect, useState } from 'react';
import { authenticatedFetch } from '../../../utils/api';
import { useApiSource } from './useApiSource';
export type CommitResult = {
hash: string;
shortHash: string;
@@ -15,44 +15,21 @@ interface CommitsResponse {
}
export function useCommitsSource(projectId: string | undefined, enabled: boolean) {
const [items, setItems] = useState<CommitResult[]>([]);
const [isLoading, setIsLoading] = useState(false);
useEffect(() => {
if (!enabled || !projectId) {
setItems([]);
return;
}
let cancelled = false;
setIsLoading(true);
const params = new URLSearchParams({ project: projectId, limit: '50' });
authenticatedFetch(`/api/git/commits?${params.toString()}`)
.then((r) => r.json() as Promise<CommitsResponse>)
.then((data) => {
if (cancelled) return;
if (!data.commits) {
setItems([]);
return;
}
setItems(
data.commits.map<CommitResult>((c) => ({
hash: c.hash,
shortHash: c.hash.slice(0, 7),
message: c.message,
author: c.author,
})),
);
})
.catch(() => {
if (!cancelled) setItems([]);
})
.finally(() => {
if (!cancelled) setIsLoading(false);
});
return () => {
cancelled = true;
};
}, [projectId, enabled]);
return { items, isLoading };
return useApiSource<CommitResult, CommitsResponse>({
enabled: enabled && !!projectId,
deps: [projectId],
fetcher: (signal) => {
const params = new URLSearchParams({ project: projectId!, limit: '50' });
return authenticatedFetch(`/api/git/commits?${params.toString()}`, { signal });
},
parse: (data) => {
if (!data.commits) return [];
return data.commits.map<CommitResult>((c) => ({
hash: c.hash,
shortHash: c.hash.slice(0, 7),
message: c.message,
author: c.author,
}));
},
});
}

View File

@@ -1,7 +1,7 @@
import { useEffect, useState } from 'react';
import { api } from '../../../utils/api';
import { useApiSource } from './useApiSource';
export type FileResult = {
path: string;
name: string;
@@ -28,36 +28,15 @@ function flatten(nodes: FileNode[], out: FileResult[]): void {
}
export function useFilesSource(projectId: string | undefined, enabled: boolean) {
const [items, setItems] = useState<FileResult[]>([]);
const [isLoading, setIsLoading] = useState(false);
useEffect(() => {
if (!enabled || !projectId) {
setItems([]);
return;
}
let cancelled = false;
setIsLoading(true);
api
.getFiles(projectId)
.then((r) => r.json())
.then((data: unknown) => {
if (cancelled) return;
const tree: FileNode[] = Array.isArray(data) ? (data as FileNode[]) : [];
const flat: FileResult[] = [];
flatten(tree, flat);
setItems(flat);
})
.catch(() => {
if (!cancelled) setItems([]);
})
.finally(() => {
if (!cancelled) setIsLoading(false);
});
return () => {
cancelled = true;
};
}, [projectId, enabled]);
return { items, isLoading };
return useApiSource<FileResult, unknown>({
enabled: enabled && !!projectId,
deps: [projectId],
fetcher: (signal) => api.getFiles(projectId!, { signal }),
parse: (data) => {
const tree: FileNode[] = Array.isArray(data) ? (data as FileNode[]) : [];
const flat: FileResult[] = [];
flatten(tree, flat);
return flat;
},
});
}

View File

@@ -1,8 +1,8 @@
import { useEffect, useState } from 'react';
import { api } from '../../../utils/api';
import { authenticatedFetch } from '../../../utils/api';
import type { LLMProvider, ProjectSession } from '../../../types/app';
import { useApiSource } from './useApiSource';
export type SessionResult = {
id: string;
label: string;
@@ -17,45 +17,28 @@ interface SessionsResponse {
}
export function useSessionsSource(projectId: string | undefined, enabled: boolean) {
const [items, setItems] = useState<SessionResult[]>([]);
const [isLoading, setIsLoading] = useState(false);
useEffect(() => {
if (!enabled || !projectId) {
setItems([]);
return;
}
let cancelled = false;
setIsLoading(true);
api
.projectSessions(projectId, { limit: 50 })
.then((r) => r.json() as Promise<SessionsResponse>)
.then((data) => {
if (cancelled) return;
const all: ProjectSession[] = [
...(data.sessions ?? []),
...(data.cursorSessions ?? []),
...(data.codexSessions ?? []),
...(data.geminiSessions ?? []),
];
setItems(
all.map<SessionResult>((s) => ({
id: s.id,
label: (s.title || s.summary || s.name || s.id) as string,
provider: s.__provider,
})),
);
})
.catch(() => {
if (!cancelled) setItems([]);
})
.finally(() => {
if (!cancelled) setIsLoading(false);
});
return () => {
cancelled = true;
};
}, [projectId, enabled]);
return { items, isLoading };
return useApiSource<SessionResult, SessionsResponse>({
enabled: enabled && !!projectId,
deps: [projectId],
fetcher: (signal) => {
const params = new URLSearchParams({ limit: '50', offset: '0' });
return authenticatedFetch(
`/api/projects/${encodeURIComponent(projectId!)}/sessions?${params.toString()}`,
{ signal },
);
},
parse: (data) => {
const all: ProjectSession[] = [
...(data.sessions ?? []),
...(data.cursorSessions ?? []),
...(data.codexSessions ?? []),
...(data.geminiSessions ?? []),
];
return all.map<SessionResult>((s) => ({
id: s.id,
label: (s.title || s.summary || s.name || s.id) as string,
provider: s.__provider,
}));
},
});
}