|
| 1 | +import { useState, useEffect, useCallback } from 'react' |
| 2 | + |
| 3 | +import noop from './shared/noop' |
| 4 | +import isClient from './shared/isClient' |
| 5 | +import isDevelopment from './shared/isDevelopment' |
| 6 | +import isAPISupported from './shared/isAPISupported' |
| 7 | +import createHandlerSetter from './factory/createHandlerSetter' |
| 8 | + |
| 9 | +export enum ECookieSameSite { |
| 10 | + STRICT = 'strict', |
| 11 | + LAX = 'lax', |
| 12 | + NONE = 'none', |
| 13 | +} |
| 14 | + |
| 15 | +interface ICookieStoreDeleteOptions { |
| 16 | + name?: string; |
| 17 | + domain?: string; |
| 18 | + path?: string; |
| 19 | +} |
| 20 | + |
| 21 | +interface ICookieInit extends ICookieStoreDeleteOptions { |
| 22 | + sameSite?: ECookieSameSite; |
| 23 | +} |
| 24 | + |
| 25 | +interface ICookieInitWithNameAndValue extends ICookieInit { |
| 26 | + name?: string; |
| 27 | + value?: string; |
| 28 | +} |
| 29 | + |
| 30 | +export interface IOptions extends ICookieInit { |
| 31 | + defaultValue?: string; |
| 32 | +} |
| 33 | + |
| 34 | +interface ICookieStore { |
| 35 | + get: (key: string) => Promise<ICookieInitWithNameAndValue>; |
| 36 | + set: (options: ICookieInitWithNameAndValue) => Promise<void>; |
| 37 | + delete: (options: ICookieStoreDeleteOptions) => Promise<void>; |
| 38 | +} |
| 39 | + |
| 40 | +const useCookie = (key: string, options?: IOptions) => { |
| 41 | + const hookNotSupportedResponse = Object.freeze({ |
| 42 | + onError: noop, |
| 43 | + updateCookie: noop, |
| 44 | + deleteCookie: noop, |
| 45 | + cookieValue: options?.defaultValue, |
| 46 | + }) |
| 47 | + |
| 48 | + if (!isClient) { |
| 49 | + if (!isDevelopment) { |
| 50 | + // eslint-disable-next-line no-console |
| 51 | + console.warn( |
| 52 | + 'Please be aware that cookieStore could not be available during SSR', |
| 53 | + ) |
| 54 | + } |
| 55 | + |
| 56 | + return hookNotSupportedResponse |
| 57 | + } |
| 58 | + |
| 59 | + if (!isAPISupported('cookieStore')) { |
| 60 | + // eslint-disable-next-line no-console |
| 61 | + console.warn( |
| 62 | + "The current device does not support the 'cookieStore' API, you should avoid using useCookie", |
| 63 | + ) |
| 64 | + |
| 65 | + return hookNotSupportedResponse |
| 66 | + } |
| 67 | + |
| 68 | + const [cookieValue, setCookieValue] = useState<string>() |
| 69 | + const [onErrorRef, setOnErrorRef] = createHandlerSetter<Error>() |
| 70 | + |
| 71 | + const cookieStoreObject = (window as any).cookieStore as ICookieStore |
| 72 | + |
| 73 | + const onError = (err: Error) => { |
| 74 | + if (onErrorRef.current) { |
| 75 | + onErrorRef.current(err) |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + useEffect(() => { |
| 80 | + const getInitialValue = async () => { |
| 81 | + try { |
| 82 | + const getFunctionResult = await cookieStoreObject.get(key) |
| 83 | + |
| 84 | + if (getFunctionResult?.value) { |
| 85 | + return setCookieValue(getFunctionResult.value) |
| 86 | + } |
| 87 | + |
| 88 | + await cookieStoreObject.set({ |
| 89 | + name: key, |
| 90 | + value: options?.defaultValue, |
| 91 | + ...options, |
| 92 | + }) |
| 93 | + return setCookieValue(options?.defaultValue) |
| 94 | + } catch (err) { |
| 95 | + return onError(err) |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + getInitialValue() |
| 100 | + }, []) |
| 101 | + |
| 102 | + const updateCookie = useCallback( |
| 103 | + (newValue: string) => cookieStoreObject |
| 104 | + .set({ name: key, value: newValue, ...options }) |
| 105 | + .then(() => setCookieValue(newValue)) |
| 106 | + .catch(onError), |
| 107 | + [], |
| 108 | + ) |
| 109 | + |
| 110 | + const deleteCookie = useCallback( |
| 111 | + () => cookieStoreObject |
| 112 | + .delete({ name: key, ...options }) |
| 113 | + .then(() => setCookieValue(undefined)) |
| 114 | + .catch(onError), |
| 115 | + [], |
| 116 | + ) |
| 117 | + |
| 118 | + return Object.freeze({ |
| 119 | + cookieValue, |
| 120 | + updateCookie, |
| 121 | + deleteCookie, |
| 122 | + onError: setOnErrorRef, |
| 123 | + }) |
| 124 | +} |
| 125 | + |
| 126 | +export default useCookie |
0 commit comments