forked from solidjs/solid-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlayout.tsx
More file actions
206 lines (188 loc) · 5.74 KB
/
layout.tsx
File metadata and controls
206 lines (188 loc) · 5.74 KB
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
import { ParentComponent, Show, children, Suspense } from "solid-js";
import { MainNavigation } from "~/ui/layout/main-navigation";
import { MainHeader } from "./layout/main-header";
import { Hero } from "./layout/hero";
import { cache, createAsync, useMatch } from "@solidjs/router";
import { DocsLayout } from "./docs-layout";
import { PageStateProvider } from "~/data/page-state";
import { Alert } from "@kobalte/core";
import { SidePanel } from "./layout/side-panel";
import { SUPPORTED_LOCALES } from "~/i18n/config";
import { getValidLocaleFromPathname } from "~/i18n/helpers";
import {
coreTree,
routerTree,
startTree,
coreEntries,
startEntries,
routerEntries,
metaTree,
metaEntries,
} from "solid:collection";
import { PathMatch } from "@solidjs/router/dist/types";
import { useCurrentRouteMetaData } from "~/utils/route-metadata-helper";
const PROJECTS = ["solid-router", "solid-start", "solid-meta"] as const;
function getDefaultTree(project: (typeof PROJECTS)[number]) {
switch (project) {
case "solid-router":
return routerTree;
case "solid-start":
return startTree;
case "solid-meta":
return metaTree;
default:
return coreTree;
}
}
function getDefaultEntries(project: (typeof PROJECTS)[number]) {
switch (project) {
case "solid-router":
return routerEntries;
case "solid-start":
return startEntries;
case "solid-meta":
return metaEntries;
default:
return coreEntries;
}
}
const getProjectFromUrl = (path: string) => {
for (const project of PROJECTS) {
if (path.includes(project)) {
return project;
}
}
return null;
};
const getDocsMetadata = cache(
async (
isFirstMatch: PathMatch | undefined,
isI18nOrProject: PathMatch | undefined,
isCore: PathMatch | undefined
) => {
"use server";
if (!isFirstMatch && !isI18nOrProject)
return {
tree: coreTree,
entries: coreEntries,
};
const { path } = (isFirstMatch || isI18nOrProject || isCore) as PathMatch;
const locale = getValidLocaleFromPathname(path);
const project = getProjectFromUrl(path);
if (project) {
if (SUPPORTED_LOCALES.some((lang) => lang === locale)) {
return {
tree: (await import(`../../.solid/${project}-tree-${locale}.ts`))
.default,
entries: (
await import(`../../.solid/${project}-flat-entries-${locale}.ts`)
).default,
};
}
return {
tree: getDefaultTree(project),
entries: getDefaultEntries(project),
};
}
if (SUPPORTED_LOCALES.some((lang) => lang === locale)) {
return {
tree: (await import(`../../.solid/tree-${locale}.ts`)).default,
entries: (await import(`../../.solid/flat-entries-${locale}.ts`))
.default,
};
} else {
return {
tree: coreTree,
entries: coreEntries,
};
}
},
"global-metadata"
);
export const Layout: ParentComponent<{ isError?: boolean }> = (props) => {
const isTranslatedProject = useMatch(() => "/:locale/:project/*", {
locale: [...SUPPORTED_LOCALES, ...PROJECTS],
project: PROJECTS,
});
// is i18n main
// is en project
const isProjectContent = useMatch(() => "/:localeOrProject/*", {
localeOrProject: [...SUPPORTED_LOCALES, ...PROJECTS],
});
const isCoreContent = useMatch(() => "/*");
const entries = createAsync(
() =>
getDocsMetadata(
isProjectContent(),
isTranslatedProject(),
isCoreContent()
),
{ deferStream: true }
);
const resolved = children(() => props.children);
return (
<Suspense>
<PageStateProvider>
<div class="relative dark:bg-slate-900 bg-slate-50">
<Alert.Root class="dark:text-slate-900 text-white text-center p-1 font-semibold border-blue-50 dark:border-blue-600 bg-[rgb(14,142,231)] dark:bg-[rgb(162,222,255)]">
These docs are currently in Beta!
</Alert.Root>
<Show when={entries()}>
{(data) => <MainHeader tree={data().tree} />}
</Show>
<Show when={useCurrentRouteMetaData().isProjectRoot} keyed>
<Hero />
</Show>
<div class="relative mx-auto flex max-w-8xl flex-auto justify-center custom-scrollbar pt-10">
<Show when={!props.isError}>
<div class="hidden md:relative lg:block lg:flex-none">
<div class="absolute inset-y-0 right-0 w-[50vw] dark:hidden" />
<div class="absolute bottom-0 right-0 top-16 hidden h-12 w-px bg-gradient-to-t from-slate-800 dark:block" />
<div class="absolute bottom-0 right-0 top-28 hidden w-px bg-slate-800 dark:block" />
<Suspense>
<Show when={entries()}>
{(data) => (
<div class="sticky top-[4.75rem] h-[calc(100vh-7rem)] w-64 pl-0.5 pr-2 xl:w-72">
<MainNavigation tree={data().tree} />
</div>
)}
</Show>
</Suspense>
</div>
</Show>
<main class="w-full md:max-w-2xl flex-auto px-4 pt-2 md:pb-16 lg:max-w-none prose prose-slate dark:prose-invert dark:text-slate-300">
<Show
when={!useCurrentRouteMetaData().isProjectRoot}
keyed
fallback={
<article class="px-2 md:px-10 expressive-code-overrides overflow-y-auto">
{resolved()}
</article>
}
>
<Show when={!props.isError} fallback={<>{resolved()}</>}>
<Suspense>
<Show when={entries()}>
{(data) => (
<DocsLayout entries={data().entries}>
{resolved()}
</DocsLayout>
)}
</Show>
</Suspense>
</Show>
</Show>
</main>
<Show when={!props.isError}>
<div class="hidden xl:block shrink-0 w-56 2xl:w-72 pr-4 prose prose-slate dark:prose-invert dark:text-slate-300">
<div class="sticky top-[4.75rem] h-[calc(100vh-7rem)] overflow-y-auto custom-scrollbar">
<SidePanel children={resolved()} />
</div>
</div>
</Show>
</div>
</div>
</PageStateProvider>
</Suspense>
);
};