Skip to content

Commit

Permalink
Merge pull request #67 from ikatson/react-zustand
Browse files Browse the repository at this point in the history
Use react zustand to reduce re-renders
  • Loading branch information
ikatson authored Dec 19, 2023
2 parents 3dc2e3e + 89bbcd2 commit 1157866
Show file tree
Hide file tree
Showing 16 changed files with 263 additions and 99 deletions.
34 changes: 25 additions & 9 deletions crates/librqbit/webui/dist/assets/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion crates/librqbit/webui/dist/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"css": [
"assets/index-d46108e9.css"
],
"file": "assets/index-c93d50ee.js",
"file": "assets/index-e6c23bc1.js",
"isEntry": true,
"src": "index.html"
}
Expand Down
41 changes: 38 additions & 3 deletions crates/librqbit/webui/node_modules/.package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 40 additions & 4 deletions crates/librqbit/webui/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion crates/librqbit/webui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"lodash.debounce": "^4.0.8",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-icons": "^4.12.0"
"react-icons": "^4.12.0",
"zustand": "^4.4.7"
},
"devDependencies": {
"@types/lodash.debounce": "^4.0.9",
Expand Down
29 changes: 15 additions & 14 deletions crates/librqbit/webui/src/components/RootContent.tsx
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
import { useContext } from "react";
import { TorrentId, ErrorDetails as ApiErrorDetails } from "../api-types";
import { AppContext } from "../context";
import { TorrentsList } from "./TorrentsList";
import { ErrorComponent } from "./ErrorComponent";
import { useTorrentStore } from "../stores/torrentStore";
import { useErrorStore } from "../stores/errorStore";

export const RootContent = (props: {}) => {
let closeableError = useErrorStore((state) => state.closeableError);
let setCloseableError = useErrorStore((state) => state.setCloseableError);
let otherError = useErrorStore((state) => state.otherError);
let torrents = useTorrentStore((state) => state.torrents);
let torrentsInitiallyLoading = useTorrentStore(
(state) => state.torrentsInitiallyLoading
);

export const RootContent = (props: {
closeableError: ApiErrorDetails | null;
otherError: ApiErrorDetails | null;
torrents: Array<TorrentId> | null;
torrentsLoading: boolean;
}) => {
let ctx = useContext(AppContext);
return (
<div className="container mx-auto">
<ErrorComponent
error={props.closeableError}
remove={() => ctx.setCloseableError(null)}
error={closeableError}
remove={() => setCloseableError(null)}
/>
<ErrorComponent error={props.otherError} />
<TorrentsList torrents={props.torrents} loading={props.torrentsLoading} />
<ErrorComponent error={otherError} />
<TorrentsList torrents={torrents} loading={torrentsInitiallyLoading} />
</div>
);
};
14 changes: 6 additions & 8 deletions crates/librqbit/webui/src/components/buttons/TorrentActions.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import { useContext, useState } from "react";
import { TorrentStats } from "../../api-types";
import {
AppContext,
APIContext,
RefreshTorrentStatsContext,
} from "../../context";
import { APIContext, RefreshTorrentStatsContext } from "../../context";
import { IconButton } from "./IconButton";
import { DeleteTorrentModal } from "../modal/DeleteTorrentModal";
import { FaPause, FaPlay, FaTrash } from "react-icons/fa";
import { useErrorStore } from "../../stores/errorStore";

export const TorrentActions: React.FC<{
id: number;
Expand All @@ -23,7 +20,8 @@ export const TorrentActions: React.FC<{
const canPause = state == "live";
const canUnpause = state == "paused" || state == "error";

const ctx = useContext(AppContext);
const setCloseableError = useErrorStore((state) => state.setCloseableError);

const API = useContext(APIContext);

const unpause = () => {
Expand All @@ -34,7 +32,7 @@ export const TorrentActions: React.FC<{
refreshCtx.refresh();
},
(e) => {
ctx.setCloseableError({
setCloseableError({
text: `Error starting torrent id=${id}`,
details: e,
});
Expand All @@ -51,7 +49,7 @@ export const TorrentActions: React.FC<{
refreshCtx.refresh();
},
(e) => {
ctx.setCloseableError({
setCloseableError({
text: `Error pausing torrent id=${id}`,
details: e,
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { useContext, useState } from "react";
import { AppContext, APIContext } from "../../context";
import { APIContext } from "../../context";
import { ErrorWithLabel } from "../../rqbit-web";
import { ErrorComponent } from "../ErrorComponent";
import { Spinner } from "../Spinner";
import { Modal } from "./Modal";
import { ModalBody } from "./ModalBody";
import { ModalFooter } from "./ModalFooter";
import { Button } from "../buttons/Button";
import { useTorrentStore } from "../../stores/torrentStore";

export const DeleteTorrentModal: React.FC<{
id: number;
Expand All @@ -20,8 +21,8 @@ export const DeleteTorrentModal: React.FC<{
const [error, setError] = useState<ErrorWithLabel | null>(null);
const [deleting, setDeleting] = useState(false);

const ctx = useContext(AppContext);
const API = useContext(APIContext);
const refreshTorrents = useTorrentStore((state) => state.refreshTorrents);

const close = () => {
setDeleteFiles(false);
Expand All @@ -37,7 +38,7 @@ export const DeleteTorrentModal: React.FC<{

call(id)
.then(() => {
ctx.refreshTorrents();
refreshTorrents();
close();
})
.catch((e) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
import { useCallback, useContext, useEffect, useState } from "react";
import { useContext, useEffect, useState } from "react";
import { AddTorrentResponse, AddTorrentOptions } from "../../api-types";
import { AppContext, APIContext } from "../../context";
import { APIContext } from "../../context";
import { ErrorComponent } from "../ErrorComponent";
import { formatBytes } from "../../helper/formatBytes";
import { ErrorWithLabel } from "../../rqbit-web";
import { Spinner } from "../Spinner";
import { Modal } from "./Modal";
import { ModalBody } from "./ModalBody";
import { ModalFooter } from "./ModalFooter";
import { Button } from "../buttons/Button";
import { FormCheckbox } from "../forms/FormCheckbox";
import { Fieldset } from "../forms/Fieldset";
import { FormInput } from "../forms/FormInput";
import { Form } from "../forms/Form";
import { FileListInput } from "../FileListInput";
import { useTorrentStore } from "../../stores/torrentStore";

export const FileSelectionModal = (props: {
onHide: () => void;
Expand All @@ -35,11 +34,10 @@ export const FileSelectionModal = (props: {
const [uploadError, setUploadError] = useState<ErrorWithLabel | null>(null);
const [unpopularTorrent, setUnpopularTorrent] = useState(false);
const [outputFolder, setOutputFolder] = useState<string>("");
const ctx = useContext(AppContext);
const refreshTorrents = useTorrentStore((state) => state.refreshTorrents);
const API = useContext(APIContext);

useEffect(() => {
console.log(listTorrentResponse);
setSelectedFiles(
new Set(listTorrentResponse?.details.files.map((_, i) => i))
);
Expand Down Expand Up @@ -77,7 +75,7 @@ export const FileSelectionModal = (props: {
.then(
() => {
onHide();
ctx.refreshTorrents();
refreshTorrents();
},
(e) => {
setUploadError({ text: "Error starting torrent", details: e });
Expand Down
4 changes: 0 additions & 4 deletions crates/librqbit/webui/src/context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,4 @@ export const APIContext = createContext<RqbitAPI>({
return null;
},
});
export const AppContext = createContext<ContextType>({
setCloseableError: (_) => {},
refreshTorrents: () => {},
});
export const RefreshTorrentStatsContext = createContext({ refresh: () => {} });
12 changes: 6 additions & 6 deletions crates/librqbit/webui/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ const RootWithVersion = () => {
}, []);

return (
<StrictMode>
<APIContext.Provider value={API}>
<RqbitWebUI title={title} />
</APIContext.Provider>
</StrictMode>
<APIContext.Provider value={API}>
<RqbitWebUI title={title} />
</APIContext.Provider>
);
};

ReactDOM.createRoot(document.getElementById("app") as HTMLInputElement).render(
<RootWithVersion />
<StrictMode>
<RootWithVersion />
</StrictMode>
);
Loading

0 comments on commit 1157866

Please sign in to comment.