Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d7455c1

Browse files
committed
Add basic page to starter template
1 parent 3062814 commit d7455c1

File tree

6 files changed

+180
-3
lines changed

6 files changed

+180
-3
lines changed

site/src/AppRouter.tsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { AuthAndFrame } from "./components/AuthAndFrame/AuthAndFrame"
2222
import { RequireAuth } from "./components/RequireAuth/RequireAuth"
2323
import { SettingsLayout } from "./components/SettingsLayout/SettingsLayout"
2424
import { DeploySettingsLayout } from "components/DeploySettingsLayout/DeploySettingsLayout"
25+
import StarterTemplatePage from "pages/StarterTemplatePage/StarterTemplatePage"
2526

2627
// Lazy load pages
2728
// - Pages that are secondary, not in the main navigation or not usually accessed
@@ -153,6 +154,15 @@ export const AppRouter: FC = () => {
153154
</AuthAndFrame>
154155
}
155156
/>
157+
158+
<Route
159+
path=":exampleId"
160+
element={
161+
<AuthAndFrame>
162+
<StarterTemplatePage />
163+
</AuthAndFrame>
164+
}
165+
></Route>
156166
</Route>
157167

158168
<Route path="templates">

site/src/components/Markdown/Markdown.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ const useStyles = makeStyles((theme) => ({
146146
},
147147

148148
codeWithoutLanguage: {
149-
padding: theme.spacing(0.5, 1),
149+
padding: theme.spacing(0.125, 0.5),
150150
background: theme.palette.divider,
151151
borderRadius: 4,
152152
color: theme.palette.text.primary,
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { useMachine } from "@xstate/react"
2+
import { useOrganizationId } from "hooks/useOrganizationId"
3+
import { FC } from "react"
4+
import { Helmet } from "react-helmet-async"
5+
import { useParams } from "react-router-dom"
6+
import { pageTitle } from "util/page"
7+
import { starterTemplateMachine } from "xServices/starterTemplates/starterTemplateXService"
8+
import { StarterTemplatePageView } from "./StarterTemplatePageView"
9+
10+
const StarterTemplatePage: FC = () => {
11+
const { exampleId } = useParams() as { exampleId: string }
12+
const organizationId = useOrganizationId()
13+
const [state] = useMachine(starterTemplateMachine, {
14+
context: {
15+
organizationId,
16+
exampleId,
17+
},
18+
})
19+
20+
return (
21+
<>
22+
<Helmet>
23+
<title>{pageTitle(exampleId)}</title>
24+
</Helmet>
25+
26+
<StarterTemplatePageView context={state.context} />
27+
</>
28+
)
29+
}
30+
31+
export default StarterTemplatePage
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import { makeStyles } from "@material-ui/core/styles"
2+
import { Loader } from "components/Loader/Loader"
3+
import { Margins } from "components/Margins/Margins"
4+
import { MemoizedMarkdown } from "components/Markdown/Markdown"
5+
import {
6+
PageHeader,
7+
PageHeaderSubtitle,
8+
PageHeaderTitle,
9+
} from "components/PageHeader/PageHeader"
10+
import { FC } from "react"
11+
import { StarterTemplateContext } from "xServices/starterTemplates/starterTemplateXService"
12+
13+
export interface StarterTemplatePageViewProps {
14+
context: StarterTemplateContext
15+
}
16+
17+
export const StarterTemplatePageView: FC<StarterTemplatePageViewProps> = ({
18+
context,
19+
}) => {
20+
const styles = useStyles()
21+
const { starterTemplate } = context
22+
23+
if (!starterTemplate) {
24+
return <Loader />
25+
}
26+
27+
return (
28+
<Margins>
29+
<PageHeader>
30+
<PageHeaderTitle>{starterTemplate.name}</PageHeaderTitle>
31+
<PageHeaderSubtitle>{starterTemplate.description}</PageHeaderSubtitle>
32+
</PageHeader>
33+
34+
<div className={styles.markdownSection} id="readme">
35+
<div className={styles.markdownWrapper}>
36+
<MemoizedMarkdown>{starterTemplate.markdown}</MemoizedMarkdown>
37+
</div>
38+
</div>
39+
</Margins>
40+
)
41+
}
42+
43+
export const useStyles = makeStyles((theme) => {
44+
return {
45+
markdownSection: {
46+
background: theme.palette.background.paper,
47+
border: `1px solid ${theme.palette.divider}`,
48+
borderRadius: theme.shape.borderRadius,
49+
},
50+
51+
markdownWrapper: {
52+
padding: theme.spacing(5, 5, 8),
53+
maxWidth: 800,
54+
margin: "auto",
55+
},
56+
}
57+
})

site/src/pages/StarterTemplatesPage/StarterTemplatesPageView.tsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
} from "components/PageHeader/PageHeader"
1111
import { FC } from "react"
1212
import { useTranslation } from "react-i18next"
13+
import { Link } from "react-router-dom"
1314
import { StarterTemplatesContext } from "xServices/starterTemplates/starterTemplatesXService"
1415

1516
export interface StarterTemplatesPageViewProps {
@@ -40,12 +41,12 @@ export const StarterTemplatesPageView: FC<StarterTemplatesPageViewProps> = ({
4041
<div className={styles.templates}>
4142
{context.starterTemplates &&
4243
context.starterTemplates.map((example) => (
43-
<div className={styles.template} key={example.id}>
44+
<Link to={example.id} className={styles.template} key={example.id}>
4445
<span className={styles.templateName}>{example.name}</span>
4546
<span className={styles.templateDescription}>
4647
{example.description}
4748
</span>
48-
</div>
49+
</Link>
4950
))}
5051
</div>
5152
</Margins>
@@ -58,6 +59,7 @@ const useStyles = makeStyles((theme) => ({
5859
gridTemplateColumns: "repeat(2, minmax(0, 1fr))",
5960
gap: theme.spacing(2),
6061
},
62+
6163
template: {
6264
padding: theme.spacing(2),
6365
border: `1px solid ${theme.palette.divider}`,
@@ -66,6 +68,12 @@ const useStyles = makeStyles((theme) => ({
6668
display: "flex",
6769
flexDirection: "column",
6870
gap: theme.spacing(0.5),
71+
textDecoration: "none",
72+
color: "inherit",
73+
74+
"&:hover": {
75+
backgroundColor: theme.palette.background.paperLight,
76+
},
6977
},
7078

7179
templateName: {
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import { getTemplateExamples } from "api/api"
2+
import { TemplateExample } from "api/typesGenerated"
3+
import { assign, createMachine } from "xstate"
4+
5+
export interface StarterTemplateContext {
6+
organizationId: string
7+
exampleId: string
8+
starterTemplate?: TemplateExample
9+
error?: unknown
10+
}
11+
12+
export const starterTemplateMachine = createMachine(
13+
{
14+
id: "starterTemplate",
15+
predictableActionArguments: true,
16+
schema: {
17+
context: {} as StarterTemplateContext,
18+
services: {} as {
19+
loadStarterTemplate: {
20+
data: TemplateExample
21+
}
22+
},
23+
},
24+
tsTypes: {} as import("./starterTemplateXService.typegen").Typegen0,
25+
initial: "loading",
26+
states: {
27+
loading: {
28+
invoke: {
29+
src: "loadStarterTemplate",
30+
onDone: {
31+
actions: ["assignStarterTemplate"],
32+
target: "idle.ok",
33+
},
34+
onError: {
35+
actions: ["assignError"],
36+
target: "idle.error",
37+
},
38+
},
39+
},
40+
idle: {
41+
initial: "ok",
42+
states: {
43+
ok: { type: "final" },
44+
error: { type: "final" },
45+
},
46+
},
47+
},
48+
},
49+
{
50+
services: {
51+
loadStarterTemplate: async ({ organizationId, exampleId }) => {
52+
const examples = await getTemplateExamples(organizationId)
53+
const starterTemplate = examples.find(
54+
(example) => example.id === exampleId,
55+
)
56+
if (!starterTemplate) {
57+
throw new Error(`Example ${exampleId} not found.`)
58+
}
59+
return starterTemplate
60+
},
61+
},
62+
actions: {
63+
assignError: assign({
64+
error: (_, { data }) => data,
65+
}),
66+
assignStarterTemplate: assign({
67+
starterTemplate: (_, { data }) => data,
68+
}),
69+
},
70+
},
71+
)

0 commit comments

Comments
 (0)