Skip to content

Commit

Permalink
Implement preconfigured labels
Browse files Browse the repository at this point in the history
These labels always are shown in filters panel and in suggestions
for torrents, regardless if they are present on the server.

Issue #175
  • Loading branch information
qu1ck committed Apr 1, 2024
1 parent ba4d52e commit aca0def
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 13 deletions.
3 changes: 2 additions & 1 deletion src/components/filters.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,7 @@ export const Filters = React.memo(function Filters({ torrents, currentFilters, s
const [labels, trackers] = useMemo(() => {
const labels: Record<string, number> = {};
const trackers: Record<string, number> = {};
config.values.interface.preconfiguredLabels.forEach((label) => { labels[label] = 0; });

torrents.forEach((t) => t.labels?.forEach((l: string) => {
if (!(l in labels)) labels[l] = 0;
Expand All @@ -343,7 +344,7 @@ export const Filters = React.memo(function Filters({ torrents, currentFilters, s
});

return [labels, trackers];
}, [torrents]);
}, [config, torrents]);

const [sections, setSections] = useReducer(
(_: SectionsVisibility<FilterSectionName>, sections: SectionsVisibility<FilterSectionName>) => {
Expand Down
7 changes: 4 additions & 3 deletions src/components/modals/common.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ export function TorrentLocation(props: LocationData) {
);
}

function Label({
export function Label({
label,
onRemove,
classNames,
Expand Down Expand Up @@ -243,14 +243,15 @@ interface TorrentLabelsProps {
}

export function TorrentLabels(props: TorrentLabelsProps) {
const config = useContext(ConfigContext);
const serverData = useServerTorrentData();

const initialLabelset = useMemo(() => {
const labels = new Set<string>();
const labels = new Set<string>(config.values.interface.preconfiguredLabels);
serverData.torrents.forEach((t) => t.labels?.forEach((l: string) => labels.add(l)));

return Array.from(labels).sort();
}, [serverData.torrents]);
}, [config, serverData.torrents]);

const [data, setData] = useState<string[]>(initialLabelset);

Expand Down
41 changes: 32 additions & 9 deletions src/components/modals/interfacepanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@

import React, { useCallback, useEffect, useState } from "react";
import type { ColorScheme } from "@mantine/core";
import { Checkbox, Grid, NativeSelect, NumberInput, Textarea, useMantineTheme } from "@mantine/core";
import { Checkbox, Grid, MultiSelect, NativeSelect, NumberInput, Textarea, useMantineTheme } from "@mantine/core";
import type { UseFormReturnType } from "@mantine/form";
import ColorChooser from "components/colorchooser";
import { useGlobalStyleOverrides } from "themehooks";
import { DeleteTorrentDataOptions, type ColorSetting, type DeleteTorrentDataOption, type StyleOverrides } from "config";
import { ColorSchemeToggle } from "components/miscbuttons";
import { Label } from "./common";
const { TAURI, invoke } = await import(/* webpackChunkName: "taurishim" */"taurishim");

export interface InterfaceFormValues {
Expand All @@ -33,6 +34,7 @@ export interface InterfaceFormValues {
skipAddDialog: boolean,
deleteTorrentData: DeleteTorrentDataOption,
numLastSaveDirs: number,
preconfiguredLabels: string[],
defaultTrackers: string[],
},
}
Expand Down Expand Up @@ -91,6 +93,10 @@ export function InterfaceSettigsPanel<V extends InterfaceFormValues>(props: { fo
? { color: "dark", shade: 7, computed: theme.colors.dark[7] }
: { color: "gray", shade: 0, computed: theme.colors.gray[0] };

const setPreconfiguredLabels = useCallback((labels: string[]) => {
setFieldValue("interface.preconfiguredLabels", labels);
}, [setFieldValue]);

return (
<Grid align="center">
<Grid.Col span={1}>
Expand All @@ -114,28 +120,45 @@ export function InterfaceSettigsPanel<V extends InterfaceFormValues>(props: { fo
<Grid.Col span={1}>
<ColorChooser value={style[theme.colorScheme].backgroundColor ?? defaultBg} onChange={setBgColor} />
</Grid.Col>
<Grid.Col>
<Checkbox label="Skip add torrent dialog"
{...props.form.getInputProps("interface.skipAddDialog", { type: "checkbox" })} />
</Grid.Col>
<Grid.Col span={8}>
<Grid.Col span={4}>
Delete torrent data
</Grid.Col>
<Grid.Col span={4}>
<NativeSelect data={DeleteTorrentDataOptions as unknown as string[]}
value={props.form.values.interface.deleteTorrentData}
onChange={(e) => { setFieldValue("interface.deleteTorrentData", e.target.value); }} />
</Grid.Col>
<Grid.Col span={8}>Max number of saved download directories</Grid.Col>
<Grid.Col span={4}>
<Checkbox label="Skip add torrent dialog"
{...props.form.getInputProps("interface.skipAddDialog", { type: "checkbox" })} />
</Grid.Col>
<Grid.Col span={6}>Max number of saved download directories</Grid.Col>
<Grid.Col span={2}>
<NumberInput
min={1}
max={100}
{...props.form.getInputProps("interface.numLastSaveDirs")} />
</Grid.Col>
<Grid.Col span={2}></Grid.Col>
<Grid.Col span={4}></Grid.Col>
<Grid.Col>
<MultiSelect
data={props.form.values.interface.preconfiguredLabels}
value={props.form.values.interface.preconfiguredLabels}
onChange={setPreconfiguredLabels}
label="Preconfigured labels"
withinPortal
searchable
creatable
getCreateLabel={(query) => `+ Add ${query}`}
onCreate={(query) => {
setPreconfiguredLabels([...props.form.values.interface.preconfiguredLabels, query]);
return query;
}}
valueComponent={Label}
/>
</Grid.Col>
<Grid.Col>
<Textarea minRows={10}
<Textarea minRows={6}
label="Default tracker list"
value={props.form.values.interface.defaultTrackers.join("\n")}
onChange={(e) => {
Expand Down
2 changes: 2 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ interface Settings {
deleteTorrentData: DeleteTorrentDataOption,
deleteTorrentDataSelection: boolean,
numLastSaveDirs: number,
preconfiguredLabels: string[],
defaultTrackers: string[],
styleOverrides: StyleOverrides,
},
Expand Down Expand Up @@ -269,6 +270,7 @@ const DefaultSettings: Settings = {
deleteTorrentData: "default off",
deleteTorrentDataSelection: false,
numLastSaveDirs: 20,
preconfiguredLabels: [],
defaultTrackers: [...DefaultTrackerList],
styleOverrides: {
dark: {},
Expand Down

0 comments on commit aca0def

Please sign in to comment.