Skip to content

Usenet streaming with a WebDAV server and a SABnzbd-compatible API

C#

Refactored /queue route.

nzbdav-devfad9f825b394
diff --git a/frontend/app/routes/queue/components/queue-table/queue-table.tsx b/frontend/app/routes/queue/components/queue-table/queue-table.tsx
index 02e22451..99718755 100644
--- a/frontend/app/routes/queue/components/queue-table/queue-table.tsx
+++ b/frontend/app/routes/queue/components/queue-table/queue-table.tsx
@@ -1,5 +1,5 @@
import { ActionButton } from "../action-button/action-button"
-import { memo, useCallback, useState } from "react"
+import { memo, useCallback, useMemo, useState } from "react"
import { ConfirmModal } from "../confirm-modal/confirm-modal"
import type { PresentationQueueSlot } from "../../route"
import type { TriCheckboxState } from "../tri-checkbox/tri-checkbox"
@@ -92,7 +92,7 @@ export function QueueTable({
}, [queueSlots, setIsConfirmingRemoval, onIsRemovingChanged, onRemoved]);
// view
- const categoryDropdown = (
+ const categoryDropdown = useMemo(() => (
<div title="Choose the category for manual nzb uploads.">
<SimpleDropdown
options={categories}
@@ -100,7 +100,7 @@ export function QueueTable({
onChange={onManualCategoryChanged}
/>
</div>
- );
+ ), [categories, manualCategory, onManualCategoryChanged]);
const sectionTitle = (
<div className={styles.sectionTitle}>
diff --git a/frontend/app/routes/queue/components/thin-viewport/thin-viewport.tsx b/frontend/app/routes/queue/components/thin-viewport/thin-viewport.tsx
index 63353c54..837cc295 100644
--- a/frontend/app/routes/queue/components/thin-viewport/thin-viewport.tsx
+++ b/frontend/app/routes/queue/components/thin-viewport/thin-viewport.tsx
@@ -1,5 +1,5 @@
import type { ReactNode } from "react"
-import { useId } from "react"
+import { memo, useId } from "react"
import styles from "./thin-viewport.module.css"
export type ThinViewportProps = {
@@ -7,7 +7,7 @@ export type ThinViewportProps = {
children: ReactNode,
}
-export function ThinViewport({ width, children }: ThinViewportProps) {
+export const ThinViewport = memo(({ width, children }: ThinViewportProps) => {
const id = useId();
const uniqueId = `thin-viewport-${id.replace(/:/g, '-')}`;
@@ -27,4 +27,4 @@ export function ThinViewport({ width, children }: ThinViewportProps) {
</div>
</>
);
-}
+});
diff --git a/frontend/app/routes/queue/controllers/dropzone-controller.ts b/frontend/app/routes/queue/controllers/dropzone-controller.ts
new file mode 100644
index 00000000..bc963543
--- /dev/null
+++ b/frontend/app/routes/queue/controllers/dropzone-controller.ts
@@ -0,0 +1,38 @@
+import { useCallback } from "react";
+import { useDropzone } from "react-dropzone";
+import type { UploadingFile } from "../route";
+
+export function useQueueDropzone(
+ setUploadingFiles: (value: React.SetStateAction<UploadingFile[]>) => void,
+ uploadQueueRef: React.RefObject<UploadingFile[]>,
+ manualCategoryRef: React.RefObject<string>,
+) {
+
+ const onDrop = useCallback((acceptedFiles: File[]) => {
+ const newFiles: UploadingFile[] = acceptedFiles.map(file => ({
+ file,
+ queueSlot: {
+ isUploading: true,
+ nzo_id: `upload-${Date.now()}-${Math.random()}`,
+ priority: 'Normal',
+ filename: file.name,
+ cat: manualCategoryRef.current,
+ percentage: "0",
+ true_percentage: "0",
+ status: "pending",
+ mb: (file.size / (1024 * 1024)).toFixed(2),
+ mbleft: (file.size / (1024 * 1024)).toFixed(2),
+ }
+ }));
+
+ setUploadingFiles(files => [...files, ...newFiles]);
+ uploadQueueRef.current = [...uploadQueueRef.current, ...newFiles];
+ }, []);
+
+ return useDropzone({
+ accept: { 'application/x-nzb': ['.nzb'] },
+ onDrop,
+ noClick: true,
+ noKeyboard: true,
+ });
+}
\ No newline at end of file
diff --git a/frontend/app/routes/queue/controllers/events-controller.ts b/frontend/app/routes/queue/controllers/events-controller.ts
new file mode 100644
index 00000000..1b1bd4cb
--- /dev/null
+++ b/frontend/app/routes/queue/controllers/events-controller.ts
@@ -0,0 +1,97 @@
+import { useCallback, useMemo } from "react";
+import type { HistorySlot, QueueSlot } from "~/clients/backend-client.server";
+import type { PresentationHistorySlot, PresentationQueueSlot, UploadingFile } from "../route";
+
+export type QueueEvents = {
+ onAddQueueSlot: (queueSlot: QueueSlot) => void,
+ onSelectQueueSlots: (ids: Set<string>, isSelected: boolean) => void,
+ onRemovingQueueSlots: (ids: Set<string>, isRemoving: boolean) => void,
+ onRemoveQueueSlots: (ids: Set<string>) => void,
+ onChangeQueueSlotStatus: (message: string) => void,
+ onChangeQueueSlotPercentage: (message: string) => void
+};
+
+export type HistoryEvents = {
+ onAddHistorySlot: (historySlot: HistorySlot) => void,
+ onSelectHistorySlots: (ids: Set<string>, isSelected: boolean) => void,
+ onRemovingHistorySlots: (ids: Set<string>, isRemoving: boolean) => void,
+ onRemoveHistorySlots: (ids: Set<string>) => void
+};
+
+export function useQueueEvents(
+ setUploadingFiles: (value: React.SetStateAction<UploadingFile[]>) => void,
+ setQueueSlots: (value: React.SetStateAction<PresentationQueueSlot[]>) => void,
+ uploadQueueRef: React.RefObject<UploadingFile[]>
+) {
+ const onAddQueueSlot = useCallback((queueSlot: QueueSlot) => {
+ uploadQueueRef.current = uploadQueueRef.current.filter(x => x.queueSlot.status === "uploading" || x.queueSlot.filename !== queueSlot.filename);
+ setUploadingFiles(files => files.filter(f => f.queueSlot.filename !== queueSlot.filename));
+ setQueueSlots(slots => [...slots, queueSlot]);
+ }, [setQueueSlots]);
+
+ const onSelectQueueSlots = useCallback((ids: Set<string>, isSelected: boolean) => {
+ setUploadingFiles(files => files.map(x => ids.has(x.queueSlot.nzo_id) ? { ...x, queueSlot: { ...x.queueSlot, isSelected } } : x));
+ setQueueSlots(slots => slots.map(x => ids.has(x.nzo_id) ? { ...x, isSelected } : x));
+ }, [setQueueSlots]);
+
+ const onRemovingQueueSlots = useCallback((ids: Set<string>, isRemoving: boolean) => {
+ setQueueSlots(slots => slots.map(x => ids.has(x.nzo_id) ? { ...x, isRemoving } : x));
+ }, [setQueueSlots]);
+
+ const onRemoveQueueSlots = useCallback((ids: Set<string>) => {
+ uploadQueueRef.current = uploadQueueRef.current.filter(x => x.queueSlot.status === "uploading" || !ids.has(x.queueSlot.nzo_id));
+ setUploadingFiles(files => files.filter(x => x.queueSlot.status === "uploading" || !ids.has(x.queueSlot.nzo_id)));
+ setQueueSlots(slots => slots.filter(x => !ids.has(x.nzo_id)));
+ }, [setQueueSlots]);
+
+ const onChangeQueueSlotStatus = useCallback((message: string) => {
+ const [nzo_id, status] = message.split('|');
+ setQueueSlots(slots => slots.map(x => x.nzo_id === nzo_id ? { ...x, status } : x));
+ }, [setQueueSlots]);
+
+ const onChangeQueueSlotPercentage = useCallback((message: string) => {
+ const [nzo_id, true_percentage] = message.split('|');
+ setQueueSlots(slots => slots.map(x => x.nzo_id === nzo_id ? { ...x, true_percentage } : x));
+ }, [setQueueSlots]);
+
+ return memoize({
+ onAddQueueSlot,
+ onSelectQueueSlots,
+ onRemovingQueueSlots,
+ onRemoveQueueSlots,
+ onChangeQueueSlotStatus,
+ onChangeQueueSlotPercentage
+ });
+}
+
+export function useHistoryEvents(
+ setHistorySlots: (value: React.SetStateAction<PresentationHistorySlot[]>) => void
+) {
+ const onAddHistorySlot = useCallback((historySlot: HistorySlot) => {
+ setHistorySlots(slots => [historySlot, ...slots]);
+ }, [setHistorySlots]);
+
+ const onSelectHistorySlots = useCallback((ids: Set<string>, isSelected: boolean) => {
+ setHistorySlots(slots => slots.map(x => ids.has(x.nzo_id) ? { ...x, isSelected } : x));
+ }, [setHistorySlots]);
+
+ const onRemovingHistorySlots = useCallback((ids: Set<string>, isRemoving: boolean) => {
+ setHistorySlots(slots => slots.map(x => ids.has(x.nzo_id) ? { ...x, isRemoving } : x));
+ }, [setHistorySlots]);
+
+ const onRemoveHistorySlots = useCallback((ids: Set<string>) => {
+ setHistorySlots(slots => slots.filter(x => !ids.has(x.nzo_id)));
+ }, [setHistorySlots]);
+
+ return memoize({
+ onAddHistorySlot,
+ onSelectHistorySlots,
+ onRemovingHistorySlots,
+ onRemoveHistorySlots
+ });
+}
+
+function memoize<T extends Record<string, unknown>>(object: T): T {
+ // eslint-disable-next-line react-hooks/exhaustive-deps
+ return useMemo(() => object, Object.values(object));
+}
\ No newline at end of file
diff --git a/frontend/app/routes/queue/controllers/nzb-upload-controller.ts b/frontend/app/routes/queue/controllers/nzb-upload-controller.ts
new file mode 100644
index 00000000..3d094db6
--- /dev/null
+++ b/frontend/app/routes/queue/controllers/nzb-upload-controller.ts
@@ -0,0 +1,96 @@
+import { useEffect } from "react";
+import type { UploadingFile } from "../route";
+
+export function initializeUploadController(
+ isUploadingRef: React.RefObject<boolean>,
+ uploadQueueRef: React.RefObject<UploadingFile[]>,
+ manualCategoryRef: React.RefObject<string>,
+ uploadingFiles: UploadingFile[],
+ setUploadingFiles: (value: React.SetStateAction<UploadingFile[]>) => void,
+) {
+ useEffect(() => {
+ processUploadQueue(isUploadingRef, uploadQueueRef, manualCategoryRef, setUploadingFiles);
+ }, [uploadingFiles]);
+}
+
+async function processUploadQueue(
+ isUploadingRef: React.RefObject<boolean>,
+ uploadQueueRef: React.RefObject<UploadingFile[]>,
+ manualCategoryRef: React.RefObject<string>,
+ setUploadingFiles: (value: React.SetStateAction<UploadingFile[]>) => void
+) {
+ if (isUploadingRef.current || uploadQueueRef.current.length === 0) return;
+
+ isUploadingRef.current = true;
+ const fileToUpload = uploadQueueRef.current[0];
+
+ setUploadingFiles(files => files.map(f =>
+ f.queueSlot.nzo_id === fileToUpload.queueSlot.nzo_id
+ ? { ...f, queueSlot: { ...f.queueSlot, status: 'uploading' } }
+ : f
+ ));
+
+ try {
+ const xhr = new XMLHttpRequest();
+ const formData = new FormData();
+ formData.append('nzbFile', fileToUpload.file, fileToUpload.file.name);
+
+ xhr.responseType = 'json';
+ xhr.upload.addEventListener('progress', (e) => {
+ if (e.lengthComputable) {
+ const progress = Math.round((e.loaded / e.total) * 100);
+ setUploadingFiles(files => files.map(f =>
+ f.queueSlot.nzo_id === fileToUpload.queueSlot.nzo_id
+ ? {
+ ...f,
+ queueSlot: {
+ ...f.queueSlot,
+ percentage: progress.toString(),
+ true_percentage: progress.toString()
+ }
+ }
+ : f
+ ));
+ }
+ });
+
+ var response: any = await new Promise<void>((resolve, reject) => {
+ xhr.addEventListener('load', () => {
+ if (xhr.status >= 200 && xhr.status < 300) {
+ resolve(xhr.response);
+ } else {
+ const errorMessage = xhr.response.error || `Upload failed with status ${xhr.status}`;
+ reject(new Error(errorMessage));
+ }
+ });
+ xhr.addEventListener('error', () => reject(new Error('Upload failed')));
+ xhr.addEventListener('abort', () => reject(new Error('Upload aborted')));
+
+ xhr.open('POST', `/api?mode=addfile&cat=${manualCategoryRef.current}&priority=0&pp=0`);
+ xhr.send(formData);
+ });
+
+ if (response.status == false) {
+ throw new Error(response.error);
+ }
+
+ } catch (error) {
+ setUploadingFiles(files => files.map(f =>
+ f.queueSlot.nzo_id === fileToUpload.queueSlot.nzo_id ? {
+ ...f,
+ queueSlot: {
+ ...f.queueSlot,
+ status: 'upload failed',
+ error: error instanceof Error ? error.message : 'Upload failed'
+ }
+ } : f
+ ));
+ }
+
+ uploadQueueRef.current = uploadQueueRef.current.filter(x => x !== fileToUpload);
+ isUploadingRef.current = false;
+
+ if (uploadQueueRef.current.length > 0) {
+ processUploadQueue(isUploadingRef, uploadQueueRef, manualCategoryRef, setUploadingFiles);
+ }
+}
\ No newline at end of file
diff --git a/frontend/app/routes/queue/controllers/websocket-controller.ts b/frontend/app/routes/queue/controllers/websocket-controller.ts
new file mode 100644
index 00000000..6b21e230
--- /dev/null
+++ b/frontend/app/routes/queue/controllers/websocket-controller.ts
@@ -0,0 +1,63 @@
+import { useCallback, useEffect } from "react";
+import type { HistoryEvents, QueueEvents } from "./events-controller";
+import { receiveMessage } from "~/utils/websocket-util";
+
+const topicNames = {
+ queueItemStatus: 'qs',
+ queueItemPercentage: 'qp',
+ queueItemAdded: 'qa',
+ queueItemRemoved: 'qr',
+ historyItemAdded: 'ha',
+ historyItemRemoved: 'hr',
+};
+
+const topicSubscriptions = {
+ [topicNames.queueItemStatus]: 'state',
+ [topicNames.queueItemPercentage]: 'state',
+ [topicNames.queueItemAdded]: 'event',
+ [topicNames.queueItemRemoved]: 'event',
+ [topicNames.historyItemAdded]: 'event',
+ [topicNames.historyItemRemoved]: 'event',
+};
+
+export function initializeQueueHistoryWebsocket(
+ queueEvents: QueueEvents,
+ historyEvents: HistoryEvents,
+ disableLiveView: boolean,
+) {
+ const onWebsocketMessage = useCallback((topic: string, message: string) => {
+ if (disableLiveView) return;
+ if (topic == topicNames.queueItemAdded)
+ queueEvents.onAddQueueSlot(JSON.parse(message));
+ else if (topic == topicNames.queueItemRemoved)
+ queueEvents.onRemoveQueueSlots(new Set<string>(message.split(',')));
+ else if (topic == topicNames.queueItemStatus)
+ queueEvents.onChangeQueueSlotStatus(message);
+ else if (topic == topicNames.queueItemPercentage)
+ queueEvents.onChangeQueueSlotPercentage(message);
+ else if (topic == topicNames.historyItemAdded)
+ historyEvents.onAddHistorySlot(JSON.parse(message));
+ else if (topic == topicNames.historyItemRemoved)
+ historyEvents.onRemoveHistorySlots(new Set<string>(message.split(',')));
+ }, [
+ queueEvents,
+ historyEvents,
+ disableLiveView
+ ]);
+
+ useEffect(() => {
+ if (disableLiveView) return;
+ let ws: WebSocket;
+ let disposed = false;
+ function connect() {
+ ws = new WebSocket(window.location.origin.replace(/^http/, 'ws'));
+ ws.onmessage = receiveMessage(onWebsocketMessage);
+ ws.onopen = () => { ws.send(JSON.stringify(topicSubscriptions)); }
+ ws.onclose = () => { !disposed && setTimeout(() => connect(), 1000); };
+ ws.onerror = () => { ws.close() };
+ return () => { disposed = true; ws.close(); }
+ }
+
+ return connect();
+ }, [onWebsocketMessage, disableLiveView]);
+}
\ No newline at end of file
diff --git a/frontend/app/routes/queue/route.tsx b/frontend/app/routes/queue/route.tsx
index a59d5dce..aeaa03d9 100644
--- a/frontend/app/routes/queue/route.tsx
+++ b/frontend/app/routes/queue/route.tsx
@@ -5,26 +5,11 @@ import { Alert } from 'react-bootstrap';
import { backendClient, type HistorySlot, type QueueSlot } from "~/clients/backend-client.server";
import { HistoryTable } from "./components/history-table/history-table";
import { QueueTable } from "./components/queue-table/queue-table";
-import { useCallback, useEffect, useState, useRef } from "react";
-import { receiveMessage } from "~/utils/websocket-util";
-import { useDropzone } from 'react-dropzone';
-
-const topicNames = {
- queueItemStatus: 'qs',
- queueItemPercentage: 'qp',
- queueItemAdded: 'qa',
- queueItemRemoved: 'qr',
- historyItemAdded: 'ha',
- historyItemRemoved: 'hr',
-}
-const topicSubscriptions = {
- [topicNames.queueItemStatus]: 'state',
- [topicNames.queueItemPercentage]: 'state',
- [topicNames.queueItemAdded]: 'event',
- [topicNames.queueItemRemoved]: 'event',
- [topicNames.historyItemAdded]: 'event',
- [topicNames.historyItemRemoved]: 'event',
-}
+import { useCallback, useState, useRef } from "react";
+import { useHistoryEvents, useQueueEvents } from "./controllers/events-controller";
+import { initializeQueueHistoryWebsocket } from "./controllers/websocket-controller";
+import { initializeUploadController } from "./controllers/nzb-upload-controller";
+import { useQueueDropzone } from "./controllers/dropzone-controller";
const maxItems = 100;
export async function loader({ request }: Route.LoaderArgs) {
@@ -72,207 +57,16 @@ export default function Queue(props: Route.ComponentProps) {
manualCategoryRef.current = category;
}, []);
- // queue events
- const onAddQueueSlot = useCallback((queueSlot: QueueSlot) => {
- uploadQueueRef.current = uploadQueueRef.current.filter(x => x.queueSlot.status === "uploading" || x.queueSlot.filename !== queueSlot.filename);
- setUploadingFiles(files => files.filter(f => f.queueSlot.filename !== queueSlot.filename));
- setQueueSlots(slots => [...slots, queueSlot]);
- }, [setQueueSlots]);
-
- const onSelectQueueSlots = useCallback((ids: Set<string>, isSelected: boolean) => {
- setUploadingFiles(files => files.map(x => ids.has(x.queueSlot.nzo_id) ? { ...x, queueSlot: { ...x.queueSlot, isSelected } } : x));
- setQueueSlots(slots => slots.map(x => ids.has(x.nzo_id) ? { ...x, isSelected } : x));
- }, [setQueueSlots]);
-
- const onRemovingQueueSlots = useCallback((ids: Set<string>, isRemoving: boolean) => {
- setQueueSlots(slots => slots.map(x => ids.has(x.nzo_id) ? { ...x, isRemoving } : x));
- }, [setQueueSlots]);
-
- const onRemoveQueueSlots = useCallback((ids: Set<string>) => {
- uploadQueueRef.current = uploadQueueRef.current.filter(x => x.queueSlot.status === "uploading" || !ids.has(x.queueSlot.nzo_id));
- setUploadingFiles(files => files.filter(x => x.queueSlot.status === "uploading" || !ids.has(x.queueSlot.nzo_id)));
- setQueueSlots(slots => slots.filter(x => !ids.has(x.nzo_id)));
- }, [setQueueSlots]);
-
- const onChangeQueueSlotStatus = useCallback((message: string) => {
- const [nzo_id, status] = message.split('|');
- setQueueSlots(slots => slots.map(x => x.nzo_id === nzo_id ? { ...x, status } : x));
- }, [setQueueSlots]);
-
- const onChangeQueueSlotPercentage = useCallback((message: string) => {
- const [nzo_id, true_percentage] = message.split('|');
- setQueueSlots(slots => slots.map(x => x.nzo_id === nzo_id ? { ...x, true_percentage } : x));
- }, [setQueueSlots]);
-
- // history events
- const onAddHistorySlot = useCallback((historySlot: HistorySlot) => {
- setHistorySlots(slots => [historySlot, ...slots]);
- }, [setHistorySlots]);
-
- const onSelectHistorySlots = useCallback((ids: Set<string>, isSelected: boolean) => {
- setHistorySlots(slots => slots.map(x => ids.has(x.nzo_id) ? { ...x, isSelected } : x));
- }, [setHistorySlots]);
-
- const onRemovingHistorySlots = useCallback((ids: Set<string>, isRemoving: boolean) => {
- setHistorySlots(slots => slots.map(x => ids.has(x.nzo_id) ? { ...x, isRemoving } : x));
- }, [setHistorySlots]);
-
- const onRemoveHistorySlots = useCallback((ids: Set<string>) => {
- setHistorySlots(slots => slots.filter(x => !ids.has(x.nzo_id)));
- }, [setHistorySlots]);
+ // queue/history events
+ const queueEvents = useQueueEvents(setUploadingFiles, setQueueSlots, uploadQueueRef);
+ const historyEvents = useHistoryEvents(setHistorySlots);
// websocket
- const onWebsocketMessage = useCallback((topic: string, message: string) => {
- if (disableLiveView) return;
- if (topic == topicNames.queueItemAdded)
- onAddQueueSlot(JSON.parse(message));
- else if (topic == topicNames.queueItemRemoved)
- onRemoveQueueSlots(new Set<string>(message.split(',')));
- else if (topic == topicNames.queueItemStatus)
- onChangeQueueSlotStatus(message);
- else if (topic == topicNames.queueItemPercentage)
- onChangeQueueSlotPercentage(message);
- else if (topic == topicNames.historyItemAdded)
- onAddHistorySlot(JSON.parse(message));
- else if (topic == topicNames.historyItemRemoved)
- onRemoveHistorySlots(new Set<string>(message.split(',')));
- }, [
- onAddQueueSlot,
- onRemoveQueueSlots,
- onChangeQueueSlotStatus,
- onChangeQueueSlotPercentage,
- onAddHistorySlot,
- onRemoveHistorySlots,
- disableLiveView
- ]);
-
- useEffect(() => {
- if (disableLiveView) return;
- let ws: WebSocket;
- let disposed = false;
- function connect() {
- ws = new WebSocket(window.location.origin.replace(/^http/, 'ws'));
- ws.onmessage = receiveMessage(onWebsocketMessage);
- ws.onopen = () => { ws.send(JSON.stringify(topicSubscriptions)); }
- ws.onclose = () => { !disposed && setTimeout(() => connect(), 1000); };
- ws.onerror = () => { ws.close() };
- return () => { disposed = true; ws.close(); }
- }
-
- return connect();
- }, [onWebsocketMessage, disableLiveView]);
-
- // upload processing
- const processUploadQueue = useCallback(async () => {
- if (isUploadingRef.current || uploadQueueRef.current.length === 0) return;
-
- isUploadingRef.current = true;
- const fileToUpload = uploadQueueRef.current[0];
-
- setUploadingFiles(files => files.map(f =>
- f.queueSlot.nzo_id === fileToUpload.queueSlot.nzo_id
- ? { ...f, queueSlot: { ...f.queueSlot, status: 'uploading' } }
- : f
- ));
-
- try {
- const xhr = new XMLHttpRequest();
- const formData = new FormData();
- formData.append('nzbFile', fileToUpload.file, fileToUpload.file.name);
-
- xhr.responseType = 'json';
- xhr.upload.addEventListener('progress', (e) => {
- if (e.lengthComputable) {
- const progress = Math.round((e.loaded / e.total) * 100);
- setUploadingFiles(files => files.map(f =>
- f.queueSlot.nzo_id === fileToUpload.queueSlot.nzo_id
- ? {
- ...f,
- queueSlot: {
- ...f.queueSlot,
- percentage: progress.toString(),
- true_percentage: progress.toString()
- }
- }
- : f
- ));
- }
- });
-
- var response: any = await new Promise<void>((resolve, reject) => {
- xhr.addEventListener('load', () => {
- if (xhr.status >= 200 && xhr.status < 300) {
- resolve(xhr.response);
- } else {
- const errorMessage = xhr.response.error || `Upload failed with status ${xhr.status}`;
- reject(new Error(errorMessage));
- }
- });
- xhr.addEventListener('error', () => reject(new Error('Upload failed')));
- xhr.addEventListener('abort', () => reject(new Error('Upload aborted')));
-
- xhr.open('POST', `/api?mode=addfile&cat=${manualCategoryRef.current}&priority=0&pp=0`);
- xhr.send(formData);
- });
-
- if (response.status == false) {
- throw new Error(response.error);
- }
-
- } catch (error) {
- setUploadingFiles(files => files.map(f =>
- f.queueSlot.nzo_id === fileToUpload.queueSlot.nzo_id ? {
- ...f,
- queueSlot: {
- ...f.queueSlot,
- status: 'upload failed',
- error: error instanceof Error ? error.message : 'Upload failed'
- }
- } : f
- ));
- }
-
- uploadQueueRef.current = uploadQueueRef.current.filter(x => x !== fileToUpload);
- isUploadingRef.current = false;
-
- if (uploadQueueRef.current.length > 0) {
- processUploadQueue();
- }
- }, []);
-
- // trigger upload processing when files are added
- useEffect(() => {
- processUploadQueue();
- }, [uploadingFiles, processUploadQueue]);
-
- // dropzone
- const onDrop = useCallback((acceptedFiles: File[]) => {
- const newFiles: UploadingFile[] = acceptedFiles.map(file => ({
- file,
- queueSlot: {
- isUploading: true,
- nzo_id: `upload-${Date.now()}-${Math.random()}`,
- priority: 'Normal',
- filename: file.name,
- cat: manualCategoryRef.current,
- percentage: "0",
- true_percentage: "0",
- status: "pending",
- mb: (file.size / (1024 * 1024)).toFixed(2),
- mbleft: (file.size / (1024 * 1024)).toFixed(2),
- }
- }));
-
- setUploadingFiles(files => [...files, ...newFiles]);
- uploadQueueRef.current = [...uploadQueueRef.current, ...newFiles];
- }, []);
+ initializeQueueHistoryWebsocket(queueEvents, historyEvents, disableLiveView);
- const dropzone = useDropzone({
- accept: { 'application/x-nzb': ['.nzb'] },
- onDrop,
- noClick: true,
- noKeyboard: true,
- });
+ // uploads
+ const dropzone = useQueueDropzone(setUploadingFiles, uploadQueueRef, manualCategoryRef);
+ initializeUploadController(isUploadingRef, uploadQueueRef, manualCategoryRef, uploadingFiles, setUploadingFiles);
// view
return (
@@ -309,9 +103,9 @@ export default function Queue(props: Route.ComponentProps) {
totalQueueCount={props.loaderData.totalQueueCount + uploadingFiles.length}
categories={props.loaderData.categories}
manualCategory={manualCategory}
- onIsSelectedChanged={onSelectQueueSlots}
- onIsRemovingChanged={onRemovingQueueSlots}
- onRemoved={onRemoveQueueSlots}
+ onIsSelectedChanged={queueEvents.onSelectQueueSlots}
+ onIsRemovingChanged={queueEvents.onRemovingQueueSlots}
+ onRemoved={queueEvents.onRemoveQueueSlots}
onManualCategoryChanged={onManualCategoryChanged}
/>
</div>
@@ -322,9 +116,9 @@ export default function Queue(props: Route.ComponentProps) {
<HistoryTable
historySlots={historySlots}
totalHistoryCount={props.loaderData.totalHistoryCount}
- onIsSelectedChanged={onSelectHistorySlots}
- onIsRemovingChanged={onRemovingHistorySlots}
- onRemoved={onRemoveHistorySlots}
+ onIsSelectedChanged={historyEvents.onSelectHistorySlots}
+ onIsRemovingChanged={historyEvents.onRemovingHistorySlots}
+ onRemoved={historyEvents.onRemoveHistorySlots}
/>
}
</div >