Skip to content

Commit 9ddc03f

Browse files
authored
feat(nav): render Preview badges consistently (#721)
1 parent 1a20db7 commit 9ddc03f

9 files changed

Lines changed: 184 additions & 65 deletions

File tree

gatsby/__tests__/toc.test.ts

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
import { mdxAstToToc } from "../toc";
2+
3+
describe("mdxAstToToc tag query parsing", () => {
4+
it("does not emit a bogus ?undefined query when the image URL has no query string", () => {
5+
const toc = mdxAstToToc(
6+
[
7+
{
8+
type: "list",
9+
children: [
10+
{
11+
type: "listItem",
12+
children: [
13+
{
14+
type: "paragraph",
15+
children: [
16+
{ type: "text", value: "Data Service" },
17+
{
18+
type: "image",
19+
alt: "PREVIEW",
20+
url: "/media/tidb-cloud/blank_transparent_placeholder.png",
21+
},
22+
],
23+
},
24+
],
25+
},
26+
],
27+
},
28+
] as any,
29+
"en/tidbcloud/master/TOC"
30+
);
31+
32+
expect(toc[0].tag).toEqual({
33+
value: "PREVIEW",
34+
query: undefined,
35+
});
36+
});
37+
38+
it("does not create a tag when the TOC image has no alt text", () => {
39+
const toc = mdxAstToToc(
40+
[
41+
{
42+
type: "list",
43+
children: [
44+
{
45+
type: "listItem",
46+
children: [
47+
{
48+
type: "paragraph",
49+
children: [
50+
{ type: "text", value: "Data Service" },
51+
{
52+
type: "image",
53+
alt: null,
54+
url: "/media/tidb-cloud/blank_transparent_placeholder.png",
55+
},
56+
],
57+
},
58+
],
59+
},
60+
],
61+
},
62+
] as any,
63+
"en/tidbcloud/master/TOC"
64+
);
65+
66+
expect(toc[0].tag).toBeUndefined();
67+
});
68+
69+
it("keeps tag query params when the image URL includes them", () => {
70+
const toc = mdxAstToToc(
71+
[
72+
{
73+
type: "list",
74+
children: [
75+
{
76+
type: "listItem",
77+
children: [
78+
{
79+
type: "paragraph",
80+
children: [
81+
{ type: "text", value: "Beta Feature" },
82+
{
83+
type: "image",
84+
alt: "BETA",
85+
url: "/media/tidb-cloud/blank_transparent_placeholder.png?color=%232d9cd2",
86+
},
87+
],
88+
},
89+
],
90+
},
91+
],
92+
},
93+
] as any,
94+
"en/tidbcloud/master/TOC"
95+
);
96+
97+
expect(toc[0].tag).toEqual({
98+
value: "BETA",
99+
query: "?color=%232d9cd2",
100+
});
101+
});
102+
});

gatsby/toc.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {
22
ListItem,
33
List,
44
Link,
5+
Image,
56
Paragraph,
67
Text,
78
Content,
@@ -157,11 +158,14 @@ function getContentFromLink(
157158

158159
const child = content.children[0] as Link | Text;
159160
// use `image` as tag
160-
const image = content.children.find((n) => n.type === "image");
161-
const tag = image && {
162-
value: image.alt!,
163-
query: `?${image.url.split("?")[1]}`,
164-
};
161+
const image = content.children.find((n): n is Image => n.type === "image");
162+
const imageQuery = image?.url.split("?")[1];
163+
const tag = image?.alt
164+
? {
165+
value: image.alt,
166+
query: imageQuery ? `?${imageQuery}` : undefined,
167+
}
168+
: undefined;
165169

166170
if (child.type === "link") {
167171
if (child.children.length === 0) {

locale/en/translation.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@
4343
"tidbOperatorReleases": "TiDB Operator Releases",
4444
"tiupReleases": "TiUP Releases",
4545
"badge": {
46-
"preview": "Preview",
47-
"beta": "Beta"
46+
"preview": "Preview"
4847
},
4948
"appdev": "App Dev",
5049
"asktug": "Forum",

locale/ja/translation.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@
4343
"tidbOperatorReleases": "TiDB Operator リリース",
4444
"tiupReleases": "TiUP リリース",
4545
"badge": {
46-
"preview": "Preview",
47-
"beta": "Beta"
46+
"preview": "Preview"
4847
},
4948
"appdev": "アプリ開発",
5049
"asktug": "Forum",

locale/zh/translation.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@
4141
"tidbOperatorReleases": "TiDB Operator 版本发布记录",
4242
"tiupReleases": "TiUP 版本发布记录",
4343
"badge": {
44-
"preview": "Preview",
45-
"beta": "Beta"
44+
"preview": "Preview"
4645
},
4746
"appdev": "开发指南",
4847
"asktug": "社区",
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import Chip from "@mui/material/Chip";
2+
import { useTheme } from "@mui/material/styles";
3+
4+
const PreviewBadge = (props: { label: string }) => {
5+
const theme = useTheme();
6+
7+
return (
8+
<Chip
9+
label={props.label}
10+
size="small"
11+
variant="outlined"
12+
sx={{
13+
height: "18px",
14+
fontSize: "10px",
15+
fontWeight: 500,
16+
borderRadius: "1000px",
17+
borderColor: theme.palette.carbon[400],
18+
pointerEvents: "none",
19+
textTransform: "uppercase",
20+
letterSpacing: "0.25px",
21+
"& .MuiChip-label": {
22+
paddingLeft: "8px",
23+
paddingRight: "8px",
24+
lineHeight: "16px",
25+
color: theme.palette.carbon[700],
26+
},
27+
}}
28+
/>
29+
);
30+
};
31+
32+
export default PreviewBadge;

src/components/Layout/Header/HeaderNavConfigData.tsx

Lines changed: 2 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -2,54 +2,11 @@ import { NavConfig } from "./HeaderNavConfigType";
22
import { CLOUD_MODE_KEY } from "shared/useCloudPlan";
33
import { CloudPlan, TOCNamespace } from "shared/interface";
44
import OpenInNewIcon from "@mui/icons-material/OpenInNew";
5-
import Chip from "@mui/material/Chip";
6-
import { useTheme } from "@mui/material/styles";
5+
import PreviewBadge from "components/Badge/PreviewBadge";
76

87
import TiDBCloudIcon from "media/icons/cloud-03.svg";
98
import TiDBIcon from "media/icons/layers-three-01.svg";
109

11-
const PreviewBadge = (props: { label: string }) => {
12-
const theme = useTheme();
13-
return (
14-
<Chip
15-
label={props.label}
16-
size="small"
17-
variant="outlined"
18-
sx={{
19-
height: "20px",
20-
fontSize: "12px",
21-
fontWeight: 400,
22-
borderRadius: "10px",
23-
pointerEvents: "none",
24-
"& .MuiChip-label": {
25-
paddingLeft: "8px",
26-
paddingRight: "8px",
27-
lineHeight: "20px",
28-
color: theme.palette.carbon[700],
29-
},
30-
}}
31-
/>
32-
);
33-
};
34-
35-
const BetaTagBadge = (props: { label: string }) => (
36-
<Chip
37-
label={props.label}
38-
variant="outlined"
39-
size="small"
40-
sx={{
41-
flexShrink: 0,
42-
textTransform: "uppercase",
43-
pointerEvents: "none",
44-
fontSize: "10px",
45-
height: "20px",
46-
borderColor: "#c0e1f1",
47-
color: "#2d9cd2",
48-
fontWeight: 500,
49-
}}
50-
/>
51-
);
52-
5310
/**
5411
* Default navigation configuration
5512
*/
@@ -167,7 +124,7 @@ const getDefaultNavConfig = (
167124
leftNavLabel: (
168125
<>
169126
{t("navbar.tidbForAI")}
170-
<BetaTagBadge label={t("navbar.badge.beta")} />
127+
<PreviewBadge label={t("navbar.badge.preview")} />
171128
</>
172129
),
173130
to: "/ai",

src/components/Layout/LeftNav/LeftNavTree.tsx

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ import Stack from "@mui/material/Stack";
77
import Typography from "@mui/material/Typography";
88
import Divider from "@mui/material/Divider";
99
import { useTheme } from "@mui/material/styles";
10+
import { useTranslation } from "gatsby-plugin-react-i18next";
1011

1112
import { RepoNavLink, RepoNav } from "shared/interface";
1213
import LinkComponent from "components/Link";
14+
import PreviewBadge from "components/Badge/PreviewBadge";
1315
import { scrollToElementIfInView } from "shared/utils";
1416
import { alpha, Chip } from "@mui/material";
1517

@@ -190,6 +192,8 @@ export default function ControlledTreeView(props: {
190192
});
191193

192194
const theme = useTheme();
195+
const { t } = useTranslation();
196+
const previewBadgeLabel = t("navbar.badge.preview");
193197
const [disableTransition, setDisableTransition] = React.useState(false);
194198
const previousUrlRef = React.useRef<string | null>(null);
195199

@@ -313,7 +317,7 @@ export default function ControlledTreeView(props: {
313317
) : (
314318
<Box sx={{ flexShrink: 0 }} width={16} height={16} />
315319
)}
316-
{generateItemLabel(item)}
320+
{generateItemLabel(item, previewBadgeLabel)}
317321
</Stack>
318322
);
319323
};
@@ -389,10 +393,22 @@ export default function ControlledTreeView(props: {
389393
);
390394
}
391395

392-
const generateItemLabel = ({ content: contents, tag }: RepoNavLink) => {
393-
const tagQuery = new URLSearchParams(tag?.query);
394-
const tagColor = tagQuery.get("color");
395-
const tagColor02 = tagColor && alpha(tagColor, 0.2);
396+
const generateItemLabel = (
397+
{ content: contents, tag }: RepoNavLink,
398+
previewBadgeLabel: string
399+
) => {
400+
const normalizedTagValue = tag?.value?.trim().toUpperCase();
401+
const isPreviewTag = normalizedTagValue === "PREVIEW";
402+
let tagColor: string | null = null;
403+
let tagColor02: string | null = null;
404+
405+
// PREVIEW intentionally ignores source color overrides to match shared badges.
406+
if (tag && !isPreviewTag) {
407+
const tagQuery = new URLSearchParams(tag.query ?? "");
408+
tagColor = tagQuery.get("color");
409+
tagColor02 = tagColor ? alpha(tagColor, 0.2) : null;
410+
}
411+
396412
return (
397413
<Stack sx={{ width: "100%" }} direction="row" gap="4px">
398414
<Box
@@ -422,7 +438,9 @@ const generateItemLabel = ({ content: contents, tag }: RepoNavLink) => {
422438
);
423439
})}
424440
</Box>
425-
{tag && (
441+
{tag && isPreviewTag ? (
442+
<PreviewBadge label={previewBadgeLabel} />
443+
) : tag ? (
426444
<Chip
427445
label={tag.value}
428446
variant="outlined"
@@ -438,7 +456,7 @@ const generateItemLabel = ({ content: contents, tag }: RepoNavLink) => {
438456
fontWeight: 500,
439457
}}
440458
/>
441-
)}
459+
) : null}
442460
</Stack>
443461
);
444462
};

src/components/Layout/VersionSelect/CloudVersionSelect.tsx

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,20 @@ const CLOUD_VERSIONS = [
4242
variant="outlined"
4343
size="small"
4444
sx={{
45-
fontSize: "12px",
46-
height: "20px",
45+
fontSize: "10px",
46+
fontWeight: 500,
47+
height: "18px",
48+
borderRadius: "1000px",
4749
pointerEvents: "none",
4850
borderColor: "#DCE3E5",
4951
color: "#6F787B",
52+
textTransform: "uppercase",
53+
letterSpacing: "0.25px",
54+
"& .MuiChip-label": {
55+
paddingLeft: "8px",
56+
paddingRight: "8px",
57+
lineHeight: "16px",
58+
},
5059
}}
5160
/>
5261
),

0 commit comments

Comments
 (0)