Skip to content

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

C#

Refactored manual-category dropdown.

nzbdav-devc06507395c79
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 99718755..79aa3c17 100644
--- a/frontend/app/routes/queue/components/queue-table/queue-table.tsx
+++ b/frontend/app/routes/queue/components/queue-table/queue-table.tsx
@@ -15,22 +15,20 @@ export type QueueTableProps = {
queueSlots: PresentationQueueSlot[],
totalQueueCount: number,
categories: string[],
- manualCategory: string,
+ manualCategoryRef: React.RefObject<string>,
onIsSelectedChanged: (nzo_ids: Set<string>, isSelected: boolean) => void,
onIsRemovingChanged: (nzo_ids: Set<string>, isRemoving: boolean) => void,
onRemoved: (nzo_ids: Set<string>) => void,
- onManualCategoryChanged: (category: string) => void,
}
export function QueueTable({
queueSlots,
totalQueueCount,
categories,
- manualCategory,
+ manualCategoryRef,
onIsSelectedChanged,
onIsRemovingChanged,
onRemoved,
- onManualCategoryChanged,
}: QueueTableProps) {
const [isConfirmingRemoval, setIsConfirmingRemoval] = useState(false);
var selectedCount = queueSlots.filter(x => !!x.isSelected).length;
@@ -94,13 +92,9 @@ export function QueueTable({
// view
const categoryDropdown = useMemo(() => (
<div title="Choose the category for manual nzb uploads.">
- <SimpleDropdown
- options={categories}
- value={manualCategory}
- onChange={onManualCategoryChanged}
- />
+ <SimpleDropdown options={categories} valueRef={manualCategoryRef}/>
</div>
- ), [categories, manualCategory, onManualCategoryChanged]);
+ ), [categories]);
const sectionTitle = (
<div className={styles.sectionTitle}>
diff --git a/frontend/app/routes/queue/components/simple-dropdown/simple-dropdown.tsx b/frontend/app/routes/queue/components/simple-dropdown/simple-dropdown.tsx
index ec57c4a7..b459104f 100644
--- a/frontend/app/routes/queue/components/simple-dropdown/simple-dropdown.tsx
+++ b/frontend/app/routes/queue/components/simple-dropdown/simple-dropdown.tsx
@@ -5,19 +5,31 @@ import { classNames } from "~/utils/styling";
export type SimpleDropdownProps = {
type?: "plain" | "bordered"
options: string[],
- value: string,
- onChange: (value: string) => void,
+ value?: string,
+ onChange?: (value: string) => void,
+ valueRef?: React.RefObject<string>,
}
-export const SimpleDropdown = memo(({ type, options, value, onChange }: SimpleDropdownProps) => {
+export const SimpleDropdown = memo(({ type, options, value, onChange, valueRef }: SimpleDropdownProps) => {
+ // validation
+ if (!valueRef && (!value || !onChange)) {
+ throw new Error("SimpleDropdown requires either the valueRef prop or both the value and onChange props.")
+ }
+
+ // state variables
+ const [internalValue, setInternalValue] = useState(options.length > 0 ? options[0] : "");
const [isOpen, setIsOpen] = useState(false);
const [openAbove, setOpenAbove] = useState(false);
const dropdownRef = useRef<HTMLDivElement>(null);
+
+ // derived variables
+ const renderedValue = value || internalValue;
const containerClassNames = classNames([
styles.container,
type === "bordered" && styles.bordered
]);
+ // events
const toggleDropdown = useCallback(() => {
if (!isOpen && dropdownRef.current) {
const rect = dropdownRef.current.getBoundingClientRect();
@@ -27,16 +39,26 @@ export const SimpleDropdown = memo(({ type, options, value, onChange }: SimpleDr
setIsOpen(prev => !prev);
}, [isOpen]);
+ const handleSelectedOptionChange = useCallback((option: string) => {
+ if (!!valueRef) {
+ setInternalValue(option);
+ valueRef.current = option;
+ }
+ else if (!!onChange) {
+ onChange(option);
+ }
+ }, [valueRef, setInternalValue, onChange]);
+
const handleOptionClick = useCallback((option: string) => {
- onChange(option);
+ handleSelectedOptionChange(option);
setIsOpen(false);
}, [onChange]);
const handleNativeChange = useCallback((e: ChangeEvent<HTMLSelectElement>) => {
- onChange(e.target.value);
- }, [onChange]);
+ handleSelectedOptionChange(e.target.value);
+ }, [handleSelectedOptionChange]);
- // Close dropdown when clicking outside
+ // close dropdown when clicking outside
useEffect(() => {
const handleClickOutside = (event: MouseEvent) => {
if (dropdownRef.current && !dropdownRef.current.contains(event.target as Node)) {
@@ -53,10 +75,11 @@ export const SimpleDropdown = memo(({ type, options, value, onChange }: SimpleDr
};
}, [isOpen]);
+ // view
return (
<div className={containerClassNames} ref={dropdownRef}>
{/* hidden native select for mobile devices */}
- <select className={styles.nativeSelect} value={value} onChange={handleNativeChange}>
+ <select className={styles.nativeSelect} value={renderedValue} onChange={handleNativeChange}>
{options.map(option => (
<option key={option} value={option}>{option}</option>
))}
@@ -64,7 +87,7 @@ export const SimpleDropdown = memo(({ type, options, value, onChange }: SimpleDr
{/* styled visible dropdown box */}
<div className={styles.selected} onClick={toggleDropdown}>
- {value}
+ {renderedValue}
<span className={styles.arrow}></span>
</div>
diff --git a/frontend/app/routes/queue/route.tsx b/frontend/app/routes/queue/route.tsx
index aeaa03d9..3f4a1196 100644
--- a/frontend/app/routes/queue/route.tsx
+++ b/frontend/app/routes/queue/route.tsx
@@ -5,7 +5,7 @@ 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, useState, useRef } from "react";
+import { useState, useRef } from "react";
import { useHistoryEvents, useQueueEvents } from "./controllers/events-controller";
import { initializeQueueHistoryWebsocket } from "./controllers/websocket-controller";
import { initializeUploadController } from "./controllers/nzb-upload-controller";
@@ -44,19 +44,12 @@ export default function Queue(props: Route.ComponentProps) {
const [queueSlots, setQueueSlots] = useState<PresentationQueueSlot[]>(props.loaderData.queueSlots);
const [historySlots, setHistorySlots] = useState<PresentationHistorySlot[]>(props.loaderData.historySlots);
const [uploadingFiles, setUploadingFiles] = useState<UploadingFile[]>([]);
- const [manualCategory, setManualCategory] = useState<string>(props.loaderData.manualCategory);
const uploadQueueRef = useRef<UploadingFile[]>([]);
- const manualCategoryRef = useRef<string>(manualCategory);
+ const manualCategoryRef = useRef<string>(props.loaderData.manualCategory);
const isUploadingRef = useRef(false);
const disableLiveView = queueSlots.length == maxItems || historySlots.length == maxItems;
const combinedQueueSlots = [...uploadingFiles.map(file => file.queueSlot), ...queueSlots];
- // category events
- const onManualCategoryChanged = useCallback((category: string) => {
- setManualCategory(category);
- manualCategoryRef.current = category;
- }, []);
-
// queue/history events
const queueEvents = useQueueEvents(setUploadingFiles, setQueueSlots, uploadQueueRef);
const historyEvents = useHistoryEvents(setHistorySlots);
@@ -102,11 +95,10 @@ export default function Queue(props: Route.ComponentProps) {
queueSlots={combinedQueueSlots}
totalQueueCount={props.loaderData.totalQueueCount + uploadingFiles.length}
categories={props.loaderData.categories}
- manualCategory={manualCategory}
+ manualCategoryRef={manualCategoryRef}
onIsSelectedChanged={queueEvents.onSelectQueueSlots}
onIsRemovingChanged={queueEvents.onRemovingQueueSlots}
onRemoved={queueEvents.onRemoveQueueSlots}
- onManualCategoryChanged={onManualCategoryChanged}
/>
</div>
</div>