File size: 4,249 Bytes
4e1096a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 | import { useEffect, useRef } from 'react';
import { useRouter } from 'next/navigation';
import { useEnv } from '@/context/EnvContext';
import { useLibraryStore } from '@/store/libraryStore';
import { useSettingsStore } from '@/store/settingsStore';
import { addPluginListener, PluginListener } from '@tauri-apps/api/core';
import { onOpenUrl } from '@tauri-apps/plugin-deep-link';
import { getCurrentWindow, getAllWindows } from '@tauri-apps/api/window';
import { isTauriAppPlatform } from '@/services/environment';
import { navigateToLibrary, showLibraryWindow } from '@/utils/nav';
interface SingleInstancePayload {
args: string[];
cwd: string;
}
interface OpenFilesPayload {
files: string[];
}
interface SharedIntentPayload {
urls: string[];
}
export function useOpenWithBooks() {
const router = useRouter();
const { appService } = useEnv();
const { setCheckOpenWithBooks } = useLibraryStore();
const listenedOpenWithBooks = useRef(false);
const isFirstWindow = async () => {
const allWindows = await getAllWindows();
const currentWindow = getCurrentWindow();
const sortedWindows = allWindows.sort((a, b) => a.label.localeCompare(b.label));
return sortedWindows[0]?.label === currentWindow.label;
};
const handleOpenWithFileUrl = async (urls: string[]) => {
console.log('Handle Open with URL:', urls);
const filePaths = [];
for (let url of urls) {
if (url.startsWith('file://')) {
if (appService?.isIOSApp) {
url = decodeURI(url);
} else {
url = decodeURI(url.replace('file://', ''));
}
}
if (!/^(https?:|data:|blob:)/i.test(url)) {
filePaths.push(url);
}
}
if (filePaths.length > 0) {
const settings = useSettingsStore.getState().settings;
if (appService?.hasWindow && settings.openBookInNewWindow) {
if (await isFirstWindow()) {
showLibraryWindow(appService, filePaths);
}
} else {
window.OPEN_WITH_FILES = filePaths;
setCheckOpenWithBooks(true);
navigateToLibrary(router, `reload=${Date.now()}`);
}
}
};
const initializeListeners = async () => {
return await addPluginListener<SharedIntentPayload>(
'native-bridge',
'shared-intent',
(payload) => {
console.log('Received shared intent:', payload);
const { urls } = payload;
handleOpenWithFileUrl(urls);
},
);
};
useEffect(() => {
if (!isTauriAppPlatform() || !appService) return;
if (listenedOpenWithBooks.current) return;
listenedOpenWithBooks.current = true;
// For Windows/Linux deep link and macOS open-file event
const unlistenDeeplink = getCurrentWindow().listen<SingleInstancePayload>(
'single-instance',
({ payload }) => {
console.log('Received deep link:', payload);
const { args } = payload;
if (args?.[1]) {
handleOpenWithFileUrl([args[1]]);
}
},
);
// macOS in-app open-files event
const unlistenOpenFiles = getCurrentWindow().listen<OpenFilesPayload>(
'open-files',
({ payload }) => {
console.log('Received open files:', payload);
const { files } = payload;
if (files && files.length > 0) {
handleOpenWithFileUrl(files);
}
},
);
// For Android "Share to Readest" intent
let unlistenSharedIntent: Promise<PluginListener> | null = null;
// FIXME: register/unregister plugin listeniner on iOS might cause app freeze for unknown reason
// so we only register it on Android for now to support "Shared to Readest" feature
if (appService?.isAndroidApp) {
unlistenSharedIntent = initializeListeners();
}
// iOS Open with URL event
const listenOpenWithFiles = async () => {
return await onOpenUrl((urls) => {
handleOpenWithFileUrl(urls);
});
};
const unlistenOpenUrl = listenOpenWithFiles();
return () => {
unlistenDeeplink.then((f) => f());
unlistenOpenFiles.then((f) => f());
unlistenOpenUrl.then((f) => f());
unlistenSharedIntent?.then((f) => f.unregister());
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [appService]);
}
|