Skip to content

Commit

Permalink
feat: restore select all targets button (scatter) (#109)
Browse files Browse the repository at this point in the history
* feat: initial toggle all ui + logic (wip)

* feat: implement select all feature
  • Loading branch information
jbrodriguez authored Dec 22, 2024
1 parent 753f9df commit 6624def
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 5 deletions.
15 changes: 13 additions & 2 deletions ui/src/flows/scatter/select/targets.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,35 @@ import {
useScatterSource,
useScatterTargets,
useScatterActions,
useScatterAllTargetsChecked,
} from '~/state/scatter';
import { Checkbox } from '~/shared/checkbox/checkbox';
import { Disk } from '~/shared/disk/disk';
import { Disk as IDisk } from '~/types';
import { Toggle } from './toggle';

export const Targets: React.FunctionComponent = () => {
const disks = useUnraidDisks();
const source = useScatterSource();
const targets = useScatterTargets();
const { toggleTarget } = useScatterActions();
const allChecked = useScatterAllTargetsChecked();
const { toggleTarget, toggleAll } = useScatterActions();

const visible = source !== '';
const elegible = disks.filter((disk) => disk.name !== source);

const onCheck = (disk: IDisk) => () => toggleTarget(disk.name);
const onToggleAll = () => toggleAll(disks.map((d) => d.name));

return (
<Panel title="Target Disk(s)">
<Panel
title="Target Disk(s)"
subtitle={
visible ? (
<Toggle allChecked={allChecked} onCheck={onToggleAll} />
) : undefined
}
>
{visible &&
elegible.map((disk) => (
<div className="flex flex-row items-center">
Expand Down
19 changes: 19 additions & 0 deletions ui/src/flows/scatter/select/toggle.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import React from 'react';
import { Checkbox } from '~/shared/checkbox/checkbox';

interface ToggleProps {
allChecked: boolean;
onCheck: () => void;
}

export const Toggle: React.FunctionComponent<ToggleProps> = ({
allChecked,
onCheck,
}) => {
return (
<div className="flex flex-row items-center gap-1">
<Checkbox checked={allChecked} onCheck={onCheck} />
<span className="text-slate-500 dark:text-gray-500">Select All</span>
</div>
);
};
11 changes: 8 additions & 3 deletions ui/src/shared/panel/panel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ interface PanelProps {
title?: string;
children: React.ReactNode;
scrollToTop?: boolean;
subtitle?: React.ReactNode;
}

export const Panel: React.FunctionComponent<PanelProps> = ({
title = '',
children,
scrollToTop = false,
subtitle = null,
}) => {
const ref = React.useRef<HTMLDivElement>(null);

Expand All @@ -25,9 +27,12 @@ export const Panel: React.FunctionComponent<PanelProps> = ({
<div className="h-full flex flex-col bg-neutral-100 dark:bg-gray-950">
{title.length > 0 ? (
<div className="flex flex-col pt-2 px-2">
<h1 className="text-lg text-slate-500 dark:text-gray-500 pb-2">
{title}
</h1>
<div className="flex flex-row gap-4 items-start justify-start pb-2">
<h1 className="text-lg text-slate-500 dark:text-gray-500">
{title}
</h1>
{subtitle}
</div>
<hr className="border-slate-300 dark:border-gray-700" />
</div>
) : null}
Expand Down
23 changes: 23 additions & 0 deletions ui/src/state/scatter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ interface ScatterStore {
targets: Targets;
tree: Nodes;
binDisk: string;
allTargetsChecked: boolean;
actions: {
setSource: (source: string) => Promise<void>;
loadBranch: (node: Node) => Promise<void>;
toggleSelected: (node: Node) => void;
toggleTarget: (name: string) => void;
toggleAll: (names: string[]) => void;
setBinDisk: (binDisk: string) => void;
};
}
Expand Down Expand Up @@ -45,6 +47,7 @@ export const useScatterStore = create<ScatterStore>()(
tree: { root: decorateNode(rootNode as Node) },
logs: [],
binDisk: '',
allTargetsChecked: false,
actions: {
setSource: async (source: string) => {
const loader = decorateNode({ ...loaderNode } as Node);
Expand Down Expand Up @@ -171,6 +174,24 @@ export const useScatterStore = create<ScatterStore>()(
}

delete state.targets[name];
state.allTargetsChecked = false;
});
},
toggleAll: (names: string[]) => {
set((state) => {
state.allTargetsChecked = !state.allTargetsChecked;

for (let i = 0; i < names.length; i++) {
if (names[i] === state.source) {
continue;
}

if (state.allTargetsChecked) {
state.targets[names[i]] = true;
} else {
delete state.targets[names[i]];
}
}
});
},
setBinDisk: (binDisk: string) => {
Expand All @@ -195,3 +216,5 @@ export const useScatterTargets = () =>
useScatterStore((state) => state.targets);
export const useScatterBinDisk = () =>
useScatterStore((state) => state.binDisk);
export const useScatterAllTargetsChecked = () =>
useScatterStore((state) => state.allTargetsChecked);

0 comments on commit 6624def

Please sign in to comment.