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
193 lines (177 loc) · 5.12 KB
/
layout.tsx
File metadata and controls
193 lines (177 loc) · 5.12 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
import { ParentComponent, Show, Suspense } from "solid-js";
import {
coreTree,
routerTree,
startTree,
coreEntries,
startEntries,
routerEntries,
metaTree,
metaEntries,
} from "solid:collection";
import { PathMatch } from "@solidjs/router";
import { MainNavigation } from "~/ui/layout/main-navigation";
import { MainHeader } from "./layout/main-header";
import { Hero } from "./layout/hero";
import { query, createAsync, useMatch } from "@solidjs/router";
import { DocsLayout } from "./docs-layout";
import { SidePanel } from "./layout/side-panel";
import { SUPPORTED_LOCALES } from "~/i18n/config";
import { getCurrentLocale } from "~/i18n/helpers";
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 = query(
async (
isFirstMatch: PathMatch | undefined,
isI18nOrProject: PathMatch | undefined,
isCore: PathMatch | undefined
) => {
if (!isFirstMatch && !isI18nOrProject)
return {
tree: coreTree,
entries: coreEntries,
};
const { path } = (isFirstMatch || isI18nOrProject || isCore) as PathMatch;
const locale = getCurrentLocale();
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 }
);
return (
<div class="relative bg-slate-50 dark:bg-slate-900">
<Show when={entries()}>
{(data) => <MainHeader tree={data().tree} />}
</Show>
<Show when={useCurrentRouteMetaData().isProjectRoot} keyed>
<Hero />
</Show>
<div class="custom-scrollbar relative mx-auto flex max-w-8xl flex-auto justify-center 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="prose prose-slate w-full flex-auto px-4 pt-2 dark:prose-invert md:max-w-2xl md:pb-16 lg:max-w-none dark:text-slate-300">
<Show
when={!useCurrentRouteMetaData().isProjectRoot}
keyed
fallback={
<article class="expressive-code-overrides overflow-y-auto px-2 md:px-10">
{props.children}
</article>
}
>
<Show when={!props.isError} fallback={<>{props.children}</>}>
<Suspense>
<Show when={entries()}>
{(data) => (
<DocsLayout entries={data().entries}>
{props.children}
</DocsLayout>
)}
</Show>
</Suspense>
</Show>
</Show>
</main>
<Show when={!props.isError}>
<div class="prose prose-slate hidden w-56 shrink-0 pr-4 dark:prose-invert xl:block 2xl:w-72 dark:text-slate-300">
<div class="custom-scrollbar sticky top-[4.75rem] h-[calc(100vh-7rem)] overflow-y-auto">
<SidePanel />
</div>
</div>
</Show>
</div>
</div>
);
};