Skip to content

Commit

Permalink
Make arrow left/right keys toggle expandable rows
Browse files Browse the repository at this point in the history
+ more render optimizations
  • Loading branch information
qu1ck committed Nov 21, 2023
1 parent b4bdc3b commit f20f832
Show file tree
Hide file tree
Showing 12 changed files with 54 additions and 45 deletions.
6 changes: 3 additions & 3 deletions src/components/details.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -305,16 +305,16 @@ function FileTreePane(props: { torrent: Torrent }) {
void refetch();
}, [props.torrent, fileTree, refetch]);

const mutation = useMutateTorrent();
const { mutate } = useMutateTorrent();

const onCheckboxChange = useUnwantedFiles(fileTree, true);
const updateUnwanted = useCallback((entryPath: string, state: boolean) => {
onCheckboxChange(entryPath, state);
mutation.mutate({
mutate({
torrentIds: [props.torrent.id],
fields: { [state ? "files-wanted" : "files-unwanted"]: fileTree.getChildFilesIndexes(entryPath) },
});
}, [fileTree, mutation, onCheckboxChange, props.torrent.id]);
}, [fileTree, mutate, onCheckboxChange, props.torrent.id]);

return (
<FileTreeTable
Expand Down
12 changes: 6 additions & 6 deletions src/components/modals/add.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ export function AddMagnet(props: AddCommonModalProps) {
});
}, []),
);
const trackersMutation = useTorrentAddTrackers();
const mutateAddTrackers = useTorrentAddTrackers();

const onAdd = useCallback(() => {
if (magnet === "") return;
Expand All @@ -213,7 +213,7 @@ export function AddMagnet(props: AddCommonModalProps) {
);
common.location.addPath(common.location.path);
} else {
trackersMutation.mutate(
mutateAddTrackers(
{ torrentId: existingTorrent.id, trackers: magnetData?.trackers ?? [] },
{
onSuccess: () => {
Expand All @@ -227,7 +227,7 @@ export function AddMagnet(props: AddCommonModalProps) {
}
setMagnet("");
close();
}, [existingTorrent, close, addMutation, magnet, common, trackersMutation, magnetData]);
}, [existingTorrent, close, addMutation, magnet, common, mutateAddTrackers, magnetData]);

const config = useContext(ConfigContext);
const shouldOpen = !config.values.interface.skipAddDialog || typeof props.uri !== "string";
Expand Down Expand Up @@ -499,7 +499,7 @@ export function AddTorrent(props: AddCommonModalProps) {
});
}, []),
);
const trackersMutation = useTorrentAddTrackers();
const mutateAddTrackers = useTorrentAddTrackers();

const onAdd = useCallback(() => {
if (torrentData === undefined) return;
Expand All @@ -521,7 +521,7 @@ export function AddTorrent(props: AddCommonModalProps) {

common.location.addPath(common.location.path);
} else {
trackersMutation.mutate(
mutateAddTrackers(
{ torrentId: existingTorrent.id, trackers: torrentData[0].trackers },
{
onSuccess: () => {
Expand All @@ -540,7 +540,7 @@ export function AddTorrent(props: AddCommonModalProps) {
}
setTorrentData(undefined);
close();
}, [torrentData, existingTorrent, close, common, addMutation, fileTree, trackersMutation, config]);
}, [torrentData, existingTorrent, close, common, addMutation, fileTree, mutateAddTrackers, config]);

const shouldOpen = !config.values.interface.skipAddDialog && torrentData !== undefined;
useEffect(() => {
Expand Down
6 changes: 3 additions & 3 deletions src/components/modals/editlabels.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export function EditLabelsModal(props: ModalState) {
if (opened) setLabels(calculateInitialLabels());
}, [calculateInitialLabels, opened]);

const mutation = useMutateTorrent();
const { mutate } = useMutateTorrent();

const onSave = useCallback(() => {
if (rpcVersion < 16) {
Expand All @@ -57,7 +57,7 @@ export function EditLabelsModal(props: ModalState) {
close();
return;
}
mutation.mutate(
mutate(
{
torrentIds: Array.from(serverSelected),
fields: { labels },
Expand All @@ -79,7 +79,7 @@ export function EditLabelsModal(props: ModalState) {
},
);
close();
}, [rpcVersion, mutation, serverSelected, labels, close]);
}, [rpcVersion, mutate, serverSelected, labels, close]);

return <>
{props.opened &&
Expand Down
6 changes: 3 additions & 3 deletions src/components/modals/edittorrent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,12 @@ export function EditTorrent(props: ModalState) {
});
}, [setValues, torrent]);

const mutation = useMutateTorrent();
const { mutate } = useMutateTorrent();

const onSave = useCallback(() => {
if (torrentId === undefined || torrent === undefined) return;

mutation.mutate(
mutate(
{
torrentIds: [...selected],
fields: {
Expand All @@ -97,7 +97,7 @@ export function EditTorrent(props: ModalState) {
},
);
props.close();
}, [torrentId, torrent, mutation, selected, form.values, props]);
}, [torrentId, torrent, mutate, selected, form.values, props]);

return <>{props.opened &&
<SaveCancelModal
Expand Down
6 changes: 3 additions & 3 deletions src/components/modals/edittrackers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export function EditTrackers(props: ModalState) {
});
}, [rpcVersion, setValues, torrent]);

const mutation = useMutateTorrent();
const { mutate } = useMutateTorrent();

const onSave = useCallback(() => {
if (torrentId === undefined || torrent === undefined) return;
Expand All @@ -77,7 +77,7 @@ export function EditTrackers(props: ModalState) {
if (toAdd.length === 0) toAdd = undefined;
if (toRemove.length === 0) toRemove = undefined;
}
mutation.mutate(
mutate(
{
torrentIds: [...selected],
fields: {
Expand All @@ -97,7 +97,7 @@ export function EditTrackers(props: ModalState) {
},
);
props.close();
}, [torrentId, torrent, rpcVersion, mutation, selected, form.values, props]);
}, [torrentId, torrent, rpcVersion, mutate, selected, form.values, props]);

const addDefaultTrackers = useCallback(() => {
let list = form.values.trackerList;
Expand Down
6 changes: 3 additions & 3 deletions src/components/modals/move.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ export function MoveModal(props: ModalState) {
const location = useTorrentLocation();
const { setPath } = location;

const mutation = useTorrentChangeDirectory();
const changeDirectory = useTorrentChangeDirectory();

const onMove = useCallback(() => {
mutation.mutate(
changeDirectory(
{
torrentIds: Array.from(serverSelected),
location: location.path,
Expand All @@ -53,7 +53,7 @@ export function MoveModal(props: ModalState) {
);

props.close();
}, [mutation, serverSelected, location.path, moveData, props]);
}, [changeDirectory, serverSelected, location.path, moveData, props]);

const calculateInitialLocation = useCallback(() => {
const [id] = [...serverSelected];
Expand Down
6 changes: 3 additions & 3 deletions src/components/modals/remove.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ export function RemoveModal(props: ModalState) {
const serverSelected = useServerSelectedTorrents();
const [deleteData, setDeleteData] = useState<boolean>(false);

const mutation = useRemoveTorrents();
const remove = useRemoveTorrents();

const onDelete = useCallback(() => {
mutation.mutate(
remove(
{
torrentIds: Array.from(serverSelected),
deleteData,
Expand All @@ -47,7 +47,7 @@ export function RemoveModal(props: ModalState) {
},
);
props.close();
}, [mutation, serverSelected, deleteData, props]);
}, [remove, serverSelected, deleteData, props]);

return (
<HkModal opened={props.opened} onClose={props.close} title="Remove torrents" centered size="lg">
Expand Down
11 changes: 8 additions & 3 deletions src/components/tables/common.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,7 @@ export function TrguiTable<TData>(props: {
interface EditableNameFieldProps extends React.PropsWithChildren {
currentName: string,
onUpdate?: (newName: string, onStart: () => void, onEnd: () => void) => void,
onArrowLeftRight?: (key: string) => void,
}

export function EditableNameField(props: EditableNameFieldProps) {
Expand Down Expand Up @@ -613,14 +614,18 @@ export function EditableNameField(props: EditableNameFieldProps) {
}
}, []);

const onF2 = useCallback((event: React.KeyboardEvent<HTMLDivElement>) => {
const { onArrowLeftRight } = props;

const onKeyDown = useCallback((event: React.KeyboardEvent<HTMLDivElement>) => {
if (event.key === "F2" && !isRenaming) {
renameHandler();
} else if (onArrowLeftRight !== undefined && ["ArrowLeft", "ArrowRight"].includes(event.key)) {
onArrowLeftRight(event.key);
}
}, [isRenaming, renameHandler]);
}, [isRenaming, onArrowLeftRight, renameHandler]);

return (
<Box ref={ref} onKeyDown={onF2} tabIndex={-1}
<Box ref={ref} onKeyDown={onKeyDown} tabIndex={-1}
onMouseEnter={() => { setHover(true); }} onMouseLeave={() => { setHover(false); }}
sx={{ display: "flex", alignItems: "center", width: "100%", height: "100%" }}>
{props.children}
Expand Down
22 changes: 13 additions & 9 deletions src/components/tables/filetreetable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,14 @@ function NameField(props: TableFieldProps) {

const rpcVersion = useServerRpcVersion();

const onArrowLeftRight = useCallback((key: string) => {
if (props.row.subRows.length === 0) return;
if (key === "ArrowLeft" && props.row.getIsExpanded()) props.row.toggleExpanded();
if (key === "ArrowRight" && !props.row.getIsExpanded()) props.row.toggleExpanded();
}, [props.row]);

return (
<EditableNameField currentName={props.entry.name}
<EditableNameField currentName={props.entry.name} onArrowLeftRight={onArrowLeftRight}
onUpdate={(props.treeName === "filetree" && rpcVersion >= 15) ? updatePath : undefined}>
<Box sx={{ width: `${props.entry.level * 1.6}rem`, flexShrink: 0 }} />
<Box w="1.4rem" mx="auto" sx={{ flexShrink: 0 }}>
Expand Down Expand Up @@ -282,9 +288,7 @@ export function FileTreeTable(props: FileTreeTableProps) {
}), [props.brief, props.fileTree, nameSortFunc, onCheckboxChange]);

const getRowId = useCallback((row: FileDirEntryView) => row.fullpath, []);
const getSubRows = useCallback((row: FileDirEntryView) => {
return row.subrows;
}, []);
const getSubRows = useCallback((row: FileDirEntryView) => row.subrows, []);

const { selected, selectedReducer } = useSelected(props);

Expand Down Expand Up @@ -412,7 +416,7 @@ function FiletreeContextMenu(props: {
onEntryOpen(rowPath, reveal);
}, [onEntryOpen, props.fileTree, props.selected]);

const mutation = useMutateTorrent();
const { mutate } = useMutateTorrent();

const setPriority = useCallback((priority: "priority-high" | "priority-normal" | "priority-low") => {
const fileIds = Array.from(props.selected
Expand All @@ -422,7 +426,7 @@ function FiletreeContextMenu(props: {
return set;
}, new Set<number>()));

mutation.mutate(
mutate(
{
torrentIds: [props.fileTree.torrentId],
fields: {
Expand All @@ -438,7 +442,7 @@ function FiletreeContextMenu(props: {
},
},
);
}, [mutation, props.fileTree, props.selected]);
}, [mutate, props.fileTree, props.selected]);

const setWanted = useCallback((wanted: boolean) => {
const fileIds = Array.from(props.selected
Expand All @@ -448,7 +452,7 @@ function FiletreeContextMenu(props: {
return set;
}, new Set<number>()));

mutation.mutate(
mutate(
{
torrentIds: [props.fileTree.torrentId],
fields: {
Expand All @@ -464,7 +468,7 @@ function FiletreeContextMenu(props: {
},
},
);
}, [mutation, props.fileTree, props.selected]);
}, [mutate, props.fileTree, props.selected]);

return (
<ContextMenu contextMenuInfo={props.contextMenuInfo} setContextMenuInfo={props.setContextMenuInfo}>
Expand Down
6 changes: 3 additions & 3 deletions src/components/tables/torrenttable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -468,10 +468,10 @@ function TorrentContextMenu(props: {
onRowDoubleClick(torrent, reveal);
}, [onRowDoubleClick, serverData.torrents, serverSelected]);

const mutation = useTorrentAction();
const mutate = useTorrentAction();

const torrentAction = useCallback((method: TorrentActionMethodsType, successMessage: string) => {
mutation.mutate(
mutate(
{
method,
torrentIds: Array.from(serverSelected),
Expand All @@ -485,7 +485,7 @@ function TorrentContextMenu(props: {
},
},
);
}, [mutation, serverSelected]);
}, [mutate, serverSelected]);

const [queueSubmenuOpened, setQueueSubmenuOpened] = useState(false);
const queueRef = useRef<HTMLButtonElement>(null);
Expand Down
10 changes: 5 additions & 5 deletions src/components/toolbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ function useButtonHandlers(
setAltSpeedMode: React.Dispatch<boolean | undefined>,
) {
const serverSelected = useServerSelectedTorrents();
const actionMutation = useTorrentAction();
const priorityMutation = useMutateTorrent();
const actionMutate = useTorrentAction();
const { mutate: mutateTorrent } = useMutateTorrent();

const handlers = useMemo(() => {
const checkSelected = (action?: () => void) => {
Expand All @@ -84,7 +84,7 @@ function useButtonHandlers(
};
};
const action = (method: TorrentActionMethodsType) => () => {
actionMutation.mutate(
actionMutate(
{
method,
torrentIds: Array.from(serverSelected),
Expand All @@ -101,7 +101,7 @@ function useButtonHandlers(
);
};
const priority = (bandwidthPriority: PriorityNumberType) => () => {
priorityMutation.mutate(
mutateTorrent(
{
torrentIds: Array.from(serverSelected),
fields: { bandwidthPriority },
Expand Down Expand Up @@ -137,7 +137,7 @@ function useButtonHandlers(
setPriorityLow: checkSelected(priority(BandwidthPriority.low)),
daemonSettings: () => { props.modals.current?.daemonSettings(); },
};
}, [actionMutation, priorityMutation, props.modals, serverSelected]);
}, [actionMutate, mutateTorrent, props.modals, serverSelected]);

const sessionMutation = useMutateSession();

Expand Down
2 changes: 1 addition & 1 deletion src/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ function useInvalidatingTorrentAction<ActionParams>(mutationFn: (params: ActionP
onSuccess: () => {
void queryClient.invalidateQueries(TorrentKeys.all(serverConfig.name));
},
});
}).mutate;
}

export interface TorrentAddQueryParams extends TorrentAddParams {
Expand Down

0 comments on commit f20f832

Please sign in to comment.