Skip to content

A modern, self-hosted backup solution for secure, encrypted backups across local and cloud storage.

feat: adds the ability to filter plans & persist sorting

towfiqiedb87694df71
diff --git a/frontend/src/components/Plan/FilterPlans/FilterPlans.module.scss b/frontend/src/components/Plan/FilterPlans/FilterPlans.module.scss
new file mode 100644
index 0000000..0885e70
--- /dev/null
+++ b/frontend/src/components/Plan/FilterPlans/FilterPlans.module.scss
@@ -0,0 +1,65 @@
+.filterItems {
+ position: relative;
+ .filterBtn {
+ display: flex;
+ align-items: center;
+ .filterCount {
+ background-color: var(--primary-color);
+ color: var(--primary-text-color);
+ padding: 1px 6px;
+ border-radius: 12px;
+ font-size: 10px;
+ }
+ &.filterBtnActive {
+ color: var(--primary-color);
+ }
+ }
+ .dropdown {
+ margin-top: -1px;
+ position: absolute;
+ z-index: 9999;
+ border: 1px solid var(--line-color);
+ background: var(--field-bg);
+ box-shadow: 0 0 15px var(--content-shadow-color);
+ width: calc(100% - 2px);
+ border-radius: 0 0 4px 4px;
+ overflow: auto;
+ font-size: 0.75rem;
+ right: 0;
+ width: max-content;
+ max-height: 320px;
+ border-radius: 6px;
+
+ .field {
+ padding: 12px 12px 0 12px;
+ label {
+ font-weight: 600;
+ margin-bottom: 4px;
+ display: block;
+ }
+ }
+ .footer {
+ border-top: 1px solid var(--line-color);
+ margin-top: 12px;
+ display: flex;
+ justify-content: space-around;
+ button:nth-child(2) {
+ padding: 6px 10px;
+ background: var(--primary-color-light);
+ color: var(--primary-color);
+ line-height: 1em;
+ height: fit-content;
+ border-radius: 4px;
+ margin: 8px;
+ font-weight: 600;
+ &:hover {
+ background: var(--primary-color);
+ color: var(--primary-text-color);
+ }
+ }
+ button:hover {
+ color: var(--primary-color);
+ }
+ }
+ }
+}
diff --git a/frontend/src/components/Plan/FilterPlans/FilterPlans.tsx b/frontend/src/components/Plan/FilterPlans/FilterPlans.tsx
new file mode 100644
index 0000000..0885309
--- /dev/null
+++ b/frontend/src/components/Plan/FilterPlans/FilterPlans.tsx
@@ -0,0 +1,126 @@
+import { useMemo, useState } from 'react';
+import classes from './FilterPlans.module.scss';
+import { Plan } from '../../..';
+import { Icon, MultiSelect } from '../..';
+
+type FilterPlansProps = {
+ onUpdate: (s: Record<PlanFilterTypes, string[]> | null) => void;
+ plans: Plan[];
+};
+export type PlanFilterTypes = 'devices' | 'tags' | 'storages' | 'methods';
+
+const defaultState = { devices: [], tags: [], storages: [], methods: [] };
+
+const FilterPlans = ({ onUpdate, plans }: FilterPlansProps) => {
+ const [showDropDown, setShowDropDown] = useState(false);
+ const [selected, setSelected] = useState<Record<PlanFilterTypes, string[]>>(() => {
+ const storedSettingsRaw = localStorage.getItem('plans_filter');
+ const storedSettings = storedSettingsRaw ? JSON.parse(storedSettingsRaw) : null;
+ return storedSettings || defaultState;
+ });
+
+ const hasFilters = Object.values(selected).some((arr) => arr.length > 0);
+
+ const filterableItems = useMemo(() => {
+ const items: Record<PlanFilterTypes, { label: string; value: string }[]> = { devices: [], tags: [], storages: [], methods: [] };
+ plans.forEach((plan) => {
+ plan.tags.forEach((tag) => {
+ if (!items.tags.find((t) => t.value === tag)) {
+ items.tags.push({ label: tag, value: tag });
+ }
+ });
+ if (!items.devices.find((t) => t.value === plan.device.id)) {
+ items.devices.push({ label: plan.device.name, value: plan.device.id });
+ }
+ if (!items.storages.find((t) => t.value === plan.storage.id)) {
+ items.storages.push({ label: plan.storage.name, value: plan.storage.id });
+ }
+ if (!items.methods.find((t) => t.value === plan.method)) {
+ items.methods.push({ label: plan.method, value: plan.method });
+ }
+ });
+ return items;
+ }, [plans]);
+
+ return (
+ <div className={classes.filterItems}>
+ <button
+ className={`${classes.filterBtn} ${showDropDown || hasFilters ? classes.filterBtnActive : ''}`}
+ onClick={() => setShowDropDown(!showDropDown)}
+ data-tooltip-id="appTooltip"
+ data-tooltip-content="Filter"
+ aria-label="Filter Plans"
+ data-tooltip-place="top"
+ data-tooltip-delay-show={500}
+ >
+ <Icon type="filter" size={18} />{' '}
+ {hasFilters && <span className={classes.filterCount}>{Object.values(selected).reduce((acc, arr) => acc + arr.length, 0)}</span>}
+ </button>
+ {showDropDown && (
+ <div className={`${classes.dropdown} styled__scrollbar`}>
+ {filterableItems.devices.length > 1 && (
+ <div className={classes.field}>
+ <label>Devices</label>
+ <MultiSelect
+ title="Devices"
+ fieldValue={selected.devices}
+ options={filterableItems.devices}
+ onUpdate={(devices) => setSelected((selected) => ({ ...selected, devices }))}
+ />
+ </div>
+ )}
+ <div className={classes.field}>
+ <label>Backup Type</label>
+ <MultiSelect
+ title="Backup Type"
+ fieldValue={selected.methods}
+ options={filterableItems.methods}
+ onUpdate={(methods) => setSelected((selected) => ({ ...selected, methods }))}
+ />
+ </div>
+ <div className={classes.field}>
+ <label>Storage</label>
+ <MultiSelect
+ title="Storages"
+ fieldValue={selected.storages}
+ options={filterableItems.storages}
+ onUpdate={(storages) => setSelected((selected) => ({ ...selected, storages }))}
+ />
+ </div>
+ <div className={classes.field}>
+ <label>Tags</label>
+ <MultiSelect
+ title="Tags"
+ fieldValue={selected.tags}
+ options={filterableItems.tags}
+ onUpdate={(tags) => setSelected((selected) => ({ ...selected, tags }))}
+ />
+ </div>
+ <div className={classes.footer}>
+ <button
+ onClick={() => {
+ localStorage.removeItem('plans_filter');
+ setSelected(defaultState);
+ onUpdate(null);
+ setShowDropDown(false);
+ }}
+ >
+ <Icon type="reload" /> Reset
+ </button>
+ <button
+ onClick={() => {
+ localStorage.setItem('plans_filter', JSON.stringify(selected));
+ onUpdate(selected);
+ setShowDropDown(false);
+ }}
+ >
+ <Icon type="check" size={12} /> Apply
+ </button>
+ </div>
+ </div>
+ )}
+ </div>
+ );
+};
+
+export default FilterPlans;
diff --git a/frontend/src/components/common/Icon/Icon.tsx b/frontend/src/components/common/Icon/Icon.tsx
index d060dfc..ca0797d 100644
--- a/frontend/src/components/common/Icon/Icon.tsx
+++ b/frontend/src/components/common/Icon/Icon.tsx
@@ -1163,6 +1163,15 @@ const Icon = ({ type, color = 'currentColor', size = 16, title = '', classes = '
></path>
</IconWrapper>
)}
+ {type === 'filter' && (
+ <IconWrapper size={size} viewBox="0 0 24 24">
+ <path
+ fill={color}
+ d="M15 19.88c.04.3-.06.62-.29.83a.996.996 0 0 1-1.41 0L9.29 16.7a.99.99 0 0 1-.29-.83v-5.12L4.21 4.62a1 1 0 0 1 .17-1.4c.19-.14.4-.22.62-.22h14c.22 0 .43.08.62.22a1 1 0 0 1 .17 1.4L15 10.75zM7.04 5L11 10.06v5.52l2 2v-7.53L16.96 5z"
+ ></path>
+ </IconWrapper>
+ )}
+
{type === 'notification' && (
<IconWrapper size={size} viewBox="0 0 24 24">
<g fill="none" stroke={color} strokeWidth={1.5}>
diff --git a/frontend/src/components/common/SortItems/SortItems.module.scss b/frontend/src/components/common/SortItems/SortItems.module.scss
index 2aec47c..2e1fb38 100644
--- a/frontend/src/components/common/SortItems/SortItems.module.scss
+++ b/frontend/src/components/common/SortItems/SortItems.module.scss
@@ -7,6 +7,7 @@
span {
opacity: 1;
color: inherit;
+ font-weight: 600;
}
}
@@ -18,11 +19,11 @@
background: var(--field-bg);
width: calc(100% - 2px);
border-radius: 0 0 4px 4px;
- max-height: 200px;
overflow: auto;
font-size: 0.75rem;
right: 0;
- width: 200px;
+ width: 210px;
+ max-height: 320px;
border-radius: 6px;
ul {
margin: 0;
diff --git a/frontend/src/components/common/SortItems/SortItems.tsx b/frontend/src/components/common/SortItems/SortItems.tsx
index 3397ff3..7815b48 100644
--- a/frontend/src/components/common/SortItems/SortItems.tsx
+++ b/frontend/src/components/common/SortItems/SortItems.tsx
@@ -2,18 +2,19 @@ import { useState } from 'react';
import Icon from '../Icon/Icon';
import classes from './SortItems.module.scss';
type SortItemsProps = {
+ id: string;
onSort: (s: string) => void;
options: { label: string; value: string }[];
};
-const SortItems = ({ options, onSort }: SortItemsProps) => {
+const SortItems = ({ id, options, onSort }: SortItemsProps) => {
const [showDropDown, setshowDropDown] = useState(false);
- const [selected, setsSelected] = useState('');
+ const [selected, setsSelected] = useState(localStorage.getItem(id) || '');
const selectedLabel = options.find((item) => item.value === selected);
return (
<div className={classes.sortItems}>
<button
- className={selected ? classes.sortActive : ''}
+ className={selected || showDropDown ? classes.sortActive : ''}
onClick={() => setshowDropDown(!showDropDown)}
data-tooltip-id="appTooltip"
data-tooltip-content="Sort"
@@ -34,6 +35,7 @@ const SortItems = ({ options, onSort }: SortItemsProps) => {
onClick={() => {
setsSelected(item.value);
onSort(item.value);
+ localStorage.setItem(id, item.value);
setshowDropDown(false);
}}
className={`${selected && item.value === selected ? classes.selectedItem : ''}`}
@@ -46,6 +48,7 @@ const SortItems = ({ options, onSort }: SortItemsProps) => {
onClick={() => {
setsSelected('');
onSort('');
+ localStorage.removeItem(id);
setshowDropDown(false);
}}
>
diff --git a/frontend/src/components/common/form/MultiSelect/MultiSelect.module.scss b/frontend/src/components/common/form/MultiSelect/MultiSelect.module.scss
index 858b28e..7230314 100644
--- a/frontend/src/components/common/form/MultiSelect/MultiSelect.module.scss
+++ b/frontend/src/components/common/form/MultiSelect/MultiSelect.module.scss
@@ -73,6 +73,7 @@
.dropBtn {
background: transparent;
color: var(--icon-color);
+ padding: 0;
}
}
diff --git a/frontend/src/components/index.ts b/frontend/src/components/index.ts
index 2fb3552..5228e06 100644
--- a/frontend/src/components/index.ts
+++ b/frontend/src/components/index.ts
@@ -27,6 +27,7 @@ export { default as PathPicker } from './common/PathPicker/PathPicker';
export { default as SearchItems } from './common/SearchItems/SearchItems';
export { default as SidePanel } from './common/SidePanel/SidePanel';
export { default as SortItems } from './common/SortItems/SortItems';
+export { default as FilterPlans } from './Plan/FilterPlans/FilterPlans';
export { default as StatusLabel } from './common/StatusLabel/StatusLabel';
export { default as Tabs, TabList, TabPanel, Tab } from './common/Tabs/Tabs';
export { default as TagsFilter } from './common/TagsFilter/TagsFilter';
diff --git a/frontend/src/routes/Plans/Plans.tsx b/frontend/src/routes/Plans/Plans.tsx
index e6d1659..cccccb8 100644
--- a/frontend/src/routes/Plans/Plans.tsx
+++ b/frontend/src/routes/Plans/Plans.tsx
@@ -11,14 +11,14 @@ import { Plan } from '../../@types/plans';
import { useComponentOverride } from '../../context/ComponentOverrideContext';
import ItemsLayout from '../../components/common/ItemsLayout/ItemsLayout';
import SkeletonItems from '../../components/Skeleton/SkeletonItems';
+import { getIntervalMinutes } from '../../utils';
+import FilterPlans, { PlanFilterTypes } from '../../components/Plan/FilterPlans/FilterPlans';
const Plans = () => {
const [showSidePanel, setShowSidePanel] = useState(false);
const [plans, setPlans] = useState<Plan[]>([]);
const [plansLayout, setPlansLayout] = useState<'list' | 'grid'>(() => (localStorage.getItem('plans_layout') === 'grid' ? 'grid' : 'list'));
const { data, isLoading } = useGetPlans();
- // const data = { result: [] };
- // const isLoading = true;
const fetchedPlans: Plan[] = data?.result || [];
@@ -27,10 +27,19 @@ const Plans = () => {
useEffect(() => {
if (data?.result) {
setPlans(data.result);
+ const sortSetting = localStorage.getItem('plans_sort');
+ const filterSettingsRaw = localStorage.getItem('plans_filter');
+ const filterSettings = filterSettingsRaw ? JSON.parse(filterSettingsRaw) : null;
+ if (filterSettings) {
+ applyFilters(filterSettings, data.result);
+ }
+ if (sortSetting) {
+ sortPlans(sortSetting, data.result);
+ }
}
}, [data]);
- console.log('plans :', plans);
+ // console.log('plans :', plans);
const allTags = useMemo(() => {
const tags: string[] = [];
@@ -63,28 +72,73 @@ const Plans = () => {
}
};
- const sortPlans = (sortBy: string) => {
+ const sortPlans = (sortBy: string, plansToSort?: Plan[]) => {
+ const thePlans = plansToSort && plansToSort.length > 0 ? plansToSort : plans;
switch (sortBy) {
case 'title_asc':
- setPlans([...plans].sort((a, b) => a.title.localeCompare(b.title)));
+ setPlans([...thePlans].sort((a, b) => a.title.localeCompare(b.title)));
break;
case 'title_desc':
- setPlans([...plans].sort((a, b) => b.title.localeCompare(a.title)));
+ setPlans([...thePlans].sort((a, b) => b.title.localeCompare(a.title)));
break;
case 'created_asc':
- setPlans([...plans].sort((a, b) => Date.parse(a.createdAt) - Date.parse(b.createdAt)));
+ setPlans([...thePlans].sort((a, b) => Date.parse(a.createdAt) - Date.parse(b.createdAt)));
break;
case 'created_desc':
- setPlans([...plans].sort((a, b) => Date.parse(b.createdAt) - Date.parse(a.createdAt)));
+ setPlans([...thePlans].sort((a, b) => Date.parse(b.createdAt) - Date.parse(a.createdAt)));
+ break;
+ case 'size_asc':
+ setPlans([...thePlans].sort((a, b) => a.stats.size - b.stats.size));
+ break;
+ case 'size_desc':
+ setPlans([...thePlans].sort((a, b) => b.stats.size - a.stats.size));
+ break;
+ case 'run_frequency_asc':
+ setPlans([...thePlans].sort((a, b) => getIntervalMinutes(a.settings.interval) - getIntervalMinutes(b.settings.interval)));
+ break;
+ case 'run_frequency_desc':
+ setPlans([...thePlans].sort((a, b) => getIntervalMinutes(b.settings.interval) - getIntervalMinutes(a.settings.interval)));
break;
case '':
- setPlans([...plans]);
+ setPlans([...fetchedPlans]);
break;
default:
break;
}
};
+ const applyFilters = (filter: Record<PlanFilterTypes, string[]> | null, plansToSort?: Plan[]) => {
+ if (!filter) {
+ setPlans(fetchedPlans);
+ const sortSetting = localStorage.getItem('plans_sort');
+ if (sortSetting) {
+ sortPlans(sortSetting, fetchedPlans);
+ }
+ return;
+ } else {
+ let filteredPlans = plansToSort && plansToSort.length > 0 ? plansToSort : plans;
+
+ if (filter.devices.length > 0) {
+ filteredPlans = filteredPlans.filter((plan) => filter.devices.includes(plan.device.id));
+ }
+ if (filter.methods.length > 0) {
+ filteredPlans = filteredPlans.filter((plan) => filter.methods.includes(plan.method));
+ }
+ if (filter.storages.length > 0) {
+ filteredPlans = filteredPlans.filter((plan) => filter.storages.includes(plan.storage.id));
+ }
+ if (filter.tags.length > 0) {
+ filteredPlans = filteredPlans.filter((plan) => plan.tags.some((tag) => filter.tags.includes(tag)));
+ }
+
+ setPlans(filteredPlans);
+ const sortSetting = localStorage.getItem('plans_sort');
+ if (sortSetting) {
+ sortPlans(sortSetting, filteredPlans);
+ }
+ }
+ };
+
return (
<div className={classes.plans}>
<PageHeader
@@ -94,17 +148,22 @@ const Plans = () => {
buttonAction={() => setShowSidePanel(true)}
rightSection={
<>
- <TagsFilter tags={allTags} onSelect={filterByTags} />
<SearchItems onSearch={(term) => searchPlans(term)} itemName="Plans" />
<SortItems
+ id={'plans_sort'}
options={[
{ label: 'Title (A to Z)', value: 'title_asc' },
{ label: 'Title (Z to A)', value: 'title_desc' },
+ { label: 'Size (Smallest First)', value: 'size_asc' },
+ { label: 'Size (Largest First)', value: 'size_desc' },
{ label: 'Date Created (Oldest First)', value: 'created_asc' },
{ label: 'Date Created (Newest First)', value: 'created_desc' },
+ { label: 'Run Frequency (Shortest First)', value: 'run_frequency_asc' },
+ { label: 'Run Frequency (Longest First)', value: 'run_frequency_desc' },
]}
onSort={(sortBy) => sortPlans(sortBy)}
/>
+ <FilterPlans plans={fetchedPlans} onUpdate={(filterSettings) => applyFilters(filterSettings)} />
<ItemsLayout onChange={(value) => setPlansLayout(value)} type="plans" value={plansLayout} />
</>
}
diff --git a/frontend/src/utils/helpers.ts b/frontend/src/utils/helpers.ts
index 5ee2ce6..042fc3b 100644
--- a/frontend/src/utils/helpers.ts
+++ b/frontend/src/utils/helpers.ts
@@ -503,3 +503,26 @@ export const secondsToMinutes = (seconds: number) => {
}
return seconds > 60 ? `${Math.round(seconds / 60)} min` : `${seconds} sec`;
};
+
+export const getIntervalMinutes = (interval: PlanInterval): number => {
+ switch (interval.type) {
+ case 'minutes':
+ return interval.minutes || 5;
+ case 'hours':
+ return parseInt(interval.hours || '1') * 60;
+ case 'hourly':
+ return 60;
+ case 'daily':
+ return 1440;
+ case 'days': {
+ const dayCount = interval.days?.split(',').filter(Boolean).length || 1;
+ return Math.round((7 / dayCount) * 1440);
+ }
+ case 'weekly':
+ return 10080;
+ case 'monthly':
+ return 43200;
+ default:
+ return 1440;
+ }
+};