FastAPI + Celery + Next.js + Postgres/Redis app with company monitoring, source collection, LLM-based change analysis, enrichment, and account security (Turnstile, escalating lockout, email verification).
44 lines
1.5 KiB
TypeScript
44 lines
1.5 KiB
TypeScript
"use client";
|
|
|
|
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
|
import { api } from "@/lib/api-client";
|
|
import type { MonitoringRunResponse } from "@/lib/types";
|
|
|
|
const ACTIVE_STATUSES = new Set(["queued", "running"]);
|
|
|
|
export function useCompanyRuns(companyId: string) {
|
|
return useQuery({
|
|
queryKey: ["runs", companyId],
|
|
queryFn: () => api.listCompanyRuns(companyId),
|
|
refetchInterval: (query) => {
|
|
const runs = query.state.data as MonitoringRunResponse[] | undefined;
|
|
return runs?.some((r) => ACTIVE_STATUSES.has(r.status)) ? 2000 : false;
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useRunCompanyNow(companyId: string) {
|
|
const queryClient = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: () => api.runCompanyNow(companyId),
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({ queryKey: ["runs", companyId] });
|
|
queryClient.invalidateQueries({ queryKey: ["companies", companyId] });
|
|
queryClient.invalidateQueries({ queryKey: ["sources", companyId] });
|
|
},
|
|
});
|
|
}
|
|
|
|
/** Same action, for call sites (like the companies list) that don't want to
|
|
* fix a single companyId at the hook-call site. */
|
|
export function useRunAnyCompanyNow() {
|
|
const queryClient = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: (companyId: string) => api.runCompanyNow(companyId),
|
|
onSuccess: (_data, companyId) => {
|
|
queryClient.invalidateQueries({ queryKey: ["runs", companyId] });
|
|
queryClient.invalidateQueries({ queryKey: ["companies"] });
|
|
},
|
|
});
|
|
}
|