Skip to content

Commit e1ef5ff

Browse files
authored
Fix component nav links (#910)
1 parent 60fe438 commit e1ef5ff

4 files changed

+50
-15
lines changed

src/layouts/css-component-layout.tsx

+12-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {ComponentPageNav} from '../components/component-page-nav'
1515
export const query = graphql`
1616
query CssComponentPageQuery($parentPath: String!) {
1717
sitePage(path: {eq: $parentPath}) {
18+
id
1819
path
1920
context {
2021
frontmatter {
@@ -36,6 +37,16 @@ export default function CssComponentLayout({data}) {
3637
const description = data.sitePage?.context.frontmatter.description || ''
3738
const stories = [{id: `deprecated-${name}--default`}]
3839

40+
const baseUrl = (() => {
41+
const slugMatch = data.sitePage.id.match(/\/components\/(\w+)\//)
42+
43+
if (slugMatch) {
44+
return `/components/${slugMatch[1]}`
45+
}
46+
47+
return data.sitePage.path;
48+
})()
49+
3950
return (
4051
<BaseLayout title={title} description={description}>
4152
<Box sx={{maxWidth: 1200, width: '100%', p: [4, 5, 6, 7], mx: 'auto'}}>
@@ -47,7 +58,7 @@ export default function CssComponentLayout({data}) {
4758
) : null}
4859
<Box sx={{mb: 4}}>
4960
<ComponentPageNav
50-
basePath={data.sitePage.path}
61+
basePath={baseUrl}
5162
includeReact={data.sitePage.context.frontmatter.reactId}
5263
includeRails={data.sitePage.context.frontmatter.railsIds}
5364
includeFigma={data.sitePage.context.frontmatter.figmaId}

src/layouts/figma-component-layout.tsx

+12-10
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import FigmaPropertyPreview from '../components/figma-property-preview'
1515
export const query = graphql`
1616
query FigmaComponentPageQuery($figmaId: String!, $parentPath: String!) {
1717
sitePage(path: {eq: $parentPath}) {
18+
id
1819
path
1920
context {
2021
frontmatter {
@@ -52,15 +53,6 @@ export const query = graphql`
5253
}
5354
`
5455

55-
const sentenceCase = str => {
56-
return str
57-
?.toLowerCase()
58-
.replace(/([A-Z])/g, ' $1')
59-
.replace(/^./, function (str) {
60-
return str.toUpperCase()
61-
})
62-
}
63-
6456
const lastUpdated = date => {
6557
return `Updated ${new Date(date).toLocaleDateString('en-GB', {year: 'numeric', month: 'short', day: 'numeric'})}`
6658
}
@@ -77,6 +69,16 @@ export default function FigmaComponentLayout({data}) {
7769
],
7870
}
7971

72+
const baseUrl = (() => {
73+
const slugMatch = data.sitePage.id.match(/\/components\/(\w+)\//)
74+
75+
if (slugMatch) {
76+
return `/components/${slugMatch[1]}`
77+
}
78+
79+
return data.sitePage.path;
80+
})()
81+
8082
return (
8183
<BaseLayout title={title} description={description}>
8284
<Box sx={{maxWidth: 1200, width: '100%', p: [4, 5, 6, 7], mx: 'auto'}}>
@@ -88,7 +90,7 @@ export default function FigmaComponentLayout({data}) {
8890
) : null}
8991
<Box sx={{mb: 4}}>
9092
<ComponentPageNav
91-
basePath={data.sitePage.path}
93+
basePath={baseUrl}
9294
includeReact={data.sitePage.context.frontmatter.reactId}
9395
includeRails={data.sitePage.context.frontmatter.railsIds}
9496
includeFigma={data.sitePage.context.frontmatter.figmaId}

src/layouts/rails-component-layout.tsx

+13-2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export const query = graphql`
2323
}
2424
2525
sitePage(path: {eq: $parentPath}) {
26+
id
2627
path
2728
context {
2829
frontmatter {
@@ -351,6 +352,16 @@ export default function RailsComponentLayout({data}) {
351352
}
352353
}
353354

355+
const baseUrl = (() => {
356+
const slugMatch = data.sitePage.id.match(/\/components\/(\w+)\//)
357+
358+
if (slugMatch) {
359+
return `/components/${slugMatch[1]}`
360+
}
361+
362+
return data.sitePage.path;
363+
})()
364+
354365
return (
355366
<RailsProvider>
356367
<BaseLayout title={title} description={description}>
@@ -363,7 +374,7 @@ export default function RailsComponentLayout({data}) {
363374
) : null}
364375
<Box sx={{mb: 4}}>
365376
<ComponentPageNav
366-
basePath={data.sitePage.path}
377+
basePath={baseUrl}
367378
includeReact={reactId}
368379
includeRails={railsIds}
369380
includeFigma={figmaId}
@@ -393,7 +404,7 @@ export default function RailsComponentLayout({data}) {
393404
<StatusLabel status={sentenceCase(status)} />
394405
<AccessibilityLabel a11yReviewed={a11y_reviewed} short={false} />
395406
{statuses.length > 1 && <Box sx={{marginLeft: 'auto', marginTop: '-4px'}}>
396-
<StatusMenu currentStatus={status} statuses={statuses} parentPath={`${data.sitePage.path}/rails`} />
407+
<StatusMenu currentStatus={status} statuses={statuses} parentPath={`${baseUrl}/rails`} />
397408
</Box>}
398409
</Box>
399410

src/layouts/react-component-layout.tsx

+13-2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export const query = graphql`
2121
version
2222
}
2323
sitePage(path: {eq: $parentPath}) {
24+
id
2425
path
2526
context {
2627
frontmatter {
@@ -111,6 +112,16 @@ export default function ReactComponentLayout({data}) {
111112
// this component has a dedicated page for its deprecated version
112113
if (data.deprecatedMdx?.id !== undefined) statuses.push("deprecated")
113114

115+
const baseUrl = (() => {
116+
const slugMatch = data.sitePage.id.match(/\/components\/(\w+)\//)
117+
118+
if (slugMatch) {
119+
return `/components/${slugMatch[1]}`
120+
}
121+
122+
return data.sitePage.path;
123+
})()
124+
114125
return (
115126
<BaseLayout title={title} description={description}>
116127
<Box sx={{maxWidth: 1200, width: '100%', p: [4, 5, 6, 7], mx: 'auto'}}>
@@ -122,7 +133,7 @@ export default function ReactComponentLayout({data}) {
122133
) : null}
123134
<Box sx={{mb: 4}}>
124135
<ComponentPageNav
125-
basePath={data.sitePage.path}
136+
basePath={baseUrl}
126137
includeReact={data.sitePage.context.frontmatter.reactId}
127138
includeRails={data.sitePage.context.frontmatter.railsIds}
128139
includeFigma={data.sitePage.context.frontmatter.figmaId}
@@ -200,7 +211,7 @@ export default function ReactComponentLayout({data}) {
200211
},
201212
}}
202213
>
203-
<StatusMenu currentStatus={status} statuses={statuses} parentPath={`${data.sitePage.path}/react`} />
214+
<StatusMenu currentStatus={status} statuses={statuses} parentPath={`${baseUrl}/react`} />
204215
</Box>
205216
}
206217
</Box>

0 commit comments

Comments
 (0)