Skip to content

#1 PDF Application on GitHub that lets you edit PDFs on any device anywhere

1.3 KB
Raw Blame
import { useEffect } from "react";
import { useBackendHealth } from "@editor/hooks/useBackendHealth";
import { useEndpointConfig } from "@editor/hooks/useEndpointConfig";
import { tauriBackendService } from "@editor/services/tauriBackendService";

/**
 * Hook to initialize backend and monitor health
 * @param enabled - Whether to initialize the backend (default: true)
 */
export function useBackendInitializer(enabled = true) {
  const { status, checkHealth } = useBackendHealth();
  const { backendUrl } = useEndpointConfig();

  useEffect(() => {
    // Skip if disabled
    if (!enabled) {
      return;
    }

    // Skip if backend already running
    if (tauriBackendService.isBackendRunning()) {
      void checkHealth();
      return;
    }

    const initializeBackend = async () => {
      try {
        await tauriBackendService.startBackend(backendUrl);

        // Begin health checks after a short delay
        setTimeout(() => {
          void checkHealth();
        }, 500);
      } catch (error) {
        console.error("[BackendInitializer] Failed to start backend:", error);
      }
    };

    // Only start backend if it's not already starting/healthy
    if (status !== "healthy" && status !== "starting") {
      void initializeBackend();
    }
  }, [enabled, status, backendUrl, checkHealth]);
}