@soroush.design/cli 0.2.0-alpha.3 → 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.d.mts +4 -4
- package/dist/index.mjs +1 -1
- package/dist/{io-Bybgct-f.mjs → io-CqI2BTaP.mjs} +430 -1032
- package/package.json +14 -5
- package/templates/astro/js/base/README.md +93 -0
- package/templates/astro/js/base/_gitignore +5 -0
- package/templates/astro/js/base/astro.config.mjs +23 -0
- package/templates/astro/js/base/src/components/About.jsx +24 -0
- package/templates/astro/js/base/src/components/Counter.jsx +26 -0
- package/templates/astro/js/base/src/components/Greeting.jsx +60 -0
- package/templates/astro/js/base/src/components/Header.jsx +99 -0
- package/templates/astro/js/base/src/components/Home.jsx +32 -0
- package/templates/astro/js/base/src/components/Home.test.jsx +39 -0
- package/templates/astro/js/base/src/components/NotFound.jsx +17 -0
- package/templates/astro/js/base/src/data/api.js +84 -0
- package/templates/astro/js/base/src/layouts/Layout.astro +52 -0
- package/templates/astro/js/base/src/pages/404.astro +12 -0
- package/templates/astro/js/base/src/pages/about.astro +12 -0
- package/templates/astro/js/base/src/pages/index.astro +23 -0
- package/templates/astro/js/base/src/theme/Island.jsx +89 -0
- package/templates/astro/js/base/src/theme/scheme.js +32 -0
- package/templates/astro/js/base/vitest.config.js +12 -0
- package/templates/astro/js/sample-data/src/components/ItemDetails.jsx +64 -0
- package/templates/astro/js/sample-data/src/components/ItemList.jsx +100 -0
- package/templates/astro/js/sample-data/src/components/StatusChip.jsx +8 -0
- package/templates/astro/js/sample-data/src/data/items.js +65 -0
- package/templates/astro/js/sample-data/src/pages/items/[id].astro +37 -0
- package/templates/astro/js/sample-data/src/pages/items/index.astro +20 -0
- package/templates/astro/js/with/jotai/src/components/Counter.jsx +26 -0
- package/templates/astro/js/with/jotai/src/state/counter.js +4 -0
- package/templates/astro/js/with/msw/_env +3 -0
- package/templates/astro/js/with/msw/src/mocks/browser.js +5 -0
- package/templates/astro/js/with/msw/src/mocks/handlers.js +24 -0
- package/templates/astro/js/with/msw/src/mocks/server.js +5 -0
- package/templates/astro/js/with/msw/src/mocks/setupTests.js +7 -0
- package/templates/astro/js/with/msw/src/mocks/start.js +14 -0
- package/templates/astro/js/with/react-hook-form/src/components/Form.jsx +313 -0
- package/templates/astro/js/with/react-hook-form/src/pages/form.astro +8 -0
- package/templates/astro/js/with/redux-toolkit/src/components/Counter.jsx +27 -0
- package/templates/astro/js/with/redux-toolkit/src/state/store.js +18 -0
- package/templates/astro/js/with/tanstack-query/src/data/queryClient.js +13 -0
- package/templates/astro/js/with/zustand/src/components/Counter.jsx +24 -0
- package/templates/astro/js/with/zustand/src/state/counter.js +14 -0
- package/templates/astro/ts/base/README.md +93 -0
- package/templates/astro/ts/base/_gitignore +5 -0
- package/templates/astro/ts/base/astro.config.mjs +23 -0
- package/templates/astro/ts/base/src/components/About.tsx +24 -0
- package/templates/astro/ts/base/src/components/Counter.tsx +26 -0
- package/templates/astro/ts/base/src/components/Greeting.tsx +61 -0
- package/templates/astro/ts/base/src/components/Header.tsx +99 -0
- package/templates/astro/ts/base/src/components/Home.test.tsx +39 -0
- package/templates/astro/ts/base/src/components/Home.tsx +33 -0
- package/templates/astro/ts/base/src/components/NotFound.tsx +17 -0
- package/templates/astro/ts/base/src/data/api.ts +92 -0
- package/templates/astro/ts/base/src/layouts/Layout.astro +52 -0
- package/templates/astro/ts/base/src/pages/404.astro +12 -0
- package/templates/astro/ts/base/src/pages/about.astro +12 -0
- package/templates/astro/ts/base/src/pages/index.astro +23 -0
- package/templates/astro/ts/base/src/theme/Island.tsx +90 -0
- package/templates/astro/ts/base/src/theme/scheme.ts +34 -0
- package/templates/astro/ts/base/tsconfig.json +9 -0
- package/templates/astro/ts/base/vitest.config.ts +12 -0
- package/templates/astro/ts/sample-data/src/components/ItemDetails.tsx +65 -0
- package/templates/astro/ts/sample-data/src/components/ItemList.tsx +109 -0
- package/templates/astro/ts/sample-data/src/components/StatusChip.tsx +9 -0
- package/templates/astro/ts/sample-data/src/data/items.ts +71 -0
- package/templates/astro/ts/sample-data/src/pages/items/[id].astro +39 -0
- package/templates/astro/ts/sample-data/src/pages/items/index.astro +20 -0
- package/templates/astro/ts/with/jotai/src/components/Counter.tsx +26 -0
- package/templates/astro/ts/with/jotai/src/state/counter.ts +4 -0
- package/templates/astro/ts/with/msw/_env +3 -0
- package/templates/astro/ts/with/msw/src/mocks/browser.ts +5 -0
- package/templates/astro/ts/with/msw/src/mocks/handlers.ts +24 -0
- package/templates/astro/ts/with/msw/src/mocks/server.ts +5 -0
- package/templates/astro/ts/with/msw/src/mocks/setupTests.ts +7 -0
- package/templates/astro/ts/with/msw/src/mocks/start.ts +14 -0
- package/templates/astro/ts/with/react-hook-form/src/components/Form.tsx +317 -0
- package/templates/astro/ts/with/react-hook-form/src/pages/form.astro +8 -0
- package/templates/astro/ts/with/redux-toolkit/src/components/Counter.tsx +27 -0
- package/templates/astro/ts/with/redux-toolkit/src/state/store.ts +19 -0
- package/templates/astro/ts/with/tanstack-query/src/data/queryClient.ts +13 -0
- package/templates/astro/ts/with/zustand/src/components/Counter.tsx +24 -0
- package/templates/astro/ts/with/zustand/src/state/counter.ts +19 -0
- package/templates/react-router/js/base/README.md +67 -0
- package/templates/react-router/js/base/_gitignore +4 -0
- package/templates/react-router/js/base/index.html +12 -0
- 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/about/Page.jsx +14 -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/about/Page.tsx +14 -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 +67 -0
- package/templates/vike/js/base/README.md +83 -0
- package/templates/vike/js/base/_gitignore +4 -0
- package/templates/vike/js/base/src/data/api.js +76 -0
- package/templates/vike/js/base/src/pages/+Layout.jsx +183 -0
- package/templates/vike/js/base/src/pages/+config.js +12 -0
- package/templates/vike/js/base/src/pages/+onRenderClient.jsx +39 -0
- package/templates/vike/js/base/src/pages/+onRenderHtml.jsx +27 -0
- package/templates/vike/js/base/src/pages/PageShell.jsx +47 -0
- package/templates/vike/js/base/src/pages/_error/+Page.jsx +14 -0
- package/templates/vike/js/base/src/pages/about/+Page.jsx +14 -0
- package/templates/vike/js/base/src/pages/index/+Page.jsx +33 -0
- package/templates/vike/js/base/src/pages/index/Counter.jsx +20 -0
- package/templates/vike/js/base/src/pages/index/Greeting.jsx +48 -0
- package/templates/vike/js/base/src/pages/index/Page.test.jsx +67 -0
- package/templates/vike/js/base/src/pages/usePageContext.jsx +20 -0
- package/templates/vike/js/base/vite.config.js +14 -0
- package/templates/vike/js/base/vitest.config.js +13 -0
- package/templates/vike/js/sample-data/src/data/items.js +58 -0
- package/templates/vike/js/sample-data/src/pages/items/+Page.jsx +49 -0
- package/templates/vike/js/sample-data/src/pages/items/+data.js +26 -0
- package/templates/vike/js/sample-data/src/pages/items/@id/+Page.jsx +40 -0
- package/templates/vike/js/sample-data/src/pages/items/@id/+data.js +33 -0
- package/templates/vike/js/sample-data/src/pages/items/@id/+onBeforePrerenderStart.js +8 -0
- package/templates/vike/js/sample-data/src/pages/items/StatusChip.jsx +8 -0
- package/templates/vike/js/ssg/src/pages/+config.js +11 -0
- package/templates/vike/js/ssg/src/pages/about/+Page.jsx +13 -0
- package/templates/vike/js/ssr/src/pages/+config.js +12 -0
- package/templates/vike/js/ssr/src/pages/+onRenderHtml.jsx +27 -0
- package/templates/vike/js/ssr/src/pages/about/+Page.jsx +14 -0
- package/templates/vike/js/with/jotai/src/pages/index/Counter.jsx +19 -0
- package/templates/vike/js/with/jotai/src/state/counter.js +4 -0
- package/templates/vike/js/with/jotai/src/state/scheme.js +4 -0
- package/templates/vike/js/with/msw/_env +3 -0
- package/templates/vike/js/with/msw/src/mocks/browser.js +5 -0
- package/templates/vike/js/with/msw/src/mocks/handlers.js +24 -0
- package/templates/vike/js/with/msw/src/mocks/server.js +5 -0
- package/templates/vike/js/with/msw/src/mocks/setupTests.js +7 -0
- package/templates/vike/js/with/msw/src/mocks/start.js +10 -0
- package/templates/vike/js/with/react-hook-form/src/pages/form/+Page.jsx +294 -0
- package/templates/vike/js/with/redux-toolkit/src/pages/index/Counter.jsx +20 -0
- package/templates/vike/js/with/redux-toolkit/src/state/store.js +27 -0
- package/templates/vike/js/with/zustand/src/pages/index/Counter.jsx +17 -0
- package/templates/vike/js/with/zustand/src/state/counter.js +14 -0
- package/templates/vike/js/with/zustand/src/state/scheme.js +14 -0
- package/templates/vike/ts/base/README.md +83 -0
- package/templates/vike/ts/base/_gitignore +4 -0
- package/templates/vike/ts/base/src/data/api.ts +84 -0
- package/templates/vike/ts/base/src/pages/+Layout.tsx +184 -0
- package/templates/vike/ts/base/src/pages/+config.ts +14 -0
- package/templates/vike/ts/base/src/pages/+onRenderClient.tsx +41 -0
- package/templates/vike/ts/base/src/pages/+onRenderHtml.tsx +28 -0
- package/templates/vike/ts/base/src/pages/PageShell.tsx +48 -0
- package/templates/vike/ts/base/src/pages/_error/+Page.tsx +14 -0
- package/templates/vike/ts/base/src/pages/about/+Page.tsx +14 -0
- package/templates/vike/ts/base/src/pages/index/+Page.tsx +33 -0
- package/templates/vike/ts/base/src/pages/index/Counter.tsx +20 -0
- package/templates/vike/ts/base/src/pages/index/Greeting.tsx +49 -0
- package/templates/vike/ts/base/src/pages/index/Page.test.tsx +67 -0
- package/templates/vike/ts/base/src/pages/usePageContext.tsx +27 -0
- package/templates/vike/ts/base/src/vike.d.ts +20 -0
- package/templates/vike/ts/base/tsconfig.json +18 -0
- package/templates/vike/ts/base/vite.config.ts +14 -0
- package/templates/vike/ts/base/vitest.config.ts +13 -0
- package/templates/vike/ts/sample-data/src/data/items.ts +64 -0
- package/templates/vike/ts/sample-data/src/pages/items/+Page.tsx +50 -0
- package/templates/vike/ts/sample-data/src/pages/items/+data.ts +28 -0
- package/templates/vike/ts/sample-data/src/pages/items/@id/+Page.tsx +41 -0
- package/templates/vike/ts/sample-data/src/pages/items/@id/+data.ts +37 -0
- package/templates/vike/ts/sample-data/src/pages/items/@id/+onBeforePrerenderStart.ts +8 -0
- package/templates/vike/ts/sample-data/src/pages/items/StatusChip.tsx +9 -0
- package/templates/vike/ts/ssg/src/pages/+config.ts +13 -0
- package/templates/vike/ts/ssg/src/pages/about/+Page.tsx +13 -0
- package/templates/vike/ts/ssr/src/pages/+config.ts +14 -0
- package/templates/vike/ts/ssr/src/pages/+onRenderHtml.tsx +28 -0
- package/templates/vike/ts/ssr/src/pages/about/+Page.tsx +14 -0
- package/templates/vike/ts/with/jotai/src/pages/index/Counter.tsx +19 -0
- package/templates/vike/ts/with/jotai/src/state/counter.ts +4 -0
- package/templates/vike/ts/with/jotai/src/state/scheme.ts +4 -0
- package/templates/vike/ts/with/msw/_env +3 -0
- package/templates/vike/ts/with/msw/src/mocks/browser.ts +5 -0
- package/templates/vike/ts/with/msw/src/mocks/handlers.ts +24 -0
- package/templates/vike/ts/with/msw/src/mocks/server.ts +5 -0
- package/templates/vike/ts/with/msw/src/mocks/setupTests.ts +7 -0
- package/templates/vike/ts/with/msw/src/mocks/start.ts +10 -0
- package/templates/vike/ts/with/react-hook-form/src/pages/form/+Page.tsx +298 -0
- package/templates/vike/ts/with/redux-toolkit/src/pages/index/Counter.tsx +20 -0
- package/templates/vike/ts/with/redux-toolkit/src/state/store.ts +29 -0
- package/templates/vike/ts/with/zustand/src/pages/index/Counter.tsx +17 -0
- package/templates/vike/ts/with/zustand/src/state/counter.ts +19 -0
- package/templates/vike/ts/with/zustand/src/state/scheme.ts +19 -0
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
// @if tanstack-query || (!zustand && !jotai && !redux-toolkit)
|
|
2
|
+
import { useState } from 'react'
|
|
3
|
+
// @endif
|
|
4
|
+
import { Button } from '@soroush.tech/design-system/Button'
|
|
5
|
+
import { Flex } from '@soroush.tech/design-system/Flex'
|
|
6
|
+
import { Typography } from '@soroush.tech/design-system/Typography'
|
|
7
|
+
import { View } from '@soroush.tech/design-system/View'
|
|
8
|
+
// @if tanstack-query
|
|
9
|
+
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
|
|
10
|
+
// @endif
|
|
11
|
+
import { Outlet } from 'react-router'
|
|
12
|
+
// @if zustand
|
|
13
|
+
import { useScheme } from '../state/scheme'
|
|
14
|
+
// @elif jotai
|
|
15
|
+
import { useAtom } from 'jotai'
|
|
16
|
+
import { schemeAtom } from '../state/scheme'
|
|
17
|
+
// @elif redux-toolkit
|
|
18
|
+
import { useDispatch, useSelector } from 'react-redux'
|
|
19
|
+
import { schemeChanged, type RootState } from '../state/store'
|
|
20
|
+
// @endif
|
|
21
|
+
import { RouterLink } from '../components/RouterLink'
|
|
22
|
+
import { Providers } from '../theme/Providers'
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
// @if zustand || jotai
|
|
26
|
+
* The frame every page renders in: the router draws the page in its Outlet. The app's providers
|
|
27
|
+
* live here, in the app itself, so the scheme, held in src/state/scheme.ts, can switch the theme.
|
|
28
|
+
// @elif redux-toolkit
|
|
29
|
+
* The frame every page renders in: the router draws the page in its Outlet. The app's providers
|
|
30
|
+
* live here, in the app itself, so the scheme, held in src/state/store.ts, can switch the theme.
|
|
31
|
+
// @else
|
|
32
|
+
* The frame every page renders in: the router draws the page in its Outlet. The app's providers
|
|
33
|
+
* live here, in the app itself, so the scheme it holds can switch the theme.
|
|
34
|
+
// @endif
|
|
35
|
+
*/
|
|
36
|
+
export function Layout() {
|
|
37
|
+
// @if tanstack-query
|
|
38
|
+
const [queryClient] = useState(() => new QueryClient())
|
|
39
|
+
// @endif
|
|
40
|
+
// @if zustand
|
|
41
|
+
const { scheme, setScheme } = useScheme()
|
|
42
|
+
// @elif jotai
|
|
43
|
+
const [scheme, setScheme] = useAtom(schemeAtom)
|
|
44
|
+
// @elif redux-toolkit
|
|
45
|
+
const scheme = useSelector((state: RootState) => state.scheme)
|
|
46
|
+
const dispatch = useDispatch()
|
|
47
|
+
const setScheme = (next: 'light' | 'dark') => dispatch(schemeChanged(next))
|
|
48
|
+
// @else
|
|
49
|
+
const [scheme, setScheme] = useState<'light' | 'dark'>('__SCHEME__')
|
|
50
|
+
// @endif
|
|
51
|
+
const otherScheme = scheme === 'light' ? 'dark' : 'light'
|
|
52
|
+
// @if tanstack-query
|
|
53
|
+
return (
|
|
54
|
+
<QueryClientProvider client={queryClient}>
|
|
55
|
+
<Providers scheme={scheme}>
|
|
56
|
+
<Flex
|
|
57
|
+
as="header"
|
|
58
|
+
flexDirection="row"
|
|
59
|
+
alignItems="center"
|
|
60
|
+
justifyContent="space-between"
|
|
61
|
+
gap={2}
|
|
62
|
+
px={3}
|
|
63
|
+
py={2}
|
|
64
|
+
bg="paper"
|
|
65
|
+
>
|
|
66
|
+
<Typography variant="h6" as="span">
|
|
67
|
+
__TITLE__
|
|
68
|
+
</Typography>
|
|
69
|
+
<Flex as="nav" flexDirection="row" alignItems="center" gap={2}>
|
|
70
|
+
<RouterLink to="/" underline="hover">
|
|
71
|
+
Home
|
|
72
|
+
</RouterLink>
|
|
73
|
+
<RouterLink to="/about" underline="hover">
|
|
74
|
+
About
|
|
75
|
+
</RouterLink>
|
|
76
|
+
{/* @if sample-data */}
|
|
77
|
+
<RouterLink to="/items" underline="hover">
|
|
78
|
+
Items
|
|
79
|
+
</RouterLink>
|
|
80
|
+
{/* @endif */}
|
|
81
|
+
{/* @if react-hook-form */}
|
|
82
|
+
<RouterLink to="/form" underline="hover">
|
|
83
|
+
Form
|
|
84
|
+
</RouterLink>
|
|
85
|
+
{/* @endif */}
|
|
86
|
+
<Button size="sm" variant="outlined" onClick={() => setScheme(otherScheme)}>
|
|
87
|
+
{otherScheme}
|
|
88
|
+
</Button>
|
|
89
|
+
</Flex>
|
|
90
|
+
</Flex>
|
|
91
|
+
<View as="main" px={3} py={4}>
|
|
92
|
+
<Outlet />
|
|
93
|
+
</View>
|
|
94
|
+
</Providers>
|
|
95
|
+
</QueryClientProvider>
|
|
96
|
+
)
|
|
97
|
+
// @else
|
|
98
|
+
return (
|
|
99
|
+
<Providers scheme={scheme}>
|
|
100
|
+
<Flex
|
|
101
|
+
as="header"
|
|
102
|
+
flexDirection="row"
|
|
103
|
+
alignItems="center"
|
|
104
|
+
justifyContent="space-between"
|
|
105
|
+
gap={2}
|
|
106
|
+
px={3}
|
|
107
|
+
py={2}
|
|
108
|
+
bg="paper"
|
|
109
|
+
>
|
|
110
|
+
<Typography variant="h6" as="span">
|
|
111
|
+
__TITLE__
|
|
112
|
+
</Typography>
|
|
113
|
+
<Flex as="nav" flexDirection="row" alignItems="center" gap={2}>
|
|
114
|
+
<RouterLink to="/" underline="hover">
|
|
115
|
+
Home
|
|
116
|
+
</RouterLink>
|
|
117
|
+
<RouterLink to="/about" underline="hover">
|
|
118
|
+
About
|
|
119
|
+
</RouterLink>
|
|
120
|
+
{/* @if sample-data */}
|
|
121
|
+
<RouterLink to="/items" underline="hover">
|
|
122
|
+
Items
|
|
123
|
+
</RouterLink>
|
|
124
|
+
{/* @endif */}
|
|
125
|
+
{/* @if react-hook-form */}
|
|
126
|
+
<RouterLink to="/form" underline="hover">
|
|
127
|
+
Form
|
|
128
|
+
</RouterLink>
|
|
129
|
+
{/* @endif */}
|
|
130
|
+
<Button size="sm" variant="outlined" onClick={() => setScheme(otherScheme)}>
|
|
131
|
+
{otherScheme}
|
|
132
|
+
</Button>
|
|
133
|
+
</Flex>
|
|
134
|
+
</Flex>
|
|
135
|
+
<View as="main" px={3} py={4}>
|
|
136
|
+
<Outlet />
|
|
137
|
+
</View>
|
|
138
|
+
</Providers>
|
|
139
|
+
)
|
|
140
|
+
// @endif
|
|
141
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { Flex } from '@soroush.tech/design-system/Flex'
|
|
2
|
+
import { Typography } from '@soroush.tech/design-system/Typography'
|
|
3
|
+
|
|
4
|
+
export default function Page() {
|
|
5
|
+
return (
|
|
6
|
+
<Flex gap={2}>
|
|
7
|
+
<Typography variant="h2">About</Typography>
|
|
8
|
+
<Typography color="secondary">
|
|
9
|
+
A second page: the browser renders it, with its styles, and the app moves to it without a
|
|
10
|
+
reload.
|
|
11
|
+
</Typography>
|
|
12
|
+
</Flex>
|
|
13
|
+
)
|
|
14
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { useState } from 'react'
|
|
2
|
+
import { Button } from '@soroush.tech/design-system/Button'
|
|
3
|
+
import { Flex } from '@soroush.tech/design-system/Flex'
|
|
4
|
+
import { Typography } from '@soroush.tech/design-system/Typography'
|
|
5
|
+
|
|
6
|
+
/** A count, held in the component's own state, and the button that adds to it. */
|
|
7
|
+
export function Counter() {
|
|
8
|
+
const [count, setCount] = useState(0)
|
|
9
|
+
const increment = () => setCount(count + 1)
|
|
10
|
+
return (
|
|
11
|
+
<Flex flexDirection="row" alignItems="center" gap={2}>
|
|
12
|
+
<Button size="sm" variant="outlined" onClick={increment}>
|
|
13
|
+
Count
|
|
14
|
+
</Button>
|
|
15
|
+
<Typography>Count: {count}</Typography>
|
|
16
|
+
</Flex>
|
|
17
|
+
)
|
|
18
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
// @if tanstack-query || swr || msw
|
|
2
|
+
// @if tanstack-query
|
|
3
|
+
import { useQuery } from '@tanstack/react-query'
|
|
4
|
+
// @elif swr
|
|
5
|
+
import useSWR from 'swr'
|
|
6
|
+
// @else
|
|
7
|
+
import { useEffect, useState } from 'react'
|
|
8
|
+
// @endif
|
|
9
|
+
import { Typography } from '@soroush.tech/design-system/Typography'
|
|
10
|
+
// @if !tanstack-query && !swr
|
|
11
|
+
import type { Greeting as Reply } from '../../data/api'
|
|
12
|
+
// @endif
|
|
13
|
+
import { fetchGreeting } from '../../data/api'
|
|
14
|
+
|
|
15
|
+
// @if tanstack-query
|
|
16
|
+
/** What the API says, read with a query of TanStack Query. */
|
|
17
|
+
export function Greeting() {
|
|
18
|
+
const { data } = useQuery({ queryKey: ['greeting'], queryFn: fetchGreeting })
|
|
19
|
+
return (
|
|
20
|
+
<Typography color="secondary">
|
|
21
|
+
{data ? data.message : 'Asking the API for a greeting...'}
|
|
22
|
+
</Typography>
|
|
23
|
+
)
|
|
24
|
+
}
|
|
25
|
+
// @elif swr
|
|
26
|
+
/** What the API says, read with SWR. */
|
|
27
|
+
export function Greeting() {
|
|
28
|
+
const { data } = useSWR('greeting', fetchGreeting)
|
|
29
|
+
return (
|
|
30
|
+
<Typography color="secondary">
|
|
31
|
+
{data ? data.message : 'Asking the API for a greeting...'}
|
|
32
|
+
</Typography>
|
|
33
|
+
)
|
|
34
|
+
}
|
|
35
|
+
// @else
|
|
36
|
+
/** What the API says, read with fetch, as the page loads. */
|
|
37
|
+
export function Greeting() {
|
|
38
|
+
const [data, setData] = useState<Reply>()
|
|
39
|
+
useEffect(() => {
|
|
40
|
+
fetchGreeting().then(setData, () => setData({ message: 'The API did not answer.' }))
|
|
41
|
+
}, [])
|
|
42
|
+
return (
|
|
43
|
+
<Typography color="secondary">
|
|
44
|
+
{data ? data.message : 'Asking the API for a greeting...'}
|
|
45
|
+
</Typography>
|
|
46
|
+
)
|
|
47
|
+
}
|
|
48
|
+
// @endif
|
|
49
|
+
// @endif
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { cleanup, fireEvent, render, screen } from '@testing-library/react'
|
|
2
|
+
// @if tanstack-query
|
|
3
|
+
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
|
|
4
|
+
// @endif
|
|
5
|
+
// @if redux-toolkit
|
|
6
|
+
import { Provider as StoreProvider } from 'react-redux'
|
|
7
|
+
// @endif
|
|
8
|
+
import { afterEach, describe, expect, it } from 'vitest'
|
|
9
|
+
// @if redux-toolkit
|
|
10
|
+
import { store } from '../../state/store'
|
|
11
|
+
// @endif
|
|
12
|
+
import { Providers } from '../../theme/Providers'
|
|
13
|
+
import Page from './Page'
|
|
14
|
+
|
|
15
|
+
/** The home page, inside the providers the app gives it. */
|
|
16
|
+
function renderPage() {
|
|
17
|
+
render(
|
|
18
|
+
<Providers>
|
|
19
|
+
{/* @if tanstack-query && redux-toolkit */}
|
|
20
|
+
<QueryClientProvider client={new QueryClient()}>
|
|
21
|
+
<StoreProvider store={store}>
|
|
22
|
+
<Page />
|
|
23
|
+
</StoreProvider>
|
|
24
|
+
</QueryClientProvider>
|
|
25
|
+
{/* @elif tanstack-query */}
|
|
26
|
+
<QueryClientProvider client={new QueryClient()}>
|
|
27
|
+
<Page />
|
|
28
|
+
</QueryClientProvider>
|
|
29
|
+
{/* @elif redux-toolkit */}
|
|
30
|
+
<StoreProvider store={store}>
|
|
31
|
+
<Page />
|
|
32
|
+
</StoreProvider>
|
|
33
|
+
{/* @else */}
|
|
34
|
+
<Page />
|
|
35
|
+
{/* @endif */}
|
|
36
|
+
</Providers>
|
|
37
|
+
)
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
describe('the home page', () => {
|
|
41
|
+
// Vitest has no globals here, so Testing Library cannot clean up on its own.
|
|
42
|
+
afterEach(cleanup)
|
|
43
|
+
|
|
44
|
+
it('shows the app title', () => {
|
|
45
|
+
renderPage()
|
|
46
|
+
expect(screen.getByRole('heading', { name: '__TITLE_STRING__' })).toBeTruthy()
|
|
47
|
+
})
|
|
48
|
+
|
|
49
|
+
it('counts each press of its button', () => {
|
|
50
|
+
renderPage()
|
|
51
|
+
fireEvent.click(screen.getByRole('button', { name: 'Count' }))
|
|
52
|
+
expect(screen.getByText('Count: 1')).toBeTruthy()
|
|
53
|
+
})
|
|
54
|
+
// @if msw
|
|
55
|
+
|
|
56
|
+
it('shows the greeting the mock API answers', async () => {
|
|
57
|
+
renderPage()
|
|
58
|
+
expect(await screen.findByText('Hello from the mock API')).toBeTruthy()
|
|
59
|
+
})
|
|
60
|
+
// @endif
|
|
61
|
+
})
|
|
@@ -0,0 +1,29 @@
|
|
|
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 { Counter } from './Counter'
|
|
5
|
+
// @if tanstack-query || swr || msw
|
|
6
|
+
import { Greeting } from './Greeting'
|
|
7
|
+
// @endif
|
|
8
|
+
|
|
9
|
+
export default function Page() {
|
|
10
|
+
return (
|
|
11
|
+
<Flex gap={2} alignItems="flex-start">
|
|
12
|
+
<Typography variant="h2">__TITLE__</Typography>
|
|
13
|
+
<Typography color="secondary">
|
|
14
|
+
A React Router app on the design system. Edit src/pages/index/Page.tsx, and make the themes
|
|
15
|
+
your own in src/theme/themes.ts.
|
|
16
|
+
</Typography>
|
|
17
|
+
{/* @if tanstack-query || swr || msw */}
|
|
18
|
+
<Greeting />
|
|
19
|
+
{/* @endif */}
|
|
20
|
+
<Counter />
|
|
21
|
+
<Flex flexDirection="row" gap={1}>
|
|
22
|
+
<Button href="https://docs.soroush.tech">Components</Button>
|
|
23
|
+
<Button href="https://reactrouter.com" variant="outlined">
|
|
24
|
+
React Router
|
|
25
|
+
</Button>
|
|
26
|
+
</Flex>
|
|
27
|
+
</Flex>
|
|
28
|
+
)
|
|
29
|
+
}
|