"use client"; import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; import { useEffect } from "react"; import toast from "react-hot-toast"; import { get, post } from "../../lib/api"; import LoadingCard from "./Loading"; import { useNotifications } from "./NotificationContext"; export default function WatcherStatus() { const queryClient = useQueryClient(); const { addNotification } = useNotifications(); const { data, isLoading, error } = useQuery({ queryKey: ["watcher", "status"], queryFn: () => get("/watcher/status"), }); const { data: _datasets, isLoading: _datasetsLoading } = useQuery({ queryKey: ["datasets"], queryFn: () => get("/files"), }); const { data: settings } = useQuery({ queryKey: ["settings", "datasets"], queryFn: () => get("/config/settings/datasets"), }); const startMutation = useMutation({ mutationFn: () => post("/watcher/start"), onSuccess: () => { toast.success("Watcher started successfully"); addNotification({ type: "success", title: "Watcher Started", message: "The file watcher has been started successfully.", }); // Invalidate and refetch to ensure status updates immediately queryClient.invalidateQueries({ queryKey: ["watcher", "status"] }); setTimeout(() => { queryClient.refetchQueries({ queryKey: ["watcher", "status"] }); }, 100); }, }); const stopMutation = useMutation({ mutationFn: () => post("/watcher/stop"), onSuccess: () => { toast.success("Watcher stopped successfully"); addNotification({ type: "success", title: "Watcher Stopped", message: "The file watcher has been stopped successfully.", }); // Invalidate and refetch to ensure status updates immediately queryClient.invalidateQueries({ queryKey: ["watcher", "status"] }); setTimeout(() => { queryClient.refetchQueries({ queryKey: ["watcher", "status"] }); }, 100); }, }); // Listen for WebSocket events useEffect(() => { const handleWatcherUpdate = (event: CustomEvent) => { const updateData = event.detail; if (updateData.type === "started" || updateData.type === "stopped") { // Invalidate and refetch the watcher status queryClient.invalidateQueries({ queryKey: ["watcher", "status"] }); } }; window.addEventListener( "watcherUpdate", handleWatcherUpdate as EventListener ); return () => { window.removeEventListener( "watcherUpdate", handleWatcherUpdate as EventListener ); }; }, [queryClient]); if (isLoading) return ; if (error) { return (

Failed to load watcher status

Unable to connect to the file watcher service.

); } return (

File Watcher

Status: {data.isWatching ? "Watching" : "Idle"}
Active Watches: {settings ? Object.values(settings).filter( (dataset: any) => dataset.enabled !== false ).length : 0}
{settings && (
Configured Datasets ({Object.keys(settings).length}):
{Object.keys(settings).map((datasetName: string) => { const datasetConfig = settings[datasetName]; const isEnabled = datasetConfig?.enabled !== false; return ( {datasetName} {!isEnabled && " (disabled)"} ); })}
)} {data.watches && data.watches.length > 0 && (
Watched Paths:
{data.watches.map((watch: any, index: number) => (
{watch.path || watch}
))}
)}
); }