mirror of
https://github.com/KuekHaoYang/KVideo.git
synced 2026-08-12 23:33:43 +08:00
45 lines
1.3 KiB
TypeScript
45 lines
1.3 KiB
TypeScript
import { useCallback } from 'react';
|
|
|
|
interface UseUtilitiesProps {
|
|
src: string;
|
|
setToastMessage: (message: string | null) => void;
|
|
setShowToast: (show: boolean) => void;
|
|
toastTimeoutRef: React.MutableRefObject<NodeJS.Timeout | null>;
|
|
}
|
|
|
|
export function useUtilities({
|
|
src,
|
|
setToastMessage,
|
|
setShowToast,
|
|
toastTimeoutRef
|
|
}: UseUtilitiesProps) {
|
|
const showToastNotification = useCallback((message: string) => {
|
|
setToastMessage(message);
|
|
setShowToast(true);
|
|
|
|
if (toastTimeoutRef.current) {
|
|
clearTimeout(toastTimeoutRef.current);
|
|
}
|
|
|
|
toastTimeoutRef.current = setTimeout(() => {
|
|
setShowToast(false);
|
|
setTimeout(() => setToastMessage(null), 300);
|
|
}, 3000);
|
|
}, [setToastMessage, setShowToast, toastTimeoutRef]);
|
|
|
|
const handleCopyLink = useCallback(async (url?: string) => {
|
|
try {
|
|
await navigator.clipboard.writeText(url || src);
|
|
showToastNotification('链接已复制到剪贴板');
|
|
} catch (error) {
|
|
console.error('Copy failed:', error);
|
|
showToastNotification('复制失败,请重试');
|
|
}
|
|
}, [src, showToastNotification]);
|
|
|
|
return {
|
|
showToastNotification,
|
|
handleCopyLink
|
|
};
|
|
}
|