toiljs 0.0.14 → 0.0.16
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/.babelrc +13 -13
- package/.gitattributes +2 -2
- package/.github/ISSUE_TEMPLATE/bug_report.md +38 -38
- package/.github/ISSUE_TEMPLATE/bug_report.yml +90 -90
- package/.github/ISSUE_TEMPLATE/config.yml +8 -8
- package/.github/ISSUE_TEMPLATE/feature_request.md +20 -20
- package/.github/PULL_REQUEST_TEMPLATE.md +43 -43
- package/.github/changelog-config.json +45 -45
- package/.github/dependabot.yml +27 -27
- package/.github/workflows/ci.yml +191 -191
- package/.prettierrc.json +11 -11
- package/.vscode/settings.json +9 -9
- package/CHANGELOG.md +5 -5
- package/LICENSE +187 -187
- package/README.md +339 -315
- package/as-pect.asconfig.json +34 -34
- package/as-pect.config.js +65 -65
- package/assets/logo.svg +36 -36
- package/build/backend/.tsbuildinfo +1 -1
- package/build/cli/.tsbuildinfo +1 -1
- package/build/cli/index.js +2926 -191
- package/build/client/.tsbuildinfo +1 -1
- package/build/client/dev/devtools.d.ts +6 -0
- package/build/client/dev/devtools.js +442 -0
- package/build/client/dev/error-overlay.d.ts +9 -0
- package/build/client/dev/error-overlay.js +19 -4
- package/build/client/head/metadata.d.ts +3 -1
- package/build/client/head/metadata.js +8 -0
- package/build/client/index.d.ts +4 -4
- package/build/client/index.js +2 -2
- package/build/client/navigation/navigation.d.ts +2 -0
- package/build/client/navigation/navigation.js +9 -1
- package/build/client/navigation/prefetch.d.ts +1 -0
- package/build/client/navigation/prefetch.js +35 -0
- package/build/client/routing/Router.js +1 -1
- package/build/client/routing/hooks.js +6 -2
- package/build/client/routing/loader.d.ts +25 -0
- package/build/client/routing/loader.js +53 -7
- package/build/client/routing/mount.js +4 -3
- package/build/compiler/.tsbuildinfo +1 -1
- package/build/compiler/config.d.ts +18 -0
- package/build/compiler/config.js +8 -0
- package/build/compiler/docs.js +16 -16
- package/build/compiler/generate.js +3 -0
- package/build/compiler/index.d.ts +2 -2
- package/build/compiler/index.js +3 -1
- package/build/compiler/plugin.js +156 -0
- package/build/compiler/prerender.d.ts +1 -0
- package/build/compiler/prerender.js +2 -1
- package/build/compiler/seo.d.ts +2 -2
- package/build/compiler/seo.js +8 -6
- package/build/compiler/ssg.d.ts +5 -0
- package/build/compiler/ssg.js +121 -0
- package/build/io/.tsbuildinfo +1 -1
- package/build/logger/.tsbuildinfo +1 -1
- package/build/shared/.tsbuildinfo +1 -1
- package/eslint.config.js +48 -48
- package/examples/basic/client/404.tsx +11 -11
- package/examples/basic/client/components/.gitkeep +1 -1
- package/examples/basic/client/global-error.tsx +13 -13
- package/examples/basic/client/layout.tsx +25 -25
- package/examples/basic/client/public/images/.gitkeep +1 -1
- package/examples/basic/client/public/images/logo.svg +36 -36
- package/examples/basic/client/public/robots.txt +2 -2
- package/examples/basic/client/routes/docs/[...slug].tsx +12 -12
- package/examples/basic/client/routes/features/error/error.tsx +16 -16
- package/examples/basic/client/routes/features/template/b.tsx +14 -14
- package/examples/basic/client/routes/files/[[...slug]].tsx +21 -21
- package/examples/basic/client/routes/gallery/layout.tsx +13 -13
- package/examples/basic/client/routes/io.tsx +24 -24
- package/examples/basic/client/routes/loader-demo/loading.tsx +13 -13
- package/examples/basic/client/routes/search.tsx +61 -61
- package/examples/basic/client/toil.tsx +5 -5
- package/package.json +155 -147
- package/presets/eslint.js +88 -88
- package/presets/no-uint8array-tostring.js +200 -200
- package/presets/prettier.json +18 -18
- package/presets/tsconfig.json +37 -37
- package/src/backend/index.ts +160 -160
- package/src/cli/proc.ts +50 -50
- package/src/cli/updates.ts +69 -69
- package/src/cli/validate.ts +31 -31
- package/src/client/channel/channel.ts +146 -146
- package/src/client/components/Form.tsx +65 -65
- package/src/client/components/Script.tsx +113 -113
- package/src/client/components/Slot.tsx +21 -21
- package/src/client/dev/devtools.tsx +973 -0
- package/src/client/dev/error-overlay.tsx +30 -4
- package/src/client/head/head.ts +167 -167
- package/src/client/head/metadata.ts +19 -1
- package/src/client/index.ts +19 -9
- package/src/client/navigation/NavLink.tsx +86 -86
- package/src/client/navigation/navigation.ts +25 -5
- package/src/client/navigation/prefetch.ts +169 -130
- package/src/client/navigation/scroll.ts +53 -53
- package/src/client/routing/Router.tsx +8 -2
- package/src/client/routing/action.ts +122 -122
- package/src/client/routing/error-boundary.tsx +43 -43
- package/src/client/routing/hooks.ts +21 -6
- package/src/client/routing/loader.ts +325 -225
- package/src/client/routing/match.ts +47 -47
- package/src/client/routing/mount.tsx +54 -52
- package/src/client/routing/params-context.ts +10 -10
- package/src/client/routing/slot-context.ts +7 -7
- package/src/client/search/search.ts +189 -189
- package/src/client/search/use-page-search.ts +73 -73
- package/src/client/types.ts +73 -73
- package/src/compiler/config.ts +47 -1
- package/src/compiler/docs.ts +228 -228
- package/src/compiler/generate.ts +394 -391
- package/src/compiler/index.ts +64 -54
- package/src/compiler/pages.ts +70 -70
- package/src/compiler/plugin.ts +170 -2
- package/src/compiler/prerender.ts +5 -1
- package/src/compiler/seo.ts +23 -7
- package/src/compiler/ssg.ts +162 -0
- package/src/io/BinaryReader.ts +340 -340
- package/src/io/BinaryWriter.ts +385 -385
- package/src/io/FastMap.ts +127 -127
- package/src/io/index.ts +11 -11
- package/src/io/lengths.ts +14 -14
- package/src/io/types.ts +18 -18
- package/src/logger/index.ts +22 -22
- package/src/server/index.ts +10 -10
- package/src/server/main.ts +13 -13
- package/src/server/tsconfig.json +4 -4
- package/src/shared/index.ts +10 -10
- package/std/client/index.d.ts +15 -15
- package/std/client/package.json +3 -3
- package/test/assembly/example.spec.ts +7 -7
- package/test/channel.test.ts +21 -21
- package/test/dom/Link.test.tsx +47 -47
- package/test/dom/NavLink.test.tsx +37 -37
- package/test/dom/error-overlay.test.tsx +44 -44
- package/test/dom/loader.test.tsx +121 -121
- package/test/dom/navigation.test.ts +59 -59
- package/test/dom/revalidate.test.tsx +38 -38
- package/test/dom/route-head.test.tsx +78 -78
- package/test/dom/router-loading.test.tsx +44 -44
- package/test/dom/scroll.test.ts +56 -56
- package/test/dom/use-metadata.test.tsx +58 -0
- package/test/io.test.ts +93 -93
- package/test/navlink.test.ts +28 -28
- package/test/placeholder.test.ts +9 -9
- package/test/routes.test.ts +76 -76
- package/test/seo.test.ts +175 -164
- package/test/slot-layouts.test.ts +69 -69
- package/test/ssg.test.ts +36 -0
- package/test/update.test.ts +44 -44
- package/test/validate.test.ts +42 -42
- package/toil-routes.d.ts +7 -0
- package/toilconfig.json +30 -30
- package/tsconfig.backend.json +13 -13
- package/tsconfig.base.json +35 -35
- package/tsconfig.cli.json +13 -13
- package/tsconfig.client.json +14 -14
- package/tsconfig.compiler.json +13 -13
- package/tsconfig.io.json +12 -12
- package/tsconfig.json +22 -22
- package/tsconfig.logger.json +12 -12
- package/tsconfig.server.json +10 -10
- package/tsconfig.shared.json +12 -12
- package/vitest.config.ts +26 -26
- package/.idea/codeStyles/Project.xml +0 -54
- package/.idea/codeStyles/codeStyleConfig.xml +0 -5
- package/.idea/inspectionProfiles/Project_Default.xml +0 -6
- package/.idea/modules.xml +0 -8
- package/.idea/prettier.xml +0 -7
- package/.idea/toiljs.iml +0 -8
- package/.idea/vcs.xml +0 -6
- package/.toil/entry.tsx +0 -9
- package/.toil/index.html +0 -12
- package/.toil/routes.ts +0 -9
- package/build/cli/configure.d.ts +0 -16
- package/build/cli/configure.js +0 -272
- package/build/cli/create.d.ts +0 -16
- package/build/cli/create.js +0 -420
- package/build/cli/diagnostics.d.ts +0 -55
- package/build/cli/diagnostics.js +0 -333
- package/build/cli/doctor.d.ts +0 -6
- package/build/cli/doctor.js +0 -249
- package/build/cli/features.d.ts +0 -25
- package/build/cli/features.js +0 -107
- package/build/cli/index.d.ts +0 -2
- package/build/cli/proc.d.ts +0 -6
- package/build/cli/proc.js +0 -31
- package/build/cli/ui.d.ts +0 -9
- package/build/cli/ui.js +0 -75
- package/build/cli/update.d.ts +0 -7
- package/build/cli/update.js +0 -117
- package/build/cli/updates.d.ts +0 -10
- package/build/cli/updates.js +0 -45
- package/build/cli/validate.d.ts +0 -4
- package/build/cli/validate.js +0 -19
- package/build/client/Link.d.ts +0 -8
- package/build/client/Link.js +0 -44
- package/build/client/NavLink.d.ts +0 -14
- package/build/client/NavLink.js +0 -37
- package/build/client/Router.d.ts +0 -7
- package/build/client/Router.js +0 -55
- package/build/client/channel.d.ts +0 -23
- package/build/client/channel.js +0 -94
- package/build/client/error-boundary.d.ts +0 -16
- package/build/client/error-boundary.js +0 -19
- package/build/client/head.d.ts +0 -26
- package/build/client/head.js +0 -87
- package/build/client/hooks.d.ts +0 -17
- package/build/client/hooks.js +0 -48
- package/build/client/lazy.d.ts +0 -16
- package/build/client/lazy.js +0 -53
- package/build/client/match.d.ts +0 -2
- package/build/client/match.js +0 -32
- package/build/client/mount.d.ts +0 -2
- package/build/client/mount.js +0 -13
- package/build/client/navigation.d.ts +0 -13
- package/build/client/navigation.js +0 -97
- package/build/client/params-context.d.ts +0 -2
- package/build/client/params-context.js +0 -2
- package/build/client/prefetch.d.ts +0 -11
- package/build/client/prefetch.js +0 -100
- package/build/client/runtime.d.ts +0 -31
- package/build/client/runtime.js +0 -112
- package/build/client/scroll.d.ts +0 -8
- package/build/client/scroll.js +0 -36
- package/toil-env.d.ts +0 -16
|
@@ -0,0 +1,973 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Development-only dev toolbar. A floating badge (bottom corner) that expands into a tabbed panel
|
|
3
|
+
* showing the matched route, build/config info, captured errors, and preferences, with live feature
|
|
4
|
+
* toggles, click-to-navigate, and open-in-editor. Rendered as a sibling at the app root in dev only
|
|
5
|
+
* (see `mount`), behind `isDevMode()`, so the whole module is dead-code-eliminated in production.
|
|
6
|
+
*
|
|
7
|
+
* It stays decoupled from the Router (it computes the current match itself via `matchRoute`) so it
|
|
8
|
+
* renders even when the app tree has crashed.
|
|
9
|
+
*/
|
|
10
|
+
import { useEffect, useState, useSyncExternalStore, type ReactNode } from 'react';
|
|
11
|
+
|
|
12
|
+
import { type DevError, getErrorLog, subscribeErrors } from './error-overlay.js';
|
|
13
|
+
import {
|
|
14
|
+
isNavigationPending,
|
|
15
|
+
navigate,
|
|
16
|
+
setTransitions,
|
|
17
|
+
setViewTransitions,
|
|
18
|
+
subscribeLocation,
|
|
19
|
+
subscribePending,
|
|
20
|
+
} from '../navigation/navigation.js';
|
|
21
|
+
import {
|
|
22
|
+
clearLoaderData,
|
|
23
|
+
inspectLoaderCache,
|
|
24
|
+
loaderKey,
|
|
25
|
+
revalidate,
|
|
26
|
+
subscribeLoaderCache,
|
|
27
|
+
type LoaderCacheSnapshot,
|
|
28
|
+
} from '../routing/loader.js';
|
|
29
|
+
import { matchRoute } from '../routing/match.js';
|
|
30
|
+
import { getPages } from '../search/search.js';
|
|
31
|
+
import type { Href, RouteDef } from '../types.js';
|
|
32
|
+
|
|
33
|
+
type Tab = 'route' | 'data' | 'head' | 'build' | 'errors' | 'ai' | 'prefs';
|
|
34
|
+
|
|
35
|
+
/** Base URL for the quick-doc links (the project homepage's docs section). */
|
|
36
|
+
const DOCS_BASE = 'https://toil.org/docs';
|
|
37
|
+
|
|
38
|
+
/** The toiljs brand mark (inlined from assets/logo.svg; unique gradient ids to avoid collisions). */
|
|
39
|
+
function ToilLogo({ size = 16 }: { size?: number }): ReactNode {
|
|
40
|
+
return (
|
|
41
|
+
<svg
|
|
42
|
+
width={size}
|
|
43
|
+
height={size}
|
|
44
|
+
viewBox="0 0 500 500"
|
|
45
|
+
aria-hidden="true"
|
|
46
|
+
style={{ display: 'block', flex: '0 0 auto' }}>
|
|
47
|
+
<defs>
|
|
48
|
+
<linearGradient
|
|
49
|
+
id="toilDtA"
|
|
50
|
+
x1="43.27"
|
|
51
|
+
y1="43.27"
|
|
52
|
+
x2="467.12"
|
|
53
|
+
y2="467.12"
|
|
54
|
+
gradientUnits="userSpaceOnUse">
|
|
55
|
+
<stop offset="0" stopColor="#6990ff" />
|
|
56
|
+
<stop offset=".28" stopColor="#521be0" />
|
|
57
|
+
<stop offset=".66" stopColor="#6900f4" />
|
|
58
|
+
<stop offset="1" stopColor="#7f00f6" />
|
|
59
|
+
</linearGradient>
|
|
60
|
+
<linearGradient
|
|
61
|
+
id="toilDtB"
|
|
62
|
+
x1="149.99"
|
|
63
|
+
y1="355.49"
|
|
64
|
+
x2="149.99"
|
|
65
|
+
y2="0"
|
|
66
|
+
gradientUnits="userSpaceOnUse">
|
|
67
|
+
<stop offset=".15" stopColor="#6990ff" stopOpacity=".6" />
|
|
68
|
+
<stop offset=".55" stopColor="#531ae1" />
|
|
69
|
+
</linearGradient>
|
|
70
|
+
</defs>
|
|
71
|
+
<rect width="500" height="500" rx="130" ry="130" fill="url(#toilDtA)" />
|
|
72
|
+
<path d="M299.98,0L0,355.49v-225.49C0,58.2,58.2,0,130,0h169.98Z" fill="url(#toilDtB)" />
|
|
73
|
+
<path
|
|
74
|
+
d="M106.17,111.11h285.24c9.9,0,16.7,9.96,13.09,19.18l-17.98,45.96c-2.11,5.39-7.31,8.94-13.09,8.94h-74.65c-7.76,0-14.06,6.29-14.06,14.06v214.94c0,7.76-6.29,14.06-14.06,14.06h-45.96c-7.76,0-14.06-6.29-14.06-14.06v-217.25c0-7.76-6.29-14.06-14.06-14.06h-73.66c-5.82,0-11.04-3.59-13.12-9.02l-16.76-43.64c-3.54-9.21,3.26-19.1,13.12-19.1Z"
|
|
75
|
+
fill="#fff"
|
|
76
|
+
/>
|
|
77
|
+
</svg>
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** Anthropic / Claude brand mark. */
|
|
82
|
+
function ClaudeLogo({ size = 16 }: { size?: number }): ReactNode {
|
|
83
|
+
return (
|
|
84
|
+
<svg
|
|
85
|
+
width={size}
|
|
86
|
+
height={size}
|
|
87
|
+
viewBox="0 0 92 65"
|
|
88
|
+
aria-hidden="true"
|
|
89
|
+
style={{ display: 'block', flex: '0 0 auto' }}>
|
|
90
|
+
<path
|
|
91
|
+
fill="#d97757"
|
|
92
|
+
d="M66.5 0H52.4L78 65h14.1L66.5 0zM25.6 0L0 65h14.4l5.2-13.6h26.8L51.6 65H66L40.4 0H25.6zm-1.2 39.3l8.8-22.8 8.8 22.8H24.4z"
|
|
93
|
+
/>
|
|
94
|
+
</svg>
|
|
95
|
+
);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** OpenAI / ChatGPT brand mark. */
|
|
99
|
+
function ChatGptLogo({ size = 16 }: { size?: number }): ReactNode {
|
|
100
|
+
return (
|
|
101
|
+
<svg
|
|
102
|
+
width={size}
|
|
103
|
+
height={size}
|
|
104
|
+
viewBox="0 0 24 24"
|
|
105
|
+
aria-hidden="true"
|
|
106
|
+
style={{ display: 'block', flex: '0 0 auto' }}>
|
|
107
|
+
<path
|
|
108
|
+
fill="#10a37f"
|
|
109
|
+
d="M22.2819 9.8211a5.9847 5.9847 0 0 0-.5157-4.9108 6.0462 6.0462 0 0 0-6.5098-2.9A6.0651 6.0651 0 0 0 4.9807 4.1818a5.9847 5.9847 0 0 0-3.9977 2.9 6.0462 6.0462 0 0 0 .7427 7.0966 5.98 5.98 0 0 0 .511 4.9107 6.051 6.051 0 0 0 6.5146 2.9001A5.9847 5.9847 0 0 0 13.2599 24a6.0557 6.0557 0 0 0 5.7718-4.2058 5.9894 5.9894 0 0 0 3.9977-2.9001 6.0557 6.0557 0 0 0-.7475-7.0729zm-9.022 12.6081a4.4755 4.4755 0 0 1-2.8764-1.0408l.1419-.0804 4.7783-2.7582a.7948.7948 0 0 0 .3927-.6813v-6.7369l2.02 1.1686a.071.071 0 0 1 .038.052v5.5826a4.504 4.504 0 0 1-4.4945 4.4944zm-9.6607-4.1254a4.4708 4.4708 0 0 1-.5346-3.0137l.142.0852 4.783 2.7582a.7712.7712 0 0 0 .7806 0l5.8428-3.3685v2.3324a.0804.0804 0 0 1-.0332.0615L9.74 19.9502a4.4992 4.4992 0 0 1-6.1408-1.6464zM2.3408 7.8956a4.485 4.485 0 0 1 2.3655-1.9728V11.6a.7664.7664 0 0 0 .3879.6765l5.8144 3.3543-2.0201 1.1685a.0757.0757 0 0 1-.071 0l-4.8303-2.7865A4.504 4.504 0 0 1 2.3408 7.872zm16.5963 3.8558L13.1038 8.364 15.1192 7.2a.0757.0757 0 0 1 .071 0l4.8303 2.7913a4.4944 4.4944 0 0 1-.6765 8.1042v-5.6772a.79.79 0 0 0-.407-.667zm2.0107-3.0231l-.142-.0852-4.7735-2.7818a.7759.7759 0 0 0-.7854 0L9.409 9.2297V6.8974a.0662.0662 0 0 1 .0284-.0615l4.8303-2.7866a4.4992 4.4992 0 0 1 6.6802 4.66zM8.3065 12.863l-2.02-1.1638a.0804.0804 0 0 1-.038-.0567V6.0742a4.4992 4.4992 0 0 1 7.3757-3.4537l-.142.0805L8.704 5.459a.7948.7948 0 0 0-.3927.6813zm1.0976-2.3654l2.602-1.4998 2.6069 1.4998v2.9994l-2.5974 1.4997-2.6067-1.4997z"
|
|
110
|
+
/>
|
|
111
|
+
</svg>
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** Build/config info served by the dev server at `/__toil/devinfo`. */
|
|
116
|
+
interface DevInfo {
|
|
117
|
+
readonly toiljs: string;
|
|
118
|
+
readonly vite: string;
|
|
119
|
+
readonly react: string;
|
|
120
|
+
readonly port: number;
|
|
121
|
+
readonly enabled: boolean;
|
|
122
|
+
readonly flags: Record<string, boolean>;
|
|
123
|
+
readonly routes: Record<string, string>; // pattern -> absolute file
|
|
124
|
+
readonly ai: boolean;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// --- persisted panel state (localStorage) --------------------------------------------------------
|
|
128
|
+
|
|
129
|
+
interface Prefs {
|
|
130
|
+
open: boolean;
|
|
131
|
+
tab: Tab;
|
|
132
|
+
side: 'left' | 'right';
|
|
133
|
+
}
|
|
134
|
+
const PREFS_KEY = 'toil.devtools';
|
|
135
|
+
const defaultPrefs: Prefs = { open: false, tab: 'route', side: 'left' };
|
|
136
|
+
|
|
137
|
+
function loadPrefs(): Prefs {
|
|
138
|
+
try {
|
|
139
|
+
const raw = localStorage.getItem(PREFS_KEY);
|
|
140
|
+
return raw ? { ...defaultPrefs, ...(JSON.parse(raw) as Partial<Prefs>) } : defaultPrefs;
|
|
141
|
+
} catch {
|
|
142
|
+
return defaultPrefs;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
let prefs: Prefs = typeof localStorage !== 'undefined' ? loadPrefs() : defaultPrefs;
|
|
147
|
+
const prefListeners = new Set<() => void>();
|
|
148
|
+
function setPrefs(next: Partial<Prefs>): void {
|
|
149
|
+
prefs = { ...prefs, ...next };
|
|
150
|
+
try {
|
|
151
|
+
localStorage.setItem(PREFS_KEY, JSON.stringify(prefs));
|
|
152
|
+
} catch {
|
|
153
|
+
/* ignore */
|
|
154
|
+
}
|
|
155
|
+
for (const l of prefListeners) l();
|
|
156
|
+
}
|
|
157
|
+
function usePrefs(): Prefs {
|
|
158
|
+
return useSyncExternalStore(
|
|
159
|
+
(l) => {
|
|
160
|
+
prefListeners.add(l);
|
|
161
|
+
return () => prefListeners.delete(l);
|
|
162
|
+
},
|
|
163
|
+
() => prefs,
|
|
164
|
+
() => defaultPrefs,
|
|
165
|
+
);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// --- live location + nav + errors ----------------------------------------------------------------
|
|
169
|
+
|
|
170
|
+
function useCurrentUrl(): string {
|
|
171
|
+
return useSyncExternalStore(
|
|
172
|
+
subscribeLocation,
|
|
173
|
+
() => window.location.pathname + window.location.search,
|
|
174
|
+
() => '/',
|
|
175
|
+
);
|
|
176
|
+
}
|
|
177
|
+
function usePending(): boolean {
|
|
178
|
+
return useSyncExternalStore(subscribePending, isNavigationPending, () => false);
|
|
179
|
+
}
|
|
180
|
+
function useErrors(): readonly DevError[] {
|
|
181
|
+
return useSyncExternalStore(subscribeErrors, getErrorLog, () => getErrorLog());
|
|
182
|
+
}
|
|
183
|
+
function useLoaderCache(): readonly LoaderCacheSnapshot[] {
|
|
184
|
+
return useSyncExternalStore(subscribeLoaderCache, inspectLoaderCache, inspectLoaderCache);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/** JSON.stringify that won't throw on cyclic/odd data. */
|
|
188
|
+
function safeJson(value: unknown): string {
|
|
189
|
+
try {
|
|
190
|
+
return JSON.stringify(value, null, 2) ?? String(value);
|
|
191
|
+
} catch {
|
|
192
|
+
return String(value);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/** Reads the current document head's meta + link tags (live). */
|
|
197
|
+
function readHead(): { metas: { name: string; content: string }[]; links: { rel: string; href: string }[] } {
|
|
198
|
+
const metas: { name: string; content: string }[] = [];
|
|
199
|
+
const links: { rel: string; href: string }[] = [];
|
|
200
|
+
if (typeof document === 'undefined') return { metas, links };
|
|
201
|
+
document.head.querySelectorAll('meta').forEach((m) => {
|
|
202
|
+
const name = m.getAttribute('name') ?? m.getAttribute('property');
|
|
203
|
+
const content = m.getAttribute('content');
|
|
204
|
+
if (name && content) metas.push({ name, content });
|
|
205
|
+
});
|
|
206
|
+
document.head.querySelectorAll('link[rel]').forEach((l) => {
|
|
207
|
+
links.push({ rel: l.getAttribute('rel') ?? '', href: l.getAttribute('href') ?? '' });
|
|
208
|
+
});
|
|
209
|
+
return { metas, links };
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
// --- styles (injected once) ----------------------------------------------------------------------
|
|
213
|
+
|
|
214
|
+
const STYLE_ID = 'toil-devtools-style';
|
|
215
|
+
const CSS = `
|
|
216
|
+
.toil-dt{position:fixed;bottom:12px;z-index:2147483646;font:12px/1.5 ui-monospace,SFMono-Regular,Menlo,monospace;color:#e7e9f0}
|
|
217
|
+
.toil-dt.left{left:12px}.toil-dt.right{right:12px}
|
|
218
|
+
.toil-dt-badge{display:flex;align-items:center;gap:7px;background:#15151c;border:1px solid #2c2c38;border-radius:999px;padding:5px 11px 5px 8px;cursor:pointer;box-shadow:0 4px 16px rgba(0,0,0,.35);user-select:none}
|
|
219
|
+
.toil-dt-badge:hover{border-color:#3a3a48}
|
|
220
|
+
.toil-dt-dot{width:8px;height:8px;border-radius:50%;background:#22e3ab;box-shadow:0 0 6px #22e3ab}
|
|
221
|
+
.toil-dt-dot.pending{background:#f7b93e;box-shadow:0 0 6px #f7b93e;animation:toil-dt-pulse 1s infinite}
|
|
222
|
+
.toil-dt-dot.error{background:#ef4444;box-shadow:0 0 6px #ef4444}
|
|
223
|
+
@keyframes toil-dt-pulse{0%,100%{opacity:1}50%{opacity:.4}}
|
|
224
|
+
.toil-dt-logo{font-weight:700;background:linear-gradient(90deg,#2563ff,#7c3aed,#22e3ab);-webkit-background-clip:text;background-clip:text;color:transparent}
|
|
225
|
+
.toil-dt-panel{width:380px;max-width:calc(100vw - 24px);max-height:min(70vh,560px);background:#101016;border:1px solid #2c2c38;border-radius:12px;box-shadow:0 16px 56px rgba(0,0,0,.55);display:flex;flex-direction:column;overflow:hidden}
|
|
226
|
+
.toil-dt-tabs{display:flex;border-bottom:1px solid #23232e;flex:0 0 auto}
|
|
227
|
+
.toil-dt-tab{flex:1;padding:8px 4px;background:none;border:0;color:#8b90a4;font:inherit;cursor:pointer;border-bottom:2px solid transparent}
|
|
228
|
+
.toil-dt-tab.active{color:#e7e9f0;border-bottom-color:#2563ff}
|
|
229
|
+
.toil-dt-tab:hover{color:#c8cee0}
|
|
230
|
+
.toil-dt-body{padding:12px 14px;overflow:auto}
|
|
231
|
+
.toil-dt-head{display:flex;align-items:center;justify-content:space-between;padding:8px 12px;border-bottom:1px solid #23232e;flex:0 0 auto}
|
|
232
|
+
.toil-dt-x{background:none;border:0;color:#8b90a4;cursor:pointer;font:inherit;font-size:14px}
|
|
233
|
+
.toil-dt-row{display:flex;justify-content:space-between;gap:10px;padding:3px 0;border-bottom:1px solid #1b1b24}
|
|
234
|
+
.toil-dt-k{color:#8b90a4}
|
|
235
|
+
.toil-dt-v{color:#e7e9f0;text-align:right;word-break:break-all}
|
|
236
|
+
.toil-dt-tag{display:inline-block;padding:1px 6px;border-radius:5px;background:#23232e;color:#a8b0c8;margin:1px 3px 1px 0;font-size:11px}
|
|
237
|
+
.toil-dt-rt{display:flex;align-items:center;gap:6px;padding:3px 0}
|
|
238
|
+
.toil-dt-rt a{color:#7aa2ff;text-decoration:none;cursor:pointer}.toil-dt-rt a:hover{text-decoration:underline}
|
|
239
|
+
.toil-dt-rt .dyn{color:#c8cee0;cursor:default}
|
|
240
|
+
.toil-dt-edit{margin-left:auto;background:none;border:0;color:#5b6178;cursor:pointer;font:inherit}.toil-dt-edit:hover{color:#7aa2ff}
|
|
241
|
+
.toil-dt-sec{margin:0 0 6px;color:#6b7088;text-transform:uppercase;letter-spacing:.05em;font-size:10px}
|
|
242
|
+
.toil-dt-sw{display:flex;align-items:center;justify-content:space-between;padding:5px 0}
|
|
243
|
+
.toil-dt-btn{font:inherit;color:#e7e9f0;background:#23232e;border:1px solid #33333f;border-radius:6px;padding:3px 9px;cursor:pointer}
|
|
244
|
+
.toil-dt-btn:hover{border-color:#454556}
|
|
245
|
+
.toil-dt-err{padding:6px 0;border-bottom:1px solid #1b1b24}
|
|
246
|
+
.toil-dt-err .msg{color:#ff8a8a;word-break:break-word}
|
|
247
|
+
.toil-dt-empty{color:#6b7088;padding:8px 0}
|
|
248
|
+
.toil-dt-pre{background:#0a0a0e;border:1px solid #1b1b24;border-radius:6px;padding:8px;max-height:200px;overflow:auto;white-space:pre-wrap;word-break:break-word;color:#c8cee0;margin:6px 0 0;font-size:11px}
|
|
249
|
+
.toil-dt-chk{display:flex;gap:8px;align-items:center;padding:3px 0}
|
|
250
|
+
.toil-dt-ok{color:#22e3ab}.toil-dt-bad{color:#ef4444}
|
|
251
|
+
.toil-dt-og{display:flex;gap:8px;border:1px solid #23232e;border-radius:8px;overflow:hidden;background:#0d0d13}
|
|
252
|
+
.toil-dt-og-img{width:72px;height:72px;object-fit:cover;flex:0 0 auto}
|
|
253
|
+
.toil-dt-og-body{padding:6px 8px;min-width:0}
|
|
254
|
+
.toil-dt-og-title{color:#e7e9f0;font-weight:600;white-space:nowrap;overflow:hidden;text-overflow:ellipsis}
|
|
255
|
+
.toil-dt-og-desc{color:#8b90a4;font-size:11px;display:-webkit-box;-webkit-line-clamp:2;-webkit-box-orient:vertical;overflow:hidden}
|
|
256
|
+
.toil-dt-ta{width:100%;box-sizing:border-box;background:#0a0a0e;border:1px solid #23232e;border-radius:6px;color:#e7e9f0;font:inherit;padding:7px 8px;resize:vertical;min-height:54px}
|
|
257
|
+
.toil-dt-ta:focus{outline:none;border-color:#2563ff}
|
|
258
|
+
.toil-dt-ai-btns{display:flex;gap:6px;flex-wrap:wrap;margin:8px 0}
|
|
259
|
+
.toil-dt-ai-btn{display:flex;align-items:center;gap:6px;font:inherit;color:#e7e9f0;background:#23232e;border:1px solid #33333f;border-radius:6px;padding:5px 10px;cursor:pointer}
|
|
260
|
+
.toil-dt-ai-btn:hover{border-color:#454556}
|
|
261
|
+
.toil-dt-doc{display:block;color:#7aa2ff;text-decoration:none;padding:3px 0}.toil-dt-doc:hover{text-decoration:underline}
|
|
262
|
+
.toil-dt-pal-wrap{position:fixed;inset:0;z-index:2147483647;display:flex;align-items:flex-start;justify-content:center;background:rgba(0,0,0,.45);padding-top:14vh}
|
|
263
|
+
.toil-dt-pal{width:440px;max-width:calc(100vw - 24px);background:#101016;border:1px solid #2c2c38;border-radius:12px;box-shadow:0 16px 56px rgba(0,0,0,.6);overflow:hidden;font:13px/1.5 ui-monospace,SFMono-Regular,Menlo,monospace;color:#e7e9f0}
|
|
264
|
+
.toil-dt-pal input{width:100%;box-sizing:border-box;background:none;border:0;border-bottom:1px solid #23232e;color:#e7e9f0;font:inherit;padding:11px 14px}
|
|
265
|
+
.toil-dt-pal input:focus{outline:none}
|
|
266
|
+
.toil-dt-pal-list{max-height:340px;overflow:auto;padding:4px}
|
|
267
|
+
.toil-dt-pal-item{display:flex;gap:8px;align-items:center;padding:7px 10px;border-radius:6px;cursor:pointer;color:#c8cee0}
|
|
268
|
+
.toil-dt-pal-item.sel{background:#1c1c26;color:#e7e9f0}
|
|
269
|
+
.toil-dt-pal-kind{color:#6b7088;font-size:11px;margin-left:auto}
|
|
270
|
+
`;
|
|
271
|
+
function injectStyles(): void {
|
|
272
|
+
if (typeof document === 'undefined' || document.getElementById(STYLE_ID)) return;
|
|
273
|
+
const el = document.createElement('style');
|
|
274
|
+
el.id = STYLE_ID;
|
|
275
|
+
el.textContent = CSS;
|
|
276
|
+
document.head.appendChild(el);
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
// --- helpers -------------------------------------------------------------------------------------
|
|
280
|
+
|
|
281
|
+
function isDynamic(pattern: string): boolean {
|
|
282
|
+
return /[:*]/.test(pattern);
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
function openInEditor(file: string): void {
|
|
286
|
+
void fetch(`/__toil/open?file=${encodeURIComponent(file)}`).catch(() => undefined);
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
function Row({ k, children }: { k: string; children: ReactNode }): ReactNode {
|
|
290
|
+
return (
|
|
291
|
+
<div className="toil-dt-row">
|
|
292
|
+
<span className="toil-dt-k">{k}</span>
|
|
293
|
+
<span className="toil-dt-v">{children}</span>
|
|
294
|
+
</div>
|
|
295
|
+
);
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
// --- tabs ----------------------------------------------------------------------------------------
|
|
299
|
+
|
|
300
|
+
function RouteTab({
|
|
301
|
+
routes,
|
|
302
|
+
slots,
|
|
303
|
+
info,
|
|
304
|
+
}: {
|
|
305
|
+
routes: RouteDef[];
|
|
306
|
+
slots: Record<string, RouteDef[]>;
|
|
307
|
+
info: DevInfo | null;
|
|
308
|
+
}): ReactNode {
|
|
309
|
+
const url = useCurrentUrl();
|
|
310
|
+
const pending = usePending();
|
|
311
|
+
const pathname = url.split('?')[0];
|
|
312
|
+
const search = url.slice(pathname.length);
|
|
313
|
+
|
|
314
|
+
let matched: { route: RouteDef; params: Record<string, string | string[]> } | null = null;
|
|
315
|
+
for (const r of routes) {
|
|
316
|
+
const params = matchRoute(r.pattern, pathname);
|
|
317
|
+
if (params) {
|
|
318
|
+
matched = { route: r, params };
|
|
319
|
+
break;
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
const activeSlots: string[] = [];
|
|
323
|
+
for (const [name, defs] of Object.entries(slots)) {
|
|
324
|
+
if (defs.some((d) => matchRoute(d.pattern, pathname))) activeSlots.push(name);
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
const has = (r: RouteDef): string =>
|
|
328
|
+
[
|
|
329
|
+
r.loading ? 'loading' : '',
|
|
330
|
+
r.errorComponent ? 'error' : '',
|
|
331
|
+
r.templates?.length ? 'template' : '',
|
|
332
|
+
r.layouts?.length ? `${String(r.layouts.length)} layout` : '',
|
|
333
|
+
]
|
|
334
|
+
.filter(Boolean)
|
|
335
|
+
.join(', ') || 'none';
|
|
336
|
+
|
|
337
|
+
return (
|
|
338
|
+
<div className="toil-dt-body">
|
|
339
|
+
<Row k="path">{pathname || '/'}</Row>
|
|
340
|
+
<Row k="match">{matched ? matched.route.pattern : 'no match (404)'}</Row>
|
|
341
|
+
{search && <Row k="query">{search}</Row>}
|
|
342
|
+
{matched && Object.keys(matched.params).length > 0 && (
|
|
343
|
+
<Row k="params">{JSON.stringify(matched.params)}</Row>
|
|
344
|
+
)}
|
|
345
|
+
{matched && <Row k="boundaries">{has(matched.route)}</Row>}
|
|
346
|
+
<Row k="slots">{activeSlots.length ? activeSlots.join(', ') : 'none'}</Row>
|
|
347
|
+
<Row k="navigating">{pending ? 'yes' : 'no'}</Row>
|
|
348
|
+
|
|
349
|
+
<p className="toil-dt-sec" style={{ marginTop: 12 }}>
|
|
350
|
+
Routes ({routes.length})
|
|
351
|
+
</p>
|
|
352
|
+
{routes.map((r) => {
|
|
353
|
+
const file = info?.routes[r.pattern];
|
|
354
|
+
return (
|
|
355
|
+
<div
|
|
356
|
+
className="toil-dt-rt"
|
|
357
|
+
key={r.pattern}>
|
|
358
|
+
{isDynamic(r.pattern) ? (
|
|
359
|
+
<span className="dyn">{r.pattern}</span>
|
|
360
|
+
) : (
|
|
361
|
+
<a
|
|
362
|
+
onClick={() => {
|
|
363
|
+
navigate(r.pattern as Href);
|
|
364
|
+
}}>
|
|
365
|
+
{r.pattern}
|
|
366
|
+
</a>
|
|
367
|
+
)}
|
|
368
|
+
{file && (
|
|
369
|
+
<button
|
|
370
|
+
className="toil-dt-edit"
|
|
371
|
+
title={`open ${file}`}
|
|
372
|
+
onClick={() => {
|
|
373
|
+
openInEditor(file);
|
|
374
|
+
}}>
|
|
375
|
+
edit
|
|
376
|
+
</button>
|
|
377
|
+
)}
|
|
378
|
+
</div>
|
|
379
|
+
);
|
|
380
|
+
})}
|
|
381
|
+
</div>
|
|
382
|
+
);
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
function DataTab(): ReactNode {
|
|
386
|
+
const url = useCurrentUrl();
|
|
387
|
+
const entries = useLoaderCache();
|
|
388
|
+
const pathname = url.split('?')[0];
|
|
389
|
+
const key = loaderKey(pathname, url.slice(pathname.length));
|
|
390
|
+
const entry = entries.find((e) => e.key === key);
|
|
391
|
+
return (
|
|
392
|
+
<div className="toil-dt-body">
|
|
393
|
+
{!entry && <p className="toil-dt-empty">No cached loader data for this route.</p>}
|
|
394
|
+
{entry && (
|
|
395
|
+
<>
|
|
396
|
+
<Row k="status">{entry.status}</Row>
|
|
397
|
+
<Row k="has loader">{entry.hasLoader ? 'yes' : 'no'}</Row>
|
|
398
|
+
<Row k="revalidate">
|
|
399
|
+
{entry.revalidate === false ? 'never' : `${String(entry.revalidate)}s`}
|
|
400
|
+
</Row>
|
|
401
|
+
<Row k="loaded">
|
|
402
|
+
{entry.loadedAt ? new Date(entry.loadedAt).toLocaleTimeString() : '-'}
|
|
403
|
+
</Row>
|
|
404
|
+
{entry.hasLoader ? (
|
|
405
|
+
<>
|
|
406
|
+
<div style={{ margin: '8px 0' }}>
|
|
407
|
+
<button
|
|
408
|
+
className="toil-dt-btn"
|
|
409
|
+
onClick={() => {
|
|
410
|
+
revalidate();
|
|
411
|
+
}}>
|
|
412
|
+
Revalidate
|
|
413
|
+
</button>{' '}
|
|
414
|
+
<button
|
|
415
|
+
className="toil-dt-btn"
|
|
416
|
+
onClick={() => {
|
|
417
|
+
clearLoaderData();
|
|
418
|
+
}}>
|
|
419
|
+
Clear cache
|
|
420
|
+
</button>
|
|
421
|
+
</div>
|
|
422
|
+
{entry.data === undefined ? (
|
|
423
|
+
<p className="toil-dt-empty">Loader returned no data.</p>
|
|
424
|
+
) : (
|
|
425
|
+
<pre className="toil-dt-pre">{safeJson(entry.data)}</pre>
|
|
426
|
+
)}
|
|
427
|
+
</>
|
|
428
|
+
) : (
|
|
429
|
+
<p className="toil-dt-empty">
|
|
430
|
+
This route has no loader, so there is no data to inspect.
|
|
431
|
+
</p>
|
|
432
|
+
)}
|
|
433
|
+
</>
|
|
434
|
+
)}
|
|
435
|
+
<p
|
|
436
|
+
className="toil-dt-sec"
|
|
437
|
+
style={{ marginTop: 12 }}>
|
|
438
|
+
Cache ({entries.length})
|
|
439
|
+
</p>
|
|
440
|
+
{entries.map((e) => (
|
|
441
|
+
<Row
|
|
442
|
+
k={e.key}
|
|
443
|
+
key={e.key}>
|
|
444
|
+
{e.status}
|
|
445
|
+
</Row>
|
|
446
|
+
))}
|
|
447
|
+
</div>
|
|
448
|
+
);
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
function Check({ ok, label }: { ok: boolean; label: string }): ReactNode {
|
|
452
|
+
return (
|
|
453
|
+
<div className="toil-dt-chk">
|
|
454
|
+
<span className={ok ? 'toil-dt-ok' : 'toil-dt-bad'}>{ok ? '✓' : '✗'}</span>
|
|
455
|
+
<span>{label}</span>
|
|
456
|
+
</div>
|
|
457
|
+
);
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
function HeadTab(): ReactNode {
|
|
461
|
+
useCurrentUrl(); // re-read the DOM head on navigation
|
|
462
|
+
const title = typeof document !== 'undefined' ? document.title : '';
|
|
463
|
+
const { metas, links } = readHead();
|
|
464
|
+
const meta = (n: string): string | undefined => metas.find((m) => m.name === n)?.content;
|
|
465
|
+
const og = {
|
|
466
|
+
title: meta('og:title') ?? title,
|
|
467
|
+
description: meta('og:description') ?? meta('description'),
|
|
468
|
+
image: meta('og:image'),
|
|
469
|
+
};
|
|
470
|
+
const pages = getPages();
|
|
471
|
+
const described = pages.filter((p) => p.metadata.description !== undefined).length;
|
|
472
|
+
|
|
473
|
+
return (
|
|
474
|
+
<div className="toil-dt-body">
|
|
475
|
+
<Row k="title">{title || '(none)'}</Row>
|
|
476
|
+
|
|
477
|
+
<p className="toil-dt-sec" style={{ marginTop: 10 }}>
|
|
478
|
+
OpenGraph preview
|
|
479
|
+
</p>
|
|
480
|
+
<div className="toil-dt-og">
|
|
481
|
+
{og.image && (
|
|
482
|
+
<img
|
|
483
|
+
src={og.image}
|
|
484
|
+
alt=""
|
|
485
|
+
className="toil-dt-og-img"
|
|
486
|
+
/>
|
|
487
|
+
)}
|
|
488
|
+
<div className="toil-dt-og-body">
|
|
489
|
+
<div className="toil-dt-og-title">{og.title || '(no title)'}</div>
|
|
490
|
+
<div className="toil-dt-og-desc">{og.description ?? '(no description)'}</div>
|
|
491
|
+
</div>
|
|
492
|
+
</div>
|
|
493
|
+
|
|
494
|
+
<p className="toil-dt-sec" style={{ marginTop: 10 }}>
|
|
495
|
+
SEO checklist
|
|
496
|
+
</p>
|
|
497
|
+
<Check ok={Boolean(title)} label="Has a title" />
|
|
498
|
+
<Check ok={meta('description') !== undefined} label="Has a meta description" />
|
|
499
|
+
<Check ok={og.image !== undefined} label="Has an og:image" />
|
|
500
|
+
<Check ok={links.some((l) => l.rel === 'canonical')} label="Has a canonical link" />
|
|
501
|
+
<Check
|
|
502
|
+
ok={pages.length === 0 || described === pages.length}
|
|
503
|
+
label={`Pages with a description: ${String(described)}/${String(pages.length)}`}
|
|
504
|
+
/>
|
|
505
|
+
|
|
506
|
+
<p className="toil-dt-sec" style={{ marginTop: 10 }}>
|
|
507
|
+
Meta ({metas.length})
|
|
508
|
+
</p>
|
|
509
|
+
{metas.map((m, i) => (
|
|
510
|
+
<Row k={m.name} key={`${m.name}:${String(i)}`}>
|
|
511
|
+
{m.content}
|
|
512
|
+
</Row>
|
|
513
|
+
))}
|
|
514
|
+
</div>
|
|
515
|
+
);
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
function BuildTab({ info }: { info: DevInfo | null }): ReactNode {
|
|
519
|
+
return (
|
|
520
|
+
<div className="toil-dt-body">
|
|
521
|
+
{!info && <p className="toil-dt-empty">Loading dev info...</p>}
|
|
522
|
+
{info && (
|
|
523
|
+
<>
|
|
524
|
+
<Row k="toiljs">{info.toiljs}</Row>
|
|
525
|
+
<Row k="vite">{info.vite}</Row>
|
|
526
|
+
<Row k="react">{info.react}</Row>
|
|
527
|
+
<Row k="dev server">{`localhost:${String(info.port)}`}</Row>
|
|
528
|
+
<p
|
|
529
|
+
className="toil-dt-sec"
|
|
530
|
+
style={{ marginTop: 12 }}>
|
|
531
|
+
Config
|
|
532
|
+
</p>
|
|
533
|
+
{Object.entries(info.flags).map(([k, v]) => (
|
|
534
|
+
<Row
|
|
535
|
+
k={k}
|
|
536
|
+
key={k}>
|
|
537
|
+
{v ? 'on' : 'off'}
|
|
538
|
+
</Row>
|
|
539
|
+
))}
|
|
540
|
+
<Row k="ai">{info.ai ? 'configured' : 'hand-off only'}</Row>
|
|
541
|
+
</>
|
|
542
|
+
)}
|
|
543
|
+
</div>
|
|
544
|
+
);
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
function ErrorsTab(): ReactNode {
|
|
548
|
+
const errors = useErrors();
|
|
549
|
+
if (errors.length === 0) return <p className="toil-dt-empty toil-dt-body">No errors captured.</p>;
|
|
550
|
+
return (
|
|
551
|
+
<div className="toil-dt-body">
|
|
552
|
+
{[...errors].reverse().map((e, i) => (
|
|
553
|
+
<div
|
|
554
|
+
className="toil-dt-err"
|
|
555
|
+
key={`${String(e.time)}:${String(i)}`}>
|
|
556
|
+
<div className="msg">
|
|
557
|
+
{e.error.name}: {e.error.message}
|
|
558
|
+
</div>
|
|
559
|
+
<div className="toil-dt-k">
|
|
560
|
+
{e.source}, {new Date(e.time).toLocaleTimeString()}
|
|
561
|
+
</div>
|
|
562
|
+
</div>
|
|
563
|
+
))}
|
|
564
|
+
</div>
|
|
565
|
+
);
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
/** Builds a context string about the current page for AI hand-off / inline ask. */
|
|
569
|
+
function buildAiContext(): string {
|
|
570
|
+
if (typeof window === 'undefined') return '';
|
|
571
|
+
const where = window.location.pathname + window.location.search;
|
|
572
|
+
const title = document.title;
|
|
573
|
+
const desc = readHead().metas.find((m) => m.name === 'description')?.content;
|
|
574
|
+
const lines = [
|
|
575
|
+
'I am working on a toiljs app (React with file-based routing, backend in toilscript/WASM).',
|
|
576
|
+
`Current page: ${where}`,
|
|
577
|
+
];
|
|
578
|
+
if (title) lines.push(`Page title: ${title}`);
|
|
579
|
+
if (desc) lines.push(`Meta description: ${desc}`);
|
|
580
|
+
return lines.join('\n');
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
const DOC_LINKS: { label: string; slug: string }[] = [
|
|
584
|
+
{ label: 'Routing and file conventions', slug: 'routing' },
|
|
585
|
+
{ label: 'Loaders and data', slug: 'loaders' },
|
|
586
|
+
{ label: 'Metadata and SEO', slug: 'metadata' },
|
|
587
|
+
{ label: 'Parallel routes and slots', slug: 'slots' },
|
|
588
|
+
];
|
|
589
|
+
|
|
590
|
+
function AiTab({ info }: { info: DevInfo | null }): ReactNode {
|
|
591
|
+
useCurrentUrl(); // rebuild page context on navigation
|
|
592
|
+
const [question, setQuestion] = useState('');
|
|
593
|
+
const [answer, setAnswer] = useState<string | null>(null);
|
|
594
|
+
const [busy, setBusy] = useState(false);
|
|
595
|
+
const configured = info?.ai === true;
|
|
596
|
+
|
|
597
|
+
const prompt = (): string => {
|
|
598
|
+
const q = question.trim() || 'Explain this page and suggest improvements.';
|
|
599
|
+
return `${buildAiContext()}\n\nQuestion: ${q}`;
|
|
600
|
+
};
|
|
601
|
+
const handOff = (base: string): void => {
|
|
602
|
+
window.open(`${base}${encodeURIComponent(prompt())}`, '_blank', 'noopener');
|
|
603
|
+
};
|
|
604
|
+
const copy = (): void => {
|
|
605
|
+
void navigator.clipboard.writeText(prompt()).catch(() => undefined);
|
|
606
|
+
};
|
|
607
|
+
const askInline = (): void => {
|
|
608
|
+
setBusy(true);
|
|
609
|
+
setAnswer(null);
|
|
610
|
+
void fetch('/__toil/ai', {
|
|
611
|
+
method: 'POST',
|
|
612
|
+
headers: { 'content-type': 'application/json' },
|
|
613
|
+
body: JSON.stringify({ prompt: prompt() }),
|
|
614
|
+
})
|
|
615
|
+
.then((r) =>
|
|
616
|
+
r.ok
|
|
617
|
+
? (r.json() as Promise<{ text?: string }>)
|
|
618
|
+
: Promise.reject(new Error(`HTTP ${String(r.status)}`)),
|
|
619
|
+
)
|
|
620
|
+
.then((d) => {
|
|
621
|
+
setAnswer(d.text ?? '(empty response)');
|
|
622
|
+
})
|
|
623
|
+
.catch((e: unknown) => {
|
|
624
|
+
setAnswer(`Error: ${e instanceof Error ? e.message : String(e)}`);
|
|
625
|
+
})
|
|
626
|
+
.finally(() => {
|
|
627
|
+
setBusy(false);
|
|
628
|
+
});
|
|
629
|
+
};
|
|
630
|
+
|
|
631
|
+
return (
|
|
632
|
+
<div className="toil-dt-body">
|
|
633
|
+
<p className="toil-dt-sec">Ask about this page</p>
|
|
634
|
+
<textarea
|
|
635
|
+
className="toil-dt-ta"
|
|
636
|
+
placeholder="Ask about the current route, or leave blank for a summary..."
|
|
637
|
+
value={question}
|
|
638
|
+
onChange={(e) => {
|
|
639
|
+
setQuestion(e.target.value);
|
|
640
|
+
}}
|
|
641
|
+
/>
|
|
642
|
+
<div className="toil-dt-ai-btns">
|
|
643
|
+
<button
|
|
644
|
+
className="toil-dt-ai-btn"
|
|
645
|
+
onClick={() => {
|
|
646
|
+
handOff('https://claude.ai/new?q=');
|
|
647
|
+
}}>
|
|
648
|
+
<ClaudeLogo size={14} /> Claude
|
|
649
|
+
</button>
|
|
650
|
+
<button
|
|
651
|
+
className="toil-dt-ai-btn"
|
|
652
|
+
onClick={() => {
|
|
653
|
+
handOff('https://chatgpt.com/?q=');
|
|
654
|
+
}}>
|
|
655
|
+
<ChatGptLogo size={14} /> ChatGPT
|
|
656
|
+
</button>
|
|
657
|
+
<button
|
|
658
|
+
className="toil-dt-ai-btn"
|
|
659
|
+
onClick={copy}>
|
|
660
|
+
Copy
|
|
661
|
+
</button>
|
|
662
|
+
{configured && (
|
|
663
|
+
<button
|
|
664
|
+
className="toil-dt-ai-btn"
|
|
665
|
+
disabled={busy}
|
|
666
|
+
onClick={askInline}>
|
|
667
|
+
{busy ? 'Asking...' : 'Ask inline'}
|
|
668
|
+
</button>
|
|
669
|
+
)}
|
|
670
|
+
</div>
|
|
671
|
+
{!configured && (
|
|
672
|
+
<p className="toil-dt-k">
|
|
673
|
+
Inline answers are off. Set <span className="toil-dt-tag">devtools.ai</span> in
|
|
674
|
+
your config to proxy a provider; the API key stays server-side.
|
|
675
|
+
</p>
|
|
676
|
+
)}
|
|
677
|
+
{answer !== null && <pre className="toil-dt-pre">{answer}</pre>}
|
|
678
|
+
|
|
679
|
+
<p
|
|
680
|
+
className="toil-dt-sec"
|
|
681
|
+
style={{ marginTop: 12 }}>
|
|
682
|
+
Quick docs
|
|
683
|
+
</p>
|
|
684
|
+
{DOC_LINKS.map((d) => (
|
|
685
|
+
<a
|
|
686
|
+
key={d.slug}
|
|
687
|
+
className="toil-dt-doc"
|
|
688
|
+
href={`${DOCS_BASE}/${d.slug}`}
|
|
689
|
+
target="_blank"
|
|
690
|
+
rel="noreferrer">
|
|
691
|
+
{d.label}
|
|
692
|
+
</a>
|
|
693
|
+
))}
|
|
694
|
+
</div>
|
|
695
|
+
);
|
|
696
|
+
}
|
|
697
|
+
|
|
698
|
+
/** A cmd/ctrl+K command palette: jump to a route or run a dev action. */
|
|
699
|
+
function Palette({ routes, onClose }: { routes: RouteDef[]; onClose: () => void }): ReactNode {
|
|
700
|
+
const [q, setQ] = useState('');
|
|
701
|
+
const [sel, setSel] = useState(0);
|
|
702
|
+
|
|
703
|
+
const items: { label: string; kind: string; run: () => void }[] = [];
|
|
704
|
+
for (const r of routes) {
|
|
705
|
+
if (!isDynamic(r.pattern)) {
|
|
706
|
+
items.push({
|
|
707
|
+
label: r.pattern,
|
|
708
|
+
kind: 'route',
|
|
709
|
+
run: () => {
|
|
710
|
+
navigate(r.pattern as Href);
|
|
711
|
+
onClose();
|
|
712
|
+
},
|
|
713
|
+
});
|
|
714
|
+
}
|
|
715
|
+
}
|
|
716
|
+
items.push(
|
|
717
|
+
{
|
|
718
|
+
label: 'Revalidate current route',
|
|
719
|
+
kind: 'action',
|
|
720
|
+
run: () => {
|
|
721
|
+
revalidate();
|
|
722
|
+
onClose();
|
|
723
|
+
},
|
|
724
|
+
},
|
|
725
|
+
{
|
|
726
|
+
label: 'Clear loader cache',
|
|
727
|
+
kind: 'action',
|
|
728
|
+
run: () => {
|
|
729
|
+
clearLoaderData();
|
|
730
|
+
onClose();
|
|
731
|
+
},
|
|
732
|
+
},
|
|
733
|
+
{
|
|
734
|
+
label: 'Ask AI about this page',
|
|
735
|
+
kind: 'action',
|
|
736
|
+
run: () => {
|
|
737
|
+
setPrefs({ open: true, tab: 'ai' });
|
|
738
|
+
onClose();
|
|
739
|
+
},
|
|
740
|
+
},
|
|
741
|
+
{
|
|
742
|
+
label: 'Open preferences',
|
|
743
|
+
kind: 'action',
|
|
744
|
+
run: () => {
|
|
745
|
+
setPrefs({ open: true, tab: 'prefs' });
|
|
746
|
+
onClose();
|
|
747
|
+
},
|
|
748
|
+
},
|
|
749
|
+
);
|
|
750
|
+
|
|
751
|
+
const needle = q.toLowerCase();
|
|
752
|
+
const filtered = items.filter((it) => it.label.toLowerCase().includes(needle));
|
|
753
|
+
const clamped = Math.min(sel, Math.max(0, filtered.length - 1));
|
|
754
|
+
|
|
755
|
+
return (
|
|
756
|
+
<div
|
|
757
|
+
className="toil-dt-pal-wrap"
|
|
758
|
+
onClick={onClose}>
|
|
759
|
+
<div
|
|
760
|
+
className="toil-dt-pal"
|
|
761
|
+
onClick={(e) => {
|
|
762
|
+
e.stopPropagation();
|
|
763
|
+
}}>
|
|
764
|
+
<input
|
|
765
|
+
autoFocus
|
|
766
|
+
placeholder="Go to a route or run an action..."
|
|
767
|
+
value={q}
|
|
768
|
+
onChange={(e) => {
|
|
769
|
+
setQ(e.target.value);
|
|
770
|
+
setSel(0);
|
|
771
|
+
}}
|
|
772
|
+
onKeyDown={(e) => {
|
|
773
|
+
if (e.key === 'ArrowDown') {
|
|
774
|
+
e.preventDefault();
|
|
775
|
+
setSel((s) => Math.min(s + 1, filtered.length - 1));
|
|
776
|
+
} else if (e.key === 'ArrowUp') {
|
|
777
|
+
e.preventDefault();
|
|
778
|
+
setSel((s) => Math.max(s - 1, 0));
|
|
779
|
+
} else if (e.key === 'Enter') {
|
|
780
|
+
e.preventDefault();
|
|
781
|
+
filtered[clamped]?.run();
|
|
782
|
+
} else if (e.key === 'Escape') {
|
|
783
|
+
onClose();
|
|
784
|
+
}
|
|
785
|
+
}}
|
|
786
|
+
/>
|
|
787
|
+
<div className="toil-dt-pal-list">
|
|
788
|
+
{filtered.length === 0 && <div className="toil-dt-pal-item">No matches</div>}
|
|
789
|
+
{filtered.map((it, i) => (
|
|
790
|
+
<div
|
|
791
|
+
key={`${it.kind}:${it.label}`}
|
|
792
|
+
className={`toil-dt-pal-item ${i === clamped ? 'sel' : ''}`}
|
|
793
|
+
onMouseEnter={() => {
|
|
794
|
+
setSel(i);
|
|
795
|
+
}}
|
|
796
|
+
onClick={it.run}>
|
|
797
|
+
<span>{it.label}</span>
|
|
798
|
+
<span className="toil-dt-pal-kind">{it.kind}</span>
|
|
799
|
+
</div>
|
|
800
|
+
))}
|
|
801
|
+
</div>
|
|
802
|
+
</div>
|
|
803
|
+
</div>
|
|
804
|
+
);
|
|
805
|
+
}
|
|
806
|
+
|
|
807
|
+
function PrefsTab(): ReactNode {
|
|
808
|
+
const p = usePrefs();
|
|
809
|
+
const [flags, setFlags] = useState({ viewTransitions: false, transitions: false });
|
|
810
|
+
const toggle = (key: 'viewTransitions' | 'transitions'): void => {
|
|
811
|
+
const next = !flags[key];
|
|
812
|
+
setFlags((f) => ({ ...f, [key]: next }));
|
|
813
|
+
if (key === 'viewTransitions') setViewTransitions(next);
|
|
814
|
+
else setTransitions(next);
|
|
815
|
+
};
|
|
816
|
+
return (
|
|
817
|
+
<div className="toil-dt-body">
|
|
818
|
+
<div className="toil-dt-sw">
|
|
819
|
+
<span>View transitions</span>
|
|
820
|
+
<button
|
|
821
|
+
className="toil-dt-btn"
|
|
822
|
+
onClick={() => {
|
|
823
|
+
toggle('viewTransitions');
|
|
824
|
+
}}>
|
|
825
|
+
{flags.viewTransitions ? 'on' : 'off'}
|
|
826
|
+
</button>
|
|
827
|
+
</div>
|
|
828
|
+
<div className="toil-dt-sw">
|
|
829
|
+
<span>Loader transition</span>
|
|
830
|
+
<button
|
|
831
|
+
className="toil-dt-btn"
|
|
832
|
+
onClick={() => {
|
|
833
|
+
toggle('transitions');
|
|
834
|
+
}}>
|
|
835
|
+
{flags.transitions ? 'on' : 'off'}
|
|
836
|
+
</button>
|
|
837
|
+
</div>
|
|
838
|
+
<div className="toil-dt-sw">
|
|
839
|
+
<span>Toolbar side</span>
|
|
840
|
+
<button
|
|
841
|
+
className="toil-dt-btn"
|
|
842
|
+
onClick={() => {
|
|
843
|
+
setPrefs({ side: p.side === 'left' ? 'right' : 'left' });
|
|
844
|
+
}}>
|
|
845
|
+
{p.side}
|
|
846
|
+
</button>
|
|
847
|
+
</div>
|
|
848
|
+
</div>
|
|
849
|
+
);
|
|
850
|
+
}
|
|
851
|
+
|
|
852
|
+
const TABS: { id: Tab; label: string }[] = [
|
|
853
|
+
{ id: 'route', label: 'Route' },
|
|
854
|
+
{ id: 'data', label: 'Data' },
|
|
855
|
+
{ id: 'head', label: 'Head' },
|
|
856
|
+
{ id: 'build', label: 'Build' },
|
|
857
|
+
{ id: 'errors', label: 'Errors' },
|
|
858
|
+
{ id: 'ai', label: 'AI' },
|
|
859
|
+
{ id: 'prefs', label: 'Prefs' },
|
|
860
|
+
];
|
|
861
|
+
|
|
862
|
+
/** The dev toolbar. Rendered once at the app root in dev mode (see `mount`). */
|
|
863
|
+
export function DevToolbar({
|
|
864
|
+
routes,
|
|
865
|
+
slots,
|
|
866
|
+
}: {
|
|
867
|
+
routes: RouteDef[];
|
|
868
|
+
slots: Record<string, RouteDef[]>;
|
|
869
|
+
}): ReactNode {
|
|
870
|
+
const p = usePrefs();
|
|
871
|
+
const pending = usePending();
|
|
872
|
+
const errors = useErrors();
|
|
873
|
+
const [info, setInfo] = useState<DevInfo | null>(null);
|
|
874
|
+
const [palette, setPalette] = useState(false);
|
|
875
|
+
|
|
876
|
+
useEffect(() => {
|
|
877
|
+
injectStyles();
|
|
878
|
+
void fetch('/__toil/devinfo')
|
|
879
|
+
.then((r) => (r.ok ? (r.json() as Promise<DevInfo>) : null))
|
|
880
|
+
.then((data) => {
|
|
881
|
+
if (data) setInfo(data);
|
|
882
|
+
})
|
|
883
|
+
.catch(() => undefined);
|
|
884
|
+
}, []);
|
|
885
|
+
|
|
886
|
+
useEffect(() => {
|
|
887
|
+
const onKey = (e: KeyboardEvent): void => {
|
|
888
|
+
if ((e.metaKey || e.ctrlKey) && e.key.toLowerCase() === 'k') {
|
|
889
|
+
e.preventDefault();
|
|
890
|
+
setPalette((v) => !v);
|
|
891
|
+
}
|
|
892
|
+
};
|
|
893
|
+
window.addEventListener('keydown', onKey);
|
|
894
|
+
return () => {
|
|
895
|
+
window.removeEventListener('keydown', onKey);
|
|
896
|
+
};
|
|
897
|
+
}, []);
|
|
898
|
+
|
|
899
|
+
if (info && !info.enabled) return null;
|
|
900
|
+
|
|
901
|
+
const dotClass = errors.length > 0 ? 'error' : pending ? 'pending' : '';
|
|
902
|
+
const pal = palette ? (
|
|
903
|
+
<Palette
|
|
904
|
+
routes={routes}
|
|
905
|
+
onClose={() => {
|
|
906
|
+
setPalette(false);
|
|
907
|
+
}}
|
|
908
|
+
/>
|
|
909
|
+
) : null;
|
|
910
|
+
|
|
911
|
+
if (!p.open) {
|
|
912
|
+
return (
|
|
913
|
+
<>
|
|
914
|
+
<div className={`toil-dt ${p.side}`}>
|
|
915
|
+
<div
|
|
916
|
+
className="toil-dt-badge"
|
|
917
|
+
onClick={() => {
|
|
918
|
+
setPrefs({ open: true });
|
|
919
|
+
}}
|
|
920
|
+
title="toiljs devtools (cmd+K)">
|
|
921
|
+
<ToilLogo size={16} />
|
|
922
|
+
<span className={`toil-dt-dot ${dotClass}`} />
|
|
923
|
+
</div>
|
|
924
|
+
</div>
|
|
925
|
+
{pal}
|
|
926
|
+
</>
|
|
927
|
+
);
|
|
928
|
+
}
|
|
929
|
+
|
|
930
|
+
return (
|
|
931
|
+
<>
|
|
932
|
+
<div className={`toil-dt ${p.side}`}>
|
|
933
|
+
<div className="toil-dt-panel">
|
|
934
|
+
<div className="toil-dt-head">
|
|
935
|
+
<span style={{ display: 'flex', alignItems: 'center', gap: 6 }}>
|
|
936
|
+
<ToilLogo size={14} />
|
|
937
|
+
<span className="toil-dt-logo">toiljs</span> devtools
|
|
938
|
+
<span className={`toil-dt-dot ${dotClass}`} />
|
|
939
|
+
</span>
|
|
940
|
+
<button
|
|
941
|
+
className="toil-dt-x"
|
|
942
|
+
onClick={() => {
|
|
943
|
+
setPrefs({ open: false });
|
|
944
|
+
}}>
|
|
945
|
+
✕
|
|
946
|
+
</button>
|
|
947
|
+
</div>
|
|
948
|
+
<div className="toil-dt-tabs">
|
|
949
|
+
{TABS.map((t) => (
|
|
950
|
+
<button
|
|
951
|
+
key={t.id}
|
|
952
|
+
className={`toil-dt-tab ${p.tab === t.id ? 'active' : ''}`}
|
|
953
|
+
onClick={() => {
|
|
954
|
+
setPrefs({ tab: t.id });
|
|
955
|
+
}}>
|
|
956
|
+
{t.label}
|
|
957
|
+
{t.id === 'errors' && errors.length > 0 ? ` (${String(errors.length)})` : ''}
|
|
958
|
+
</button>
|
|
959
|
+
))}
|
|
960
|
+
</div>
|
|
961
|
+
{p.tab === 'route' && <RouteTab routes={routes} slots={slots} info={info} />}
|
|
962
|
+
{p.tab === 'data' && <DataTab />}
|
|
963
|
+
{p.tab === 'head' && <HeadTab />}
|
|
964
|
+
{p.tab === 'build' && <BuildTab info={info} />}
|
|
965
|
+
{p.tab === 'errors' && <ErrorsTab />}
|
|
966
|
+
{p.tab === 'ai' && <AiTab info={info} />}
|
|
967
|
+
{p.tab === 'prefs' && <PrefsTab />}
|
|
968
|
+
</div>
|
|
969
|
+
</div>
|
|
970
|
+
{pal}
|
|
971
|
+
</>
|
|
972
|
+
);
|
|
973
|
+
}
|