Skip to content

Commit

Permalink
Move active calculation into torrentProgressbarStyle
Browse files Browse the repository at this point in the history
  • Loading branch information
melyux committed Aug 29, 2024
1 parent a593949 commit f2a28c0
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 19 deletions.
8 changes: 3 additions & 5 deletions src/components/details.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import React, { memo, useCallback, useContext, useEffect, useMemo, useState } from "react";
import type { Torrent, TrackerStats } from "../rpc/torrent";
import { bytesToHumanReadableStr, ensurePathDelimiter, fileSystemSafeName, secondsToHumanReadableStr, timestampToDateString, torrentProgressbarVariant } from "../trutil";
import { bytesToHumanReadableStr, ensurePathDelimiter, fileSystemSafeName, secondsToHumanReadableStr, timestampToDateString, torrentProgressbarStyle } from "../trutil";
import { FileTreeTable, useUnwantedFiles } from "./tables/filetreetable";
import { PiecesCanvas } from "./piecescanvas";
import { ProgressBar } from "./progressbar";
Expand Down Expand Up @@ -59,17 +59,15 @@ function DownloadBar(props: { torrent: Torrent }) {
const config = useContext(ConfigContext);
const now = Math.floor(percent * 1000);
const nowStr = `${prefix}: ${now / 10}%`;
const active = props.torrent.rateDownload > 0 || props.torrent.rateUpload > 0;
const variant = torrentProgressbarVariant(props, config, active);
const progressbarStyle = torrentProgressbarStyle(props.torrent, config);

return (
<Box w="100%" my="0.5rem">
<ProgressBar
now={now}
max={1000}
label={nowStr}
animate={config.values.interface.animatedProgressbars && active}
variant={variant}
{...progressbarStyle}
/>
</Box>
);
Expand Down
9 changes: 4 additions & 5 deletions src/components/tables/torrenttable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { useServerTorrentData, useServerRpcVersion, useServerSelectedTorrents }
import type { TorrentAllFieldsType, TorrentFieldsType } from "rpc/transmission";
import { PriorityColors, PriorityStrings, Status, StatusStrings, TorrentMinimumFields } from "rpc/transmission";
import type { ColumnDef, VisibilityState } from "@tanstack/react-table";
import { bytesToHumanReadableStr, fileSystemSafeName, modKeyString, pathMapFromServer, secondsToHumanReadableStr, timestampToDateString, torrentProgressbarVariant } from "trutil";
import { bytesToHumanReadableStr, fileSystemSafeName, modKeyString, pathMapFromServer, secondsToHumanReadableStr, timestampToDateString, torrentProgressbarStyle } from "trutil";
import { ProgressBar } from "../progressbar";
import type { AccessorFn, CellContext } from "@tanstack/table-core";
import type { TableSelectReducer } from "./common";
Expand Down Expand Up @@ -335,14 +335,13 @@ function ByteRateField(props: TableFieldProps) {
function PercentBarField(props: TableFieldProps) {
const config = useContext(ConfigContext);
const now = props.torrent[props.fieldName] * 100;
const active = props.torrent.rateDownload > 0 || props.torrent.rateUpload > 0;
const variant = torrentProgressbarVariant(props, config, active);
const progressbarStyle = torrentProgressbarStyle(props.torrent, config);

return <ProgressBar
now={now}
className="white-outline"
animate={config.values.interface.animatedProgressbars && active}
variant={variant} />;
{...progressbarStyle}
/>;
}

const Columns = AllFields.map((f): ColumnDef<Torrent> => {
Expand Down
20 changes: 11 additions & 9 deletions src/trutil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,32 +235,34 @@ export function * chainedIterables<T>(...iterables: Array<Iterable<T>>) {
for (const iterable of iterables) yield * iterable;
}

export function torrentProgressbarVariant(props: { torrent: Torrent }, config: Config, active: boolean) {
export function torrentProgressbarStyle(torrent: Torrent, config: Config) {
const active = torrent.rateDownload > 0 || torrent.rateUpload > 0;
const animate = config.values.interface.animatedProgressbars && active;
let variant: ProgressBarVariant = "default";

if (config.values.interface.colorfulProgressbars) {
if ((props.torrent.error !== undefined && props.torrent.error > 0) || props.torrent.cachedError !== "") {
if ((torrent.error !== undefined && torrent.error > 0) || torrent.cachedError !== "") {
variant = "red";
} else {
if (!config.values.interface.animatedProgressbars) {
if (active) variant = "green";
} else {
if (props.torrent.status === Status.stopped && props.torrent.sizeWhenDone > 0) {
if (props.torrent.leftUntilDone === 0) {
if (torrent.status === Status.stopped && torrent.sizeWhenDone > 0) {
if (torrent.leftUntilDone === 0) {
variant = "dark-green";
} else {
variant = "yellow";
}
} else if (props.torrent.status === Status.seeding) {
} else if (torrent.status === Status.seeding) {
variant = "green";
} else if (props.torrent.status === Status.queuedToVerify ||
props.torrent.status === Status.queuedToDownload ||
props.torrent.status === Status.queuedToSeed) {
} else if (torrent.status === Status.queuedToVerify ||
torrent.status === Status.queuedToDownload ||
torrent.status === Status.queuedToSeed) {
variant = "grey";
}
}
}
}

return variant;
return { animate, variant };
}

0 comments on commit f2a28c0

Please sign in to comment.