tauri notifications (#5875)

This commit is contained in:
Anthony Stirling
2026-03-05 18:30:20 +00:00
committed by GitHub
parent 0f7ee5c5b0
commit 879ffc066f
10 changed files with 168 additions and 1 deletions

View File

@@ -17,6 +17,7 @@ import { ToolId } from '@app/types/toolId';
import { ensureBackendReady } from '@app/services/backendReadinessGuard';
import { useWillUseCloud } from '@app/hooks/useWillUseCloud';
import { useCreditCheck } from '@app/hooks/useCreditCheck';
import { notifyPdfProcessingComplete } from '@app/services/desktopNotificationService';
// Re-export for backwards compatibility
export type { ProcessingProgress, ResponseHandler };
@@ -476,6 +477,9 @@ export const useToolOperation = <TParams>(
console.debug('[useToolOperation] Consuming files', { inputCount: inputFileIds.length, toConsume: toConsumeInputIds.length });
const outputFileIds = await consumeFiles(toConsumeInputIds, outputStirlingFiles, outputStirlingFileStubs);
// Notify on desktop when processing completes
await notifyPdfProcessingComplete(outputFileIds.length);
if (toConsumeInputIds.length === 1 && outputFileIds.length === 1) {
const inputStub = selectors.getStirlingFileStub(toConsumeInputIds[0]);
if (inputStub?.localFilePath) {

View File

@@ -0,0 +1,4 @@
// Stub - overridden in desktop builds
export async function notifyPdfProcessingComplete(_fileCount: number): Promise<void> {
// Web builds: no-op
}

View File

@@ -0,0 +1,74 @@
import { isTauri } from '@tauri-apps/api/core';
import { getCurrentWindow } from '@tauri-apps/api/window';
import {
isPermissionGranted,
requestPermission,
sendNotification,
} from '@tauri-apps/plugin-notification';
import i18n from '@app/i18n';
const APP_TITLE = 'Stirling-PDF';
async function shouldShowBackgroundNotification(): Promise<boolean> {
if (!isTauri()) {
return false;
}
try {
const window = getCurrentWindow();
const [isMinimized, isFocused] = await Promise.all([
window.isMinimized().catch(() => false),
window.isFocused().catch(() => true),
]);
return isMinimized || !isFocused || document.visibilityState !== 'visible';
} catch {
return false;
}
}
export async function notifyPdfProcessingComplete(fileCount: number): Promise<void> {
console.log('[DesktopNotification] notifyPdfProcessingComplete called with fileCount:', fileCount);
if (!isTauri() || fileCount <= 0) {
console.log('[DesktopNotification] Skipped: !isTauri() or fileCount <= 0');
return;
}
const canNotify = await shouldShowBackgroundNotification();
console.log('[DesktopNotification] canNotify (background):', canNotify);
if (!canNotify) {
console.log('[DesktopNotification] App is in focus, skipping notification');
return;
}
try {
// Check and request permission if needed
let permissionGranted = await isPermissionGranted();
console.log('[DesktopNotification] Permission check:', permissionGranted);
if (!permissionGranted) {
console.log('[DesktopNotification] Requesting permission...');
const permission = await requestPermission();
permissionGranted = permission === 'granted';
console.log('[DesktopNotification] Permission result:', permission);
}
if (!permissionGranted) {
console.log('[DesktopNotification] Permission not granted, skipping notification');
return;
}
const body = fileCount === 1
? i18n.t('processingComplete', 'Your file is ready.')
: i18n.t('processingCompleteMultiple', '{{count}} files are ready.', { count: fileCount });
console.log('[DesktopNotification] Sending notification:', body);
await sendNotification({
title: APP_TITLE,
body,
});
console.log('[DesktopNotification] Notification sent successfully');
} catch (error) {
console.warn('[DesktopNotification] Unable to send notification', error);
}
}