Skip to content

Commit 1e89833

Browse files
authored
Merge pull request #1081 from zihanKuang/version-public
fix: Unify version display for public and private designs
2 parents 16c2684 + 81e6605 commit 1e89833

File tree

1 file changed

+24
-9
lines changed

1 file changed

+24
-9
lines changed

src/custom/CustomCatalog/Helper.ts

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,18 @@ const getValidSvgPaths = async (
4242
return validSvgPaths;
4343
};
4444

45+
// Parses the pattern file content (YAML) and returns the version field.
46+
// If parsing fails or version is missing, returns the default version.
47+
const getWorkingVersionFromPatternFile = (patternFileContent: string) => {
48+
try {
49+
const patternFile = jsyaml.load(patternFileContent);
50+
return patternFile?.version || DEFAULT_DESIGN_VERSION;
51+
} catch (e) {
52+
console.error('Failed to parse pattern file to get version:', e);
53+
return DEFAULT_DESIGN_VERSION;
54+
}
55+
};
56+
4557
interface HandleImageProps {
4658
technologies: string[];
4759
basePath?: string;
@@ -60,15 +72,18 @@ export const handleImage = async ({
6072
};
6173
export const DEFAULT_DESIGN_VERSION = '0.0.0';
6274

75+
// Returns the version of a design based on its visibility.
76+
// - For 'published' designs, returns the stable published version.
77+
// - For 'public' or 'private', returns the working version from the pattern file.
78+
// - Defaults to the working version if visibility is unrecognized.
6379
export const getVersion = (design: Pattern) => {
64-
if (design.visibility === 'public') {
65-
return design?.catalog_data?.published_version || DEFAULT_DESIGN_VERSION;
66-
}
67-
try {
68-
const patternFile = jsyaml.load(design.pattern_file);
69-
return patternFile?.version || DEFAULT_DESIGN_VERSION;
70-
} catch (e) {
71-
console.error(e);
72-
return DEFAULT_DESIGN_VERSION;
80+
switch (design.visibility) {
81+
case 'published':
82+
return design?.catalog_data?.published_version || DEFAULT_DESIGN_VERSION;
83+
case 'public':
84+
case 'private':
85+
return getWorkingVersionFromPatternFile(design.pattern_file);
86+
default:
87+
return getWorkingVersionFromPatternFile(design.pattern_file);
7388
}
7489
};

0 commit comments

Comments
 (0)