-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add wip useUser hook * add hook useLocalStorage * feat: unify authentication with new useUser hook * chore: update dependencies * fix logout * improve dahsboard * move register logic to useUser store
- Loading branch information
Showing
18 changed files
with
738 additions
and
4,786 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { Center, Stack, Text } from "@mantine/core"; | ||
import { AppIcon } from "../AppIcon"; | ||
|
||
export const LoadingScreen = () => { | ||
return ( | ||
<Center h={'100vh'}> | ||
<Stack align="center"> | ||
<AppIcon animated size={100} /> | ||
<Text size="xl" mt={18}>Cargando metagallery...</Text> | ||
</Stack> | ||
</Center> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { panicIfNull } from '@/utils'; | ||
import { useCallback, useEffect, useState } from 'react'; | ||
import { z } from 'zod'; | ||
|
||
type UseLocalStorageProps<S extends z.ZodType<any>, O> = { | ||
schema?: S, | ||
otherwise: O, | ||
}; | ||
|
||
/** | ||
* Try to parse the value from localStorage with the given schema. | ||
* Returns null when no value, invalid value, or invalid JSON. | ||
*/ | ||
const parseValue = <T>(value: any, schema: z.ZodType<T>): T | null => { | ||
try { | ||
const unmarshalled = JSON.parse(value); | ||
return schema.parse(unmarshalled); | ||
} catch { | ||
return null; | ||
} | ||
}; | ||
|
||
export function useLocalStorage<S extends z.ZodType<z.infer<S>> = z.ZodNull, O = z.infer<S> | null>( | ||
key: string, { schema, otherwise }: UseLocalStorageProps<S, O> | ||
) { | ||
const [value, _setValue] = useState(() => { | ||
const item = window.localStorage.getItem(key); | ||
const parsedItem = schema ? (item !== null ? parseValue(item, schema) : null) : item; | ||
|
||
if (parsedItem == null) { | ||
window.localStorage.setItem(key, JSON.stringify(otherwise)); | ||
return otherwise; | ||
} | ||
return parsedItem; | ||
}); | ||
|
||
const setValue = (val: (O | z.TypeOf<S>) | null, opts?: { strict: boolean }) => { | ||
if (val === null) { | ||
window.localStorage.removeItem(key); | ||
_setValue(otherwise); | ||
return; | ||
} | ||
|
||
let valToSerialize: O | z.TypeOf<S> = val; | ||
if (opts && opts.strict && schema) { | ||
valToSerialize = schema.parse(val); | ||
} | ||
window.localStorage.setItem(key, JSON.stringify(valToSerialize)); | ||
panicIfNull(valToSerialize); // unreachable, to make typescript happy | ||
_setValue(valToSerialize); | ||
}; | ||
|
||
const onLocalStorageChange = useCallback((e: StorageEvent) => { | ||
if (e.key === key) { | ||
const parsed = schema ? parseValue(e.newValue, schema) : e.newValue; | ||
_setValue(parsed ?? otherwise); | ||
} | ||
}, [key, schema, otherwise]); | ||
|
||
useEffect(() => { | ||
window.addEventListener('storage', onLocalStorageChange); | ||
return () => { | ||
window.removeEventListener('storage', onLocalStorageChange); | ||
}; | ||
}, []); | ||
|
||
return [value, setValue] as const; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.