Skip to content

Commit

Permalink
Persist filters (#94)
Browse files Browse the repository at this point in the history
  • Loading branch information
FrederikBolding authored Sep 2, 2023
1 parent 7ceadd1 commit aeb8bb0
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/components/Filters.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export const Filters = () => {
<Switch
id="testnet-switch"
onChange={handleTestnetsToggle}
value={showTestnets}
isChecked={showTestnets}
/>
</FormControl>

Expand All @@ -71,7 +71,7 @@ export const Filters = () => {
<Switch
id="deprecated-switch"
onChange={handleDeprecatedToggle}
value={showDeprecated}
isChecked={showDeprecated}
/>
</FormControl>
</PopoverBody>
Expand Down
31 changes: 27 additions & 4 deletions src/context/SearchContext.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,34 @@
import React, { createContext, useState } from "react";
import React, { createContext, useEffect, useState } from "react";

export const SearchContext = createContext({});

const LOCALSTORAGE_KEY = "filters";

export const SearchProvider = ({ children }) => {
const loadPersisted = () => {
try {
const result = window.localStorage.getItem(LOCALSTORAGE_KEY);
if (result) {
return JSON.parse(result);
}
} catch {}
return null;
};

const defaults = loadPersisted();

const [query, setQuery] = useState("");
const [showTestnets, setShowTestnets] = useState(false);
const [showDeprecated, setShowDeprecated] = useState(false);
const [showTestnets, setShowTestnets] = useState(
defaults?.showTestnets ?? false
);
const [showDeprecated, setShowDeprecated] = useState(
defaults?.showDeprecated ?? false
);

useEffect(() => {
const str = JSON.stringify({ showTestnets, showDeprecated });
window.localStorage.setItem(LOCALSTORAGE_KEY, str);
}, [showTestnets, showDeprecated]);

return (
<SearchContext.Provider
Expand All @@ -15,7 +38,7 @@ export const SearchProvider = ({ children }) => {
showTestnets,
setShowTestnets,
showDeprecated,
setShowDeprecated
setShowDeprecated,
}}
>
{children}
Expand Down

1 comment on commit aeb8bb0

@StemmlerSisters
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#17

Please sign in to comment.