Skip to content
Source
Sign inSign up
TanStack/router
Open History to see the last commit

main.tsx

145 lines4.0 KB

1import * as React from 'react'
2import ReactDOM from 'react-dom/client'
3import {
4 ErrorComponent,
5 RouterProvider,
6 createRouter,
7} from '@tanstack/react-router'
8import { auth } from './utils/auth'
9import { Spinner } from './components/Spinner'
10import { routeTree } from './routeTree.gen'
11import { useSessionStorage } from './hooks/useSessionStorage'
12import './styles.css'
13​
14//
15​
16const router = createRouter({
17 routeTree,
18 defaultPendingComponent: () => (
19 <div className={`p-2 text-2xl`}>
20 <Spinner />
21 </div>
22 ),
23 defaultErrorComponent: ({ error }) => <ErrorComponent error={error} />,
24 context: {
25 auth: undefined!, // We'll inject this when we render
26 },
27 defaultPreload: 'intent',
28 scrollRestoration: true,
29})
30​
31declare module '@tanstack/react-router' {
32 interface Register {
33 router: typeof router
34 }
36​
37function App() {
38 // This stuff is just to tweak our sandbox setup in real-time
39 const [loaderDelay, setLoaderDelay] = useSessionStorage('loaderDelay', 500)
40 const [pendingMs, setPendingMs] = useSessionStorage('pendingMs', 1000)
41 const [pendingMinMs, setPendingMinMs] = useSessionStorage('pendingMinMs', 500)
42​
43 return (
44 <>
45 <div className="text-xs fixed w-52 shadow-md shadow-black/20 rounded-sm bottom-2 left-2 bg-white dark:bg-gray-800 bg-opacity-75 border-b flex flex-col gap-1 flex-wrap items-left divide-y">
46 <div className="p-2 space-y-2">
47 <div className="flex gap-2">
48 <button
49 className="bg-blue-500 text-white rounded-sm p-1 px-2"
50 onClick={() => {
51 setLoaderDelay(150)
52 }}
53 >
54 Fast
55 </button>
56 <button
57 className="bg-blue-500 text-white rounded-sm p-1 px-2"
58 onClick={() => {
59 setLoaderDelay(500)
60 }}
61 >
62 Fast 3G
63 </button>
64 <button
65 className="bg-blue-500 text-white rounded-sm p-1 px-2"
66 onClick={() => {
67 setLoaderDelay(2000)
68 }}
69 >
70 Slow 3G
71 </button>
72 </div>
73 <div>
74 <div>Loader Delay: {loaderDelay}ms</div>
75 <input
76 type="range"
77 min="0"
78 max="5000"
79 step="100"
80 value={loaderDelay}
81 onChange={(e) => setLoaderDelay(e.target.valueAsNumber)}
82 className="w-full"
83 />
84 </div>
85 </div>
86 <div className="p-2 space-y-2">
87 <div className="flex gap-2">
88 <button
89 className="bg-blue-500 text-white rounded-sm p-1 px-2"
90 onClick={() => {
91 setPendingMs(1000)
92 setPendingMinMs(500)
93 }}
94 >
95 Reset to Default
96 </button>
97 </div>
98 <div>
99 <div>defaultPendingMs: {pendingMs}ms</div>
100 <input
101 type="range"
102 min="0"
103 max="5000"
104 step="100"
105 value={pendingMs}
106 onChange={(e) => setPendingMs(e.target.valueAsNumber)}
107 className="w-full"
108 />
109 </div>
110 <div>
111 <div>defaultPendingMinMs: {pendingMinMs}ms</div>
112 <input
113 type="range"
114 min="0"
115 max="5000"
116 step="100"
117 value={pendingMinMs}
118 onChange={(e) => setPendingMinMs(e.target.valueAsNumber)}
119 className="w-full"
120 />
121 </div>
122 </div>
123 </div>
124 <RouterProvider
125 router={router}
126 defaultPreload="intent"
127 defaultPendingMs={pendingMs}
128 defaultPendingMinMs={pendingMinMs}
129 context={{
130 auth,
131 }}
132 />
133 </>
134 )
136​
137const rootElement = document.getElementById('app')!
138if (!rootElement.innerHTML) {
139 const root = ReactDOM.createRoot(rootElement)
140 root.render(
141 <React.StrictMode>
142 <App />
143 </React.StrictMode>,
144 )