-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathindex.tsx
243 lines (207 loc) · 7.19 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
import { useSearchParams } from "next/navigation"
import { useRouter } from "next/router"
import { useThemeConfig } from "nextra-theme-docs"
import { Tabs } from "nextra/components"
import React, { Children, useEffect, MouseEvent } from "react"
interface ChildrenProps {
children: React.ReactNode
}
const AUTHJS_TAB_KEY = "authjs.codeTab.framework"
const AUTHJS_TAB_KEY_ALL = "authjs.codeTab.framework.all"
Code.Next = NextCode
Code.NextClient = NextClientCode
Code.Svelte = SvelteCode
// Code.Solid = SolidCode;
Code.Express = ExpressCode
Code.Qwik = QwikCode
const baseFrameworks = {
[NextCode.name]: "Next.js",
[QwikCode.name]: "Qwik",
[SvelteCode.name]: "SvelteKit",
[ExpressCode.name]: "Express",
// [SolidCode.name]: "SolidStart",
}
const allFrameworks = {
[NextCode.name]: "Next.js",
[NextClientCode.name]: "Next.js (Client)",
[QwikCode.name]: "Qwik",
[SvelteCode.name]: "SvelteKit",
// [SolidCode.name]: "SolidStart",
[ExpressCode.name]: "Express",
}
const findFrameworkKey = (
text: string,
frameworks: Record<string, string>
): string | null => {
const entry = Object.entries(frameworks).find(([_, value]) => value === text)
return entry ? entry[0] : null
}
const getIndexFrameworkFromUrl = (
url: string,
frameworks: Record<string, string>
): number | null => {
const params = new URLSearchParams(url)
const paramValue = params.get("framework")
if (!paramValue) return null
const decodedValue = decodeURI(paramValue)
const index = Object.values(frameworks).findIndex(
(value) => value === decodedValue
)
return index === -1 ? null : index
}
const getIndexFrameworkFromStorage = (
frameworks: Record<string, string>,
isAllFrameworks: boolean
): number | null => {
const storageKey = isAllFrameworks ? AUTHJS_TAB_KEY_ALL : AUTHJS_TAB_KEY
const storedIndex = window.localStorage.getItem(storageKey)
if (!storedIndex) {
return null
}
return parseInt(storedIndex) % Object.keys(frameworks).length
}
const updateFrameworkStorage = (
frameworkURI: string,
frameworks: Record<string, string>,
isAllFrameworks: boolean
): void => {
const index = Object.values(frameworks).findIndex(
(value) => encodeURI(value) === frameworkURI
)
if (index === -1) return
const storageKey = isAllFrameworks ? AUTHJS_TAB_KEY_ALL : AUTHJS_TAB_KEY
window.localStorage.setItem(storageKey, index.toString())
// Update other storage if framework exists in other object
const otherFrameworksValues = Object.values(
isAllFrameworks ? baseFrameworks : allFrameworks
)
const otherStorageKey = isAllFrameworks ? AUTHJS_TAB_KEY : AUTHJS_TAB_KEY_ALL
const encodedFrameworksValues = otherFrameworksValues.map((value) =>
encodeURI(value)
)
const existsInOther = encodedFrameworksValues.some(
(encodedFramework) => encodedFramework === frameworkURI
)
if (existsInOther) {
const otherIndex = otherFrameworksValues.findIndex(
(encodedFramework) => encodedFramework === frameworkURI
)
window.localStorage.setItem(otherStorageKey, otherIndex.toString())
// see https://github.com/shuding/nextra/blob/7ae958f02922e608151411042f658480b75164a6/packages/nextra/src/client/components/tabs/index.client.tsx#L106
window.dispatchEvent(
new StorageEvent("storage", {
key: otherStorageKey,
newValue: otherIndex.toString(),
})
)
}
}
export function Code({ children }: ChildrenProps) {
const router = useRouter()
const searchParams = useSearchParams()
const childElements = Children.toArray(children)
const { project } = useThemeConfig()
const withNextJsPages = childElements.some(
// @ts-expect-error: Hacky dynamic child wrangling
(p) => p && p.type.name === NextClientCode.name
)
const renderedFrameworks = withNextJsPages ? allFrameworks : baseFrameworks
const updateFrameworkInUrl = (frameworkURI: string): void => {
if (frameworkURI === "undefined") return
const params = new URLSearchParams(searchParams?.toString())
params.set("framework", frameworkURI)
router.push(`${router.pathname}?${params.toString()}`, undefined, {
scroll: false,
})
}
const handleClickFramework = (event: MouseEvent<HTMLDivElement>) => {
if (!(event.target instanceof HTMLButtonElement)) return
const { textContent } = event.target as unknown as HTMLDivElement
if (!textContent) return
const frameworkURI = encodeURI(textContent)
updateFrameworkInUrl(frameworkURI)
updateFrameworkStorage(frameworkURI, renderedFrameworks, withNextJsPages)
// Focus and scroll to maintain position when code blocks above are expanded
const element = event.target as HTMLButtonElement
const rect = element.getBoundingClientRect()
requestAnimationFrame(() => {
element.focus()
window.scrollBy(0, element.getBoundingClientRect().top - rect.top)
})
}
useEffect(() => {
const indexFrameworkFromStorage = getIndexFrameworkFromStorage(
renderedFrameworks,
withNextJsPages
)
const indexFrameworkFromUrl = getIndexFrameworkFromUrl(
router.asPath,
renderedFrameworks
)
if (indexFrameworkFromStorage === null) {
updateFrameworkStorage(
encodeURI(renderedFrameworks[indexFrameworkFromUrl ?? 0]),
renderedFrameworks,
withNextJsPages
)
}
if (!indexFrameworkFromUrl) {
const index = indexFrameworkFromStorage ?? 0
updateFrameworkInUrl(encodeURI(renderedFrameworks[index]))
}
}, [router.pathname, renderedFrameworks, withNextJsPages])
return (
<div
className="[&_div[role='tablist']]:!pb-0"
onClick={handleClickFramework}
>
<Tabs
storageKey={withNextJsPages ? AUTHJS_TAB_KEY_ALL : AUTHJS_TAB_KEY}
items={Object.values(renderedFrameworks)}
>
{Object.keys(renderedFrameworks).map((f) => {
// @ts-expect-error: Hacky dynamic child wrangling
const child = childElements.find((c) => c?.type?.name === f)
// @ts-expect-error: Hacky dynamic child wrangling
return Object.keys(child?.props ?? {}).length ? (
child
) : (
<Tabs.Tab key={f}>
<p className="rounded-lg bg-slate-100 p-6 font-semibold dark:bg-neutral-950">
{renderedFrameworks[f]} not documented yet. Help us by
contributing{" "}
<a
className="underline"
target="_blank"
href={`${project.link}/edit/main/docs/pages${router.pathname}.mdx`}
rel="noreferrer"
>
here
</a>
.
</p>
</Tabs.Tab>
)
})}
</Tabs>
</div>
)
}
function NextClientCode({ children }: ChildrenProps) {
return <Tabs.Tab>{children}</Tabs.Tab>
}
function NextCode({ children }: ChildrenProps) {
return <Tabs.Tab>{children}</Tabs.Tab>
}
function SvelteCode({ children }: ChildrenProps) {
return <Tabs.Tab>{children}</Tabs.Tab>
}
// function SolidCode({ children }: ChildrenProps) {
// return <Tabs.Tab>{children}</Tabs.Tab>;
// }
function ExpressCode({ children }: ChildrenProps) {
return <Tabs.Tab>{children}</Tabs.Tab>
}
function QwikCode({ children }: ChildrenProps) {
return <Tabs.Tab>{children}</Tabs.Tab>
}