2024-03-19 20:54:30 -04:00
|
|
|
import { useState } from 'react';
|
|
|
|
|
import { useToastContext } from '~/Providers/ToastContext';
|
|
|
|
|
import useLocalize from '~/hooks/useLocalize';
|
|
|
|
|
|
|
|
|
|
export const useDelayedUploadToast = () => {
|
|
|
|
|
const localize = useLocalize();
|
|
|
|
|
const { showToast } = useToastContext();
|
2024-05-05 21:35:51 +02:00
|
|
|
const [uploadTimers, setUploadTimers] = useState<Record<string, NodeJS.Timeout>>({});
|
|
|
|
|
|
|
|
|
|
const determineDelay = (fileSize: number): number => {
|
|
|
|
|
const baseDelay = 5000;
|
|
|
|
|
const additionalDelay = Math.floor(fileSize / 1000000) * 2000;
|
|
|
|
|
return baseDelay + additionalDelay;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const startUploadTimer = (fileId: string, fileName: string, fileSize: number) => {
|
|
|
|
|
const delay = determineDelay(fileSize);
|
|
|
|
|
|
|
|
|
|
if (uploadTimers[fileId]) {
|
|
|
|
|
clearTimeout(uploadTimers[fileId]);
|
|
|
|
|
}
|
2024-03-19 20:54:30 -04:00
|
|
|
|
|
|
|
|
const timer = setTimeout(() => {
|
|
|
|
|
const message = localize('com_ui_upload_delay', fileName);
|
|
|
|
|
showToast({
|
|
|
|
|
message,
|
|
|
|
|
status: 'warning',
|
|
|
|
|
duration: 7000,
|
|
|
|
|
});
|
2024-05-05 21:35:51 +02:00
|
|
|
}, delay);
|
2024-03-19 20:54:30 -04:00
|
|
|
|
|
|
|
|
setUploadTimers((prev) => ({ ...prev, [fileId]: timer }));
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const clearUploadTimer = (fileId: string) => {
|
|
|
|
|
if (uploadTimers[fileId]) {
|
|
|
|
|
clearTimeout(uploadTimers[fileId]);
|
|
|
|
|
setUploadTimers((prev) => {
|
2024-05-05 21:35:51 +02:00
|
|
|
const { [fileId]: _, ...rest } = prev;
|
2024-03-19 20:54:30 -04:00
|
|
|
return rest;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return { startUploadTimer, clearUploadTimer };
|
|
|
|
|
};
|