Catch clipboard errors

This commit is contained in:
Gregory Schier
2024-08-10 07:33:10 -07:00
parent 352ffe9415
commit 7e73b680e6
4 changed files with 18 additions and 8 deletions

View File

@@ -11,6 +11,7 @@
"core:event:allow-listen", "core:event:allow-listen",
"core:event:allow-unlisten", "core:event:allow-unlisten",
"os:allow-os-type", "os:allow-os-type",
"clipboard-manager:allow-clear",
"clipboard-manager:allow-write-text", "clipboard-manager:allow-write-text",
"clipboard-manager:allow-read-text", "clipboard-manager:allow-read-text",
"dialog:allow-open", "dialog:allow-open",

View File

@@ -1 +1 @@
{"main":{"identifier":"main","description":"Main permissions","local":true,"windows":["*"],"permissions":["core:event:allow-emit","core:event:allow-listen","core:event:allow-unlisten","os:allow-os-type","clipboard-manager:allow-write-text","clipboard-manager:allow-read-text","dialog:allow-open","dialog:allow-save","fs:allow-read-file","fs:allow-read-text-file",{"identifier":"fs:scope","allow":[{"path":"$APPDATA"},{"path":"$APPDATA/**"}]},"shell:allow-open","core:webview:allow-set-webview-zoom","core:window:allow-close","core:window:allow-is-fullscreen","core:window:allow-maximize","core:window:allow-minimize","core:window:allow-toggle-maximize","core:window:allow-set-decorations","core:window:allow-set-title","core:window:allow-start-dragging","core:window:allow-unmaximize","core:window:allow-theme","clipboard-manager:allow-read-text","clipboard-manager:allow-write-text"]}} {"main":{"identifier":"main","description":"Main permissions","local":true,"windows":["*"],"permissions":["core:event:allow-emit","core:event:allow-listen","core:event:allow-unlisten","os:allow-os-type","clipboard-manager:allow-write-text","clipboard-manager:allow-clear","clipboard-manager:allow-read-text","dialog:allow-open","dialog:allow-save","fs:allow-read-file","fs:allow-read-text-file",{"identifier":"fs:scope","allow":[{"path":"$APPDATA"},{"path":"$APPDATA/**"}]},"shell:allow-open","core:webview:allow-set-webview-zoom","core:window:allow-close","core:window:allow-is-fullscreen","core:window:allow-maximize","core:window:allow-minimize","core:window:allow-toggle-maximize","core:window:allow-set-decorations","core:window:allow-set-title","core:window:allow-start-dragging","core:window:allow-unmaximize","core:window:allow-theme","clipboard-manager:allow-read-text","clipboard-manager:allow-write-text"]}}

View File

@@ -1,8 +1,8 @@
import { readText, writeText } from '@tauri-apps/plugin-clipboard-manager'; import { clear, readText, writeText } from '@tauri-apps/plugin-clipboard-manager';
import { useCallback, useEffect } from 'react'; import { useCallback, useEffect } from 'react';
import { createGlobalState } from 'react-use';
import { useToast } from '../components/ToastContext'; import { useToast } from '../components/ToastContext';
import { useWindowFocus } from './useWindowFocus'; import { useWindowFocus } from './useWindowFocus';
import { createGlobalState } from 'react-use';
const useClipboardTextState = createGlobalState<string>(''); const useClipboardTextState = createGlobalState<string>('');
@@ -12,12 +12,21 @@ export function useClipboardText({ disableToast }: { disableToast?: boolean } =
const toast = useToast(); const toast = useToast();
useEffect(() => { useEffect(() => {
readText().then(setValue); readText()
.then(setValue)
.catch(() => {
// Clipboard probably empty
setValue('');
});
}, [focused, setValue]); }, [focused, setValue]);
const setText = useCallback( const setText = useCallback(
(text: string) => { (text: string | null) => {
writeText(text).catch(console.error); if (text == null) {
clear().catch(console.error);
} else {
writeText(text).catch(console.error);
}
if (text != '' && !disableToast) { if (text != '' && !disableToast) {
toast.show({ toast.show({
id: 'copied', id: 'copied',
@@ -25,7 +34,7 @@ export function useClipboardText({ disableToast }: { disableToast?: boolean } =
message: 'Copied to clipboard', message: 'Copied to clipboard',
}); });
} }
setValue(text); setValue(text || '');
}, },
[disableToast, setValue, toast], [disableToast, setValue, toast],
); );

View File

@@ -40,7 +40,7 @@ export function useImportCurl({ clearClipboard }: { clearClipboard?: boolean } =
}); });
if (clearClipboard) { if (clearClipboard) {
setClipboardText(''); setClipboardText(null);
} }
}, },
}); });