initial
This commit is contained in:
46
apps/web/src/hooks/useSessions.ts
Normal file
46
apps/web/src/hooks/useSessions.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
import { useCallback, useEffect, useState } from 'react';
|
||||
import { api } from '@/api/client';
|
||||
import type { Session } from '@/api/types';
|
||||
|
||||
export function useSessions(projectId: string | undefined) {
|
||||
const [sessions, setSessions] = useState<Session[] | null>(null);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
|
||||
const refresh = useCallback(async () => {
|
||||
if (!projectId) {
|
||||
setSessions(null);
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const list = await api.sessions.listForProject(projectId);
|
||||
setSessions(list);
|
||||
setError(null);
|
||||
} catch (err) {
|
||||
setError(err instanceof Error ? err.message : 'failed to load sessions');
|
||||
}
|
||||
}, [projectId]);
|
||||
|
||||
useEffect(() => {
|
||||
void refresh();
|
||||
}, [refresh]);
|
||||
|
||||
const create = useCallback(
|
||||
async (body: { name?: string; model?: string; system_prompt?: string }) => {
|
||||
if (!projectId) throw new Error('no project');
|
||||
const created = await api.sessions.create(projectId, body);
|
||||
await refresh();
|
||||
return created;
|
||||
},
|
||||
[projectId, refresh]
|
||||
);
|
||||
|
||||
const remove = useCallback(
|
||||
async (id: string) => {
|
||||
await api.sessions.remove(id);
|
||||
await refresh();
|
||||
},
|
||||
[refresh]
|
||||
);
|
||||
|
||||
return { sessions, error, refresh, create, remove };
|
||||
}
|
||||
Reference in New Issue
Block a user