@soroush.design/cli 0.2.0-alpha.2 → 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/README.md +9 -112
- package/dist/bin.mjs +1 -1
- package/dist/index.d.mts +14 -3
- package/dist/index.mjs +1 -1
- package/dist/{io-uDtjQuG6.mjs → io-C_8_JerZ.mjs} +579 -643
- package/package.json +26 -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,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,89 @@
|
|
|
1
|
+
// @if tanstack-query
|
|
2
|
+
import { useState } from 'react'
|
|
3
|
+
import { QueryClientProvider } from '@tanstack/react-query'
|
|
4
|
+
// @else
|
|
5
|
+
// @endif
|
|
6
|
+
// @if redux-toolkit
|
|
7
|
+
import { Provider as StoreProvider } from 'react-redux'
|
|
8
|
+
// @endif
|
|
9
|
+
import { ThemeProvider } from '@soroush.tech/design-system/theme'
|
|
10
|
+
// @if tanstack-query
|
|
11
|
+
import { getQueryClient } from '../data/queryClient'
|
|
12
|
+
// @endif
|
|
13
|
+
// @if redux-toolkit
|
|
14
|
+
import { store } from '../state/store'
|
|
15
|
+
// @endif
|
|
16
|
+
// @if !ssg
|
|
17
|
+
import { useScheme } from './scheme'
|
|
18
|
+
// @endif
|
|
19
|
+
import themes from './themes'
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
// @if tanstack-query && redux-toolkit
|
|
23
|
+
* What every island renders inside: its theme, the query client and the Redux store. Astro
|
|
24
|
+
* renders each island as a React root of its own, so each one renders inside Island, and in the
|
|
25
|
+
* browser they share one client and one store; the header's Providers draw the base styles.
|
|
26
|
+
// @elif tanstack-query
|
|
27
|
+
* What every island renders inside: its theme and the query client. Astro renders each island as
|
|
28
|
+
* a React root of its own, so each one renders inside Island, and in the browser they share one
|
|
29
|
+
* client; the header's Providers draw the base styles.
|
|
30
|
+
// @elif redux-toolkit
|
|
31
|
+
* What every island renders inside: its theme and the Redux store. Astro renders each island as a
|
|
32
|
+
* React root of its own, so each one renders inside Island, and they share one store; the
|
|
33
|
+
* header's Providers draw the base styles.
|
|
34
|
+
// @else
|
|
35
|
+
* What every island renders inside: its theme. Astro renders each island as a React root of its
|
|
36
|
+
* own, so each one renders inside Island; the header's Providers draw the base styles, once a page.
|
|
37
|
+
// @endif
|
|
38
|
+
*/
|
|
39
|
+
// @if ssg
|
|
40
|
+
function Island({ children }) {
|
|
41
|
+
return <ThemeProvider theme={themes.__SCHEME__}>{children}</ThemeProvider>
|
|
42
|
+
}
|
|
43
|
+
// @elif tanstack-query && redux-toolkit
|
|
44
|
+
function Island({ children }) {
|
|
45
|
+
const [queryClient] = useState(getQueryClient)
|
|
46
|
+
return (
|
|
47
|
+
<QueryClientProvider client={queryClient}>
|
|
48
|
+
<StoreProvider store={store}>
|
|
49
|
+
<ThemeProvider theme={themes[useScheme()]}>{children}</ThemeProvider>
|
|
50
|
+
</StoreProvider>
|
|
51
|
+
</QueryClientProvider>
|
|
52
|
+
)
|
|
53
|
+
}
|
|
54
|
+
// @elif tanstack-query
|
|
55
|
+
function Island({ children }) {
|
|
56
|
+
const [queryClient] = useState(getQueryClient)
|
|
57
|
+
return (
|
|
58
|
+
<QueryClientProvider client={queryClient}>
|
|
59
|
+
<ThemeProvider theme={themes[useScheme()]}>{children}</ThemeProvider>
|
|
60
|
+
</QueryClientProvider>
|
|
61
|
+
)
|
|
62
|
+
}
|
|
63
|
+
// @elif redux-toolkit
|
|
64
|
+
function Island({ children }) {
|
|
65
|
+
return (
|
|
66
|
+
<StoreProvider store={store}>
|
|
67
|
+
<ThemeProvider theme={themes[useScheme()]}>{children}</ThemeProvider>
|
|
68
|
+
</StoreProvider>
|
|
69
|
+
)
|
|
70
|
+
}
|
|
71
|
+
// @else
|
|
72
|
+
function Island({ children }) {
|
|
73
|
+
return <ThemeProvider theme={themes[useScheme()]}>{children}</ThemeProvider>
|
|
74
|
+
}
|
|
75
|
+
// @endif
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* `Component` as an island: rendered inside Island, so its hooks find what Island provides. Every
|
|
79
|
+
* island of the app is exported this way.
|
|
80
|
+
*/
|
|
81
|
+
export function withIsland(Component) {
|
|
82
|
+
return function InIsland(props) {
|
|
83
|
+
return (
|
|
84
|
+
<Island>
|
|
85
|
+
<Component {...props} />
|
|
86
|
+
</Island>
|
|
87
|
+
)
|
|
88
|
+
}
|
|
89
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
// @if !ssg
|
|
2
|
+
import { useSyncExternalStore } from 'react'
|
|
3
|
+
|
|
4
|
+
let current = '__SCHEME__'
|
|
5
|
+
const listeners = new Set()
|
|
6
|
+
|
|
7
|
+
/** Switches the color scheme of every island, on this page and the pages the app moves to. */
|
|
8
|
+
export function setScheme(scheme) {
|
|
9
|
+
current = scheme
|
|
10
|
+
for (const listener of listeners) listener()
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function subscribe(listener) {
|
|
14
|
+
listeners.add(listener)
|
|
15
|
+
return () => {
|
|
16
|
+
listeners.delete(listener)
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* The color scheme every island draws in. Each island is a React root of its own, so they share
|
|
22
|
+
* it here, not through context. An island first draws the default, as the server did, so it
|
|
23
|
+
* hydrates on the HTML it was given, then the scheme picked.
|
|
24
|
+
*/
|
|
25
|
+
export function useScheme() {
|
|
26
|
+
return useSyncExternalStore(
|
|
27
|
+
subscribe,
|
|
28
|
+
() => current,
|
|
29
|
+
() => '__SCHEME__'
|
|
30
|
+
)
|
|
31
|
+
}
|
|
32
|
+
// @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.js'] },
|
|
9
|
+
// @else
|
|
10
|
+
test: { environment: 'jsdom' },
|
|
11
|
+
// @endif
|
|
12
|
+
})
|
|
@@ -0,0 +1,64 @@
|
|
|
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 { withIsland } from '../theme/Island'
|
|
12
|
+
import { StatusChip } from './StatusChip'
|
|
13
|
+
|
|
14
|
+
// @if ssr
|
|
15
|
+
/** One item, as src/pages/items/[id].astro reads it on each request. */
|
|
16
|
+
// @else
|
|
17
|
+
/** One item, as src/pages/items/[id].astro reads it at build time. */
|
|
18
|
+
// @endif
|
|
19
|
+
// @if tanstack-query
|
|
20
|
+
function ItemDetails({ item: initialItem }) {
|
|
21
|
+
const { data: item } = useQuery({
|
|
22
|
+
queryKey: ['items', initialItem.id],
|
|
23
|
+
queryFn: () => fetchItem(initialItem.id),
|
|
24
|
+
initialData: initialItem,
|
|
25
|
+
})
|
|
26
|
+
return (
|
|
27
|
+
<Flex gap={2} alignItems="flex-start" px={3} py={4}>
|
|
28
|
+
<Typography variant="h2">{item.title}</Typography>
|
|
29
|
+
<StatusChip status={item.status} />
|
|
30
|
+
<Typography color="secondary">{item.summary}</Typography>
|
|
31
|
+
<Link href="/items">All items</Link>
|
|
32
|
+
</Flex>
|
|
33
|
+
)
|
|
34
|
+
}
|
|
35
|
+
// @elif swr
|
|
36
|
+
function ItemDetails({ item: initialItem }) {
|
|
37
|
+
const { data: item = initialItem } = useSWR(
|
|
38
|
+
['items', initialItem.id],
|
|
39
|
+
() => fetchItem(initialItem.id),
|
|
40
|
+
{ fallbackData: initialItem }
|
|
41
|
+
)
|
|
42
|
+
return (
|
|
43
|
+
<Flex gap={2} alignItems="flex-start" px={3} py={4}>
|
|
44
|
+
<Typography variant="h2">{item.title}</Typography>
|
|
45
|
+
<StatusChip status={item.status} />
|
|
46
|
+
<Typography color="secondary">{item.summary}</Typography>
|
|
47
|
+
<Link href="/items">All items</Link>
|
|
48
|
+
</Flex>
|
|
49
|
+
)
|
|
50
|
+
}
|
|
51
|
+
// @else
|
|
52
|
+
function ItemDetails({ item }) {
|
|
53
|
+
return (
|
|
54
|
+
<Flex gap={2} alignItems="flex-start" px={3} py={4}>
|
|
55
|
+
<Typography variant="h2">{item.title}</Typography>
|
|
56
|
+
<StatusChip status={item.status} />
|
|
57
|
+
<Typography color="secondary">{item.summary}</Typography>
|
|
58
|
+
<Link href="/items">All items</Link>
|
|
59
|
+
</Flex>
|
|
60
|
+
)
|
|
61
|
+
}
|
|
62
|
+
// @endif
|
|
63
|
+
|
|
64
|
+
export default withIsland(ItemDetails)
|
|
@@ -0,0 +1,100 @@
|
|
|
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 { fetchItems } from '../data/api'
|
|
7
|
+
// @elif swr
|
|
8
|
+
import useSWR from 'swr'
|
|
9
|
+
import { fetchItems } from '../data/api'
|
|
10
|
+
// @endif
|
|
11
|
+
import { withIsland } from '../theme/Island'
|
|
12
|
+
import { StatusChip } from './StatusChip'
|
|
13
|
+
|
|
14
|
+
// @if ssr
|
|
15
|
+
/** The list of items, as src/pages/items/index.astro reads it on each request. */
|
|
16
|
+
// @else
|
|
17
|
+
/** The list of items, as src/pages/items/index.astro reads it at build time. */
|
|
18
|
+
// @endif
|
|
19
|
+
// @if tanstack-query
|
|
20
|
+
function ItemList({ items: initialItems }) {
|
|
21
|
+
// It starts on the items read with the page, then queries them again in the browser.
|
|
22
|
+
const { data: items } = useQuery({
|
|
23
|
+
queryKey: ['items'],
|
|
24
|
+
queryFn: fetchItems,
|
|
25
|
+
initialData: initialItems,
|
|
26
|
+
})
|
|
27
|
+
return (
|
|
28
|
+
<Flex gap={2} px={3} py={4}>
|
|
29
|
+
<Typography variant="h2">Items</Typography>
|
|
30
|
+
<Typography color="secondary">
|
|
31
|
+
{/* @if ssr */}
|
|
32
|
+
Sample data from src/data/items.js, which this page reads on each request.
|
|
33
|
+
{/* @else */}
|
|
34
|
+
Sample data from src/data/items.js, which this page reads at build time.
|
|
35
|
+
{/* @endif */}
|
|
36
|
+
</Typography>
|
|
37
|
+
<Flex gap={1}>
|
|
38
|
+
{items.map((item) => (
|
|
39
|
+
<Flex key={item.id} flexDirection="row" alignItems="center" gap={2}>
|
|
40
|
+
<Link href={`/items/${item.id}`}>{item.title}</Link>
|
|
41
|
+
<StatusChip status={item.status} />
|
|
42
|
+
</Flex>
|
|
43
|
+
))}
|
|
44
|
+
</Flex>
|
|
45
|
+
</Flex>
|
|
46
|
+
)
|
|
47
|
+
}
|
|
48
|
+
// @elif swr
|
|
49
|
+
function ItemList({ items: initialItems }) {
|
|
50
|
+
// It starts on the items read with the page, then fetches them again in the browser.
|
|
51
|
+
const { data: items = initialItems } = useSWR('items', fetchItems, {
|
|
52
|
+
fallbackData: initialItems,
|
|
53
|
+
})
|
|
54
|
+
return (
|
|
55
|
+
<Flex gap={2} px={3} py={4}>
|
|
56
|
+
<Typography variant="h2">Items</Typography>
|
|
57
|
+
<Typography color="secondary">
|
|
58
|
+
{/* @if ssr */}
|
|
59
|
+
Sample data from src/data/items.js, which this page reads on each request.
|
|
60
|
+
{/* @else */}
|
|
61
|
+
Sample data from src/data/items.js, which this page reads at build time.
|
|
62
|
+
{/* @endif */}
|
|
63
|
+
</Typography>
|
|
64
|
+
<Flex gap={1}>
|
|
65
|
+
{items.map((item) => (
|
|
66
|
+
<Flex key={item.id} flexDirection="row" alignItems="center" gap={2}>
|
|
67
|
+
<Link href={`/items/${item.id}`}>{item.title}</Link>
|
|
68
|
+
<StatusChip status={item.status} />
|
|
69
|
+
</Flex>
|
|
70
|
+
))}
|
|
71
|
+
</Flex>
|
|
72
|
+
</Flex>
|
|
73
|
+
)
|
|
74
|
+
}
|
|
75
|
+
// @else
|
|
76
|
+
function ItemList({ items }) {
|
|
77
|
+
return (
|
|
78
|
+
<Flex gap={2} px={3} py={4}>
|
|
79
|
+
<Typography variant="h2">Items</Typography>
|
|
80
|
+
<Typography color="secondary">
|
|
81
|
+
{/* @if ssr */}
|
|
82
|
+
Sample data from src/data/items.js, which this page reads on each request.
|
|
83
|
+
{/* @else */}
|
|
84
|
+
Sample data from src/data/items.js, which this page reads at build time.
|
|
85
|
+
{/* @endif */}
|
|
86
|
+
</Typography>
|
|
87
|
+
<Flex gap={1}>
|
|
88
|
+
{items.map((item) => (
|
|
89
|
+
<Flex key={item.id} flexDirection="row" alignItems="center" gap={2}>
|
|
90
|
+
<Link href={`/items/${item.id}`}>{item.title}</Link>
|
|
91
|
+
<StatusChip status={item.status} />
|
|
92
|
+
</Flex>
|
|
93
|
+
))}
|
|
94
|
+
</Flex>
|
|
95
|
+
</Flex>
|
|
96
|
+
)
|
|
97
|
+
}
|
|
98
|
+
// @endif
|
|
99
|
+
|
|
100
|
+
export default withIsland(ItemList)
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { Chip } from '@soroush.tech/design-system/Chip'
|
|
2
|
+
|
|
3
|
+
const COLORS = { todo: 'default', doing: 'primary', done: 'success' }
|
|
4
|
+
|
|
5
|
+
/** An item's status, in its palette color. */
|
|
6
|
+
export function StatusChip({ status }) {
|
|
7
|
+
return <Chip label={status} size="sm" variant="outlined" color={COLORS[status]} />
|
|
8
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
// @if ts
|
|
2
|
+
/** One sample item. */
|
|
3
|
+
|
|
4
|
+
// @endif
|
|
5
|
+
// @if ssr
|
|
6
|
+
/**
|
|
7
|
+
* Sample data, so the items pages have something to show: the pages in src/pages/items read it
|
|
8
|
+
* on each request. Make them fetch your own data instead, and remove this file.
|
|
9
|
+
*/
|
|
10
|
+
// @else
|
|
11
|
+
/**
|
|
12
|
+
* Sample data, so the items pages have something to show: the pages in src/pages/items read it
|
|
13
|
+
* at build time. Make them fetch your own data instead, and remove this file.
|
|
14
|
+
*/
|
|
15
|
+
// @endif
|
|
16
|
+
export const items = [
|
|
17
|
+
{
|
|
18
|
+
id: 'theme',
|
|
19
|
+
title: 'Make the theme your own',
|
|
20
|
+
summary: 'Change the colors and fonts of both themes in src/theme/themes.js.',
|
|
21
|
+
status: 'doing',
|
|
22
|
+
},
|
|
23
|
+
{
|
|
24
|
+
id: 'fonts',
|
|
25
|
+
title: 'Load the fonts',
|
|
26
|
+
summary: 'The theme names Space Grotesk and JetBrains Mono, and does not load them.',
|
|
27
|
+
status: 'todo',
|
|
28
|
+
},
|
|
29
|
+
{
|
|
30
|
+
id: 'pages',
|
|
31
|
+
title: 'Add a page',
|
|
32
|
+
summary: 'An .astro file in src/pages is a new route: it renders your components.',
|
|
33
|
+
status: 'todo',
|
|
34
|
+
},
|
|
35
|
+
{
|
|
36
|
+
id: 'data',
|
|
37
|
+
title: 'Read your own data',
|
|
38
|
+
// @if ssr
|
|
39
|
+
summary: 'The items pages read this file on each request. Make them fetch from your API.',
|
|
40
|
+
// @else
|
|
41
|
+
summary: 'The items pages read this file at build time. Make them fetch from your API.',
|
|
42
|
+
// @endif
|
|
43
|
+
status: 'todo',
|
|
44
|
+
},
|
|
45
|
+
{
|
|
46
|
+
id: 'build',
|
|
47
|
+
title: 'Build it',
|
|
48
|
+
// @if ssr
|
|
49
|
+
summary: 'The server renders every page, these items among them, as HTML with its styles.',
|
|
50
|
+
// @else
|
|
51
|
+
summary: 'The build writes every page, these items among them, as HTML with its styles.',
|
|
52
|
+
// @endif
|
|
53
|
+
status: 'done',
|
|
54
|
+
},
|
|
55
|
+
{
|
|
56
|
+
id: 'deploy',
|
|
57
|
+
title: 'Deploy it',
|
|
58
|
+
// @if ssr
|
|
59
|
+
summary: 'npm start runs the server the build wrote, on any Node host.',
|
|
60
|
+
// @else
|
|
61
|
+
summary: 'The build is static files in dist, which any static host serves.',
|
|
62
|
+
// @endif
|
|
63
|
+
status: 'todo',
|
|
64
|
+
},
|
|
65
|
+
]
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
---
|
|
2
|
+
import ItemDetails from '../../components/ItemDetails'
|
|
3
|
+
import Layout from '../../layouts/Layout.astro'
|
|
4
|
+
// @if ssr
|
|
5
|
+
import { items } from '../../data/items'
|
|
6
|
+
|
|
7
|
+
// The item of the id in the URL, read on each request, or the 404 page for an id with no item.
|
|
8
|
+
const item = items.find(({ id }) => id === Astro.params.id)
|
|
9
|
+
if (!item) return Astro.rewrite('/404')
|
|
10
|
+
// @elif ts
|
|
11
|
+
import { items } from '../../data/items'
|
|
12
|
+
|
|
13
|
+
/** The pages this route writes at build time: one per item. */
|
|
14
|
+
export const getStaticPaths = (() =>
|
|
15
|
+
items.map((item) => ({ params: { id: item.id }, props: { item } })));
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
const { item } = Astro.props
|
|
19
|
+
// @else
|
|
20
|
+
import { items } from '../../data/items'
|
|
21
|
+
|
|
22
|
+
/** The pages this route writes at build time: one per item. */
|
|
23
|
+
export function getStaticPaths() {
|
|
24
|
+
return items.map((item) => ({ params: { id: item.id }, props: { item } }))
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const { item } = Astro.props
|
|
28
|
+
// @endif
|
|
29
|
+
---
|
|
30
|
+
|
|
31
|
+
<Layout>
|
|
32
|
+
{/* @if ssg */}
|
|
33
|
+
<ItemDetails item={item} />
|
|
34
|
+
{/* @else */}
|
|
35
|
+
<ItemDetails item={item} client:load />
|
|
36
|
+
{/* @endif */}
|
|
37
|
+
</Layout>
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
---
|
|
2
|
+
import ItemList from '../../components/ItemList'
|
|
3
|
+
import Layout from '../../layouts/Layout.astro'
|
|
4
|
+
import { items } from '../../data/items'
|
|
5
|
+
|
|
6
|
+
// @if ssr
|
|
7
|
+
// What the list shows of each item, read on each request.
|
|
8
|
+
// @else
|
|
9
|
+
// What the list shows of each item, read at build time.
|
|
10
|
+
// @endif
|
|
11
|
+
const summaries = items.map(({ id, title, status }) => ({ id, title, status }))
|
|
12
|
+
---
|
|
13
|
+
|
|
14
|
+
<Layout>
|
|
15
|
+
{/* @if ssg */}
|
|
16
|
+
<ItemList items={summaries} />
|
|
17
|
+
{/* @else */}
|
|
18
|
+
<ItemList items={summaries} client:load />
|
|
19
|
+
{/* @endif */}
|
|
20
|
+
</Layout>
|
|
@@ -0,0 +1,26 @@
|
|
|
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 { withIsland } from '../theme/Island'
|
|
5
|
+
import { useAtom } from 'jotai'
|
|
6
|
+
import { countAtom } from '../state/counter'
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* A count, held in a Jotai atom, src/state/counter, and the button that adds to it.
|
|
10
|
+
* An island of its own, which renders in the browser only: the count it shows is the store's,
|
|
11
|
+
* not the HTML's.
|
|
12
|
+
*/
|
|
13
|
+
function Counter() {
|
|
14
|
+
const [count, setCount] = useAtom(countAtom)
|
|
15
|
+
const increment = () => setCount(count + 1)
|
|
16
|
+
return (
|
|
17
|
+
<Flex flexDirection="row" alignItems="center" gap={2}>
|
|
18
|
+
<Button size="sm" variant="outlined" onClick={increment}>
|
|
19
|
+
Count
|
|
20
|
+
</Button>
|
|
21
|
+
<Typography>Count: {count}</Typography>
|
|
22
|
+
</Flex>
|
|
23
|
+
)
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export default withIsland(Counter)
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { http, HttpResponse } from 'msw'
|
|
2
|
+
// @if sample-data
|
|
3
|
+
import { items } from '../data/items'
|
|
4
|
+
// @endif
|
|
5
|
+
|
|
6
|
+
/** The mock API: what it answers, by method and path. Add a handler for each call the app makes. */
|
|
7
|
+
export const handlers = [
|
|
8
|
+
http.get('/api/greeting', () => HttpResponse.json({ message: 'Hello from the mock API' })),
|
|
9
|
+
// @if react-hook-form
|
|
10
|
+
http.post('/api/messages', async ({ request }) => {
|
|
11
|
+
const { name } = await request.json()
|
|
12
|
+
return HttpResponse.json({ message: `Thanks, ${name}: the mock API took your message.` })
|
|
13
|
+
}),
|
|
14
|
+
// @endif
|
|
15
|
+
// @if sample-data
|
|
16
|
+
http.get('/api/items', () =>
|
|
17
|
+
HttpResponse.json(items.map(({ id, title, status }) => ({ id, title, status })))
|
|
18
|
+
),
|
|
19
|
+
http.get('/api/items/:id', ({ params }) => {
|
|
20
|
+
const item = items.find(({ id }) => id === params.id)
|
|
21
|
+
return item ? HttpResponse.json(item) : new HttpResponse(null, { status: 404 })
|
|
22
|
+
}),
|
|
23
|
+
// @endif
|
|
24
|
+
]
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { afterAll, afterEach, beforeAll } from 'vitest'
|
|
2
|
+
import { server } from './server'
|
|
3
|
+
|
|
4
|
+
// The mock API answers every request a test makes; one it has no handler for fails the test.
|
|
5
|
+
beforeAll(() => server.listen({ onUnhandledFrame: 'error' }))
|
|
6
|
+
afterEach(() => server.resetHandlers())
|
|
7
|
+
afterAll(() => server.close())
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The mock API in the browser, started once, while PUBLIC_MSW, in .env, is true: src/data/api waits
|
|
3
|
+
* for it before each request. On the server, and in the tests, where msw's Node server answers,
|
|
4
|
+
* nothing starts. Set PUBLIC_MSW to false once the app's own API answers.
|
|
5
|
+
*/
|
|
6
|
+
export const mocks = startMocks()
|
|
7
|
+
|
|
8
|
+
async function startMocks() {
|
|
9
|
+
const isBrowser = typeof window !== 'undefined' && import.meta.env.MODE !== 'test'
|
|
10
|
+
if (!isBrowser || import.meta.env.PUBLIC_MSW !== 'true') return
|
|
11
|
+
const { worker } = await import('./browser')
|
|
12
|
+
// A request the mock API has no handler for - a page the router fetches - goes to the network.
|
|
13
|
+
await worker.start({ onUnhandledFrame: 'bypass' })
|
|
14
|
+
}
|