@soroush.design/cli 0.2.0-alpha.3 → 0.2.0-alpha.4
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 +1 -1
- package/dist/index.d.mts +4 -4
- package/dist/index.mjs +1 -1
- package/dist/{io-Bybgct-f.mjs → io-C_8_JerZ.mjs} +368 -1039
- package/package.json +13 -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/templates.md +66 -0
- package/templates/vike/js/base/README.md +87 -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/spa/src/pages/+config.js +12 -0
- package/templates/vike/js/spa/src/pages/+onRenderClient.jsx +25 -0
- package/templates/vike/js/spa/src/pages/+onRenderHtml.jsx +16 -0
- package/templates/vike/js/spa/src/pages/about/+Page.jsx +14 -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 +87 -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/spa/src/pages/+config.ts +14 -0
- package/templates/vike/ts/spa/src/pages/+onRenderClient.tsx +27 -0
- package/templates/vike/ts/spa/src/pages/+onRenderHtml.tsx +17 -0
- package/templates/vike/ts/spa/src/pages/about/+Page.tsx +14 -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,99 @@
|
|
|
1
|
+
// @if !ssg
|
|
2
|
+
import { Button } from '@soroush.tech/design-system/Button'
|
|
3
|
+
// @endif
|
|
4
|
+
import { Flex } from '@soroush.tech/design-system/Flex'
|
|
5
|
+
import { Link } from '@soroush.tech/design-system/Link'
|
|
6
|
+
import { Typography } from '@soroush.tech/design-system/Typography'
|
|
7
|
+
import { Providers } from '../theme/Providers'
|
|
8
|
+
// @if !ssg
|
|
9
|
+
import { setScheme, useScheme } from '../theme/scheme'
|
|
10
|
+
// @endif
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
// @if ssg
|
|
14
|
+
* The header of every page. Its Providers draw the base styles, once a
|
|
15
|
+
* page.
|
|
16
|
+
// @else
|
|
17
|
+
* The header of every page, an island of its own. Its Providers draw the base styles, once a
|
|
18
|
+
* page, and its button switches the scheme of every island.
|
|
19
|
+
// @endif
|
|
20
|
+
*/
|
|
21
|
+
// @if ssg
|
|
22
|
+
export default function Header() {
|
|
23
|
+
return (
|
|
24
|
+
<Providers>
|
|
25
|
+
<Flex
|
|
26
|
+
as="header"
|
|
27
|
+
flexDirection="row"
|
|
28
|
+
alignItems="center"
|
|
29
|
+
justifyContent="space-between"
|
|
30
|
+
gap={2}
|
|
31
|
+
px={3}
|
|
32
|
+
py={2}
|
|
33
|
+
bg="paper"
|
|
34
|
+
>
|
|
35
|
+
<Typography variant="h6" as="span">
|
|
36
|
+
__TITLE__
|
|
37
|
+
</Typography>
|
|
38
|
+
<Flex as="nav" flexDirection="row" alignItems="center" gap={2}>
|
|
39
|
+
<Link href="/" underline="hover">
|
|
40
|
+
Home
|
|
41
|
+
</Link>
|
|
42
|
+
<Link href="/about" underline="hover">
|
|
43
|
+
About
|
|
44
|
+
</Link>
|
|
45
|
+
{/* @if sample-data */}
|
|
46
|
+
<Link href="/items" underline="hover">
|
|
47
|
+
Items
|
|
48
|
+
</Link>
|
|
49
|
+
{/* @endif */}
|
|
50
|
+
</Flex>
|
|
51
|
+
</Flex>
|
|
52
|
+
</Providers>
|
|
53
|
+
)
|
|
54
|
+
}
|
|
55
|
+
// @else
|
|
56
|
+
export default function Header() {
|
|
57
|
+
const scheme = useScheme()
|
|
58
|
+
const otherScheme = scheme === 'light' ? 'dark' : 'light'
|
|
59
|
+
return (
|
|
60
|
+
<Providers scheme={scheme}>
|
|
61
|
+
<Flex
|
|
62
|
+
as="header"
|
|
63
|
+
flexDirection="row"
|
|
64
|
+
alignItems="center"
|
|
65
|
+
justifyContent="space-between"
|
|
66
|
+
gap={2}
|
|
67
|
+
px={3}
|
|
68
|
+
py={2}
|
|
69
|
+
bg="paper"
|
|
70
|
+
>
|
|
71
|
+
<Typography variant="h6" as="span">
|
|
72
|
+
__TITLE__
|
|
73
|
+
</Typography>
|
|
74
|
+
<Flex as="nav" flexDirection="row" alignItems="center" gap={2}>
|
|
75
|
+
<Link href="/" underline="hover">
|
|
76
|
+
Home
|
|
77
|
+
</Link>
|
|
78
|
+
<Link href="/about" underline="hover">
|
|
79
|
+
About
|
|
80
|
+
</Link>
|
|
81
|
+
{/* @if sample-data */}
|
|
82
|
+
<Link href="/items" underline="hover">
|
|
83
|
+
Items
|
|
84
|
+
</Link>
|
|
85
|
+
{/* @endif */}
|
|
86
|
+
{/* @if react-hook-form */}
|
|
87
|
+
<Link href="/form" underline="hover">
|
|
88
|
+
Form
|
|
89
|
+
</Link>
|
|
90
|
+
{/* @endif */}
|
|
91
|
+
<Button size="sm" variant="outlined" onClick={() => setScheme(otherScheme)}>
|
|
92
|
+
{otherScheme}
|
|
93
|
+
</Button>
|
|
94
|
+
</Flex>
|
|
95
|
+
</Flex>
|
|
96
|
+
</Providers>
|
|
97
|
+
)
|
|
98
|
+
}
|
|
99
|
+
// @endif
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
// @if ssg
|
|
2
|
+
import { cleanup, render, screen } from '@testing-library/react'
|
|
3
|
+
// @else
|
|
4
|
+
import { cleanup, fireEvent, render, screen } from '@testing-library/react'
|
|
5
|
+
// @endif
|
|
6
|
+
import { afterEach, describe, expect, it } from 'vitest'
|
|
7
|
+
// @if !ssg
|
|
8
|
+
import Counter from './Counter'
|
|
9
|
+
// @endif
|
|
10
|
+
// @if msw
|
|
11
|
+
import Greeting from './Greeting'
|
|
12
|
+
// @endif
|
|
13
|
+
import Home from './Home'
|
|
14
|
+
|
|
15
|
+
// Each island renders inside its own providers, so a test renders it as it is.
|
|
16
|
+
describe('the home page', () => {
|
|
17
|
+
// Vitest has no globals here, so Testing Library cannot clean up on its own.
|
|
18
|
+
afterEach(cleanup)
|
|
19
|
+
|
|
20
|
+
it('shows the app title', () => {
|
|
21
|
+
render(<Home />)
|
|
22
|
+
expect(screen.getByRole('heading', { name: '__TITLE_STRING__' })).toBeTruthy()
|
|
23
|
+
})
|
|
24
|
+
// @if !ssg
|
|
25
|
+
|
|
26
|
+
it('counts each press of its button', () => {
|
|
27
|
+
render(<Counter />)
|
|
28
|
+
fireEvent.click(screen.getByRole('button', { name: 'Count' }))
|
|
29
|
+
expect(screen.getByText('Count: 1')).toBeTruthy()
|
|
30
|
+
})
|
|
31
|
+
// @endif
|
|
32
|
+
// @if msw
|
|
33
|
+
|
|
34
|
+
it('shows the greeting the mock API answers', async () => {
|
|
35
|
+
render(<Greeting />)
|
|
36
|
+
expect(await screen.findByText('Hello from the mock API')).toBeTruthy()
|
|
37
|
+
})
|
|
38
|
+
// @endif
|
|
39
|
+
})
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { ReactNode } 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
|
+
import { withIsland } from '../theme/Island'
|
|
6
|
+
|
|
7
|
+
// @if ssg
|
|
8
|
+
/** The home page. */
|
|
9
|
+
// @elif tanstack-query || swr || msw
|
|
10
|
+
/** The home page. Its children are the islands of its greeting and its counter. */
|
|
11
|
+
// @else
|
|
12
|
+
/** The home page. Its child is the island of its counter. */
|
|
13
|
+
// @endif
|
|
14
|
+
function Home({ children }: { readonly children?: ReactNode }) {
|
|
15
|
+
return (
|
|
16
|
+
<Flex gap={2} alignItems="flex-start" px={3} py={4}>
|
|
17
|
+
<Typography variant="h2">__TITLE__</Typography>
|
|
18
|
+
<Typography color="secondary">
|
|
19
|
+
An Astro app on the design system. Edit src/components/Home.tsx, and make the themes your
|
|
20
|
+
own in src/theme/themes.ts.
|
|
21
|
+
</Typography>
|
|
22
|
+
{children}
|
|
23
|
+
<Flex flexDirection="row" gap={1}>
|
|
24
|
+
<Button href="https://docs.soroush.tech">Components</Button>
|
|
25
|
+
<Button href="https://astro.build" variant="outlined">
|
|
26
|
+
Astro
|
|
27
|
+
</Button>
|
|
28
|
+
</Flex>
|
|
29
|
+
</Flex>
|
|
30
|
+
)
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export default withIsland(Home)
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { Flex } from '@soroush.tech/design-system/Flex'
|
|
2
|
+
import { Link } from '@soroush.tech/design-system/Link'
|
|
3
|
+
import { Typography } from '@soroush.tech/design-system/Typography'
|
|
4
|
+
import { withIsland } from '../theme/Island'
|
|
5
|
+
|
|
6
|
+
/** Shown for a page that does not exist. */
|
|
7
|
+
function NotFound() {
|
|
8
|
+
return (
|
|
9
|
+
<Flex gap={2} alignItems="flex-start" px={3} py={4}>
|
|
10
|
+
<Typography variant="h2">Page not found</Typography>
|
|
11
|
+
<Typography color="secondary">This page does not exist.</Typography>
|
|
12
|
+
<Link href="/">Back home</Link>
|
|
13
|
+
</Flex>
|
|
14
|
+
)
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export default withIsland(NotFound)
|
|
@@ -0,0 +1,92 @@
|
|
|
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 msw
|
|
10
|
+
import { mocks } from '../mocks/start'
|
|
11
|
+
|
|
12
|
+
// @endif
|
|
13
|
+
// @if ts
|
|
14
|
+
/** What the API says to the app. */
|
|
15
|
+
export interface Greeting {
|
|
16
|
+
message: string
|
|
17
|
+
}
|
|
18
|
+
// @if react-hook-form
|
|
19
|
+
|
|
20
|
+
/** What the form sends. */
|
|
21
|
+
export interface Message {
|
|
22
|
+
name: string
|
|
23
|
+
email: string
|
|
24
|
+
}
|
|
25
|
+
// @endif
|
|
26
|
+
|
|
27
|
+
// @endif
|
|
28
|
+
// @if msw
|
|
29
|
+
/** A greeting, from the API: while VITE_MSW is true, the mock API of src/mocks answers. */
|
|
30
|
+
export async function fetchGreeting(): Promise<Greeting> {
|
|
31
|
+
await mocks
|
|
32
|
+
const response = await fetch('/api/greeting')
|
|
33
|
+
if (!response.ok) throw new Error(`GET /api/greeting answered ${response.status}`)
|
|
34
|
+
return response.json()
|
|
35
|
+
}
|
|
36
|
+
// @else
|
|
37
|
+
/** A greeting, as an API would answer: make it fetch from yours. */
|
|
38
|
+
export function fetchGreeting(): Promise<Greeting> {
|
|
39
|
+
return Promise.resolve({ message: 'Hello from src/data/api: make it fetch from your API.' })
|
|
40
|
+
}
|
|
41
|
+
// @endif
|
|
42
|
+
// @if react-hook-form && msw
|
|
43
|
+
|
|
44
|
+
/** Sends the form's values to the API, which answers what it made of them. */
|
|
45
|
+
export async function sendMessage(message: Message): Promise<Greeting> {
|
|
46
|
+
await mocks
|
|
47
|
+
const response = await fetch('/api/messages', {
|
|
48
|
+
method: 'POST',
|
|
49
|
+
headers: { 'Content-Type': 'application/json' },
|
|
50
|
+
body: JSON.stringify(message),
|
|
51
|
+
})
|
|
52
|
+
if (!response.ok) throw new Error(`POST /api/messages answered ${response.status}`)
|
|
53
|
+
return response.json()
|
|
54
|
+
}
|
|
55
|
+
// @elif react-hook-form
|
|
56
|
+
|
|
57
|
+
/** Sends the form's values, as an API would take them: make it post to yours. */
|
|
58
|
+
export function sendMessage(message: Message): Promise<Greeting> {
|
|
59
|
+
return Promise.resolve({ message: `Thanks, ${message.name}: your message was sent.` })
|
|
60
|
+
}
|
|
61
|
+
// @endif
|
|
62
|
+
// @if sample-data && msw && (tanstack-query || swr)
|
|
63
|
+
|
|
64
|
+
/** The items, from the API: while VITE_MSW is true, the mock API of src/mocks answers. */
|
|
65
|
+
export async function fetchItems(): Promise<Pick<Item, 'id' | 'title' | 'status'>[]> {
|
|
66
|
+
await mocks
|
|
67
|
+
const response = await fetch('/api/items')
|
|
68
|
+
if (!response.ok) throw new Error(`GET /api/items answered ${response.status}`)
|
|
69
|
+
return response.json()
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** One item, from the API. */
|
|
73
|
+
export async function fetchItem(id: string): Promise<Item> {
|
|
74
|
+
await mocks
|
|
75
|
+
const response = await fetch(`/api/items/${id}`)
|
|
76
|
+
if (!response.ok) throw new Error(`GET /api/items/${id} answered ${response.status}`)
|
|
77
|
+
return response.json()
|
|
78
|
+
}
|
|
79
|
+
// @elif sample-data && (tanstack-query || swr)
|
|
80
|
+
|
|
81
|
+
/** The items, as an API would answer: make it fetch from yours. */
|
|
82
|
+
export function fetchItems() {
|
|
83
|
+
return Promise.resolve(items.map(({ id, title, status }) => ({ id, title, status })))
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** One item, as an API would answer: make it fetch from yours. */
|
|
87
|
+
export function fetchItem(id: string) {
|
|
88
|
+
const item = items.find((candidate) => candidate.id === id)
|
|
89
|
+
return item ? Promise.resolve(item) : Promise.reject(new Error(`There is no item ${id}.`))
|
|
90
|
+
}
|
|
91
|
+
// @endif
|
|
92
|
+
// @endif
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
---
|
|
2
|
+
// @if !ssg
|
|
3
|
+
import { ClientRouter } from 'astro:transitions'
|
|
4
|
+
// @endif
|
|
5
|
+
import Header from '../components/Header'
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
<html lang="en">
|
|
9
|
+
<head>
|
|
10
|
+
<meta charset="UTF-8" />
|
|
11
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
12
|
+
<title>__TITLE__</title>
|
|
13
|
+
{/* @if !ssg */}
|
|
14
|
+
<ClientRouter />
|
|
15
|
+
{/* @endif */}
|
|
16
|
+
</head>
|
|
17
|
+
<body>
|
|
18
|
+
{/* @if ssg */}
|
|
19
|
+
<Header />
|
|
20
|
+
{/* @else */}
|
|
21
|
+
<Header client:load transition:persist />
|
|
22
|
+
{/* @endif */}
|
|
23
|
+
<main>
|
|
24
|
+
<slot />
|
|
25
|
+
</main>
|
|
26
|
+
{/* @if !ssg */}
|
|
27
|
+
<script>
|
|
28
|
+
// Astro's client router replaces the head on each move between pages, and with it the
|
|
29
|
+
// styles the design system keeps there. Each of them gets an id the router keeps. The next
|
|
30
|
+
// page's own styles, which its HTML holds next to each island, move into its head, where
|
|
31
|
+
// the islands expect them as they hydrate - but not its base styles: the header, which
|
|
32
|
+
// the router keeps, draws those in the scheme picked.
|
|
33
|
+
document.addEventListener('astro:before-swap', ({ newDocument }) => {
|
|
34
|
+
const kept = new Set()
|
|
35
|
+
document.head.querySelectorAll('style[data-emotion]').forEach((style, index) => {
|
|
36
|
+
const id = `soroush-styles-${index}`
|
|
37
|
+
style.setAttribute('data-astro-transition-persist', id)
|
|
38
|
+
const placeholder = newDocument.createElement('style')
|
|
39
|
+
placeholder.setAttribute('data-astro-transition-persist', id)
|
|
40
|
+
newDocument.head.append(placeholder)
|
|
41
|
+
kept.add(style.getAttribute('data-emotion'))
|
|
42
|
+
})
|
|
43
|
+
newDocument.body.querySelectorAll('style[data-emotion]').forEach((style) => {
|
|
44
|
+
const name = style.getAttribute('data-emotion') ?? ''
|
|
45
|
+
if (kept.has(name) || name.includes('-global')) style.remove()
|
|
46
|
+
else newDocument.head.append(style)
|
|
47
|
+
})
|
|
48
|
+
})
|
|
49
|
+
</script>
|
|
50
|
+
{/* @endif */}
|
|
51
|
+
</body>
|
|
52
|
+
</html>
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
---
|
|
2
|
+
// @if !ssg
|
|
3
|
+
import Counter from '../components/Counter'
|
|
4
|
+
// @endif
|
|
5
|
+
// @if tanstack-query || swr || msw
|
|
6
|
+
import Greeting from '../components/Greeting'
|
|
7
|
+
// @endif
|
|
8
|
+
import Home from '../components/Home'
|
|
9
|
+
import Layout from '../layouts/Layout.astro'
|
|
10
|
+
---
|
|
11
|
+
|
|
12
|
+
<Layout>
|
|
13
|
+
{/* @if ssg */}
|
|
14
|
+
<Home />
|
|
15
|
+
{/* @else */}
|
|
16
|
+
<Home client:load>
|
|
17
|
+
{/* @if tanstack-query || swr || msw */}
|
|
18
|
+
<Greeting client:only="react" />
|
|
19
|
+
{/* @endif */}
|
|
20
|
+
<Counter client:only="react" />
|
|
21
|
+
</Home>
|
|
22
|
+
{/* @endif */}
|
|
23
|
+
</Layout>
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
// @if tanstack-query
|
|
2
|
+
import { useState, type ComponentType, type ReactNode } from 'react'
|
|
3
|
+
import { QueryClientProvider } from '@tanstack/react-query'
|
|
4
|
+
// @else
|
|
5
|
+
import type { ComponentType, ReactNode } from 'react'
|
|
6
|
+
// @endif
|
|
7
|
+
// @if redux-toolkit
|
|
8
|
+
import { Provider as StoreProvider } from 'react-redux'
|
|
9
|
+
// @endif
|
|
10
|
+
import { ThemeProvider } from '@soroush.tech/design-system/theme'
|
|
11
|
+
// @if tanstack-query
|
|
12
|
+
import { getQueryClient } from '../data/queryClient'
|
|
13
|
+
// @endif
|
|
14
|
+
// @if redux-toolkit
|
|
15
|
+
import { store } from '../state/store'
|
|
16
|
+
// @endif
|
|
17
|
+
// @if !ssg
|
|
18
|
+
import { useScheme } from './scheme'
|
|
19
|
+
// @endif
|
|
20
|
+
import themes from './themes'
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
// @if tanstack-query && redux-toolkit
|
|
24
|
+
* What every island renders inside: its theme, the query client and the Redux store. Astro
|
|
25
|
+
* renders each island as a React root of its own, so each one renders inside Island, and in the
|
|
26
|
+
* browser they share one client and one store; the header's Providers draw the base styles.
|
|
27
|
+
// @elif tanstack-query
|
|
28
|
+
* What every island renders inside: its theme and the query client. Astro renders each island as
|
|
29
|
+
* a React root of its own, so each one renders inside Island, and in the browser they share one
|
|
30
|
+
* client; the header's Providers draw the base styles.
|
|
31
|
+
// @elif redux-toolkit
|
|
32
|
+
* What every island renders inside: its theme and the Redux store. Astro renders each island as a
|
|
33
|
+
* React root of its own, so each one renders inside Island, and they share one store; the
|
|
34
|
+
* header's Providers draw the base styles.
|
|
35
|
+
// @else
|
|
36
|
+
* What every island renders inside: its theme. Astro renders each island as a React root of its
|
|
37
|
+
* own, so each one renders inside Island; the header's Providers draw the base styles, once a page.
|
|
38
|
+
// @endif
|
|
39
|
+
*/
|
|
40
|
+
// @if ssg
|
|
41
|
+
function Island({ children }: { readonly children: ReactNode }) {
|
|
42
|
+
return <ThemeProvider theme={themes.__SCHEME__}>{children}</ThemeProvider>
|
|
43
|
+
}
|
|
44
|
+
// @elif tanstack-query && redux-toolkit
|
|
45
|
+
function Island({ children }: { readonly children: ReactNode }) {
|
|
46
|
+
const [queryClient] = useState(getQueryClient)
|
|
47
|
+
return (
|
|
48
|
+
<QueryClientProvider client={queryClient}>
|
|
49
|
+
<StoreProvider store={store}>
|
|
50
|
+
<ThemeProvider theme={themes[useScheme()]}>{children}</ThemeProvider>
|
|
51
|
+
</StoreProvider>
|
|
52
|
+
</QueryClientProvider>
|
|
53
|
+
)
|
|
54
|
+
}
|
|
55
|
+
// @elif tanstack-query
|
|
56
|
+
function Island({ children }: { readonly children: ReactNode }) {
|
|
57
|
+
const [queryClient] = useState(getQueryClient)
|
|
58
|
+
return (
|
|
59
|
+
<QueryClientProvider client={queryClient}>
|
|
60
|
+
<ThemeProvider theme={themes[useScheme()]}>{children}</ThemeProvider>
|
|
61
|
+
</QueryClientProvider>
|
|
62
|
+
)
|
|
63
|
+
}
|
|
64
|
+
// @elif redux-toolkit
|
|
65
|
+
function Island({ children }: { readonly children: ReactNode }) {
|
|
66
|
+
return (
|
|
67
|
+
<StoreProvider store={store}>
|
|
68
|
+
<ThemeProvider theme={themes[useScheme()]}>{children}</ThemeProvider>
|
|
69
|
+
</StoreProvider>
|
|
70
|
+
)
|
|
71
|
+
}
|
|
72
|
+
// @else
|
|
73
|
+
function Island({ children }: { readonly children: ReactNode }) {
|
|
74
|
+
return <ThemeProvider theme={themes[useScheme()]}>{children}</ThemeProvider>
|
|
75
|
+
}
|
|
76
|
+
// @endif
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* `Component` as an island: rendered inside Island, so its hooks find what Island provides. Every
|
|
80
|
+
* island of the app is exported this way.
|
|
81
|
+
*/
|
|
82
|
+
export function withIsland<Props extends object>(Component: ComponentType<Props>) {
|
|
83
|
+
return function InIsland(props: Props) {
|
|
84
|
+
return (
|
|
85
|
+
<Island>
|
|
86
|
+
<Component {...props} />
|
|
87
|
+
</Island>
|
|
88
|
+
)
|
|
89
|
+
}
|
|
90
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
// @if !ssg
|
|
2
|
+
import { useSyncExternalStore } from 'react'
|
|
3
|
+
|
|
4
|
+
type Scheme = 'light' | 'dark'
|
|
5
|
+
|
|
6
|
+
let current: Scheme = '__SCHEME__'
|
|
7
|
+
const listeners = new Set<() => void>()
|
|
8
|
+
|
|
9
|
+
/** Switches the color scheme of every island, on this page and the pages the app moves to. */
|
|
10
|
+
export function setScheme(scheme: Scheme) {
|
|
11
|
+
current = scheme
|
|
12
|
+
for (const listener of listeners) listener()
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function subscribe(listener: () => void) {
|
|
16
|
+
listeners.add(listener)
|
|
17
|
+
return () => {
|
|
18
|
+
listeners.delete(listener)
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* The color scheme every island draws in. Each island is a React root of its own, so they share
|
|
24
|
+
* it here, not through context. An island first draws the default, as the server did, so it
|
|
25
|
+
* hydrates on the HTML it was given, then the scheme picked.
|
|
26
|
+
*/
|
|
27
|
+
export function useScheme(): Scheme {
|
|
28
|
+
return useSyncExternalStore(
|
|
29
|
+
subscribe,
|
|
30
|
+
() => current,
|
|
31
|
+
() => '__SCHEME__'
|
|
32
|
+
)
|
|
33
|
+
}
|
|
34
|
+
// @endif
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/// <reference types="vitest/config" />
|
|
2
|
+
import { getViteConfig } from 'astro/config'
|
|
3
|
+
|
|
4
|
+
// The tests render components in jsdom, through the app's own Vite settings, as Astro builds them.
|
|
5
|
+
export default getViteConfig({
|
|
6
|
+
// @if msw
|
|
7
|
+
// The mock API answers the tests' requests, from Node, as it answers the app's in the browser.
|
|
8
|
+
test: { environment: 'jsdom', setupFiles: ['./src/mocks/setupTests.ts'] },
|
|
9
|
+
// @else
|
|
10
|
+
test: { environment: 'jsdom' },
|
|
11
|
+
// @endif
|
|
12
|
+
})
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { Flex } from '@soroush.tech/design-system/Flex'
|
|
2
|
+
import { Link } from '@soroush.tech/design-system/Link'
|
|
3
|
+
import { Typography } from '@soroush.tech/design-system/Typography'
|
|
4
|
+
// @if tanstack-query
|
|
5
|
+
import { useQuery } from '@tanstack/react-query'
|
|
6
|
+
import { fetchItem } from '../data/api'
|
|
7
|
+
// @elif swr
|
|
8
|
+
import useSWR from 'swr'
|
|
9
|
+
import { fetchItem } from '../data/api'
|
|
10
|
+
// @endif
|
|
11
|
+
import type { Item } from '../data/items'
|
|
12
|
+
import { withIsland } from '../theme/Island'
|
|
13
|
+
import { StatusChip } from './StatusChip'
|
|
14
|
+
|
|
15
|
+
// @if ssr
|
|
16
|
+
/** One item, as src/pages/items/[id].astro reads it on each request. */
|
|
17
|
+
// @else
|
|
18
|
+
/** One item, as src/pages/items/[id].astro reads it at build time. */
|
|
19
|
+
// @endif
|
|
20
|
+
// @if tanstack-query
|
|
21
|
+
function ItemDetails({ item: initialItem }: { readonly item: Item }) {
|
|
22
|
+
const { data: item } = useQuery({
|
|
23
|
+
queryKey: ['items', initialItem.id],
|
|
24
|
+
queryFn: () => fetchItem(initialItem.id),
|
|
25
|
+
initialData: initialItem,
|
|
26
|
+
})
|
|
27
|
+
return (
|
|
28
|
+
<Flex gap={2} alignItems="flex-start" px={3} py={4}>
|
|
29
|
+
<Typography variant="h2">{item.title}</Typography>
|
|
30
|
+
<StatusChip status={item.status} />
|
|
31
|
+
<Typography color="secondary">{item.summary}</Typography>
|
|
32
|
+
<Link href="/items">All items</Link>
|
|
33
|
+
</Flex>
|
|
34
|
+
)
|
|
35
|
+
}
|
|
36
|
+
// @elif swr
|
|
37
|
+
function ItemDetails({ item: initialItem }: { readonly item: Item }) {
|
|
38
|
+
const { data: item = initialItem } = useSWR(
|
|
39
|
+
['items', initialItem.id],
|
|
40
|
+
() => fetchItem(initialItem.id),
|
|
41
|
+
{ fallbackData: initialItem }
|
|
42
|
+
)
|
|
43
|
+
return (
|
|
44
|
+
<Flex gap={2} alignItems="flex-start" px={3} py={4}>
|
|
45
|
+
<Typography variant="h2">{item.title}</Typography>
|
|
46
|
+
<StatusChip status={item.status} />
|
|
47
|
+
<Typography color="secondary">{item.summary}</Typography>
|
|
48
|
+
<Link href="/items">All items</Link>
|
|
49
|
+
</Flex>
|
|
50
|
+
)
|
|
51
|
+
}
|
|
52
|
+
// @else
|
|
53
|
+
function ItemDetails({ item }: { readonly item: Item }) {
|
|
54
|
+
return (
|
|
55
|
+
<Flex gap={2} alignItems="flex-start" px={3} py={4}>
|
|
56
|
+
<Typography variant="h2">{item.title}</Typography>
|
|
57
|
+
<StatusChip status={item.status} />
|
|
58
|
+
<Typography color="secondary">{item.summary}</Typography>
|
|
59
|
+
<Link href="/items">All items</Link>
|
|
60
|
+
</Flex>
|
|
61
|
+
)
|
|
62
|
+
}
|
|
63
|
+
// @endif
|
|
64
|
+
|
|
65
|
+
export default withIsland(ItemDetails)
|