SurfSense/surfsense_web/hooks/use-logs.ts
DESKTOP-RTLN3BA\$punk a64c8205fe feat: implement ensure_publication for zero_publication management
- Added the ensure_publication function to create and verify the zero_publication if it is missing, ensuring idempotency during database initialization.
- Integrated ensure_publication into the create_db_and_tables function to prevent zero-cache crash loops on startup.
- Introduced a self-check script to validate the ensure_publication functionality on a create_all-bootstrapped database.
- Updated various components to reflect the transition from search space to workspace, including adjustments in imports and routing paths.
2026-07-05 23:17:13 -07:00

122 lines
3.3 KiB
TypeScript

"use client";
import { useQuery } from "@tanstack/react-query";
import { useCallback, useMemo } from "react";
import type { LogFilters } from "@/contracts/types/log.types";
import { logsApiService } from "@/lib/apis/logs-api.service";
import { cacheKeys } from "@/lib/query-client/cache-keys";
export type {
Log,
LogFilters,
LogLevel,
LogStatus,
LogSummary,
} from "@/contracts/types/log.types";
export function useLogs(searchSpaceId?: number, filters: LogFilters = {}) {
const filtersKey = JSON.stringify(filters);
// biome-ignore lint/correctness/useExhaustiveDependencies: stable serialized key used intentionally
const memoizedFilters = useMemo(() => filters, [filtersKey]);
const buildQueryParams = useCallback(
(customFilters: LogFilters = {}) => {
const params: Record<string, string> = {};
const allFilters = { ...memoizedFilters, ...customFilters };
if (allFilters.workspace_id) {
params.workspace_id = allFilters.workspace_id.toString();
}
if (allFilters.level) {
params.level = allFilters.level;
}
if (allFilters.status) {
params.status = allFilters.status;
}
if (allFilters.source) {
params.source = allFilters.source;
}
if (allFilters.start_date) {
params.start_date = allFilters.start_date;
}
if (allFilters.end_date) {
params.end_date = allFilters.end_date;
}
return params;
},
[memoizedFilters]
);
const {
data: logs,
isLoading: loading,
error,
refetch,
} = useQuery({
queryKey: cacheKeys.logs.withQueryParams({
workspace_id: searchSpaceId,
...buildQueryParams(filters ?? {}),
}),
queryFn: () =>
logsApiService.getLogs({
queryParams: {
workspace_id: searchSpaceId,
...buildQueryParams(filters ?? {}),
},
}),
enabled: !!searchSpaceId,
staleTime: 3 * 60 * 1000,
});
return {
logs: logs ?? [],
loading,
error,
refreshLogs: refetch,
};
}
// Separate hook for log summary with smart polling support for document processing indicator UI
// Polling only happens when there are active tasks, otherwise it stops to save resources
export function useLogsSummary(
searchSpaceId: number,
hours: number = 24,
options: { refetchInterval?: number; enablePolling?: boolean } = {}
) {
const { enablePolling = false, refetchInterval = 10000 } = options;
const {
data: summary,
isLoading: loading,
error,
refetch,
} = useQuery({
queryKey: cacheKeys.logs.summary(searchSpaceId),
queryFn: () =>
logsApiService.getLogSummary({
workspace_id: searchSpaceId,
hours: hours,
}),
enabled: !!searchSpaceId,
staleTime: 3 * 60 * 1000,
// Always refetch on mount to show fresh processing tasks when navigating to the page
refetchOnMount: "always",
// Smart polling: only poll when there are active tasks and polling is enabled
// This prevents unnecessary API calls when nothing is being processed
refetchInterval: enablePolling
? (query) => {
const data = query.state.data;
// Only continue polling if there are active tasks
if (data?.active_tasks && data.active_tasks.length > 0) {
return refetchInterval;
}
// No active tasks - stop polling but check again after a longer interval
// to catch any newly started tasks
return 30000; // Check every 30 seconds when idle
}
: undefined,
});
return { summary, loading, error, refreshSummary: refetch };
}