@sixb/app 0.0.1
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.
- package/LICENSE +21 -0
- package/README.md +172 -0
- package/dist/agents.d.ts +6 -0
- package/dist/agents.d.ts.map +1 -0
- package/dist/agents.js +26 -0
- package/dist/agents.js.map +10 -0
- package/dist/build.d.ts +21 -0
- package/dist/build.d.ts.map +1 -0
- package/dist/codegen.d.ts +43 -0
- package/dist/codegen.d.ts.map +1 -0
- package/dist/createCustomApp.d.ts +43 -0
- package/dist/createCustomApp.d.ts.map +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +1464 -0
- package/dist/index.js.map +18 -0
- package/dist/manifest.d.ts +3 -0
- package/dist/manifest.d.ts.map +1 -0
- package/dist/metadata.d.ts +29 -0
- package/dist/metadata.d.ts.map +1 -0
- package/dist/runtime.d.ts +12 -0
- package/dist/runtime.d.ts.map +1 -0
- package/dist/scanner.d.ts +19 -0
- package/dist/scanner.d.ts.map +1 -0
- package/dist/styles.d.ts +33 -0
- package/dist/styles.d.ts.map +1 -0
- package/dist/tailwind.d.ts +62 -0
- package/dist/tailwind.d.ts.map +1 -0
- package/dist/tsconfig.json +5 -0
- package/package.json +74 -0
- package/src/agents.ts +23 -0
- package/src/build.ts +105 -0
- package/src/codegen.ts +587 -0
- package/src/createCustomApp.ts +770 -0
- package/src/index.ts +15 -0
- package/src/manifest.ts +29 -0
- package/src/metadata.ts +206 -0
- package/src/runtime.ts +16 -0
- package/src/scanner.ts +81 -0
- package/src/styles.ts +83 -0
- package/src/tailwind.ts +195 -0
package/src/build.ts
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { copyFile, mkdir, readFile, rename, rm, writeFile } from "node:fs/promises"
|
|
2
|
+
import { basename, dirname, join } from "node:path"
|
|
3
|
+
|
|
4
|
+
const STATIC_ASSET_ORIGIN = "https://sixb-static.invalid"
|
|
5
|
+
|
|
6
|
+
export interface BuildAppOptions {
|
|
7
|
+
/** Path to the generated index.html entry point */
|
|
8
|
+
entryPath: string
|
|
9
|
+
/** Generated manifest copied to the stable `/app.webmanifest` output path. */
|
|
10
|
+
manifestPath?: string
|
|
11
|
+
/** Output directory, defaults to `.sixb/dist/app` */
|
|
12
|
+
outdir?: string
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface BuildAppResult {
|
|
16
|
+
success: boolean
|
|
17
|
+
outdir: string
|
|
18
|
+
logs?: string[]
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Builds the app using `Bun.build()` on the generated HTML entry point.
|
|
23
|
+
* Outputs a production-ready static bundle.
|
|
24
|
+
*/
|
|
25
|
+
export async function buildApp(options: BuildAppOptions): Promise<BuildAppResult> {
|
|
26
|
+
const outdir = options.outdir ?? join(process.cwd(), ".sixb", "dist", "app")
|
|
27
|
+
// The outdir is build-owned. Clear it so hashed chunks from previous builds
|
|
28
|
+
// don't accumulate (and get served) forever.
|
|
29
|
+
await rm(outdir, { recursive: true, force: true })
|
|
30
|
+
await mkdir(outdir, { recursive: true })
|
|
31
|
+
|
|
32
|
+
const bundleEntryPath = await prepareAppHtmlBundleEntry(options.entryPath)
|
|
33
|
+
let result: Awaited<ReturnType<typeof Bun.build>>
|
|
34
|
+
try {
|
|
35
|
+
result = await Bun.build({
|
|
36
|
+
entrypoints: [bundleEntryPath],
|
|
37
|
+
outdir,
|
|
38
|
+
target: "browser",
|
|
39
|
+
conditions: ["bun"],
|
|
40
|
+
publicPath: "/",
|
|
41
|
+
minify: true,
|
|
42
|
+
// React is bundled into this browser output, so without pinning NODE_ENV the production build
|
|
43
|
+
// of a user's app ships React's development build: dev-only checks on every render, and the
|
|
44
|
+
// warnings that go with them. This is also the only knob that selects the production JSX
|
|
45
|
+
// runtime — an ambient NODE_ENV does not.
|
|
46
|
+
define: { "process.env.NODE_ENV": '"production"' },
|
|
47
|
+
sourcemap: "external",
|
|
48
|
+
})
|
|
49
|
+
} finally {
|
|
50
|
+
await rm(bundleEntryPath, { force: true })
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
if (!result.success) {
|
|
54
|
+
return {
|
|
55
|
+
success: false,
|
|
56
|
+
outdir,
|
|
57
|
+
logs: result.logs.map(String),
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const bundleHtmlPath = join(outdir, basename(bundleEntryPath))
|
|
62
|
+
await rename(bundleHtmlPath, join(outdir, "index.html"))
|
|
63
|
+
await rewriteIndexAssetPaths(outdir)
|
|
64
|
+
if (options.manifestPath) {
|
|
65
|
+
await copyFile(options.manifestPath, join(outdir, "app.webmanifest"))
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
return { success: true, outdir }
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// Bun's HTML bundler currently tries to resolve root-relative link assets from
|
|
72
|
+
// the filesystem root. A temporary external origin keeps these stable URLs out
|
|
73
|
+
// of the asset graph; dev responses and production output restore the originals.
|
|
74
|
+
export async function prepareAppHtmlBundleEntry(entryPath: string): Promise<string> {
|
|
75
|
+
const html = await readFile(entryPath, "utf-8")
|
|
76
|
+
const bundleHtml = html.replace(
|
|
77
|
+
/(<link\s+rel=["'](?:manifest|icon|apple-touch-icon)["']\s+href=["'])(\/[^"']*)(["'][^>]*>)/g,
|
|
78
|
+
`$1${STATIC_ASSET_ORIGIN}$2$3`
|
|
79
|
+
)
|
|
80
|
+
const bundleEntryPath = join(dirname(entryPath), "index.sixb-bundle.html")
|
|
81
|
+
await writeFile(bundleEntryPath, bundleHtml, "utf-8")
|
|
82
|
+
return bundleEntryPath
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export function restoreAppStaticUrls(html: string): string {
|
|
86
|
+
return html.replaceAll(STATIC_ASSET_ORIGIN, "")
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
async function rewriteIndexAssetPaths(outdir: string): Promise<void> {
|
|
90
|
+
const indexPath = join(outdir, "index.html")
|
|
91
|
+
const originalHtml = await readFile(indexPath, "utf-8")
|
|
92
|
+
const html = restoreAppStaticUrls(originalHtml)
|
|
93
|
+
|
|
94
|
+
const rewritten = html.replace(
|
|
95
|
+
/(href|src)=(["'])((?!\/|#|[a-zA-Z][a-zA-Z\d+.-]*:)(?:\.\/)?[^"'?#]+(?:[?#][^"']*)?)\2/g,
|
|
96
|
+
(_match, attr: string, quote: string, url: string) => {
|
|
97
|
+
const normalized = url.startsWith("./") ? url.slice(2) : url
|
|
98
|
+
return `${attr}=${quote}/${normalized}${quote}`
|
|
99
|
+
}
|
|
100
|
+
)
|
|
101
|
+
|
|
102
|
+
if (rewritten !== originalHtml) {
|
|
103
|
+
await writeFile(indexPath, rewritten, "utf-8")
|
|
104
|
+
}
|
|
105
|
+
}
|
package/src/codegen.ts
ADDED
|
@@ -0,0 +1,587 @@
|
|
|
1
|
+
import { access, mkdir, readFile, writeFile } from "node:fs/promises"
|
|
2
|
+
import { join, relative, resolve } from "node:path"
|
|
3
|
+
import type { AuthSessionAudience } from "@sixb/core"
|
|
4
|
+
import { renderAppManifest } from "./manifest"
|
|
5
|
+
import { resolveAppMetadata } from "./metadata"
|
|
6
|
+
import { renderCustomAppRuntimeScript } from "./runtime"
|
|
7
|
+
import type { PageRoute } from "./scanner"
|
|
8
|
+
|
|
9
|
+
export interface BuiltInRouteManifestEntry {
|
|
10
|
+
readonly path: string
|
|
11
|
+
readonly moduleSpecifier: string
|
|
12
|
+
readonly exportName?: string
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface GenerateRouteManifestOptions {
|
|
16
|
+
readonly builtInRoutes?: readonly BuiltInRouteManifestEntry[]
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Generates `.sixb/generated/routes.ts` with static route imports. Pages are
|
|
21
|
+
* eager on purpose: project-specific apps bundle small, and a single bundle
|
|
22
|
+
* (one JS file, one render-blocking CSS file) means no Suspense gap or
|
|
23
|
+
* late-arriving styles when navigating — matching how Atlas routes.
|
|
24
|
+
*/
|
|
25
|
+
export async function generateRouteManifest(
|
|
26
|
+
routes: PageRoute[],
|
|
27
|
+
generatedDir: string,
|
|
28
|
+
options: GenerateRouteManifestOptions = {}
|
|
29
|
+
): Promise<string> {
|
|
30
|
+
await mkdir(generatedDir, { recursive: true })
|
|
31
|
+
|
|
32
|
+
const imports = routes
|
|
33
|
+
.map((route, index) => {
|
|
34
|
+
const rel = relativeTo(generatedDir, route.filePath)
|
|
35
|
+
return `import Page${index} from ${JSON.stringify(rel)}`
|
|
36
|
+
})
|
|
37
|
+
.join("\n")
|
|
38
|
+
const builtInRoutes = options.builtInRoutes ?? []
|
|
39
|
+
const builtInImports = builtInRoutes
|
|
40
|
+
.map((route, index) => {
|
|
41
|
+
if (route.exportName) {
|
|
42
|
+
return `import { ${route.exportName} as BuiltInPage${index} } from ${JSON.stringify(route.moduleSpecifier)}`
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return `import BuiltInPage${index} from ${JSON.stringify(route.moduleSpecifier)}`
|
|
46
|
+
})
|
|
47
|
+
.join("\n")
|
|
48
|
+
|
|
49
|
+
const entries = routes
|
|
50
|
+
.map((route, index) => ` { path: ${JSON.stringify(route.path)}, component: Page${index} },`)
|
|
51
|
+
.join("\n")
|
|
52
|
+
const builtInEntries = builtInRoutes
|
|
53
|
+
.map(
|
|
54
|
+
(route, index) => ` { path: ${JSON.stringify(route.path)}, component: BuiltInPage${index} },`
|
|
55
|
+
)
|
|
56
|
+
.join("\n")
|
|
57
|
+
const routeEntries = [entries, builtInEntries].filter(Boolean).join("\n")
|
|
58
|
+
const importEntries = [imports, builtInImports].filter(Boolean).join("\n")
|
|
59
|
+
|
|
60
|
+
const content = `${importEntries}
|
|
61
|
+
|
|
62
|
+
export const routes = [
|
|
63
|
+
${routeEntries}
|
|
64
|
+
]
|
|
65
|
+
`
|
|
66
|
+
|
|
67
|
+
const outPath = join(generatedDir, "routes.ts")
|
|
68
|
+
await writeFileIfChanged(outPath, content)
|
|
69
|
+
return outPath
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Generates the app entry files:
|
|
74
|
+
* - `.sixb/generated/index.html` — HTML shell
|
|
75
|
+
* - `.sixb/generated/main.tsx` — React entry with BrowserRouter
|
|
76
|
+
* - `.sixb/generated/app.webmanifest` — installable app identity
|
|
77
|
+
*/
|
|
78
|
+
export async function generateAppEntry(
|
|
79
|
+
projectRoot: string,
|
|
80
|
+
generatedDir: string,
|
|
81
|
+
options: {
|
|
82
|
+
apiBaseUrl?: string
|
|
83
|
+
audience?: AuthSessionAudience
|
|
84
|
+
authEnabled?: boolean
|
|
85
|
+
appDir?: string
|
|
86
|
+
publicDir?: string
|
|
87
|
+
/**
|
|
88
|
+
* Stylesheet to import from the generated entry. `undefined` auto-detects
|
|
89
|
+
* `app/globals.css` and imports it when it exists; `null` skips the import;
|
|
90
|
+
* a path imports that file (e.g. compiled Tailwind output).
|
|
91
|
+
*/
|
|
92
|
+
stylesheetPath?: string | null
|
|
93
|
+
/** Framework-owned stylesheets imported before the app stylesheet. */
|
|
94
|
+
frameworkStylesheetPaths?: readonly string[]
|
|
95
|
+
} = {}
|
|
96
|
+
): Promise<{ htmlPath: string; mainPath: string; manifestPath: string }> {
|
|
97
|
+
await mkdir(generatedDir, { recursive: true })
|
|
98
|
+
|
|
99
|
+
const appDir = options.appDir ? resolve(projectRoot, options.appDir) : join(projectRoot, "app")
|
|
100
|
+
const publicDir = options.publicDir
|
|
101
|
+
? resolve(projectRoot, options.publicDir)
|
|
102
|
+
: join(appDir, "public")
|
|
103
|
+
const globalsCssPath = join(appDir, "globals.css")
|
|
104
|
+
const layoutPath = join(appDir, "layout.tsx")
|
|
105
|
+
const metadata = await resolveAppMetadata({ layoutPath, publicDir })
|
|
106
|
+
const manifestPath = join(generatedDir, "app.webmanifest")
|
|
107
|
+
await writeFileIfChanged(manifestPath, renderAppManifest(metadata))
|
|
108
|
+
const layoutRel = relativeTo(generatedDir, layoutPath)
|
|
109
|
+
const hasLayout = await fileExists(layoutPath)
|
|
110
|
+
const stylesheetPath =
|
|
111
|
+
options.stylesheetPath !== undefined
|
|
112
|
+
? options.stylesheetPath
|
|
113
|
+
: (await fileExists(globalsCssPath))
|
|
114
|
+
? globalsCssPath
|
|
115
|
+
: null
|
|
116
|
+
const stylesheetPaths = [
|
|
117
|
+
...(options.frameworkStylesheetPaths ?? []),
|
|
118
|
+
...(stylesheetPath ? [stylesheetPath] : []),
|
|
119
|
+
]
|
|
120
|
+
const globalsCssImport = stylesheetPaths
|
|
121
|
+
.map((path) => `import ${JSON.stringify(relativeTo(generatedDir, path))}`)
|
|
122
|
+
.join("\n")
|
|
123
|
+
const layoutImport = hasLayout ? `import RootLayout from ${JSON.stringify(layoutRel)}\n` : ""
|
|
124
|
+
const layoutWrapperStart = hasLayout ? "<RootLayout>" : "<>"
|
|
125
|
+
const layoutWrapperEnd = hasLayout ? "</RootLayout>" : "</>"
|
|
126
|
+
const runtimeConfigScript = options.apiBaseUrl
|
|
127
|
+
? renderCustomAppRuntimeScript({
|
|
128
|
+
api: { baseUrl: options.apiBaseUrl },
|
|
129
|
+
auth: { audience: options.audience ?? "app", enabled: options.authEnabled ?? true },
|
|
130
|
+
})
|
|
131
|
+
: ""
|
|
132
|
+
|
|
133
|
+
// Generate main.tsx
|
|
134
|
+
const mainContent = `import React from "react"
|
|
135
|
+
import { createRoot, type Root } from "react-dom/client"
|
|
136
|
+
import { signOut } from "@sixb/client"
|
|
137
|
+
import { BrowserRouter, Routes, Route, matchPath, useNavigate, useLocation } from "react-router-dom"
|
|
138
|
+
import { QueryClient, QueryClientProvider } from "@tanstack/react-query"
|
|
139
|
+
import {
|
|
140
|
+
configureSixbBrowserClient,
|
|
141
|
+
isSixbApiError,
|
|
142
|
+
readSixbBrowserRuntimeConfig,
|
|
143
|
+
requireSixbBrowserAuthSession,
|
|
144
|
+
} from "@sixb/client/browser"
|
|
145
|
+
import { routes } from "./routes"
|
|
146
|
+
${globalsCssImport}
|
|
147
|
+
${layoutImport}
|
|
148
|
+
|
|
149
|
+
interface CustomAppHotData {
|
|
150
|
+
root?: Root
|
|
151
|
+
queryClient?: QueryClient
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
const runtimeConfig = readSixbBrowserRuntimeConfig({ audience: "app" })
|
|
155
|
+
const browserClient = configureSixbBrowserClient(runtimeConfig)
|
|
156
|
+
|
|
157
|
+
if (import.meta.hot) {
|
|
158
|
+
import.meta.hot.accept()
|
|
159
|
+
import.meta.hot.dispose(() => {
|
|
160
|
+
browserClient.dispose()
|
|
161
|
+
})
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
const authSession = runtimeConfig.auth.enabled
|
|
165
|
+
? await requireSixbBrowserAuthSession(runtimeConfig, browserClient)
|
|
166
|
+
: null
|
|
167
|
+
const applicationAccessDenied =
|
|
168
|
+
authSession?.authenticated === true && !authSession.applicationAccess.allowed
|
|
169
|
+
const canRenderApp =
|
|
170
|
+
!runtimeConfig.auth.enabled ||
|
|
171
|
+
(authSession?.authenticated === true && authSession.applicationAccess.allowed)
|
|
172
|
+
|
|
173
|
+
const queryClient = getQueryClient()
|
|
174
|
+
|
|
175
|
+
function getRoot(): Root {
|
|
176
|
+
const element = document.getElementById("root")
|
|
177
|
+
if (!element) {
|
|
178
|
+
throw new Error("[SixbApp] Could not find the root element.")
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
if (import.meta.hot) {
|
|
182
|
+
const data = import.meta.hot.data as CustomAppHotData
|
|
183
|
+
data.root ??= createRoot(element)
|
|
184
|
+
return data.root
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
return createRoot(element)
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
function getQueryClient(): QueryClient {
|
|
191
|
+
if (import.meta.hot) {
|
|
192
|
+
const data = import.meta.hot.data as CustomAppHotData
|
|
193
|
+
data.queryClient ??= createQueryClient()
|
|
194
|
+
return data.queryClient
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
return createQueryClient()
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
function createQueryClient(): QueryClient {
|
|
201
|
+
return new QueryClient({
|
|
202
|
+
defaultOptions: {
|
|
203
|
+
queries: {
|
|
204
|
+
refetchOnWindowFocus: false,
|
|
205
|
+
retry: 1,
|
|
206
|
+
},
|
|
207
|
+
},
|
|
208
|
+
})
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
const RESERVED_PATH_PREFIXES = ["/api", "/auth", "/ws", "/docs"]
|
|
212
|
+
|
|
213
|
+
function isReservedPath(pathname: string) {
|
|
214
|
+
return RESERVED_PATH_PREFIXES.some(
|
|
215
|
+
(prefix) => pathname === prefix || pathname.startsWith(prefix + "/")
|
|
216
|
+
)
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
// Makes plain same-origin <a href="/..."> clicks navigate client-side, so app
|
|
220
|
+
// authors get SPA navigation without remembering react-router's <Link>.
|
|
221
|
+
// Deliberately conservative: anything unusual falls through to the browser.
|
|
222
|
+
function InternalLinkInterceptor() {
|
|
223
|
+
const navigate = useNavigate()
|
|
224
|
+
|
|
225
|
+
React.useEffect(() => {
|
|
226
|
+
function onClick(event: MouseEvent) {
|
|
227
|
+
if (event.defaultPrevented) return
|
|
228
|
+
if (event.button !== 0) return
|
|
229
|
+
if (event.metaKey || event.ctrlKey || event.shiftKey || event.altKey) return
|
|
230
|
+
|
|
231
|
+
const target = event.target
|
|
232
|
+
if (!(target instanceof Element)) return
|
|
233
|
+
const anchor = target.closest("a")
|
|
234
|
+
if (!(anchor instanceof HTMLAnchorElement)) return
|
|
235
|
+
if (anchor.target && anchor.target !== "_self") return
|
|
236
|
+
if (anchor.hasAttribute("download")) return
|
|
237
|
+
if (anchor.relList.contains("external")) return
|
|
238
|
+
if (!anchor.getAttribute("href")) return
|
|
239
|
+
|
|
240
|
+
const url = new URL(anchor.href, window.location.href)
|
|
241
|
+
if (url.origin !== window.location.origin) return
|
|
242
|
+
if (isReservedPath(url.pathname)) return
|
|
243
|
+
// Only intercept destinations the app actually routes; anything else may
|
|
244
|
+
// be a real server resource and keeps native navigation.
|
|
245
|
+
if (!routes.some((route) => matchPath(route.path, url.pathname))) return
|
|
246
|
+
// Same-document hash links keep native scroll behavior.
|
|
247
|
+
if (
|
|
248
|
+
url.hash &&
|
|
249
|
+
url.pathname === window.location.pathname &&
|
|
250
|
+
url.search === window.location.search
|
|
251
|
+
) {
|
|
252
|
+
return
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
event.preventDefault()
|
|
256
|
+
navigate(url.pathname + url.search + url.hash)
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
document.addEventListener("click", onClick)
|
|
260
|
+
return () => document.removeEventListener("click", onClick)
|
|
261
|
+
}, [navigate])
|
|
262
|
+
|
|
263
|
+
return null
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
// Shared presentational fallback used by the not-found view and the error
|
|
267
|
+
// boundary. Styled with @sixb/ui design tokens (var(--token)) so it adopts the
|
|
268
|
+
// app's theme when those tokens are loaded, but every token has a hardcoded
|
|
269
|
+
// fallback so it stays self-contained and readable when they are not. The
|
|
270
|
+
// background/foreground pair is always set together so text stays legible
|
|
271
|
+
// regardless of the surrounding app's background.
|
|
272
|
+
function AppFallback({
|
|
273
|
+
title,
|
|
274
|
+
detail,
|
|
275
|
+
showReload = true,
|
|
276
|
+
action,
|
|
277
|
+
}: {
|
|
278
|
+
title: string
|
|
279
|
+
detail: string
|
|
280
|
+
showReload?: boolean
|
|
281
|
+
action?: React.ReactNode
|
|
282
|
+
}) {
|
|
283
|
+
return (
|
|
284
|
+
<div
|
|
285
|
+
role="alert"
|
|
286
|
+
style={{
|
|
287
|
+
minHeight: "100dvh",
|
|
288
|
+
display: "flex",
|
|
289
|
+
flexDirection: "column",
|
|
290
|
+
alignItems: "center",
|
|
291
|
+
justifyContent: "center",
|
|
292
|
+
gap: "1rem",
|
|
293
|
+
padding: "2rem",
|
|
294
|
+
textAlign: "center",
|
|
295
|
+
fontFamily:
|
|
296
|
+
"var(--font-sans, system-ui, -apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif)",
|
|
297
|
+
background: "var(--background, #ffffff)",
|
|
298
|
+
color: "var(--foreground, #1f2933)",
|
|
299
|
+
}}
|
|
300
|
+
>
|
|
301
|
+
<h1 style={{ margin: 0, fontSize: "1.5rem", fontWeight: 600 }}>{title}</h1>
|
|
302
|
+
<p style={{ margin: 0, maxWidth: "32rem", color: "var(--muted-foreground, #52606d)" }}>
|
|
303
|
+
{detail}
|
|
304
|
+
</p>
|
|
305
|
+
<div style={{ display: "flex", gap: "0.75rem" }}>
|
|
306
|
+
{action ?? (
|
|
307
|
+
<>
|
|
308
|
+
<a
|
|
309
|
+
href="/"
|
|
310
|
+
style={{
|
|
311
|
+
padding: "0.5rem 1rem",
|
|
312
|
+
borderRadius: "var(--radius, 0.5rem)",
|
|
313
|
+
background: "var(--primary, #1f2933)",
|
|
314
|
+
color: "var(--primary-foreground, #ffffff)",
|
|
315
|
+
textDecoration: "none",
|
|
316
|
+
}}
|
|
317
|
+
>
|
|
318
|
+
Go home
|
|
319
|
+
</a>
|
|
320
|
+
{showReload ? (
|
|
321
|
+
<button
|
|
322
|
+
type="button"
|
|
323
|
+
onClick={() => window.location.reload()}
|
|
324
|
+
style={{
|
|
325
|
+
padding: "0.5rem 1rem",
|
|
326
|
+
borderRadius: "var(--radius, 0.5rem)",
|
|
327
|
+
border: "1px solid var(--border, #cbd2d9)",
|
|
328
|
+
background: "transparent",
|
|
329
|
+
color: "inherit",
|
|
330
|
+
cursor: "pointer",
|
|
331
|
+
}}
|
|
332
|
+
>
|
|
333
|
+
Reload
|
|
334
|
+
</button>
|
|
335
|
+
) : null}
|
|
336
|
+
</>
|
|
337
|
+
)}
|
|
338
|
+
</div>
|
|
339
|
+
</div>
|
|
340
|
+
)
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
function AccessDeniedView() {
|
|
344
|
+
const [isSigningOut, setIsSigningOut] = React.useState(false)
|
|
345
|
+
|
|
346
|
+
async function handleSignOut() {
|
|
347
|
+
setIsSigningOut(true)
|
|
348
|
+
try {
|
|
349
|
+
await signOut({ throwOnError: true })
|
|
350
|
+
window.location.reload()
|
|
351
|
+
} catch {
|
|
352
|
+
setIsSigningOut(false)
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
return (
|
|
357
|
+
<AppFallback
|
|
358
|
+
title="Access required"
|
|
359
|
+
detail="Your account is signed in, but it does not have permission to access this app."
|
|
360
|
+
showReload={false}
|
|
361
|
+
action={
|
|
362
|
+
<button
|
|
363
|
+
type="button"
|
|
364
|
+
disabled={isSigningOut}
|
|
365
|
+
onClick={handleSignOut}
|
|
366
|
+
style={{
|
|
367
|
+
padding: "0.5rem 1rem",
|
|
368
|
+
borderRadius: "var(--radius, 0.5rem)",
|
|
369
|
+
border: "1px solid var(--border, #cbd2d9)",
|
|
370
|
+
background: "transparent",
|
|
371
|
+
color: "inherit",
|
|
372
|
+
cursor: isSigningOut ? "default" : "pointer",
|
|
373
|
+
}}
|
|
374
|
+
>
|
|
375
|
+
{isSigningOut ? "Signing out…" : "Sign out"}
|
|
376
|
+
</button>
|
|
377
|
+
}
|
|
378
|
+
/>
|
|
379
|
+
)
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
// The not-found view. Rendered both for unmatched client routes (the catch-all
|
|
383
|
+
// route below) and for an expected 404 surfaced through a query. Apps will be
|
|
384
|
+
// able to override this via app/not-found.tsx.
|
|
385
|
+
function NotFoundView() {
|
|
386
|
+
return (
|
|
387
|
+
<AppFallback
|
|
388
|
+
title="Not found"
|
|
389
|
+
detail="The page or resource you requested does not exist."
|
|
390
|
+
showReload={false}
|
|
391
|
+
/>
|
|
392
|
+
)
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
// Last-resort safety net so an uncaught render throw (a 404 surfaced through a
|
|
396
|
+
// suspense/throwOnError query, a bug, a failed import) shows a fallback instead
|
|
397
|
+
// of a blank page and a console error. A missing object is an expected state,
|
|
398
|
+
// so a 404 reuses the not-found view rather than the generic crash screen.
|
|
399
|
+
// Apps will be able to override this via app/error.tsx.
|
|
400
|
+
function AppErrorFallback({ error }: { error: unknown }) {
|
|
401
|
+
if (isSixbApiError(error) && error.status === 404) {
|
|
402
|
+
return <NotFoundView />
|
|
403
|
+
}
|
|
404
|
+
return (
|
|
405
|
+
<AppFallback
|
|
406
|
+
title="Something went wrong"
|
|
407
|
+
detail={error instanceof Error ? error.message : String(error)}
|
|
408
|
+
/>
|
|
409
|
+
)
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
class AppErrorBoundary extends React.Component<
|
|
413
|
+
{ resetKey: string; children: React.ReactNode },
|
|
414
|
+
{ error: unknown }
|
|
415
|
+
> {
|
|
416
|
+
state: { error: unknown } = { error: null }
|
|
417
|
+
|
|
418
|
+
static getDerivedStateFromError(error: unknown) {
|
|
419
|
+
return { error }
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
componentDidCatch(error: unknown) {
|
|
423
|
+
console.error("[SixbApp] Uncaught render error:", error)
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
// Clear the error when the route changes so navigating away (or an in-app
|
|
427
|
+
// link) recovers without a full reload.
|
|
428
|
+
componentDidUpdate(prevProps: { resetKey: string }) {
|
|
429
|
+
if (this.state.error !== null && prevProps.resetKey !== this.props.resetKey) {
|
|
430
|
+
this.setState({ error: null })
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
render() {
|
|
435
|
+
if (this.state.error !== null) {
|
|
436
|
+
return <AppErrorFallback error={this.state.error} />
|
|
437
|
+
}
|
|
438
|
+
return this.props.children
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
function RoutedErrorBoundary({ children }: { children: React.ReactNode }) {
|
|
443
|
+
const location = useLocation()
|
|
444
|
+
return (
|
|
445
|
+
<AppErrorBoundary resetKey={location.pathname + location.search}>{children}</AppErrorBoundary>
|
|
446
|
+
)
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
function App() {
|
|
450
|
+
return (
|
|
451
|
+
<QueryClientProvider client={queryClient}>
|
|
452
|
+
<BrowserRouter>
|
|
453
|
+
<InternalLinkInterceptor />
|
|
454
|
+
<RoutedErrorBoundary>
|
|
455
|
+
${layoutWrapperStart}
|
|
456
|
+
<Routes>
|
|
457
|
+
{routes.map((route) => (
|
|
458
|
+
<Route key={route.path} path={route.path} element={<route.component />} />
|
|
459
|
+
))}
|
|
460
|
+
<Route path="*" element={<NotFoundView />} />
|
|
461
|
+
</Routes>
|
|
462
|
+
${layoutWrapperEnd}
|
|
463
|
+
</RoutedErrorBoundary>
|
|
464
|
+
</BrowserRouter>
|
|
465
|
+
</QueryClientProvider>
|
|
466
|
+
)
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
if (canRenderApp) {
|
|
470
|
+
getRoot().render(<App />)
|
|
471
|
+
} else if (applicationAccessDenied) {
|
|
472
|
+
getRoot().render(<AccessDeniedView />)
|
|
473
|
+
}
|
|
474
|
+
`
|
|
475
|
+
|
|
476
|
+
const mainPath = join(generatedDir, "main.tsx")
|
|
477
|
+
await writeFileIfChanged(mainPath, mainContent)
|
|
478
|
+
|
|
479
|
+
// Generate index.html with static identity and only structural reset rules.
|
|
480
|
+
// Apps own visual styling through app/globals.css.
|
|
481
|
+
const metadataHead = renderMetadataHead(metadata)
|
|
482
|
+
const htmlContent = `<!DOCTYPE html>
|
|
483
|
+
<html lang="en">
|
|
484
|
+
<head>
|
|
485
|
+
<meta charset="UTF-8" />
|
|
486
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0, viewport-fit=cover" />
|
|
487
|
+
${metadataHead}
|
|
488
|
+
${runtimeConfigScript}
|
|
489
|
+
<style>
|
|
490
|
+
* {
|
|
491
|
+
box-sizing: border-box;
|
|
492
|
+
}
|
|
493
|
+
html,
|
|
494
|
+
body,
|
|
495
|
+
#root {
|
|
496
|
+
margin: 0;
|
|
497
|
+
min-height: 100%;
|
|
498
|
+
}
|
|
499
|
+
body,
|
|
500
|
+
#root {
|
|
501
|
+
min-height: 100vh;
|
|
502
|
+
min-height: 100dvh;
|
|
503
|
+
}
|
|
504
|
+
@media (display-mode: standalone) {
|
|
505
|
+
html,
|
|
506
|
+
body {
|
|
507
|
+
overscroll-behavior: none;
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
</style>
|
|
511
|
+
</head>
|
|
512
|
+
<body>
|
|
513
|
+
<div id="root"></div>
|
|
514
|
+
<script type="module" src="./main.tsx"></script>
|
|
515
|
+
</body>
|
|
516
|
+
</html>
|
|
517
|
+
`
|
|
518
|
+
|
|
519
|
+
const htmlPath = join(generatedDir, "index.html")
|
|
520
|
+
await writeFileIfChanged(htmlPath, htmlContent)
|
|
521
|
+
|
|
522
|
+
return { htmlPath, mainPath, manifestPath }
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
function renderMetadataHead(metadata: Awaited<ReturnType<typeof resolveAppMetadata>>): string {
|
|
526
|
+
const tags = [
|
|
527
|
+
` <title>${escapeHtmlText(metadata.title)}</title>`,
|
|
528
|
+
...(metadata.description
|
|
529
|
+
? [` <meta name="description" content="${escapeHtmlAttribute(metadata.description)}" />`]
|
|
530
|
+
: []),
|
|
531
|
+
` <meta name="theme-color" content="${escapeHtmlAttribute(metadata.themeColor)}" />`,
|
|
532
|
+
' <link rel="manifest" href="/app.webmanifest" />',
|
|
533
|
+
...(metadata.favicon
|
|
534
|
+
? [` <link rel="icon" href="${escapeHtmlAttribute(metadata.favicon)}" />`]
|
|
535
|
+
: []),
|
|
536
|
+
...(metadata.appleTouchIcon
|
|
537
|
+
? [` <link rel="apple-touch-icon" href="${metadata.appleTouchIcon}" />`]
|
|
538
|
+
: []),
|
|
539
|
+
]
|
|
540
|
+
return tags.join("\n")
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
function escapeHtmlText(value: string): string {
|
|
544
|
+
return value.replaceAll("&", "&").replaceAll("<", "<").replaceAll(">", ">")
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
function escapeHtmlAttribute(value: string): string {
|
|
548
|
+
return escapeHtmlText(value).replaceAll('"', """).replaceAll("'", "'")
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
async function writeFileIfChanged(path: string, content: string): Promise<void> {
|
|
552
|
+
try {
|
|
553
|
+
if ((await readFile(path, "utf-8")) === content) {
|
|
554
|
+
return
|
|
555
|
+
}
|
|
556
|
+
} catch (error) {
|
|
557
|
+
if (!isFileNotFoundError(error)) {
|
|
558
|
+
throw error
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
await writeFile(path, content, "utf-8")
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
function isFileNotFoundError(error: unknown): boolean {
|
|
566
|
+
return (
|
|
567
|
+
error instanceof Error &&
|
|
568
|
+
"code" in error &&
|
|
569
|
+
(error as Error & { readonly code?: unknown }).code === "ENOENT"
|
|
570
|
+
)
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
function relativeTo(from: string, to: string): string {
|
|
574
|
+
let rel = relative(from, to)
|
|
575
|
+
// Ensure it starts with ./ or ../
|
|
576
|
+
if (!rel.startsWith(".")) rel = `./${rel}`
|
|
577
|
+
return rel
|
|
578
|
+
}
|
|
579
|
+
|
|
580
|
+
async function fileExists(path: string): Promise<boolean> {
|
|
581
|
+
try {
|
|
582
|
+
await access(path)
|
|
583
|
+
return true
|
|
584
|
+
} catch {
|
|
585
|
+
return false
|
|
586
|
+
}
|
|
587
|
+
}
|