From 795b92cc1a168c48c7710ca6e0e1ef5974013d95 Mon Sep 17 00:00:00 2001
From: Armand Philippot <git@armandphilippot.com>
Date: Thu, 26 Oct 2023 19:07:31 +0200
Subject: refactor(hooks): rewrite useLocalStorage hook

* return a tuple instead of an object
* add a validator function as parameter (if the stored value
is manually changed, it is not safe to cast its type)
* add tests
---
 src/components/organisms/forms/ackee-toggle/ackee-toggle.tsx | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

(limited to 'src/components/organisms/forms/ackee-toggle/ackee-toggle.tsx')

diff --git a/src/components/organisms/forms/ackee-toggle/ackee-toggle.tsx b/src/components/organisms/forms/ackee-toggle/ackee-toggle.tsx
index 8ada948..a9c172b 100644
--- a/src/components/organisms/forms/ackee-toggle/ackee-toggle.tsx
+++ b/src/components/organisms/forms/ackee-toggle/ackee-toggle.tsx
@@ -15,6 +15,9 @@ import {
   type TooltipProps,
 } from '../../../molecules';
 
+const validator = (value: unknown): value is AckeeOptions =>
+  value === 'full' || value === 'partial';
+
 export type AckeeToggleProps = Omit<
   SwitchProps,
   'isInline' | 'items' | 'name' | 'onSwitch' | 'value'
@@ -46,9 +49,10 @@ export const AckeeToggle: FC<AckeeToggleProps> = ({
   ...props
 }) => {
   const intl = useIntl();
-  const { value, setValue } = useLocalStorage<AckeeOptions>(
+  const [value, setValue] = useLocalStorage(
     storageKey,
-    defaultValue
+    defaultValue,
+    validator
   );
   const [isTooltipOpened, setIsTooltipOpened] = useState(false);
 
-- 
cgit v1.2.3

