diff --git a/striker-ui/hooks/useCookieJar.ts b/striker-ui/hooks/useCookieJar.ts new file mode 100644 index 00000000..d0767b2c --- /dev/null +++ b/striker-ui/hooks/useCookieJar.ts @@ -0,0 +1,58 @@ +import { useCallback, useEffect, useState } from 'react'; + +import useIsFirstRender from './useIsFirstRender'; + +const useCookieJar = (): { + cookieJar: CookieJar; + getCookie: (key: string) => T | undefined; + getSessionUser: () => SessionUser | undefined; +} => { + const isFirstRender = useIsFirstRender(); + + const [cookieJar, setCookieJar] = useState({}); + + const getCookie = useCallback( + (key: string, prefix = 'suiapi.') => + cookieJar[`${prefix}${key}`] as T | undefined, + [cookieJar], + ); + + const getSessionUser = useCallback( + () => getCookie('user'), + [getCookie], + ); + + useEffect(() => { + if (isFirstRender) { + const lines = document.cookie.split(/\s*;\s*/); + + setCookieJar( + lines.reduce((previous, line) => { + const [key, value] = line.split('=', 2); + + const decoded = decodeURIComponent(value); + + let result: unknown; + + if (decoded.startsWith('j:')) { + try { + result = JSON.parse(decoded.substring(2)); + } catch (error) { + result = value; + } + } else { + result = value; + } + + previous[key] = result; + + return previous; + }, {}), + ); + } + }, [isFirstRender]); + + return { cookieJar, getCookie, getSessionUser }; +}; + +export default useCookieJar; diff --git a/striker-ui/types/CookieJar.d.ts b/striker-ui/types/CookieJar.d.ts new file mode 100644 index 00000000..50001b2b --- /dev/null +++ b/striker-ui/types/CookieJar.d.ts @@ -0,0 +1,3 @@ +type CookieJar = Record; + +type SessionUser = { name: string; uuid: string };