toiljs 0.0.15 → 0.0.19

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.
Files changed (273) hide show
  1. package/.babelrc +13 -13
  2. package/.gitattributes +2 -2
  3. package/.github/ISSUE_TEMPLATE/bug_report.md +38 -38
  4. package/.github/ISSUE_TEMPLATE/bug_report.yml +90 -90
  5. package/.github/ISSUE_TEMPLATE/config.yml +8 -8
  6. package/.github/ISSUE_TEMPLATE/feature_request.md +20 -20
  7. package/.github/PULL_REQUEST_TEMPLATE.md +43 -43
  8. package/.github/changelog-config.json +45 -45
  9. package/.github/dependabot.yml +27 -27
  10. package/.github/workflows/ci.yml +191 -191
  11. package/.prettierrc.json +11 -11
  12. package/.vscode/settings.json +9 -9
  13. package/CHANGELOG.md +116 -5
  14. package/LICENSE +187 -187
  15. package/README.md +524 -315
  16. package/as-pect.asconfig.json +34 -34
  17. package/as-pect.config.js +65 -65
  18. package/assets/logo.svg +36 -36
  19. package/build/backend/.tsbuildinfo +1 -1
  20. package/build/backend/index.d.ts +1 -0
  21. package/build/backend/index.js +20 -1
  22. package/build/cli/.tsbuildinfo +1 -1
  23. package/build/cli/index.js +1320 -696
  24. package/build/client/.tsbuildinfo +1 -1
  25. package/build/client/dev/devtools.d.ts +6 -0
  26. package/build/client/dev/devtools.js +479 -0
  27. package/build/client/dev/error-overlay.d.ts +9 -0
  28. package/build/client/dev/error-overlay.js +19 -4
  29. package/build/client/errors.d.ts +1 -0
  30. package/build/client/errors.js +3 -0
  31. package/build/client/index.d.ts +2 -0
  32. package/build/client/index.js +2 -0
  33. package/build/client/navigation/prefetch.d.ts +1 -0
  34. package/build/client/navigation/prefetch.js +35 -0
  35. package/build/client/routing/Router.js +1 -1
  36. package/build/client/routing/hooks.js +6 -2
  37. package/build/client/routing/loader.d.ts +23 -0
  38. package/build/client/routing/loader.js +53 -7
  39. package/build/client/routing/mount.js +4 -3
  40. package/build/client/rpc.d.ts +1 -0
  41. package/build/client/rpc.js +37 -0
  42. package/build/compiler/.tsbuildinfo +1 -1
  43. package/build/compiler/config.d.ts +16 -0
  44. package/build/compiler/config.js +9 -0
  45. package/build/compiler/docs.js +78 -21
  46. package/build/compiler/generate.js +5 -4
  47. package/build/compiler/index.d.ts +3 -2
  48. package/build/compiler/index.js +2 -2
  49. package/build/compiler/plugin.js +228 -0
  50. package/build/compiler/prerender.d.ts +1 -0
  51. package/build/compiler/prerender.js +1 -1
  52. package/build/compiler/seo.d.ts +1 -1
  53. package/build/compiler/seo.js +20 -5
  54. package/build/compiler/ssg.js +39 -2
  55. package/build/compiler/vite.js +25 -0
  56. package/build/io/.tsbuildinfo +1 -1
  57. package/build/io/codec.d.ts +54 -0
  58. package/build/io/codec.js +143 -0
  59. package/build/io/index.d.ts +1 -2
  60. package/build/io/index.js +1 -2
  61. package/build/logger/.tsbuildinfo +1 -1
  62. package/build/shared/.tsbuildinfo +1 -1
  63. package/eslint.config.js +48 -48
  64. package/examples/basic/client/404.tsx +11 -11
  65. package/examples/basic/client/components/.gitkeep +1 -1
  66. package/examples/basic/client/global-error.tsx +13 -13
  67. package/examples/basic/client/layout.tsx +25 -25
  68. package/examples/basic/client/public/images/.gitkeep +1 -1
  69. package/examples/basic/client/public/images/logo.svg +36 -36
  70. package/examples/basic/client/public/robots.txt +2 -2
  71. package/examples/basic/client/routes/docs/[...slug].tsx +12 -12
  72. package/examples/basic/client/routes/features/error/error.tsx +16 -16
  73. package/examples/basic/client/routes/features/index.tsx +1 -1
  74. package/examples/basic/client/routes/features/template/b.tsx +14 -14
  75. package/examples/basic/client/routes/files/[[...slug]].tsx +21 -21
  76. package/examples/basic/client/routes/gallery/layout.tsx +13 -13
  77. package/examples/basic/client/routes/io.tsx +23 -24
  78. package/examples/basic/client/routes/loader-demo/loading.tsx +13 -13
  79. package/examples/basic/client/routes/rest.tsx +74 -0
  80. package/examples/basic/client/routes/rpc.tsx +43 -0
  81. package/examples/basic/client/routes/search.tsx +61 -61
  82. package/examples/basic/client/toil.tsx +5 -5
  83. package/package.json +167 -148
  84. package/presets/eslint.js +88 -88
  85. package/presets/no-uint8array-tostring.js +200 -200
  86. package/presets/prettier-plugin.js +51 -0
  87. package/presets/prettier.json +19 -18
  88. package/presets/tsconfig.json +37 -37
  89. package/server/runtime/README.md +97 -0
  90. package/server/runtime/abort/abort.ts +27 -0
  91. package/server/runtime/env/Server.ts +61 -0
  92. package/server/runtime/envelope.ts +191 -0
  93. package/server/runtime/exports/index.ts +52 -0
  94. package/server/runtime/handlers/ToilHandler.ts +34 -0
  95. package/server/runtime/index.ts +26 -0
  96. package/server/runtime/lang/Potential.ts +5 -0
  97. package/server/runtime/memory.ts +81 -0
  98. package/server/runtime/request.ts +55 -0
  99. package/server/runtime/response.ts +86 -0
  100. package/server/runtime/rest/Rest.ts +39 -0
  101. package/server/runtime/rest/RestHandler.ts +20 -0
  102. package/server/runtime/rest/RouteContext.ts +82 -0
  103. package/server/runtime/rest/match.ts +48 -0
  104. package/server/runtime/tsconfig.json +7 -0
  105. package/src/backend/index.ts +202 -160
  106. package/src/cli/create.ts +15 -5
  107. package/src/cli/diagnostics.ts +81 -0
  108. package/src/cli/doctor.ts +384 -7
  109. package/src/cli/index.ts +11 -2
  110. package/src/cli/proc.ts +50 -50
  111. package/src/cli/updates.ts +69 -69
  112. package/src/cli/validate.ts +31 -31
  113. package/src/client/channel/channel.ts +146 -146
  114. package/src/client/components/Form.tsx +65 -65
  115. package/src/client/components/Script.tsx +113 -113
  116. package/src/client/components/Slot.tsx +21 -21
  117. package/src/client/dev/devtools.tsx +1018 -0
  118. package/src/client/dev/error-overlay.tsx +30 -4
  119. package/src/client/errors.ts +11 -0
  120. package/src/client/head/head.ts +167 -167
  121. package/src/client/head/metadata.ts +112 -112
  122. package/src/client/index.ts +91 -89
  123. package/src/client/navigation/NavLink.tsx +86 -86
  124. package/src/client/navigation/navigation.ts +235 -235
  125. package/src/client/navigation/prefetch.ts +169 -130
  126. package/src/client/navigation/scroll.ts +53 -53
  127. package/src/client/routing/Router.tsx +8 -2
  128. package/src/client/routing/action.ts +122 -122
  129. package/src/client/routing/error-boundary.tsx +43 -43
  130. package/src/client/routing/hooks.ts +21 -6
  131. package/src/client/routing/loader.ts +325 -235
  132. package/src/client/routing/match.ts +47 -47
  133. package/src/client/routing/mount.tsx +54 -52
  134. package/src/client/routing/params-context.ts +10 -10
  135. package/src/client/routing/slot-context.ts +7 -7
  136. package/src/client/rpc.ts +64 -0
  137. package/src/client/search/search.ts +189 -189
  138. package/src/client/search/use-page-search.ts +73 -73
  139. package/src/client/types.ts +73 -73
  140. package/src/compiler/config.ts +221 -182
  141. package/src/compiler/docs.ts +285 -228
  142. package/src/compiler/generate.ts +395 -394
  143. package/src/compiler/index.ts +66 -57
  144. package/src/compiler/pages.ts +70 -70
  145. package/src/compiler/plugin.ts +258 -2
  146. package/src/compiler/prerender.ts +156 -156
  147. package/src/compiler/seo.ts +417 -390
  148. package/src/compiler/ssg.ts +171 -126
  149. package/src/compiler/vite.ts +34 -0
  150. package/src/io/FastMap.ts +151 -127
  151. package/src/io/FastSet.ts +15 -1
  152. package/src/io/codec.ts +217 -0
  153. package/src/io/index.ts +10 -11
  154. package/src/io/lengths.ts +14 -14
  155. package/src/io/types.ts +19 -18
  156. package/src/logger/index.ts +22 -22
  157. package/src/shared/index.ts +10 -10
  158. package/std/client/index.d.ts +15 -15
  159. package/std/client/package.json +3 -3
  160. package/test/assembly/example.spec.ts +17 -7
  161. package/test/channel.test.ts +21 -21
  162. package/test/doctor.test.ts +65 -0
  163. package/test/dom/Link.test.tsx +47 -47
  164. package/test/dom/NavLink.test.tsx +37 -37
  165. package/test/dom/error-overlay.test.tsx +44 -44
  166. package/test/dom/loader.test.tsx +121 -121
  167. package/test/dom/navigation.test.ts +59 -59
  168. package/test/dom/revalidate.test.tsx +38 -38
  169. package/test/dom/route-head.test.tsx +78 -78
  170. package/test/dom/router-loading.test.tsx +44 -44
  171. package/test/dom/scroll.test.ts +56 -56
  172. package/test/dom/use-metadata.test.tsx +58 -58
  173. package/test/errors.test.ts +21 -0
  174. package/test/io.test.ts +117 -93
  175. package/test/navlink.test.ts +28 -28
  176. package/test/placeholder.test.ts +9 -9
  177. package/test/prettier-plugin.test.ts +46 -0
  178. package/test/routes.test.ts +76 -76
  179. package/test/rpc.test.ts +50 -0
  180. package/test/seo.test.ts +175 -164
  181. package/test/slot-layouts.test.ts +69 -69
  182. package/test/ssg.test.ts +36 -36
  183. package/test/update.test.ts +44 -44
  184. package/test/validate.test.ts +42 -42
  185. package/tests/data-parity/generated-parity.ts +99 -0
  186. package/tests/data-parity/parity.ts +80 -0
  187. package/tests/data-parity/spec.ts +46 -0
  188. package/toil-routes.d.ts +7 -0
  189. package/tsconfig.backend.json +13 -13
  190. package/tsconfig.base.json +35 -35
  191. package/tsconfig.cli.json +13 -13
  192. package/tsconfig.client.json +14 -14
  193. package/tsconfig.compiler.json +13 -13
  194. package/tsconfig.io.json +12 -12
  195. package/tsconfig.json +22 -22
  196. package/tsconfig.logger.json +12 -12
  197. package/tsconfig.server.json +10 -10
  198. package/tsconfig.shared.json +12 -12
  199. package/vitest.config.ts +26 -26
  200. package/.idea/codeStyles/Project.xml +0 -54
  201. package/.idea/codeStyles/codeStyleConfig.xml +0 -5
  202. package/.idea/inspectionProfiles/Project_Default.xml +0 -6
  203. package/.idea/modules.xml +0 -8
  204. package/.idea/prettier.xml +0 -7
  205. package/.idea/toiljs.iml +0 -8
  206. package/.idea/vcs.xml +0 -6
  207. package/.toil/entry.tsx +0 -9
  208. package/.toil/index.html +0 -12
  209. package/.toil/routes.ts +0 -9
  210. package/build/cli/configure.d.ts +0 -16
  211. package/build/cli/configure.js +0 -272
  212. package/build/cli/create.d.ts +0 -16
  213. package/build/cli/create.js +0 -420
  214. package/build/cli/diagnostics.d.ts +0 -55
  215. package/build/cli/diagnostics.js +0 -333
  216. package/build/cli/doctor.d.ts +0 -6
  217. package/build/cli/doctor.js +0 -249
  218. package/build/cli/features.d.ts +0 -25
  219. package/build/cli/features.js +0 -107
  220. package/build/cli/index.d.ts +0 -2
  221. package/build/cli/proc.d.ts +0 -6
  222. package/build/cli/proc.js +0 -31
  223. package/build/cli/ui.d.ts +0 -9
  224. package/build/cli/ui.js +0 -75
  225. package/build/cli/update.d.ts +0 -7
  226. package/build/cli/update.js +0 -117
  227. package/build/cli/updates.d.ts +0 -10
  228. package/build/cli/updates.js +0 -45
  229. package/build/cli/validate.d.ts +0 -4
  230. package/build/cli/validate.js +0 -19
  231. package/build/client/Link.d.ts +0 -8
  232. package/build/client/Link.js +0 -44
  233. package/build/client/NavLink.d.ts +0 -14
  234. package/build/client/NavLink.js +0 -37
  235. package/build/client/Router.d.ts +0 -7
  236. package/build/client/Router.js +0 -55
  237. package/build/client/channel.d.ts +0 -23
  238. package/build/client/channel.js +0 -94
  239. package/build/client/error-boundary.d.ts +0 -16
  240. package/build/client/error-boundary.js +0 -19
  241. package/build/client/head.d.ts +0 -26
  242. package/build/client/head.js +0 -87
  243. package/build/client/hooks.d.ts +0 -17
  244. package/build/client/hooks.js +0 -48
  245. package/build/client/lazy.d.ts +0 -16
  246. package/build/client/lazy.js +0 -53
  247. package/build/client/match.d.ts +0 -2
  248. package/build/client/match.js +0 -32
  249. package/build/client/mount.d.ts +0 -2
  250. package/build/client/mount.js +0 -13
  251. package/build/client/navigation.d.ts +0 -13
  252. package/build/client/navigation.js +0 -97
  253. package/build/client/params-context.d.ts +0 -2
  254. package/build/client/params-context.js +0 -2
  255. package/build/client/prefetch.d.ts +0 -11
  256. package/build/client/prefetch.js +0 -100
  257. package/build/client/runtime.d.ts +0 -31
  258. package/build/client/runtime.js +0 -112
  259. package/build/client/scroll.d.ts +0 -8
  260. package/build/client/scroll.js +0 -36
  261. package/build/io/BinaryReader.d.ts +0 -44
  262. package/build/io/BinaryReader.js +0 -244
  263. package/build/io/BinaryWriter.d.ts +0 -44
  264. package/build/io/BinaryWriter.js +0 -297
  265. package/build/server/release.wasm +0 -0
  266. package/build/server/release.wat +0 -9
  267. package/src/io/BinaryReader.ts +0 -340
  268. package/src/io/BinaryWriter.ts +0 -385
  269. package/src/server/index.ts +0 -10
  270. package/src/server/main.ts +0 -13
  271. package/src/server/tsconfig.json +0 -4
  272. package/toil-env.d.ts +0 -16
  273. package/toilconfig.json +0 -30
@@ -0,0 +1,1018 @@
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
+ /** Max chars of route source to inline into the prompt (keeps the hand-off URL usable). */
591
+ const AI_CODE_MAX = 8000;
592
+
593
+ function AiTab({ info, routes }: { info: DevInfo | null; routes: RouteDef[] }): ReactNode {
594
+ const url = useCurrentUrl(); // rebuild page context + refetch source on navigation
595
+ const [question, setQuestion] = useState('');
596
+ const [answer, setAnswer] = useState<string | null>(null);
597
+ const [busy, setBusy] = useState(false);
598
+ const [source, setSource] = useState<{ file: string; code: string } | null>(null);
599
+ const configured = info?.ai === true;
600
+
601
+ // Resolve the current route's source file (pattern -> absolute path from the dev server).
602
+ const pathname = url.split('?')[0];
603
+ let file: string | undefined;
604
+ for (const r of routes) {
605
+ if (matchRoute(r.pattern, pathname)) {
606
+ file = info?.routes[r.pattern];
607
+ break;
608
+ }
609
+ }
610
+
611
+ useEffect(() => {
612
+ if (!file) {
613
+ setSource(null);
614
+ return;
615
+ }
616
+ let cancelled = false;
617
+ void fetch(`/__toil/source?file=${encodeURIComponent(file)}`)
618
+ .then((r) => (r.ok ? r.text() : null))
619
+ .then((code) => {
620
+ if (!cancelled) setSource(code !== null ? { file, code } : null);
621
+ })
622
+ .catch(() => {
623
+ if (!cancelled) setSource(null);
624
+ });
625
+ return () => {
626
+ cancelled = true;
627
+ };
628
+ }, [file]);
629
+
630
+ const prompt = (): string => {
631
+ const q = question.trim() || 'Explain this page and suggest improvements.';
632
+ const parts = [buildAiContext()];
633
+ if (source) {
634
+ const code = source.code.slice(0, AI_CODE_MAX);
635
+ const cut = source.code.length > AI_CODE_MAX ? '\n... (truncated)' : '';
636
+ parts.push(`\nPage source (${source.file}):\n\`\`\`tsx\n${code}${cut}\n\`\`\``);
637
+ }
638
+ parts.push(`\nQuestion: ${q}`);
639
+ return parts.join('\n');
640
+ };
641
+ const handOff = (base: string): void => {
642
+ window.open(`${base}${encodeURIComponent(prompt())}`, '_blank', 'noopener');
643
+ };
644
+ const copy = (): void => {
645
+ void navigator.clipboard.writeText(prompt()).catch(() => undefined);
646
+ };
647
+ const askInline = (): void => {
648
+ setBusy(true);
649
+ setAnswer(null);
650
+ void fetch('/__toil/ai', {
651
+ method: 'POST',
652
+ headers: { 'content-type': 'application/json' },
653
+ body: JSON.stringify({ prompt: prompt() }),
654
+ })
655
+ .then((r) =>
656
+ r.ok
657
+ ? (r.json() as Promise<{ text?: string }>)
658
+ : Promise.reject(new Error(`HTTP ${String(r.status)}`)),
659
+ )
660
+ .then((d) => {
661
+ setAnswer(d.text ?? '(empty response)');
662
+ })
663
+ .catch((e: unknown) => {
664
+ setAnswer(`Error: ${e instanceof Error ? e.message : String(e)}`);
665
+ })
666
+ .finally(() => {
667
+ setBusy(false);
668
+ });
669
+ };
670
+
671
+ return (
672
+ <div className="toil-dt-body">
673
+ <p className="toil-dt-sec">Ask about this page</p>
674
+ <textarea
675
+ className="toil-dt-ta"
676
+ placeholder="Ask about the current route, or leave blank for a summary..."
677
+ value={question}
678
+ onChange={(e) => {
679
+ setQuestion(e.target.value);
680
+ }}
681
+ />
682
+ <div className="toil-dt-ai-btns">
683
+ <button
684
+ className="toil-dt-ai-btn"
685
+ onClick={() => {
686
+ handOff('https://claude.ai/new?q=');
687
+ }}>
688
+ <ClaudeLogo size={14} /> Claude
689
+ </button>
690
+ <button
691
+ className="toil-dt-ai-btn"
692
+ onClick={() => {
693
+ handOff('https://chatgpt.com/?q=');
694
+ }}>
695
+ <ChatGptLogo size={14} /> ChatGPT
696
+ </button>
697
+ <button
698
+ className="toil-dt-ai-btn"
699
+ onClick={copy}>
700
+ Copy
701
+ </button>
702
+ {configured && (
703
+ <button
704
+ className="toil-dt-ai-btn"
705
+ disabled={busy}
706
+ onClick={askInline}>
707
+ {busy ? 'Asking...' : 'Ask inline'}
708
+ </button>
709
+ )}
710
+ </div>
711
+ {source && (
712
+ <p className="toil-dt-k">
713
+ Prompt includes this route&apos;s source ({source.file.split('/').pop()}).
714
+ </p>
715
+ )}
716
+ {!configured && (
717
+ <p className="toil-dt-k">
718
+ Inline answers are off. Set <span className="toil-dt-tag">devtools.ai</span> in
719
+ your config to proxy a provider; the API key stays server-side.
720
+ </p>
721
+ )}
722
+ {answer !== null && <pre className="toil-dt-pre">{answer}</pre>}
723
+
724
+ <p
725
+ className="toil-dt-sec"
726
+ style={{ marginTop: 12 }}>
727
+ Quick docs
728
+ </p>
729
+ {DOC_LINKS.map((d) => (
730
+ <a
731
+ key={d.slug}
732
+ className="toil-dt-doc"
733
+ href={`${DOCS_BASE}/${d.slug}`}
734
+ target="_blank"
735
+ rel="noreferrer">
736
+ {d.label}
737
+ </a>
738
+ ))}
739
+ </div>
740
+ );
741
+ }
742
+
743
+ /** A cmd/ctrl+K command palette: jump to a route or run a dev action. */
744
+ function Palette({ routes, onClose }: { routes: RouteDef[]; onClose: () => void }): ReactNode {
745
+ const [q, setQ] = useState('');
746
+ const [sel, setSel] = useState(0);
747
+
748
+ const items: { label: string; kind: string; run: () => void }[] = [];
749
+ for (const r of routes) {
750
+ if (!isDynamic(r.pattern)) {
751
+ items.push({
752
+ label: r.pattern,
753
+ kind: 'route',
754
+ run: () => {
755
+ navigate(r.pattern as Href);
756
+ onClose();
757
+ },
758
+ });
759
+ }
760
+ }
761
+ items.push(
762
+ {
763
+ label: 'Revalidate current route',
764
+ kind: 'action',
765
+ run: () => {
766
+ revalidate();
767
+ onClose();
768
+ },
769
+ },
770
+ {
771
+ label: 'Clear loader cache',
772
+ kind: 'action',
773
+ run: () => {
774
+ clearLoaderData();
775
+ onClose();
776
+ },
777
+ },
778
+ {
779
+ label: 'Ask AI about this page',
780
+ kind: 'action',
781
+ run: () => {
782
+ setPrefs({ open: true, tab: 'ai' });
783
+ onClose();
784
+ },
785
+ },
786
+ {
787
+ label: 'Open preferences',
788
+ kind: 'action',
789
+ run: () => {
790
+ setPrefs({ open: true, tab: 'prefs' });
791
+ onClose();
792
+ },
793
+ },
794
+ );
795
+
796
+ const needle = q.toLowerCase();
797
+ const filtered = items.filter((it) => it.label.toLowerCase().includes(needle));
798
+ const clamped = Math.min(sel, Math.max(0, filtered.length - 1));
799
+
800
+ return (
801
+ <div
802
+ className="toil-dt-pal-wrap"
803
+ onClick={onClose}>
804
+ <div
805
+ className="toil-dt-pal"
806
+ onClick={(e) => {
807
+ e.stopPropagation();
808
+ }}>
809
+ <input
810
+ autoFocus
811
+ placeholder="Go to a route or run an action..."
812
+ value={q}
813
+ onChange={(e) => {
814
+ setQ(e.target.value);
815
+ setSel(0);
816
+ }}
817
+ onKeyDown={(e) => {
818
+ if (e.key === 'ArrowDown') {
819
+ e.preventDefault();
820
+ setSel((s) => Math.min(s + 1, filtered.length - 1));
821
+ } else if (e.key === 'ArrowUp') {
822
+ e.preventDefault();
823
+ setSel((s) => Math.max(s - 1, 0));
824
+ } else if (e.key === 'Enter') {
825
+ e.preventDefault();
826
+ filtered[clamped]?.run();
827
+ } else if (e.key === 'Escape') {
828
+ onClose();
829
+ }
830
+ }}
831
+ />
832
+ <div className="toil-dt-pal-list">
833
+ {filtered.length === 0 && <div className="toil-dt-pal-item">No matches</div>}
834
+ {filtered.map((it, i) => (
835
+ <div
836
+ key={`${it.kind}:${it.label}`}
837
+ className={`toil-dt-pal-item ${i === clamped ? 'sel' : ''}`}
838
+ onMouseEnter={() => {
839
+ setSel(i);
840
+ }}
841
+ onClick={it.run}>
842
+ <span>{it.label}</span>
843
+ <span className="toil-dt-pal-kind">{it.kind}</span>
844
+ </div>
845
+ ))}
846
+ </div>
847
+ </div>
848
+ </div>
849
+ );
850
+ }
851
+
852
+ function PrefsTab(): ReactNode {
853
+ const p = usePrefs();
854
+ const [flags, setFlags] = useState({ viewTransitions: false, transitions: false });
855
+ const toggle = (key: 'viewTransitions' | 'transitions'): void => {
856
+ const next = !flags[key];
857
+ setFlags((f) => ({ ...f, [key]: next }));
858
+ if (key === 'viewTransitions') setViewTransitions(next);
859
+ else setTransitions(next);
860
+ };
861
+ return (
862
+ <div className="toil-dt-body">
863
+ <div className="toil-dt-sw">
864
+ <span>View transitions</span>
865
+ <button
866
+ className="toil-dt-btn"
867
+ onClick={() => {
868
+ toggle('viewTransitions');
869
+ }}>
870
+ {flags.viewTransitions ? 'on' : 'off'}
871
+ </button>
872
+ </div>
873
+ <div className="toil-dt-sw">
874
+ <span>Loader transition</span>
875
+ <button
876
+ className="toil-dt-btn"
877
+ onClick={() => {
878
+ toggle('transitions');
879
+ }}>
880
+ {flags.transitions ? 'on' : 'off'}
881
+ </button>
882
+ </div>
883
+ <div className="toil-dt-sw">
884
+ <span>Toolbar side</span>
885
+ <button
886
+ className="toil-dt-btn"
887
+ onClick={() => {
888
+ setPrefs({ side: p.side === 'left' ? 'right' : 'left' });
889
+ }}>
890
+ {p.side}
891
+ </button>
892
+ </div>
893
+ </div>
894
+ );
895
+ }
896
+
897
+ const TABS: { id: Tab; label: string }[] = [
898
+ { id: 'route', label: 'Route' },
899
+ { id: 'data', label: 'Data' },
900
+ { id: 'head', label: 'Head' },
901
+ { id: 'build', label: 'Build' },
902
+ { id: 'errors', label: 'Errors' },
903
+ { id: 'ai', label: 'AI' },
904
+ { id: 'prefs', label: 'Prefs' },
905
+ ];
906
+
907
+ /** The dev toolbar. Rendered once at the app root in dev mode (see `mount`). */
908
+ export function DevToolbar({
909
+ routes,
910
+ slots,
911
+ }: {
912
+ routes: RouteDef[];
913
+ slots: Record<string, RouteDef[]>;
914
+ }): ReactNode {
915
+ const p = usePrefs();
916
+ const pending = usePending();
917
+ const errors = useErrors();
918
+ const [info, setInfo] = useState<DevInfo | null>(null);
919
+ const [palette, setPalette] = useState(false);
920
+
921
+ useEffect(() => {
922
+ injectStyles();
923
+ void fetch('/__toil/devinfo')
924
+ .then((r) => (r.ok ? (r.json() as Promise<DevInfo>) : null))
925
+ .then((data) => {
926
+ if (data) setInfo(data);
927
+ })
928
+ .catch(() => undefined);
929
+ }, []);
930
+
931
+ useEffect(() => {
932
+ const onKey = (e: KeyboardEvent): void => {
933
+ if ((e.metaKey || e.ctrlKey) && e.key.toLowerCase() === 'k') {
934
+ e.preventDefault();
935
+ setPalette((v) => !v);
936
+ }
937
+ };
938
+ window.addEventListener('keydown', onKey);
939
+ return () => {
940
+ window.removeEventListener('keydown', onKey);
941
+ };
942
+ }, []);
943
+
944
+ if (info && !info.enabled) return null;
945
+
946
+ const dotClass = errors.length > 0 ? 'error' : pending ? 'pending' : '';
947
+ const pal = palette ? (
948
+ <Palette
949
+ routes={routes}
950
+ onClose={() => {
951
+ setPalette(false);
952
+ }}
953
+ />
954
+ ) : null;
955
+
956
+ if (!p.open) {
957
+ return (
958
+ <>
959
+ <div className={`toil-dt ${p.side}`}>
960
+ <div
961
+ className="toil-dt-badge"
962
+ onClick={() => {
963
+ setPrefs({ open: true });
964
+ }}
965
+ title="toiljs devtools (cmd+K)">
966
+ <ToilLogo size={16} />
967
+ <span className={`toil-dt-dot ${dotClass}`} />
968
+ </div>
969
+ </div>
970
+ {pal}
971
+ </>
972
+ );
973
+ }
974
+
975
+ return (
976
+ <>
977
+ <div className={`toil-dt ${p.side}`}>
978
+ <div className="toil-dt-panel">
979
+ <div className="toil-dt-head">
980
+ <span style={{ display: 'flex', alignItems: 'center', gap: 6 }}>
981
+ <ToilLogo size={14} />
982
+ <span className="toil-dt-logo">toiljs</span> devtools
983
+ <span className={`toil-dt-dot ${dotClass}`} />
984
+ </span>
985
+ <button
986
+ className="toil-dt-x"
987
+ onClick={() => {
988
+ setPrefs({ open: false });
989
+ }}>
990
+
991
+ </button>
992
+ </div>
993
+ <div className="toil-dt-tabs">
994
+ {TABS.map((t) => (
995
+ <button
996
+ key={t.id}
997
+ className={`toil-dt-tab ${p.tab === t.id ? 'active' : ''}`}
998
+ onClick={() => {
999
+ setPrefs({ tab: t.id });
1000
+ }}>
1001
+ {t.label}
1002
+ {t.id === 'errors' && errors.length > 0 ? ` (${String(errors.length)})` : ''}
1003
+ </button>
1004
+ ))}
1005
+ </div>
1006
+ {p.tab === 'route' && <RouteTab routes={routes} slots={slots} info={info} />}
1007
+ {p.tab === 'data' && <DataTab />}
1008
+ {p.tab === 'head' && <HeadTab />}
1009
+ {p.tab === 'build' && <BuildTab info={info} />}
1010
+ {p.tab === 'errors' && <ErrorsTab />}
1011
+ {p.tab === 'ai' && <AiTab info={info} routes={routes} />}
1012
+ {p.tab === 'prefs' && <PrefsTab />}
1013
+ </div>
1014
+ </div>
1015
+ {pal}
1016
+ </>
1017
+ );
1018
+ }