-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathindex.tsx
133 lines (115 loc) · 3.73 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
import { useThemeConfig } from "nextra-theme-docs"
import { useRouter } from "next/router"
import { Tabs } from "nextra/components"
import React, { Children, useEffect, useState } from "react"
interface ChildrenProps {
children: React.ReactNode
}
const AUTHJS_TAB_KEY = "authjs.codeTab.framework"
Code.Next = NextCode
Code.NextClient = NextClientCode
Code.Svelte = SvelteCode
// Code.Solid = SolidCode;
Code.Express = ExpressCode
Code.Fastify = FastifyCode
const baseFrameworks = {
[NextCode.name]: "Next.js",
[SvelteCode.name]: "SvelteKit",
[ExpressCode.name]: "Express",
// [SolidCode.name]: "SolidStart",
[FastifyCode.name]: "Fastify",
}
const allFrameworks = {
[NextCode.name]: "Next.js",
[NextClientCode.name]: "Next.js (Client)",
[SvelteCode.name]: "SvelteKit",
// [SolidCode.name]: "SolidStart",
[ExpressCode.name]: "Express",
[FastifyCode.name]: "Fastify",
}
const findTabIndex = (frameworks: Record<string, string>, tab: string) => {
// TODO: Slugify instead of matching on indexes
return Object.values(frameworks).findIndex(
(f) => f.toLowerCase() === tab.toLowerCase()
)
}
export function Code({ children }: ChildrenProps) {
const router = useRouter()
const {
query: { framework },
} = router
const childs = Children.toArray(children)
const { project } = useThemeConfig()
const withNextJsPages = childs.some(
// @ts-expect-error: Hacky dynamic child wrangling
(p) => p && p.type.name === NextClientCode.name
)
const renderedFrameworks = withNextJsPages ? allFrameworks : baseFrameworks
const [tabIndex, setTabIndex] = useState(0)
useEffect(() => {
const savedTabPreference = Number(
window.localStorage.getItem(AUTHJS_TAB_KEY)
)
if (framework) {
window.localStorage.setItem(
AUTHJS_TAB_KEY,
String(findTabIndex(renderedFrameworks, framework as string))
)
setTabIndex(findTabIndex(renderedFrameworks, framework as string))
} else if (savedTabPreference) {
setTabIndex(savedTabPreference)
}
}, [framework, renderedFrameworks])
return (
<div className="[&_div[role='tablist']]:!pb-0">
<Tabs
storageKey={AUTHJS_TAB_KEY}
items={Object.values(renderedFrameworks)}
selectedIndex={tabIndex}
>
{Object.keys(renderedFrameworks).map((f) => {
// @ts-expect-error: Hacky dynamic child wrangling
const child = childs.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="p-6 font-semibold rounded-lg bg-slate-100 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 FastifyCode({ children }: ChildrenProps) {
return <Tabs.Tab>{children}</Tabs.Tab>
}