Files
Stirling-PDF/frontend/src/desktop/hooks/useWillUseCloud.ts
T
ConnorYohandGitHub 8bc37bf5ae Desktop: Fallback to local backend if self-hosted server is offline (#5880)
* Adds a fallback mechanism so the desktop app routes tool operations to
the local bundled backend when the user's self-hosted Stirling-PDF
server goes offline, and disables tools in the UI that aren't supported
locally.

* `selfHostedServerMonitor.ts` independently polls the self-hosted
server every 15s and exposes which tool endpoints are unavailable when
it goes offline
* `operationRouter.ts` intercepts operations destined for the
self-hosted server and reroutes them to the local bundled backend when
the monitor reports it offline
* `useSelfHostedToolAvailability.ts` feeds the offline tool set into
useToolManagement, disabling affected tools in the UI with a
selfHostedOffline reason and banner warning

- `SelfHostedOfflineBanner `is a dismissable (session-only) gray bar
shown at the top of the UI when in self-hosted mode and the server goes
offline. It shows:
2026-03-10 10:04:56 +00:00

52 lines
1.6 KiB
TypeScript

import { useState, useEffect } from 'react';
import { operationRouter } from '@app/services/operationRouter';
import { tauriBackendService } from '@app/services/tauriBackendService';
/**
* Desktop hook to detect if an operation will use cloud/SaaS backend
* @param endpoint - The API endpoint to check (e.g., '/api/v1/misc/compress-pdf')
* @returns true if the operation will use cloud credits, false otherwise
*/
export function useWillUseCloud(endpoint?: string): boolean {
const [willUseCloud, setWillUseCloud] = useState(false);
useEffect(() => {
const checkCloudRouting = async () => {
if (!endpoint) {
setWillUseCloud(false);
return;
}
// Don't show cloud badges until backend is healthy
// This prevents showing incorrect cloud status during startup
if (!tauriBackendService.isOnline) {
setWillUseCloud(false);
return;
}
// Check if this endpoint will route to SaaS
try {
const willRoute = await operationRouter.willRouteToSaaS(endpoint);
setWillUseCloud(willRoute);
} catch (error) {
console.error('[useWillUseCloud] Failed to check cloud routing for endpoint:', endpoint, error);
setWillUseCloud(false);
}
};
// Initial check
checkCloudRouting();
// Subscribe to backend status changes to re-check when backend becomes healthy
const unsubscribe = tauriBackendService.subscribeToStatus((status) => {
if (status === 'healthy') {
checkCloudRouting();
}
});
return unsubscribe;
}, [endpoint]);
return willUseCloud;
}