@soroush.design/cli 0.2.0-alpha.4 → 0.2.0-alpha.5
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/dist/bin.mjs +2 -15
- package/dist/index.mjs +1 -1
- package/dist/{io-C_8_JerZ.mjs → io-CqI2BTaP.mjs} +81 -12
- package/package.json +3 -2
- package/templates/react-router/js/base/README.md +67 -0
- package/templates/react-router/js/base/_gitignore +4 -0
- package/templates/{vike/js/spa/src/pages/+onRenderHtml.jsx → react-router/js/base/index.html} +3 -7
- package/templates/react-router/js/base/src/components/RouterLink.jsx +12 -0
- package/templates/react-router/js/base/src/data/api.js +76 -0
- package/templates/react-router/js/base/src/main.jsx +38 -0
- package/templates/react-router/js/base/src/pages/ErrorPage.jsx +14 -0
- package/templates/react-router/js/base/src/pages/Layout.jsx +141 -0
- package/templates/react-router/js/base/src/pages/index/Counter.jsx +18 -0
- package/templates/react-router/js/base/src/pages/index/Greeting.jsx +48 -0
- package/templates/react-router/js/base/src/pages/index/Page.jsx +29 -0
- package/templates/react-router/js/base/src/pages/index/Page.test.jsx +61 -0
- package/templates/react-router/js/base/src/routes.jsx +46 -0
- package/templates/react-router/js/base/vite.config.js +13 -0
- package/templates/react-router/js/base/vitest.config.js +13 -0
- package/templates/react-router/js/sample-data/src/data/items.js +55 -0
- package/templates/react-router/js/sample-data/src/pages/item/Page.jsx +49 -0
- package/templates/react-router/js/sample-data/src/pages/item/loader.js +14 -0
- package/templates/react-router/js/sample-data/src/pages/items/Page.jsx +48 -0
- package/templates/react-router/js/sample-data/src/pages/items/StatusChip.jsx +8 -0
- package/templates/react-router/js/sample-data/src/pages/items/loader.js +11 -0
- package/templates/react-router/js/with/jotai/src/pages/index/Counter.jsx +19 -0
- package/templates/react-router/js/with/jotai/src/state/counter.js +4 -0
- package/templates/react-router/js/with/jotai/src/state/scheme.js +4 -0
- package/templates/react-router/js/with/msw/_env +3 -0
- package/templates/react-router/js/with/msw/src/mocks/browser.js +5 -0
- package/templates/react-router/js/with/msw/src/mocks/handlers.js +24 -0
- package/templates/react-router/js/with/msw/src/mocks/server.js +5 -0
- package/templates/react-router/js/with/msw/src/mocks/setupTests.js +7 -0
- package/templates/react-router/js/with/msw/src/mocks/start.js +10 -0
- package/templates/react-router/js/with/react-hook-form/src/pages/form/Page.jsx +294 -0
- package/templates/react-router/js/with/redux-toolkit/src/pages/index/Counter.jsx +20 -0
- package/templates/react-router/js/with/redux-toolkit/src/state/store.js +27 -0
- package/templates/react-router/js/with/zustand/src/pages/index/Counter.jsx +17 -0
- package/templates/react-router/js/with/zustand/src/state/counter.js +14 -0
- package/templates/react-router/js/with/zustand/src/state/scheme.js +14 -0
- package/templates/react-router/ts/base/README.md +67 -0
- package/templates/react-router/ts/base/_gitignore +4 -0
- package/templates/react-router/ts/base/index.html +12 -0
- package/templates/react-router/ts/base/src/components/RouterLink.tsx +12 -0
- package/templates/react-router/ts/base/src/data/api.ts +84 -0
- package/templates/react-router/ts/base/src/main.tsx +38 -0
- package/templates/react-router/ts/base/src/pages/ErrorPage.tsx +14 -0
- package/templates/react-router/ts/base/src/pages/Layout.tsx +141 -0
- package/templates/react-router/ts/base/src/pages/index/Counter.tsx +18 -0
- package/templates/react-router/ts/base/src/pages/index/Greeting.tsx +49 -0
- package/templates/react-router/ts/base/src/pages/index/Page.test.tsx +61 -0
- package/templates/react-router/ts/base/src/pages/index/Page.tsx +29 -0
- package/templates/react-router/ts/base/src/routes.tsx +47 -0
- package/templates/react-router/ts/base/tsconfig.json +18 -0
- package/templates/react-router/ts/base/vite.config.ts +13 -0
- package/templates/react-router/ts/base/vitest.config.ts +13 -0
- package/templates/react-router/ts/sample-data/src/data/items.ts +61 -0
- package/templates/react-router/ts/sample-data/src/pages/item/Page.tsx +50 -0
- package/templates/react-router/ts/sample-data/src/pages/item/loader.ts +14 -0
- package/templates/react-router/ts/sample-data/src/pages/items/Page.tsx +49 -0
- package/templates/react-router/ts/sample-data/src/pages/items/StatusChip.tsx +9 -0
- package/templates/react-router/ts/sample-data/src/pages/items/loader.ts +11 -0
- package/templates/react-router/ts/with/jotai/src/pages/index/Counter.tsx +19 -0
- package/templates/react-router/ts/with/jotai/src/state/counter.ts +4 -0
- package/templates/react-router/ts/with/jotai/src/state/scheme.ts +4 -0
- package/templates/react-router/ts/with/msw/_env +3 -0
- package/templates/react-router/ts/with/msw/src/mocks/browser.ts +5 -0
- package/templates/react-router/ts/with/msw/src/mocks/handlers.ts +24 -0
- package/templates/react-router/ts/with/msw/src/mocks/server.ts +5 -0
- package/templates/react-router/ts/with/msw/src/mocks/setupTests.ts +7 -0
- package/templates/react-router/ts/with/msw/src/mocks/start.ts +10 -0
- package/templates/react-router/ts/with/react-hook-form/src/pages/form/Page.tsx +298 -0
- package/templates/react-router/ts/with/redux-toolkit/src/pages/index/Counter.tsx +20 -0
- package/templates/react-router/ts/with/redux-toolkit/src/state/store.ts +29 -0
- package/templates/react-router/ts/with/zustand/src/pages/index/Counter.tsx +17 -0
- package/templates/react-router/ts/with/zustand/src/state/counter.ts +19 -0
- package/templates/react-router/ts/with/zustand/src/state/scheme.ts +19 -0
- package/templates/templates.md +2 -1
- package/templates/vike/js/base/README.md +0 -4
- package/templates/vike/ts/base/README.md +0 -4
- package/templates/vike/js/spa/src/pages/+config.js +0 -12
- package/templates/vike/js/spa/src/pages/+onRenderClient.jsx +0 -25
- package/templates/vike/ts/spa/src/pages/+config.ts +0 -14
- package/templates/vike/ts/spa/src/pages/+onRenderClient.tsx +0 -27
- package/templates/vike/ts/spa/src/pages/+onRenderHtml.tsx +0 -17
- /package/templates/{vike/js/spa/src/pages/about/+Page.jsx → react-router/js/base/src/pages/about/Page.jsx} +0 -0
- /package/templates/{vike/ts/spa/src/pages/about/+Page.tsx → react-router/ts/base/src/pages/about/Page.tsx} +0 -0
|
@@ -0,0 +1,294 @@
|
|
|
1
|
+
// @if !tanstack-query && !swr
|
|
2
|
+
import { useState } from 'react'
|
|
3
|
+
// @endif
|
|
4
|
+
import { zodResolver } from '@hookform/resolvers/zod'
|
|
5
|
+
// @if tanstack-query
|
|
6
|
+
import { useMutation } from '@tanstack/react-query'
|
|
7
|
+
// @endif
|
|
8
|
+
import { Controller, useForm } from 'react-hook-form'
|
|
9
|
+
// @if swr
|
|
10
|
+
import useSWRMutation from 'swr/mutation'
|
|
11
|
+
// @endif
|
|
12
|
+
import { z } from 'zod'
|
|
13
|
+
import { Button } from '@soroush.tech/design-system/Button'
|
|
14
|
+
import { Flex } from '@soroush.tech/design-system/Flex'
|
|
15
|
+
import { TextInput } from '@soroush.tech/design-system/TextInput'
|
|
16
|
+
import { Typography } from '@soroush.tech/design-system/Typography'
|
|
17
|
+
// @if tanstack-query || swr || msw
|
|
18
|
+
import { sendMessage } from '../../data/api'
|
|
19
|
+
// @endif
|
|
20
|
+
|
|
21
|
+
/** What the form takes, and what it says when a value will not do. */
|
|
22
|
+
const schema = z.object({
|
|
23
|
+
name: z.string().trim().min(1, 'Your name, please.'),
|
|
24
|
+
email: z.email('An email address, please.'),
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
// @if tanstack-query
|
|
28
|
+
/** A form whose values a Zod schema checks; once they pass, it sends them with a mutation of TanStack Query. */
|
|
29
|
+
export default function Page() {
|
|
30
|
+
const mutation = useMutation({ mutationFn: sendMessage })
|
|
31
|
+
const { control, handleSubmit } = useForm({
|
|
32
|
+
resolver: zodResolver(schema),
|
|
33
|
+
defaultValues: { name: '', email: '' },
|
|
34
|
+
})
|
|
35
|
+
return (
|
|
36
|
+
<Flex gap={2} alignItems="flex-start">
|
|
37
|
+
<Typography variant="h2">Form</Typography>
|
|
38
|
+
<Typography color="secondary">
|
|
39
|
+
React Hook Form holds the values, and a Zod schema checks them when the form is sent.
|
|
40
|
+
</Typography>
|
|
41
|
+
<form noValidate onSubmit={handleSubmit((values) => mutation.mutate(values))}>
|
|
42
|
+
<Flex gap={2} alignItems="flex-start">
|
|
43
|
+
<Controller
|
|
44
|
+
name="name"
|
|
45
|
+
control={control}
|
|
46
|
+
render={({ field, fieldState }) => (
|
|
47
|
+
<Flex gap={1}>
|
|
48
|
+
<TextInput
|
|
49
|
+
name={field.name}
|
|
50
|
+
value={field.value}
|
|
51
|
+
onChange={field.onChange}
|
|
52
|
+
placeholder="Name"
|
|
53
|
+
error={fieldState.invalid}
|
|
54
|
+
inputProps={{ 'aria-label': 'Name', autoComplete: 'name', onBlur: field.onBlur }}
|
|
55
|
+
/>
|
|
56
|
+
<Typography variant="caption" color="error">
|
|
57
|
+
{fieldState.error?.message}
|
|
58
|
+
</Typography>
|
|
59
|
+
</Flex>
|
|
60
|
+
)}
|
|
61
|
+
/>
|
|
62
|
+
<Controller
|
|
63
|
+
name="email"
|
|
64
|
+
control={control}
|
|
65
|
+
render={({ field, fieldState }) => (
|
|
66
|
+
<Flex gap={1}>
|
|
67
|
+
<TextInput
|
|
68
|
+
name={field.name}
|
|
69
|
+
value={field.value}
|
|
70
|
+
onChange={field.onChange}
|
|
71
|
+
type="email"
|
|
72
|
+
placeholder="Email"
|
|
73
|
+
error={fieldState.invalid}
|
|
74
|
+
inputProps={{
|
|
75
|
+
'aria-label': 'Email',
|
|
76
|
+
autoComplete: 'email',
|
|
77
|
+
onBlur: field.onBlur,
|
|
78
|
+
}}
|
|
79
|
+
/>
|
|
80
|
+
<Typography variant="caption" color="error">
|
|
81
|
+
{fieldState.error?.message}
|
|
82
|
+
</Typography>
|
|
83
|
+
</Flex>
|
|
84
|
+
)}
|
|
85
|
+
/>
|
|
86
|
+
<Button type="submit">Send</Button>
|
|
87
|
+
</Flex>
|
|
88
|
+
</form>
|
|
89
|
+
{mutation.data ? <Typography>{mutation.data.message}</Typography> : null}
|
|
90
|
+
</Flex>
|
|
91
|
+
)
|
|
92
|
+
}
|
|
93
|
+
// @elif swr
|
|
94
|
+
/** A form whose values a Zod schema checks; once they pass, it sends them with SWR's mutation. */
|
|
95
|
+
export default function Page() {
|
|
96
|
+
const { trigger, data } = useSWRMutation('messages', (_key, { arg }) => sendMessage(arg))
|
|
97
|
+
const { control, handleSubmit } = useForm({
|
|
98
|
+
resolver: zodResolver(schema),
|
|
99
|
+
defaultValues: { name: '', email: '' },
|
|
100
|
+
})
|
|
101
|
+
return (
|
|
102
|
+
<Flex gap={2} alignItems="flex-start">
|
|
103
|
+
<Typography variant="h2">Form</Typography>
|
|
104
|
+
<Typography color="secondary">
|
|
105
|
+
React Hook Form holds the values, and a Zod schema checks them when the form is sent.
|
|
106
|
+
</Typography>
|
|
107
|
+
<form noValidate onSubmit={handleSubmit((values) => trigger(values))}>
|
|
108
|
+
<Flex gap={2} alignItems="flex-start">
|
|
109
|
+
<Controller
|
|
110
|
+
name="name"
|
|
111
|
+
control={control}
|
|
112
|
+
render={({ field, fieldState }) => (
|
|
113
|
+
<Flex gap={1}>
|
|
114
|
+
<TextInput
|
|
115
|
+
name={field.name}
|
|
116
|
+
value={field.value}
|
|
117
|
+
onChange={field.onChange}
|
|
118
|
+
placeholder="Name"
|
|
119
|
+
error={fieldState.invalid}
|
|
120
|
+
inputProps={{ 'aria-label': 'Name', autoComplete: 'name', onBlur: field.onBlur }}
|
|
121
|
+
/>
|
|
122
|
+
<Typography variant="caption" color="error">
|
|
123
|
+
{fieldState.error?.message}
|
|
124
|
+
</Typography>
|
|
125
|
+
</Flex>
|
|
126
|
+
)}
|
|
127
|
+
/>
|
|
128
|
+
<Controller
|
|
129
|
+
name="email"
|
|
130
|
+
control={control}
|
|
131
|
+
render={({ field, fieldState }) => (
|
|
132
|
+
<Flex gap={1}>
|
|
133
|
+
<TextInput
|
|
134
|
+
name={field.name}
|
|
135
|
+
value={field.value}
|
|
136
|
+
onChange={field.onChange}
|
|
137
|
+
type="email"
|
|
138
|
+
placeholder="Email"
|
|
139
|
+
error={fieldState.invalid}
|
|
140
|
+
inputProps={{
|
|
141
|
+
'aria-label': 'Email',
|
|
142
|
+
autoComplete: 'email',
|
|
143
|
+
onBlur: field.onBlur,
|
|
144
|
+
}}
|
|
145
|
+
/>
|
|
146
|
+
<Typography variant="caption" color="error">
|
|
147
|
+
{fieldState.error?.message}
|
|
148
|
+
</Typography>
|
|
149
|
+
</Flex>
|
|
150
|
+
)}
|
|
151
|
+
/>
|
|
152
|
+
<Button type="submit">Send</Button>
|
|
153
|
+
</Flex>
|
|
154
|
+
</form>
|
|
155
|
+
{data ? <Typography>{data.message}</Typography> : null}
|
|
156
|
+
</Flex>
|
|
157
|
+
)
|
|
158
|
+
}
|
|
159
|
+
// @elif msw
|
|
160
|
+
/** A form whose values a Zod schema checks; once they pass, it sends them to the API. */
|
|
161
|
+
export default function Page() {
|
|
162
|
+
const [answer, setAnswer] = useState(null)
|
|
163
|
+
const { control, handleSubmit } = useForm({
|
|
164
|
+
resolver: zodResolver(schema),
|
|
165
|
+
defaultValues: { name: '', email: '' },
|
|
166
|
+
})
|
|
167
|
+
return (
|
|
168
|
+
<Flex gap={2} alignItems="flex-start">
|
|
169
|
+
<Typography variant="h2">Form</Typography>
|
|
170
|
+
<Typography color="secondary">
|
|
171
|
+
React Hook Form holds the values, and a Zod schema checks them when the form is sent.
|
|
172
|
+
</Typography>
|
|
173
|
+
<form
|
|
174
|
+
noValidate
|
|
175
|
+
onSubmit={handleSubmit(async (values) => setAnswer((await sendMessage(values)).message))}
|
|
176
|
+
>
|
|
177
|
+
<Flex gap={2} alignItems="flex-start">
|
|
178
|
+
<Controller
|
|
179
|
+
name="name"
|
|
180
|
+
control={control}
|
|
181
|
+
render={({ field, fieldState }) => (
|
|
182
|
+
<Flex gap={1}>
|
|
183
|
+
<TextInput
|
|
184
|
+
name={field.name}
|
|
185
|
+
value={field.value}
|
|
186
|
+
onChange={field.onChange}
|
|
187
|
+
placeholder="Name"
|
|
188
|
+
error={fieldState.invalid}
|
|
189
|
+
inputProps={{ 'aria-label': 'Name', autoComplete: 'name', onBlur: field.onBlur }}
|
|
190
|
+
/>
|
|
191
|
+
<Typography variant="caption" color="error">
|
|
192
|
+
{fieldState.error?.message}
|
|
193
|
+
</Typography>
|
|
194
|
+
</Flex>
|
|
195
|
+
)}
|
|
196
|
+
/>
|
|
197
|
+
<Controller
|
|
198
|
+
name="email"
|
|
199
|
+
control={control}
|
|
200
|
+
render={({ field, fieldState }) => (
|
|
201
|
+
<Flex gap={1}>
|
|
202
|
+
<TextInput
|
|
203
|
+
name={field.name}
|
|
204
|
+
value={field.value}
|
|
205
|
+
onChange={field.onChange}
|
|
206
|
+
type="email"
|
|
207
|
+
placeholder="Email"
|
|
208
|
+
error={fieldState.invalid}
|
|
209
|
+
inputProps={{
|
|
210
|
+
'aria-label': 'Email',
|
|
211
|
+
autoComplete: 'email',
|
|
212
|
+
onBlur: field.onBlur,
|
|
213
|
+
}}
|
|
214
|
+
/>
|
|
215
|
+
<Typography variant="caption" color="error">
|
|
216
|
+
{fieldState.error?.message}
|
|
217
|
+
</Typography>
|
|
218
|
+
</Flex>
|
|
219
|
+
)}
|
|
220
|
+
/>
|
|
221
|
+
<Button type="submit">Send</Button>
|
|
222
|
+
</Flex>
|
|
223
|
+
</form>
|
|
224
|
+
{answer ? <Typography>{answer}</Typography> : null}
|
|
225
|
+
</Flex>
|
|
226
|
+
)
|
|
227
|
+
}
|
|
228
|
+
// @else
|
|
229
|
+
/** A form whose values a Zod schema checks; once they pass, it shows them. */
|
|
230
|
+
export default function Page() {
|
|
231
|
+
const [sent, setSent] = useState(null)
|
|
232
|
+
const { control, handleSubmit } = useForm({
|
|
233
|
+
resolver: zodResolver(schema),
|
|
234
|
+
defaultValues: { name: '', email: '' },
|
|
235
|
+
})
|
|
236
|
+
return (
|
|
237
|
+
<Flex gap={2} alignItems="flex-start">
|
|
238
|
+
<Typography variant="h2">Form</Typography>
|
|
239
|
+
<Typography color="secondary">
|
|
240
|
+
React Hook Form holds the values, and a Zod schema checks them when the form is sent.
|
|
241
|
+
</Typography>
|
|
242
|
+
<form noValidate onSubmit={handleSubmit(setSent)}>
|
|
243
|
+
<Flex gap={2} alignItems="flex-start">
|
|
244
|
+
<Controller
|
|
245
|
+
name="name"
|
|
246
|
+
control={control}
|
|
247
|
+
render={({ field, fieldState }) => (
|
|
248
|
+
<Flex gap={1}>
|
|
249
|
+
<TextInput
|
|
250
|
+
name={field.name}
|
|
251
|
+
value={field.value}
|
|
252
|
+
onChange={field.onChange}
|
|
253
|
+
placeholder="Name"
|
|
254
|
+
error={fieldState.invalid}
|
|
255
|
+
inputProps={{ 'aria-label': 'Name', autoComplete: 'name', onBlur: field.onBlur }}
|
|
256
|
+
/>
|
|
257
|
+
<Typography variant="caption" color="error">
|
|
258
|
+
{fieldState.error?.message}
|
|
259
|
+
</Typography>
|
|
260
|
+
</Flex>
|
|
261
|
+
)}
|
|
262
|
+
/>
|
|
263
|
+
<Controller
|
|
264
|
+
name="email"
|
|
265
|
+
control={control}
|
|
266
|
+
render={({ field, fieldState }) => (
|
|
267
|
+
<Flex gap={1}>
|
|
268
|
+
<TextInput
|
|
269
|
+
name={field.name}
|
|
270
|
+
value={field.value}
|
|
271
|
+
onChange={field.onChange}
|
|
272
|
+
type="email"
|
|
273
|
+
placeholder="Email"
|
|
274
|
+
error={fieldState.invalid}
|
|
275
|
+
inputProps={{
|
|
276
|
+
'aria-label': 'Email',
|
|
277
|
+
autoComplete: 'email',
|
|
278
|
+
onBlur: field.onBlur,
|
|
279
|
+
}}
|
|
280
|
+
/>
|
|
281
|
+
<Typography variant="caption" color="error">
|
|
282
|
+
{fieldState.error?.message}
|
|
283
|
+
</Typography>
|
|
284
|
+
</Flex>
|
|
285
|
+
)}
|
|
286
|
+
/>
|
|
287
|
+
<Button type="submit">Send</Button>
|
|
288
|
+
</Flex>
|
|
289
|
+
</form>
|
|
290
|
+
{sent ? <Typography>Thanks, {sent.name}: the form was valid.</Typography> : null}
|
|
291
|
+
</Flex>
|
|
292
|
+
)
|
|
293
|
+
}
|
|
294
|
+
// @endif
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { Button } from '@soroush.tech/design-system/Button'
|
|
2
|
+
import { Flex } from '@soroush.tech/design-system/Flex'
|
|
3
|
+
import { Typography } from '@soroush.tech/design-system/Typography'
|
|
4
|
+
import { useDispatch, useSelector } from 'react-redux'
|
|
5
|
+
import { incremented } from '../../state/store'
|
|
6
|
+
|
|
7
|
+
/** A count, held in the Redux store, src/state/store, and the button that adds to it. */
|
|
8
|
+
export function Counter() {
|
|
9
|
+
const count = useSelector((state) => state.count)
|
|
10
|
+
const dispatch = useDispatch()
|
|
11
|
+
const increment = () => dispatch(incremented())
|
|
12
|
+
return (
|
|
13
|
+
<Flex flexDirection="row" alignItems="center" gap={2}>
|
|
14
|
+
<Button size="sm" variant="outlined" onClick={increment}>
|
|
15
|
+
Count
|
|
16
|
+
</Button>
|
|
17
|
+
<Typography>Count: {count}</Typography>
|
|
18
|
+
</Flex>
|
|
19
|
+
)
|
|
20
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { configureStore, createSlice } from '@reduxjs/toolkit'
|
|
2
|
+
|
|
3
|
+
/** The color scheme the app is drawn in: a slice of the store. */
|
|
4
|
+
const schemeSlice = createSlice({
|
|
5
|
+
name: 'scheme',
|
|
6
|
+
initialState: '__SCHEME__',
|
|
7
|
+
reducers: {
|
|
8
|
+
schemeChanged: (_scheme, action) => action.payload,
|
|
9
|
+
},
|
|
10
|
+
})
|
|
11
|
+
|
|
12
|
+
/** The home page's count: another slice. */
|
|
13
|
+
const countSlice = createSlice({
|
|
14
|
+
name: 'count',
|
|
15
|
+
initialState: 0,
|
|
16
|
+
reducers: {
|
|
17
|
+
incremented: (count) => count + 1,
|
|
18
|
+
},
|
|
19
|
+
})
|
|
20
|
+
|
|
21
|
+
export const { schemeChanged } = schemeSlice.actions
|
|
22
|
+
export const { incremented } = countSlice.actions
|
|
23
|
+
|
|
24
|
+
/** The app's Redux store, which src/main.jsx provides to every page. */
|
|
25
|
+
export const store = configureStore({
|
|
26
|
+
reducer: { scheme: schemeSlice.reducer, count: countSlice.reducer },
|
|
27
|
+
})
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { Button } from '@soroush.tech/design-system/Button'
|
|
2
|
+
import { Flex } from '@soroush.tech/design-system/Flex'
|
|
3
|
+
import { Typography } from '@soroush.tech/design-system/Typography'
|
|
4
|
+
import { useCounter } from '../../state/counter'
|
|
5
|
+
|
|
6
|
+
/** A count, held in a zustand store, src/state/counter, and the button that adds to it. */
|
|
7
|
+
export function Counter() {
|
|
8
|
+
const { count, increment } = useCounter()
|
|
9
|
+
return (
|
|
10
|
+
<Flex flexDirection="row" alignItems="center" gap={2}>
|
|
11
|
+
<Button size="sm" variant="outlined" onClick={increment}>
|
|
12
|
+
Count
|
|
13
|
+
</Button>
|
|
14
|
+
<Typography>Count: {count}</Typography>
|
|
15
|
+
</Flex>
|
|
16
|
+
)
|
|
17
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { create } from 'zustand'
|
|
2
|
+
|
|
3
|
+
/** The home page's count: a zustand store, which any component can read. */
|
|
4
|
+
// @if ts
|
|
5
|
+
export const useCounter = create()((set) => ({
|
|
6
|
+
count: 0,
|
|
7
|
+
increment: () => set((state) => ({ count: state.count + 1 })),
|
|
8
|
+
}))
|
|
9
|
+
// @else
|
|
10
|
+
export const useCounter = create((set) => ({
|
|
11
|
+
count: 0,
|
|
12
|
+
increment: () => set((state) => ({ count: state.count + 1 })),
|
|
13
|
+
}))
|
|
14
|
+
// @endif
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { create } from 'zustand'
|
|
2
|
+
|
|
3
|
+
/** The color scheme the app is drawn in: a zustand store, which any component can read. */
|
|
4
|
+
// @if ts
|
|
5
|
+
export const useScheme = create()((set) => ({
|
|
6
|
+
scheme: '__SCHEME__',
|
|
7
|
+
setScheme: (scheme) => set({ scheme }),
|
|
8
|
+
}))
|
|
9
|
+
// @else
|
|
10
|
+
export const useScheme = create((set) => ({
|
|
11
|
+
scheme: '__SCHEME__',
|
|
12
|
+
setScheme: (scheme) => set({ scheme }),
|
|
13
|
+
}))
|
|
14
|
+
// @endif
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
# __TITLE__
|
|
2
|
+
|
|
3
|
+
A single-page app on [React Router](https://reactrouter.com) and the
|
|
4
|
+
[@soroush.tech design system](https://docs.soroush.tech): the browser renders each page, and moves
|
|
5
|
+
between them with no reload.
|
|
6
|
+
|
|
7
|
+
- `npm run dev` starts it, `npm run build` builds it, and `npm run preview` serves the build.
|
|
8
|
+
- `dist/` is static files: any host serves it, once it answers every path with `index.html`, so the
|
|
9
|
+
router shows the page a URL names.
|
|
10
|
+
- `src/routes.tsx` lists the pages by path, and `src/main.tsx` renders the router in `index.html`.
|
|
11
|
+
- `src/pages/` holds the pages, and `src/pages/Layout.tsx` is the frame every page renders in,
|
|
12
|
+
inside the app's providers. `src/components/RouterLink.tsx` is the design system's `Link`, moving
|
|
13
|
+
to a page with no reload.
|
|
14
|
+
- `src/theme/themes.ts` holds the light and the dark theme: make them your own.
|
|
15
|
+
<!-- @if sample-data && (tanstack-query || swr) -->
|
|
16
|
+
- `src/pages/items/` lists the sample items of `src/data/items.ts`, and `src/pages/item/` shows one: both read them through `src/data/api.ts`.
|
|
17
|
+
<!-- @elif sample-data -->
|
|
18
|
+
- `src/pages/items/` lists the sample items of `src/data/items.ts`, and `src/pages/item/` shows one: their loaders read the data before the router shows the page. Make them fetch your own.
|
|
19
|
+
<!-- @endif -->
|
|
20
|
+
<!-- @if !zustand && !jotai && !redux-toolkit -->
|
|
21
|
+
- `src/pages/index/Counter.tsx` counts the presses of its button, in its own state.
|
|
22
|
+
<!-- @endif -->
|
|
23
|
+
<!-- @if tanstack-query && react-hook-form && sample-data -->
|
|
24
|
+
- [TanStack Query](https://tanstack.com/query): the query client lives in `Layout`. The home page's `Greeting` and the items pages read `src/data/api.ts` with queries, and the form sends its values with a mutation.
|
|
25
|
+
<!-- @elif tanstack-query && react-hook-form -->
|
|
26
|
+
- [TanStack Query](https://tanstack.com/query): the query client lives in `Layout`. The home page's `Greeting` reads `src/data/api.ts` with a query, and the form sends its values with a mutation.
|
|
27
|
+
<!-- @elif tanstack-query && sample-data -->
|
|
28
|
+
- [TanStack Query](https://tanstack.com/query): the query client lives in `Layout`. The home page's `Greeting` and the items pages read `src/data/api.ts` with queries.
|
|
29
|
+
<!-- @elif tanstack-query -->
|
|
30
|
+
- [TanStack Query](https://tanstack.com/query): the query client lives in `Layout`. The home page's `Greeting` reads `src/data/api.ts` with a query.
|
|
31
|
+
<!-- @endif -->
|
|
32
|
+
<!-- @if swr && react-hook-form && sample-data -->
|
|
33
|
+
- [SWR](https://swr.vercel.app): the home page's `Greeting` and the items pages read `src/data/api.ts` with SWR, and the form sends its values with a mutation.
|
|
34
|
+
<!-- @elif swr && react-hook-form -->
|
|
35
|
+
- [SWR](https://swr.vercel.app): the home page's `Greeting` reads `src/data/api.ts` with SWR, and the form sends its values with a mutation.
|
|
36
|
+
<!-- @elif swr && sample-data -->
|
|
37
|
+
- [SWR](https://swr.vercel.app): the home page's `Greeting` and the items pages read `src/data/api.ts` with SWR.
|
|
38
|
+
<!-- @elif swr -->
|
|
39
|
+
- [SWR](https://swr.vercel.app): the home page's `Greeting` reads `src/data/api.ts` with SWR.
|
|
40
|
+
<!-- @endif -->
|
|
41
|
+
<!-- @if msw && react-hook-form && sample-data && (tanstack-query || swr) -->
|
|
42
|
+
- [msw](https://mswjs.io): `src/mocks/handlers.ts` is a mock API, which answers the greeting, the form, the sample items in the browser while `VITE_MSW` in `.env` is true.
|
|
43
|
+
<!-- @elif msw && react-hook-form -->
|
|
44
|
+
- [msw](https://mswjs.io): `src/mocks/handlers.ts` is a mock API, which answers the greeting, the form in the browser while `VITE_MSW` in `.env` is true.
|
|
45
|
+
<!-- @elif msw && sample-data && (tanstack-query || swr) -->
|
|
46
|
+
- [msw](https://mswjs.io): `src/mocks/handlers.ts` is a mock API, which answers the greeting, the sample items in the browser while `VITE_MSW` in `.env` is true.
|
|
47
|
+
<!-- @elif msw -->
|
|
48
|
+
- [msw](https://mswjs.io): `src/mocks/handlers.ts` is a mock API, which answers the greeting in the browser while `VITE_MSW` in `.env` is true.
|
|
49
|
+
<!-- @endif -->
|
|
50
|
+
<!-- @if zustand -->
|
|
51
|
+
- [zustand](https://zustand.docs.pmnd.rs): `src/state/scheme.ts` holds the color scheme and `src/state/counter.ts` the home page's count, in stores.
|
|
52
|
+
<!-- @elif jotai -->
|
|
53
|
+
- [Jotai](https://jotai.org): `src/state/scheme.ts` holds the color scheme and `src/state/counter.ts` the home page's count, in atoms.
|
|
54
|
+
<!-- @elif redux-toolkit -->
|
|
55
|
+
- [Redux Toolkit](https://redux-toolkit.js.org): `src/state/store.ts` holds the color scheme and the home page's count, in slices of the store `src/main.tsx` provides.
|
|
56
|
+
<!-- @endif -->
|
|
57
|
+
<!-- @if react-hook-form -->
|
|
58
|
+
- [React Hook Form](https://react-hook-form.com) and [Zod](https://zod.dev): `/form` is a form whose values a Zod schema checks.
|
|
59
|
+
<!-- @endif -->
|
|
60
|
+
<!-- @if msw -->
|
|
61
|
+
- [Vitest](https://vitest.dev) and [Testing Library](https://testing-library.com): `npm run test` runs `src/pages/index/Page.test.tsx`, which checks the title, the counter and the greeting, which msw's Node server, `src/mocks/server.ts`, answers.
|
|
62
|
+
<!-- @else -->
|
|
63
|
+
- [Vitest](https://vitest.dev) and [Testing Library](https://testing-library.com): `npm run test` runs `src/pages/index/Page.test.tsx`, which checks the title and the counter.
|
|
64
|
+
<!-- @endif -->
|
|
65
|
+
|
|
66
|
+
Fonts: the theme names Space Grotesk and JetBrains Mono, and does not load them. See
|
|
67
|
+
https://docs.soroush.tech/design-system/getting-started/installation/#fonts
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
<!doctype html>
|
|
2
|
+
<html lang="en">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="UTF-8" />
|
|
5
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
6
|
+
<title>__TITLE__</title>
|
|
7
|
+
</head>
|
|
8
|
+
<body>
|
|
9
|
+
<div id="root"></div>
|
|
10
|
+
<script type="module" src="/src/main.tsx"></script>
|
|
11
|
+
</body>
|
|
12
|
+
</html>
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { Link, type LinkProps } from '@soroush.tech/design-system/Link'
|
|
2
|
+
import { useHref, useLinkClickHandler } from 'react-router'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The design system's Link to a page of the app: a click moves to it with no reload, as the
|
|
6
|
+
* router's own Link does, and a click that opens a new tab still does.
|
|
7
|
+
*/
|
|
8
|
+
export function RouterLink({ to, ...props }: Omit<LinkProps, 'href' | 'onClick'> & { to: string }) {
|
|
9
|
+
const href = useHref(to)
|
|
10
|
+
const handleClick = useLinkClickHandler(to)
|
|
11
|
+
return <Link {...props} href={href} onClick={handleClick} />
|
|
12
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
// @if tanstack-query || swr || msw
|
|
2
|
+
// @if ts && sample-data && msw && (tanstack-query || swr)
|
|
3
|
+
import type { Item } from './items'
|
|
4
|
+
|
|
5
|
+
// @elif sample-data && !msw && (tanstack-query || swr)
|
|
6
|
+
import { items } from './items'
|
|
7
|
+
|
|
8
|
+
// @endif
|
|
9
|
+
// @if ts
|
|
10
|
+
/** What the API says to the app. */
|
|
11
|
+
export interface Greeting {
|
|
12
|
+
message: string
|
|
13
|
+
}
|
|
14
|
+
// @if react-hook-form
|
|
15
|
+
|
|
16
|
+
/** What the form sends. */
|
|
17
|
+
export interface Message {
|
|
18
|
+
name: string
|
|
19
|
+
email: string
|
|
20
|
+
}
|
|
21
|
+
// @endif
|
|
22
|
+
|
|
23
|
+
// @endif
|
|
24
|
+
// @if msw
|
|
25
|
+
/** A greeting, from the API: while VITE_MSW is true, the mock API of src/mocks answers. */
|
|
26
|
+
export async function fetchGreeting(): Promise<Greeting> {
|
|
27
|
+
const response = await fetch('/api/greeting')
|
|
28
|
+
if (!response.ok) throw new Error(`GET /api/greeting answered ${response.status}`)
|
|
29
|
+
return response.json()
|
|
30
|
+
}
|
|
31
|
+
// @else
|
|
32
|
+
/** A greeting, as an API would answer: make it fetch from yours. */
|
|
33
|
+
export function fetchGreeting(): Promise<Greeting> {
|
|
34
|
+
return Promise.resolve({ message: 'Hello from src/data/api: make it fetch from your API.' })
|
|
35
|
+
}
|
|
36
|
+
// @endif
|
|
37
|
+
// @if react-hook-form && msw
|
|
38
|
+
|
|
39
|
+
/** Sends the form's values to the API, which answers what it made of them. */
|
|
40
|
+
export async function sendMessage(message: Message): Promise<Greeting> {
|
|
41
|
+
const response = await fetch('/api/messages', {
|
|
42
|
+
method: 'POST',
|
|
43
|
+
headers: { 'Content-Type': 'application/json' },
|
|
44
|
+
body: JSON.stringify(message),
|
|
45
|
+
})
|
|
46
|
+
if (!response.ok) throw new Error(`POST /api/messages answered ${response.status}`)
|
|
47
|
+
return response.json()
|
|
48
|
+
}
|
|
49
|
+
// @elif react-hook-form
|
|
50
|
+
|
|
51
|
+
/** Sends the form's values, as an API would take them: make it post to yours. */
|
|
52
|
+
export function sendMessage(message: Message): Promise<Greeting> {
|
|
53
|
+
return Promise.resolve({ message: `Thanks, ${message.name}: your message was sent.` })
|
|
54
|
+
}
|
|
55
|
+
// @endif
|
|
56
|
+
// @if sample-data && msw && (tanstack-query || swr)
|
|
57
|
+
|
|
58
|
+
/** The items, from the API: while VITE_MSW is true, the mock API of src/mocks answers. */
|
|
59
|
+
export async function fetchItems(): Promise<Pick<Item, 'id' | 'title' | 'status'>[]> {
|
|
60
|
+
const response = await fetch('/api/items')
|
|
61
|
+
if (!response.ok) throw new Error(`GET /api/items answered ${response.status}`)
|
|
62
|
+
return response.json()
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** One item, from the API. */
|
|
66
|
+
export async function fetchItem(id: string): Promise<Item> {
|
|
67
|
+
const response = await fetch(`/api/items/${id}`)
|
|
68
|
+
if (!response.ok) throw new Error(`GET /api/items/${id} answered ${response.status}`)
|
|
69
|
+
return response.json()
|
|
70
|
+
}
|
|
71
|
+
// @elif sample-data && (tanstack-query || swr)
|
|
72
|
+
|
|
73
|
+
/** The items, as an API would answer: make it fetch from yours. */
|
|
74
|
+
export function fetchItems() {
|
|
75
|
+
return Promise.resolve(items.map(({ id, title, status }) => ({ id, title, status })))
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** One item, as an API would answer: make it fetch from yours. */
|
|
79
|
+
export function fetchItem(id: string) {
|
|
80
|
+
const item = items.find((candidate) => candidate.id === id)
|
|
81
|
+
return item ? Promise.resolve(item) : Promise.reject(new Error(`There is no item ${id}.`))
|
|
82
|
+
}
|
|
83
|
+
// @endif
|
|
84
|
+
// @endif
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { StrictMode } from 'react'
|
|
2
|
+
import { createRoot } from 'react-dom/client'
|
|
3
|
+
// @if redux-toolkit
|
|
4
|
+
import { Provider as StoreProvider } from 'react-redux'
|
|
5
|
+
// @endif
|
|
6
|
+
import { createBrowserRouter } from 'react-router'
|
|
7
|
+
import { RouterProvider } from 'react-router/dom'
|
|
8
|
+
// @if msw
|
|
9
|
+
import { startMocks } from './mocks/start'
|
|
10
|
+
// @endif
|
|
11
|
+
import { routes } from './routes'
|
|
12
|
+
// @if redux-toolkit
|
|
13
|
+
import { store } from './state/store'
|
|
14
|
+
// @endif
|
|
15
|
+
|
|
16
|
+
// @if msw
|
|
17
|
+
// The mock API starts first, so it answers the app's first request.
|
|
18
|
+
await startMocks()
|
|
19
|
+
|
|
20
|
+
// @endif
|
|
21
|
+
/** The router: it shows the page the URL names, and moves between pages with no reload. */
|
|
22
|
+
const router = createBrowserRouter(routes)
|
|
23
|
+
|
|
24
|
+
// @if redux-toolkit
|
|
25
|
+
createRoot(document.getElementById('root')!).render(
|
|
26
|
+
<StrictMode>
|
|
27
|
+
<StoreProvider store={store}>
|
|
28
|
+
<RouterProvider router={router} />
|
|
29
|
+
</StoreProvider>
|
|
30
|
+
</StrictMode>
|
|
31
|
+
)
|
|
32
|
+
// @else
|
|
33
|
+
createRoot(document.getElementById('root')!).render(
|
|
34
|
+
<StrictMode>
|
|
35
|
+
<RouterProvider router={router} />
|
|
36
|
+
</StrictMode>
|
|
37
|
+
)
|
|
38
|
+
// @endif
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { Flex } from '@soroush.tech/design-system/Flex'
|
|
2
|
+
import { Typography } from '@soroush.tech/design-system/Typography'
|
|
3
|
+
import { RouterLink } from '../components/RouterLink'
|
|
4
|
+
|
|
5
|
+
/** Shown for a path with no page, and when a page fails to load or to render. */
|
|
6
|
+
export function ErrorPage() {
|
|
7
|
+
return (
|
|
8
|
+
<Flex gap={2} alignItems="flex-start">
|
|
9
|
+
<Typography variant="h2">Page not found</Typography>
|
|
10
|
+
<Typography color="secondary">This page does not exist, or it could not be shown.</Typography>
|
|
11
|
+
<RouterLink to="/">Back home</RouterLink>
|
|
12
|
+
</Flex>
|
|
13
|
+
)
|
|
14
|
+
}
|