6ede33ccdb
Build and Push Docker Images / create_manifest (web, surfsense-web, , cpu) (push) Has been cancelled
Build and Push Docker Images / finalize_release (push) Has been cancelled
Obsidian Plugin Lint / lint (push) Has been cancelled
Build and Push Docker Images / build (./surfsense_web, cpu, ./surfsense_web/Dockerfile, web, surfsense-web, ubuntu-24.04-arm, linux/arm64, arm64, , runner, false, cpu) (push) Has been cancelled
Build and Push Docker Images / build (./surfsense_web, cpu, ./surfsense_web/Dockerfile, web, surfsense-web, ubuntu-latest, linux/amd64, amd64, , runner, false, cpu) (push) Has been cancelled
Build and Push Docker Images / compute_version (push) Has been cancelled
Build and Push Docker Images / build (./surfsense_backend, cpu, ./surfsense_backend/Dockerfile, backend, surfsense-backend, ubuntu-24.04-arm, linux/arm64, arm64, , production, false, cpu) (push) Has been cancelled
Build and Push Docker Images / build (./surfsense_backend, cpu, ./surfsense_backend/Dockerfile, backend, surfsense-backend, ubuntu-latest, linux/amd64, amd64, , production, false, cpu) (push) Has been cancelled
Build and Push Docker Images / build (./surfsense_backend, cu126, ./surfsense_backend/Dockerfile, backend, surfsense-backend, ubuntu-24.04-arm, linux/arm64, arm64, -cuda126, production, true, cuda126) (push) Has been cancelled
Build and Push Docker Images / build (./surfsense_backend, cu126, ./surfsense_backend/Dockerfile, backend, surfsense-backend, ubuntu-latest, linux/amd64, amd64, -cuda126, production, true, cuda126) (push) Has been cancelled
Build and Push Docker Images / build (./surfsense_backend, cu128, ./surfsense_backend/Dockerfile, backend, surfsense-backend, ubuntu-24.04-arm, linux/arm64, arm64, -cuda, production, true, cuda) (push) Has been cancelled
Build and Push Docker Images / build (./surfsense_backend, cu128, ./surfsense_backend/Dockerfile, backend, surfsense-backend, ubuntu-latest, linux/amd64, amd64, -cuda, production, true, cuda) (push) Has been cancelled
Build and Push Docker Images / verify_digests (push) Has been cancelled
Build and Push Docker Images / create_manifest (backend, surfsense-backend, , cpu) (push) Has been cancelled
Build and Push Docker Images / create_manifest (backend, surfsense-backend, -cuda, cuda) (push) Has been cancelled
Build and Push Docker Images / create_manifest (backend, surfsense-backend, -cuda126, cuda126) (push) Has been cancelled
111 lines
3.2 KiB
TypeScript
111 lines
3.2 KiB
TypeScript
"use client";
|
|
|
|
import { useQuery } from "@rocicorp/zero/react";
|
|
import { useEffect, useRef, useState } from "react";
|
|
import { queries } from "@/zero/queries";
|
|
|
|
export type DocumentsProcessingStatus =
|
|
| "idle"
|
|
| "processing"
|
|
| "background_sync"
|
|
| "success"
|
|
| "error";
|
|
|
|
const SUCCESS_LINGER_MS = 5000;
|
|
|
|
interface UseDocumentsProcessingOptions {
|
|
hasPeriodicSyncEnabled?: boolean;
|
|
}
|
|
|
|
/**
|
|
* Returns the processing status of documents in the workspace:
|
|
* - "processing" — docs are queued or actively being prepared for search
|
|
* - "background_sync" — existing docs are being refreshed in the background
|
|
* - "error" — nothing processing, but failed docs exist (show red icon)
|
|
* - "success" — just transitioned from processing → all clear (green check, auto-dismisses)
|
|
* - "idle" — nothing noteworthy (show normal icon)
|
|
*/
|
|
export function useDocumentsProcessing(
|
|
workspaceId: number | null,
|
|
{ hasPeriodicSyncEnabled = false }: UseDocumentsProcessingOptions = {}
|
|
): DocumentsProcessingStatus {
|
|
const [status, setStatus] = useState<DocumentsProcessingStatus>("idle");
|
|
const wasProcessingRef = useRef(false);
|
|
const successTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
|
|
|
|
const [documents] = useQuery(queries.documents.bySpace({ workspaceId: workspaceId ?? -1 }));
|
|
|
|
useEffect(() => {
|
|
if (!workspaceId || !documents) return;
|
|
|
|
const clearSuccessTimer = () => {
|
|
if (successTimerRef.current) {
|
|
clearTimeout(successTimerRef.current);
|
|
successTimerRef.current = null;
|
|
}
|
|
};
|
|
|
|
let pendingCount = 0;
|
|
let processingCount = 0;
|
|
let failedCount = 0;
|
|
let readyCount = 0;
|
|
|
|
for (const doc of documents) {
|
|
// Keep the nav indicator aligned with what the Documents sidebar actually renders.
|
|
// Some connectors can create temporary untitled placeholder rows that remain hidden
|
|
// from the sidebar, and those should not keep the whole section looking "stuck".
|
|
if (!doc.title || doc.title.trim() === "") {
|
|
continue;
|
|
}
|
|
|
|
const state = (doc.status as { state?: string } | null)?.state;
|
|
if (state === "pending") {
|
|
pendingCount++;
|
|
} else if (state === "processing") {
|
|
processingCount++;
|
|
} else if (state === "failed") {
|
|
failedCount++;
|
|
} else {
|
|
readyCount++;
|
|
}
|
|
}
|
|
|
|
if (pendingCount > 0) {
|
|
wasProcessingRef.current = true;
|
|
clearSuccessTimer();
|
|
setStatus("processing");
|
|
} else if (processingCount > 0) {
|
|
wasProcessingRef.current = true;
|
|
clearSuccessTimer();
|
|
|
|
const isBackgroundSync = hasPeriodicSyncEnabled && readyCount > 0;
|
|
setStatus(isBackgroundSync ? "background_sync" : "processing");
|
|
} else if (failedCount > 0) {
|
|
wasProcessingRef.current = false;
|
|
clearSuccessTimer();
|
|
setStatus("error");
|
|
} else if (wasProcessingRef.current) {
|
|
wasProcessingRef.current = false;
|
|
setStatus("success");
|
|
clearSuccessTimer();
|
|
successTimerRef.current = setTimeout(() => {
|
|
setStatus("idle");
|
|
successTimerRef.current = null;
|
|
}, SUCCESS_LINGER_MS);
|
|
} else {
|
|
setStatus("idle");
|
|
}
|
|
}, [workspaceId, documents, hasPeriodicSyncEnabled]);
|
|
|
|
useEffect(() => {
|
|
return () => {
|
|
if (successTimerRef.current) {
|
|
clearTimeout(successTimerRef.current);
|
|
successTimerRef.current = null;
|
|
}
|
|
};
|
|
}, []);
|
|
|
|
return status;
|
|
}
|