Skip to content

Commit cf82c62

Browse files
committed
refactor: enhance TOC handling in getTidbCloudFilesFromTocs function
- Introduced a new TocMap type to streamline the structure of TOC data, including the 'premium' category. - Updated the getTidbCloudFilesFromTocs function to utilize the new TocMap type for improved clarity and maintainability. - Modified determineInDefaultPlan function to accommodate the premium category in the plan determination logic.
1 parent b09c92e commit cf82c62

File tree

1 file changed

+24
-23
lines changed

1 file changed

+24
-23
lines changed

gatsby/cloud-plan.ts

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,21 @@ import { generateConfig } from "./path";
33
import { extractFilesFromToc } from "./toc-filter";
44
import { CloudPlan } from "shared/useCloudPlan";
55

6+
type TocMap = Map<
7+
string,
8+
{
9+
dedicated: Set<string>;
10+
starter: Set<string>;
11+
essential: Set<string>;
12+
premium: Set<string>;
13+
}
14+
>;
15+
616
/**
717
* Get files from different TOC types for tidbcloud
818
* Returns a Map where key is "locale/repo/version" and value is object with dedicated, starter, essential file sets
919
*/
10-
export async function getTidbCloudFilesFromTocs(
11-
graphql: any
12-
): Promise<
13-
Map<
14-
string,
15-
{ dedicated: Set<string>; starter: Set<string>; essential: Set<string> }
16-
>
17-
> {
20+
export async function getTidbCloudFilesFromTocs(graphql: any): Promise<TocMap> {
1821
const tocQuery = await graphql(`
1922
{
2023
allMdx(
@@ -39,15 +42,7 @@ export async function getTidbCloudFilesFromTocs(
3942
}
4043

4144
const tocNodes = tocQuery.data!.allMdx.nodes;
42-
const tidbCloudTocFilesMap = new Map<
43-
string,
44-
{
45-
dedicated: Set<string>;
46-
starter: Set<string>;
47-
essential: Set<string>;
48-
premium: Set<string>;
49-
}
50-
>();
45+
const tidbCloudTocFilesMap: TocMap = new Map();
5146

5247
tocNodes.forEach((node: TocQueryData["allMdx"]["nodes"][0]) => {
5348
const { config } = generateConfig(node.slug);
@@ -97,11 +92,8 @@ export async function getTidbCloudFilesFromTocs(
9792
export function determineInDefaultPlan(
9893
fileName: string,
9994
pathConfig: any,
100-
tidbCloudTocFilesMap: Map<
101-
string,
102-
{ dedicated: Set<string>; starter: Set<string>; essential: Set<string> }
103-
>
104-
): string | null {
95+
tidbCloudTocFilesMap: TocMap
96+
): CloudPlan | null {
10597
// Only apply this logic for tidbcloud articles
10698
if (pathConfig.repo !== "tidbcloud") {
10799
return null;
@@ -116,7 +108,7 @@ export function determineInDefaultPlan(
116108
return null;
117109
}
118110

119-
const { dedicated, starter, essential } = tocData;
111+
const { dedicated, starter, essential, premium } = tocData;
120112

121113
// Check if article is in TOC.md (dedicated)
122114
if (dedicated.has(fileName)) {
@@ -137,5 +129,14 @@ export function determineInDefaultPlan(
137129
return "essential";
138130
}
139131

132+
if (
133+
premium.has(fileName) &&
134+
!essential.has(fileName) &&
135+
!dedicated.has(fileName) &&
136+
!starter.has(fileName)
137+
) {
138+
return "premium";
139+
}
140+
140141
return null;
141142
}

0 commit comments

Comments
 (0)