@quiltt/react 2.1.1 → 2.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,105 @@
1
+ 'use client'
2
+ import { useEffect, useState } from 'react'
3
+
4
+ export type UseScriptStatus = 'idle' | 'loading' | 'ready' | 'error'
5
+ export interface UseScriptOptions {
6
+ shouldPreventLoad?: boolean
7
+ removeOnUnmount?: boolean
8
+ }
9
+
10
+ // Cached script statuses
11
+ const cachedScriptStatuses: Record<string, UseScriptStatus | undefined> = {}
12
+
13
+ function getScriptNode(src: string) {
14
+ const node: HTMLScriptElement | null = document.querySelector(`script[src="${src}"]`)
15
+ const status = node?.getAttribute('data-status') as UseScriptStatus | undefined
16
+
17
+ return {
18
+ node,
19
+ status,
20
+ }
21
+ }
22
+
23
+ // @see https://usehooks-ts.com/react-hook/use-script
24
+ export function useScript(src: string | null, options?: UseScriptOptions): UseScriptStatus {
25
+ const [status, setStatus] = useState<UseScriptStatus>(() => {
26
+ if (!src || options?.shouldPreventLoad) {
27
+ return 'idle'
28
+ }
29
+
30
+ if (typeof window === 'undefined') {
31
+ // SSR Handling - always return 'loading'
32
+ return 'loading'
33
+ }
34
+
35
+ return cachedScriptStatuses[src] ?? 'loading'
36
+ })
37
+
38
+ useEffect(() => {
39
+ if (!src || options?.shouldPreventLoad) {
40
+ return
41
+ }
42
+
43
+ const cachedScriptStatus = cachedScriptStatuses[src]
44
+ if (cachedScriptStatus === 'ready' || cachedScriptStatus === 'error') {
45
+ // If the script is already cached, set its status immediately
46
+ setStatus(cachedScriptStatus)
47
+ return
48
+ }
49
+
50
+ // Fetch existing script element by src
51
+ // It may have been added by another instance of this hook
52
+ const script = getScriptNode(src)
53
+ let scriptNode = script.node
54
+
55
+ if (!scriptNode) {
56
+ // Create script element and add it to document body
57
+ scriptNode = document.createElement('script')
58
+ scriptNode.src = src
59
+ scriptNode.async = true
60
+ scriptNode.setAttribute('data-status', 'loading')
61
+ document.body.appendChild(scriptNode)
62
+
63
+ // Store status in attribute on script
64
+ // This can be read by other instances of this hook
65
+ const setAttributeFromEvent = (event: Event) => {
66
+ const scriptStatus: UseScriptStatus = event.type === 'load' ? 'ready' : 'error'
67
+
68
+ scriptNode?.setAttribute('data-status', scriptStatus)
69
+ }
70
+
71
+ scriptNode.addEventListener('load', setAttributeFromEvent)
72
+ scriptNode.addEventListener('error', setAttributeFromEvent)
73
+ } else {
74
+ // Grab existing script status from attribute and set to state.
75
+ setStatus(script.status ?? cachedScriptStatus ?? 'loading')
76
+ }
77
+
78
+ // Script event handler to update status in state
79
+ // Note: Even if the script already exists we still need to add
80
+ // event handlers to update the state for *this* hook instance.
81
+ const setStateFromEvent = (event: Event) => {
82
+ const newStatus = event.type === 'load' ? 'ready' : 'error'
83
+ setStatus(newStatus)
84
+ cachedScriptStatuses[src] = newStatus
85
+ }
86
+
87
+ // Add event listeners
88
+ scriptNode.addEventListener('load', setStateFromEvent)
89
+ scriptNode.addEventListener('error', setStateFromEvent)
90
+
91
+ // Remove event listeners on cleanup
92
+ return () => {
93
+ if (scriptNode) {
94
+ scriptNode.removeEventListener('load', setStateFromEvent)
95
+ scriptNode.removeEventListener('error', setStateFromEvent)
96
+ }
97
+
98
+ if (scriptNode && options?.removeOnUnmount) {
99
+ scriptNode.remove()
100
+ }
101
+ }
102
+ }, [src, options?.shouldPreventLoad, options?.removeOnUnmount])
103
+
104
+ return status
105
+ }
@@ -32,8 +32,8 @@ const sessionTimer = new Timeoutable()
32
32
  * This happens when a page is reloaded or a person returns, and everything is
33
33
  * reinitialized.
34
34
  */
35
- export const useSession = (): [Maybe<QuilttJWT> | undefined, SetSession] => {
36
- const [token, setToken] = useStorage<string>('session')
35
+ export const useSession = (storageKey = 'session'): [Maybe<QuilttJWT> | undefined, SetSession] => {
36
+ const [token, setToken] = useStorage<string>(storageKey)
37
37
  const session = useMemo(() => parse(token), [token])
38
38
 
39
39
  // Clear session if/when it expires