Skip to content

Commit

Permalink
fix: useLocalStorage
Browse files Browse the repository at this point in the history
  • Loading branch information
zovomat committed May 11, 2022
1 parent 94c0e03 commit 03bbc8e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 20 deletions.
38 changes: 22 additions & 16 deletions src/search/search-bar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ const StyledContainer = styled(Container)`
}
`;

type SearchLocalStorage = Array<{
value: string;
label: string;
icon: string;
app: string;
id: string;
}>;
export const SearchBar: FC<SearchBarProps> = ({
activeRoute
// primaryAction,
Expand All @@ -60,7 +67,10 @@ export const SearchBar: FC<SearchBarProps> = ({
const [searchIsEnabled, setSearchIsEnabled] = useState(false);
const inputRef = useRef<HTMLInputElement>();
const [t] = useTranslation();
const [storedValue, setStoredValue] = useLocalStorage('search_suggestions', []);
const [storedValue, setStoredValue] = useLocalStorage<SearchLocalStorage>(
'search_suggestions',
[]
);
const [inputTyped, setInputTyped] = useState('');
const history = useHistory();
const { updateQuery, module, query, searchDisabled, setSearchDisabled } = useSearchStore();
Expand Down Expand Up @@ -187,21 +197,17 @@ export const SearchBar: FC<SearchBarProps> = ({
module &&
!find(appSuggestions, (v) => v.label === newQuery[newQuery.length - 1]?.label)
) {
setStoredValue(
(
value: Array<{ value: string; label: string; icon: string; app: string; id: string }>
) => [
...value,
{
value: newQuery[newQuery.length - 1].label,
label: newQuery[newQuery.length - 1].label,
icon: 'ClockOutline',
app: module,
id: `${value.length}`,
hasAvatar: false
}
]
);
setStoredValue((value) => [
...value,
{
value: newQuery[newQuery.length - 1].label,
label: newQuery[newQuery.length - 1].label,
icon: 'ClockOutline',
app: module,
id: `${value.length}`,
hasAvatar: false
}
]);
}
/** Commented for future reference */
// if (inputRef.current) {
Expand Down
6 changes: 3 additions & 3 deletions src/shell/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

/* eslint-disable react-hooks/rules-of-hooks */

import { useContext, useState } from 'react';
import { Dispatch, SetStateAction, useContext, useState } from 'react';

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
Expand All @@ -17,7 +17,7 @@ export function useIsMobile(): boolean {
return isMobile;
}

export function useLocalStorage<T>(key: string, initialValue: T): any {
export function useLocalStorage<T>(key: string, initialValue: T): [T, Dispatch<SetStateAction<T>>] {
const [storedValue, setStoredValue] = useState<T>(() => {
try {
const item = window.localStorage.getItem(key);
Expand All @@ -36,5 +36,5 @@ export function useLocalStorage<T>(key: string, initialValue: T): any {
console.error(error);
}
};
return [storedValue, setValue] as const;
return [storedValue, setValue];
}
4 changes: 3 additions & 1 deletion types/exports/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/
/* eslint-disable @typescript-eslint/ban-types */

import { ComponentType, FC } from 'react';
import { ComponentType, Dispatch, FC, SetStateAction } from 'react';
import { LinkProps } from 'react-router-dom';
import { Reducer, Store } from 'redux';
import { TFunction } from 'react-i18next';
Expand Down Expand Up @@ -225,3 +225,5 @@ export const useFoldersAccordionByView: (

// Run Search
export const runSearch: (query: Array<QueryChip>, module: string) => void;

export const useLocalStorage: <T>(key: string, initialValue: T) => [T, Dispatch<SetStateAction<T>>];

0 comments on commit 03bbc8e

Please sign in to comment.