refactor: move fetching messages to module

This commit is contained in:
Haileyesus
2026-04-27 14:30:09 +03:00
parent 9663f08fcb
commit 16954c883b
9 changed files with 116 additions and 136 deletions

View File

@@ -8,8 +8,9 @@
*/
import { useCallback, useMemo, useRef, useState } from 'react';
import type { LLMProvider } from '../types/app';
import { authenticatedFetch } from '../utils/api';
import type { LLMProvider } from '../types/app';
// ─── NormalizedMessage (mirrors server/adapters/types.js) ────────────────────
@@ -164,11 +165,9 @@ export function useSessionStore() {
const has = useCallback((sessionId: string) => storeRef.current.has(sessionId), []);
/**
* Fetch messages from the unified endpoint and populate serverMessages.
* Fetch messages from the provider sessions endpoint and populate serverMessages.
*
* `projectId` is the DB-assigned identifier used by the backend to resolve
* the project's on-disk directory; it replaces the legacy `projectName`
* Claude folder encoding that callers used to pass.
* Provider and project metadata are resolved server-side from `sessionId`.
*/
const fetchFromServer = useCallback(async (
sessionId: string,
@@ -186,16 +185,13 @@ export function useSessionStore() {
try {
const params = new URLSearchParams();
if (opts.provider) params.append('provider', opts.provider);
if (opts.projectId) params.append('projectId', opts.projectId);
if (opts.projectPath) params.append('projectPath', opts.projectPath);
if (opts.limit !== null && opts.limit !== undefined) {
params.append('limit', String(opts.limit));
params.append('offset', String(opts.offset ?? 0));
}
const qs = params.toString();
const url = `/api/sessions/${encodeURIComponent(sessionId)}/messages${qs ? `?${qs}` : ''}`;
const url = `/api/providers/sessions/${encodeURIComponent(sessionId)}/messages${qs ? `?${qs}` : ''}`;
const response = await authenticatedFetch(url);
if (!response.ok) {
@@ -228,9 +224,6 @@ export function useSessionStore() {
/**
* Load older (paginated) messages and prepend to serverMessages.
*
* Accepts `projectId` (the DB primary key) so the unified messages endpoint
* can resolve the project path through the database.
*/
const fetchMore = useCallback(async (
sessionId: string,
@@ -245,15 +238,12 @@ export function useSessionStore() {
if (!slot.hasMore) return slot;
const params = new URLSearchParams();
if (opts.provider) params.append('provider', opts.provider);
if (opts.projectId) params.append('projectId', opts.projectId);
if (opts.projectPath) params.append('projectPath', opts.projectPath);
const limit = opts.limit ?? 20;
params.append('limit', String(limit));
params.append('offset', String(slot.offset));
const qs = params.toString();
const url = `/api/sessions/${encodeURIComponent(sessionId)}/messages${qs ? `?${qs}` : ''}`;
const url = `/api/providers/sessions/${encodeURIComponent(sessionId)}/messages${qs ? `?${qs}` : ''}`;
try {
const response = await authenticatedFetch(url);
@@ -305,14 +295,11 @@ export function useSessionStore() {
}, [getSlot, notify]);
/**
* Re-fetch serverMessages from the unified endpoint (e.g., on projects_updated).
*
* Uses the DB-assigned `projectId`; the legacy folder-derived projectName
* is no longer accepted here.
* Re-fetch serverMessages from the provider sessions endpoint.
*/
const refreshFromServer = useCallback(async (
sessionId: string,
opts: {
_opts: {
provider?: LLMProvider;
projectId?: string;
projectPath?: string;
@@ -321,12 +308,9 @@ export function useSessionStore() {
const slot = getSlot(sessionId);
try {
const params = new URLSearchParams();
if (opts.provider) params.append('provider', opts.provider);
if (opts.projectId) params.append('projectId', opts.projectId);
if (opts.projectPath) params.append('projectPath', opts.projectPath);
const qs = params.toString();
const url = `/api/sessions/${encodeURIComponent(sessionId)}/messages${qs ? `?${qs}` : ''}`;
const url = `/api/providers/sessions/${encodeURIComponent(sessionId)}/messages${qs ? `?${qs}` : ''}`;
const response = await authenticatedFetch(url);
if (!response.ok) throw new Error(`HTTP ${response.status}`);