2026-01-16 11:32:06 -08:00
|
|
|
"use client";
|
|
|
|
|
|
2026-03-24 16:07:28 +02:00
|
|
|
import { useQuery } from "@rocicorp/zero/react";
|
2026-03-23 19:04:51 +02:00
|
|
|
import { useMemo } from "react";
|
2026-01-16 11:32:06 -08:00
|
|
|
import type { SearchSourceConnector } from "@/contracts/types/connector.types";
|
2026-03-23 19:04:51 +02:00
|
|
|
import { queries } from "@/zero/queries";
|
2026-01-16 11:32:06 -08:00
|
|
|
|
|
|
|
|
/**
|
2026-07-06 15:12:40 +05:30
|
|
|
* Syncs connectors for a workspace via Zero.
|
2026-03-23 19:04:51 +02:00
|
|
|
* Returns connectors, loading state, error, and a refresh function.
|
2026-01-16 11:32:06 -08:00
|
|
|
*/
|
2026-07-06 15:12:40 +05:30
|
|
|
export function useConnectorsSync(workspaceId: number | string | null) {
|
|
|
|
|
const spaceId = workspaceId ? Number(workspaceId) : -1;
|
2026-03-23 19:04:51 +02:00
|
|
|
|
2026-07-06 15:12:40 +05:30
|
|
|
const [data, result] = useQuery(queries.connectors.bySpace({ workspaceId: spaceId }));
|
2026-03-23 19:04:51 +02:00
|
|
|
|
|
|
|
|
const connectors: SearchSourceConnector[] = useMemo(() => {
|
2026-07-06 15:12:40 +05:30
|
|
|
if (!workspaceId || !data) return [];
|
2026-03-23 19:04:51 +02:00
|
|
|
return data.map((c) => ({
|
|
|
|
|
id: c.id,
|
|
|
|
|
name: c.name,
|
|
|
|
|
connector_type: c.connectorType as SearchSourceConnector["connector_type"],
|
|
|
|
|
is_indexable: c.isIndexable,
|
|
|
|
|
is_active: true,
|
|
|
|
|
last_indexed_at: c.lastIndexedAt ? new Date(c.lastIndexedAt).toISOString() : null,
|
|
|
|
|
config: (c.config as Record<string, unknown>) ?? {},
|
|
|
|
|
periodic_indexing_enabled: c.periodicIndexingEnabled,
|
|
|
|
|
indexing_frequency_minutes: c.indexingFrequencyMinutes ?? null,
|
|
|
|
|
next_scheduled_at: c.nextScheduledAt ? new Date(c.nextScheduledAt).toISOString() : null,
|
2026-07-06 15:12:40 +05:30
|
|
|
workspace_id: c.workspaceId,
|
2026-03-23 19:04:51 +02:00
|
|
|
user_id: c.userId,
|
|
|
|
|
created_at: c.createdAt ? new Date(c.createdAt).toISOString() : new Date().toISOString(),
|
|
|
|
|
}));
|
2026-07-06 15:12:40 +05:30
|
|
|
}, [workspaceId, data]);
|
2026-03-23 19:04:51 +02:00
|
|
|
|
2026-07-06 15:12:40 +05:30
|
|
|
const loading = !workspaceId ? false : result.type !== "complete";
|
|
|
|
|
const error = !workspaceId ? null : null;
|
2026-03-23 19:04:51 +02:00
|
|
|
|
2026-03-23 19:29:08 +02:00
|
|
|
const refreshConnectors = async () => {};
|
2026-01-16 11:32:06 -08:00
|
|
|
|
|
|
|
|
return { connectors, loading, error, refreshConnectors };
|
|
|
|
|
}
|