From 54eb674e7f66ddd0733625701b9b0ad81fd97645 Mon Sep 17 00:00:00 2001 From: qu1ck Date: Mon, 17 Jul 2023 05:36:14 -0700 Subject: [PATCH] Refactor ServerData as context, optimize some unnecessary renders --- src/components/details.tsx | 64 ++++++++++++++------------ src/components/filters.tsx | 10 ++-- src/components/modals/add.tsx | 58 ++++++++++------------- src/components/modals/common.tsx | 20 ++++---- src/components/modals/editlabels.tsx | 20 ++++---- src/components/modals/edittorrent.tsx | 9 ++-- src/components/modals/move.tsx | 18 ++++---- src/components/modals/remove.tsx | 12 +++-- src/components/modals/servermodals.tsx | 8 ---- src/components/server.tsx | 28 ++++++----- src/components/tables/torrenttable.tsx | 29 ++++++------ src/components/toolbar.tsx | 12 ++--- src/rpc/torrent.ts | 30 +++++------- 13 files changed, 155 insertions(+), 163 deletions(-) diff --git a/src/components/details.tsx b/src/components/details.tsx index 9a558b6..24e2476 100644 --- a/src/components/details.tsx +++ b/src/components/details.tsx @@ -371,6 +371,39 @@ function ServerStats() { ); } +const DetailsPanels = React.memo(function DetailsPanels({ torrent }: { torrent: Torrent | undefined }) { + return (<> + + {torrent !== undefined + ? + : <>} + + + {torrent !== undefined + ? + : <>} + + + {torrent !== undefined + ? + : <>} + + + {torrent !== undefined + ? + : <>} + + + {torrent !== undefined + ? + : <>} + + + + + ); +}); + function Details(props: DetailsProps) { const config = useContext(ConfigContext); const peersTableVisibility = config.getTableColumnVisibility("peers"); @@ -388,7 +421,7 @@ function Details(props: DetailsProps) { return ( + h="100%" w="100%" sx={{ display: "flex", flexDirection: "column" }}> @@ -432,34 +465,7 @@ function Details(props: DetailsProps) { visible={props.torrentId !== undefined && isLoading} transitionDuration={500} loaderProps={{ size: "xl" }} overlayOpacity={0.35} /> - - {torrent !== undefined - ? - : <>} - - - {torrent !== undefined - ? - : <>} - - - {torrent !== undefined - ? - : <>} - - - {torrent !== undefined - ? - : <>} - - - {torrent !== undefined - ? - : <>} - - - - + ); diff --git a/src/components/filters.tsx b/src/components/filters.tsx index e866f06..13800c2 100644 --- a/src/components/filters.tsx +++ b/src/components/filters.tsx @@ -251,7 +251,7 @@ function flattenTree(root: Directory): Directory[] { return result; } -export function Filters(props: FiltersProps) { +export const Filters = React.memo(function Filters(props: FiltersProps) { const config = useContext(ConfigContext); const serverConfig = useContext(ServerConfigContext); const forceRender = useForceRender(); @@ -274,11 +274,9 @@ export function Filters(props: FiltersProps) { () => props.torrents.map((t) => t.downloadDir as string).sort(), [props.torrents]); - const [dirs, setDirs] = useState([]); - - useEffect(() => { + const dirs = useMemo(() => { const tree = buildDirTree(paths, serverConfig.expandedDirFilters); - setDirs(flattenTree(tree)); + return flattenTree(tree); }, [paths, serverConfig.expandedDirFilters]); const allFilters = useMemo(() => { @@ -350,4 +348,4 @@ export function Filters(props: FiltersProps) { } ); -} +}); diff --git a/src/components/modals/add.tsx b/src/components/modals/add.tsx index 0b4a058..ac04419 100644 --- a/src/components/modals/add.tsx +++ b/src/components/modals/add.tsx @@ -18,11 +18,11 @@ import { Box, Button, Checkbox, Divider, Flex, Group, Menu, Overlay, SegmentedControl, Text, TextInput, useMantineTheme } from "@mantine/core"; import React, { useCallback, useContext, useEffect, useMemo, useRef, useState } from "react"; -import type { ActionModalState, LabelsData, LocationData } from "./common"; +import type { ModalState, LocationData } from "./common"; import { HkModal, TorrentLabels, TorrentLocation, limitTorrentNames, useTorrentLocation } from "./common"; import type { PriorityNumberType } from "rpc/transmission"; import { PriorityColors, PriorityStrings } from "rpc/transmission"; -import type { Torrent } from "rpc/torrent"; +import { useServerTorrentData, type Torrent } from "rpc/torrent"; import { CachedFileTree } from "cachedfiletree"; import { FileTreeTable, useUnwantedFiles } from "components/tables/filetreetable"; import { notifications } from "@mantine/notifications"; @@ -35,7 +35,8 @@ const { TAURI, dialogOpen, invoke } = await import(/* webpackChunkName: "taurish interface AddCommonProps extends React.PropsWithChildren { location: LocationData, - labelsData: LabelsData, + labels: string[], + setLabels: React.Dispatch, start: boolean, setStart: (b: boolean) => void, priority: PriorityNumberType, @@ -46,7 +47,7 @@ interface AddCommonProps extends React.PropsWithChildren { function AddCommon(props: AddCommonProps) { return <> - + ; } -interface AddCommonModalProps extends ActionModalState { +interface AddCommonModalProps extends ModalState { serverName: string, uri: string | File | undefined, tabsRef: React.RefObject, } -function useCommonProps(modalProps: AddCommonModalProps) { +function useCommonProps() { const location = useTorrentLocation(); const [labels, setLabels] = useState([]); - const [labelsData, setLabelData] = useState({ - labels, - setLabels, - allLabels: modalProps.serverData.current.allLabels, - }); const [start, setStart] = useState(true); const [priority, setPriority] = useState(0); - useEffect(() => { - setLabelData({ - labels, - setLabels, - allLabels: modalProps.serverData.current.allLabels, - }); - }, [labels, modalProps.opened, modalProps.serverData]); - const props = useMemo(() => ({ location, - labelsData, + labels, + setLabels, start, setStart, priority, setPriority, - }), [location, labelsData, start, priority]); + }), [location, labels, start, priority]); - return { + return useMemo(() => ({ location, - labels, start, priority, props, - }; + }), [location, priority, props, start]); } function TabSwitchDropdown({ tabsRef }: { tabsRef: React.RefObject }) { @@ -147,6 +135,7 @@ function TabSwitchDropdown({ tabsRef }: { tabsRef: React.RefObject(""); useEffect(() => { @@ -166,14 +155,14 @@ export function AddMagnet(props: AddCommonModalProps) { useEffect(() => { if (magnetData !== undefined && magnetData.hash !== "") { - const torrent = props.serverData.current?.torrents.find((t) => t.hashString === magnetData.hash); + const torrent = serverData.torrents.find((t) => t.hashString === magnetData.hash); setExistingTorrent(torrent); } else { setExistingTorrent(undefined); } - }, [props.serverData, props.serverName, magnetData]); + }, [serverData, props.serverName, magnetData]); - const common = useCommonProps(props); + const common = useCommonProps(); const { close } = props; const addMutation = useAddTorrent( useCallback((response: any) => { @@ -211,7 +200,7 @@ export function AddMagnet(props: AddCommonModalProps) { { url: magnet, downloadDir: common.location.path, - labels: common.labels, + labels: common.props.labels, paused: !common.start, priority: common.priority, }, @@ -231,7 +220,7 @@ export function AddMagnet(props: AddCommonModalProps) { ); } close(); - }, [existingTorrent, close, addMutation, magnet, common.location, common.labels, common.start, common.priority, trackersMutation, magnetData]); + }, [existingTorrent, close, addMutation, magnet, common, trackersMutation, magnetData]); return ( - +