mirror of
https://github.com/MODSetter/SurfSense.git
synced 2026-05-17 18:35:19 +02:00
Removes duplicated LogLevel, LogStatus, Log, LogFilters and LogSummary definitions from surfsense_web/hooks/use-logs.ts. These shapes already live as Zod-derived types in contracts/types/log.types.ts, which is the source of truth used by logs-api.service.ts and log-mutation.atoms.ts. Adds LogLevel and LogStatus aliases for LogLevelEnum/LogStatusEnum in log.types.ts so the existing public surface from use-logs is preserved without per-hook re-exports. The hook re-exports the canonical names so callers (app/dashboard/[search_space_id]/logs/(manage)/page.tsx) do not need to change. Closes #1372
122 lines
3.3 KiB
TypeScript
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.search_space_id) {
|
|
params.search_space_id = allFilters.search_space_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({
|
|
search_space_id: searchSpaceId,
|
|
...buildQueryParams(filters ?? {}),
|
|
}),
|
|
queryFn: () =>
|
|
logsApiService.getLogs({
|
|
queryParams: {
|
|
search_space_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({
|
|
search_space_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 };
|
|
}
|