Skip to content

Commit 88dc0b8

Browse files
use query param for alert search
1 parent aeefc2c commit 88dc0b8

3 files changed

Lines changed: 18 additions & 12 deletions

File tree

src/atoms/alertAtom.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,4 @@ export const alertAtom = atom(initialState);
1717
export const alertHowManyAtom = atom({
1818
howManyAlerts: 0,
1919
howManyAlertSoft: 0,
20-
});
21-
22-
export const alertSearchTextAtom = atom('');
20+
});

src/components/alerts/AlertItems.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ import QuietFor from './QuietFor';
2424

2525
import { Alert } from 'types/hostAndServiceTypes';
2626
import { ClientSettings } from 'types/settings';
27-
import { useAtomValue } from 'jotai';
28-
import { alertSearchTextAtom } from '../../atoms/alertAtom';
27+
import { useQueryParams } from '../../hooks/useQueryParams';
2928
// CSS
3029
import '../animation.css';
3130
import '../services/ServiceItems.css';
@@ -39,7 +38,8 @@ interface AlertItemsProps {
3938

4039
const AlertItems = ({ items, settings, isDemoMode }: AlertItemsProps) => {
4140

42-
const alertSearchText = useAtomValue(alertSearchTextAtom);
41+
const queryParams = useQueryParams();
42+
const alertSearchText = queryParams.get('alertSearch') || '';
4343

4444
const [howManyToRender, setHowManyToRender] = useState(100);
4545
const pageSize = 100;

src/components/alerts/AlertSection.tsx

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ import * as motion from "motion/react-client";
2222
// State Management
2323
import { useAtom, useAtomValue } from 'jotai';
2424
import { bigStateAtom, clientSettingsAtom, clientSettingsInitial } from '../../atoms/settingsState';
25-
import { alertIsFetchingAtom, alertAtom, alertHowManyAtom, alertSearchTextAtom } from '../../atoms/alertAtom';
25+
import { alertIsFetchingAtom, alertAtom, alertHowManyAtom } from '../../atoms/alertAtom';
26+
import { useQueryParams } from '../../hooks/useQueryParams';
2627

2728
import { translate } from '../../helpers/language';
2829

@@ -44,13 +45,20 @@ const AlertSection = () => {
4445

4546
//console.log('AlertSection run');
4647

47-
const [alertSearchText, setAlertSearchText] = useAtom(alertSearchTextAtom);
48+
const queryParams = useQueryParams();
49+
const alertSearchText = queryParams.get('alertSearch') || '';
4850
const [localSearchText, setLocalSearchText] = useState(alertSearchText);
4951

50-
// Debounce updating the shared atom so filtering doesn't run on every keystroke
52+
// Debounce updating the URL query param so it doesn't update on every keystroke
5153
const debouncedSetSearchText = useMemo(
52-
() => _.debounce((value: string) => setAlertSearchText(value), 300),
53-
[setAlertSearchText],
54+
() => _.debounce((value: string) => {
55+
if (value) {
56+
queryParams.set({ alertSearch: value });
57+
} else {
58+
queryParams.remove('alertSearch');
59+
}
60+
}, 300),
61+
[queryParams],
5462
);
5563

5664
// Cleanup debounce on unmount
@@ -69,7 +77,7 @@ const AlertSection = () => {
6977
const handleSearchClear = () => {
7078
setLocalSearchText('');
7179
debouncedSetSearchText.cancel();
72-
setAlertSearchText('');
80+
queryParams.remove('alertSearch');
7381
};
7482

7583
// State Management state (this section)

0 commit comments

Comments
 (0)