Skip to content

Commit f5be8f8

Browse files
committed
feat: add getversion and js-yaml library
Signed-off-by: Amit Amrutiya <amitamrutiya2210@gmail.com>
1 parent f27a0c7 commit f5be8f8

4 files changed

Lines changed: 86 additions & 53 deletions

File tree

package-lock.json

Lines changed: 2 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@
116116
"access": "public"
117117
},
118118
"dependencies": {
119+
"js-yaml": "^4.1.0",
119120
"lodash": "^4.17.21"
120121
}
121122
}

src/custom/CustomCatalog/CustomCard.tsx

Lines changed: 15 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import CalendarMonthIcon from '@mui/icons-material/CalendarMonth';
2-
import { Avatar, styled, useTheme } from '@mui/material';
2+
import { Avatar, styled } from '@mui/material';
33
import React, { useEffect, useState } from 'react';
44
import { Grid } from '../../base';
55
import { CloneIcon, CommunityClassIcon, OfficialClassIcon, OpenIcon, ShareIcon } from '../../icons';
66
import VerificationClassIcon from '../../icons/ContentClassIcons/VerificationClassIcon';
77
import DeploymentsIcon from '../../icons/Deployments/DeploymentsIcon';
88
import { DownloadIcon } from '../../icons/Download';
9-
import { DARK_TEAL } from '../../theme';
9+
import { DARK_TEAL, useTheme } from '../../theme';
1010
import { SNOW_WHITE } from '../../theme/colors/colors';
1111
import { CustomTooltip } from '../CustomTooltip';
12+
import { getVersion, handleImage } from './Helper';
1213
import {
1314
CardBack,
1415
CardFront,
@@ -37,7 +38,10 @@ export const DesignCardUrl = styled('a')(() => ({
3738
textDecoration: 'none'
3839
}));
3940

40-
interface Pattern {
41+
export interface Pattern {
42+
id: string;
43+
user_id: string;
44+
pattern_file: string;
4145
name: string;
4246
download_count: number;
4347
clone_count: number;
@@ -53,8 +57,10 @@ interface Pattern {
5357
};
5458
catalog_data?: {
5559
content_class?: string;
56-
imageURL?: string;
60+
imageURL?: string[];
5761
compatibility?: string[];
62+
published_version?: string;
63+
type?: string;
5864
};
5965
visibility: string;
6066
updated_at: Date;
@@ -63,21 +69,15 @@ interface Pattern {
6369
type CatalogCardProps = {
6470
pattern: Pattern;
6571
patternType: string;
66-
cardLink: string;
6772
cardHeight: string;
6873
cardWidth: string;
6974
cardStyles: React.CSSProperties;
70-
version?: string;
7175
avatarUrl: string;
7276
shouldFlip?: boolean;
7377
cardTechnologies?: boolean;
7478
isDetailed?: boolean;
75-
cardAvatarUrl?: boolean;
76-
date?: boolean;
77-
cardVersion?: boolean;
7879
UserName?: string;
7980
children?: React.ReactNode; // catalogImage
80-
TechnologyComponent?: React.ReactNode;
8181
basePath?: string; // path of meshmodel img stored
8282
subBasePath?: string; // path of meshmodel img stored
8383
getHostUrl?: () => string;
@@ -111,7 +111,6 @@ const CustomCatalogCard: React.FC<CatalogCardProps> = ({
111111
shouldFlip,
112112
isDetailed,
113113
cardTechnologies,
114-
cardVersion,
115114
avatarUrl,
116115
UserName,
117116
children,
@@ -127,45 +126,13 @@ const CustomCatalogCard: React.FC<CatalogCardProps> = ({
127126
};
128127
const theme = useTheme();
129128

130-
const technologies = pattern.catalog_data?.compatibility || []; // an array
129+
const technologies = pattern.catalog_data?.compatibility || [];
131130
const techlimit = 5;
132131
const [availableTechnologies, setAvailableTechnologies] = useState<string[]>([]);
133-
const checkImageUrlValidity = async (url: string, appendHostUrl = true) => {
134-
return new Promise((resolve) => {
135-
const img = new Image();
136-
// Only append host if the URL does not start with "http" or "https"
137-
if (appendHostUrl && !url.startsWith('http')) {
138-
img.src = (getHostUrl ? getHostUrl() : '') + url;
139-
} else {
140-
img.src = url;
141-
}
142-
img.onload = () => {
143-
// Check if the image loaded successfully
144-
resolve(true);
145-
};
146-
147-
img.onerror = () => {
148-
// Handle the case where the image could not be loaded
149-
resolve(false);
150-
};
151-
});
152-
};
153-
154-
const handleImage = async () => {
155-
const validSvgPaths = [];
156-
for (const technology of technologies) {
157-
const svgIconPath = `${basePath}/${technology.toLowerCase()}/${subBasePath}/${technology.toLowerCase()}-color.svg`;
158-
const isSvgPathValid = await checkImageUrlValidity(svgIconPath as string);
159-
if (isSvgPathValid) {
160-
validSvgPaths.push(technology);
161-
}
162-
}
163-
164-
setAvailableTechnologies(validSvgPaths);
165-
};
132+
const version = getVersion(pattern);
166133

167134
useEffect(() => {
168-
handleImage();
135+
handleImage(technologies, basePath, subBasePath, setAvailableTechnologies);
169136
// eslint-disable-next-line react-hooks/exhaustive-deps
170137
}, []);
171138

@@ -336,9 +303,9 @@ const CustomCatalogCard: React.FC<CatalogCardProps> = ({
336303
</Grid>
337304
</DesignDetailsDiv>
338305
)}
339-
{cardVersion && (
306+
{version && (
340307
<VersionDiv>
341-
<VersionText>v{cardVersion}</VersionText>
308+
<VersionText>v{version}</VersionText>
342309
</VersionDiv>
343310
)}
344311
</CardBack>

src/custom/CustomCatalog/Helper.ts

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import jsyaml from 'js-yaml';
2+
import { Pattern } from './CustomCard';
3+
4+
const checkImageUrlValidity = async (
5+
url: string,
6+
appendHostUrl = true,
7+
getHostUrl?: () => string
8+
): Promise<boolean> => {
9+
return new Promise((resolve) => {
10+
const img = new Image();
11+
// Only append host if the URL does not start with "http" or "https"
12+
if (appendHostUrl && !url.startsWith('http')) {
13+
img.src = (getHostUrl ? getHostUrl() : '') + url;
14+
} else {
15+
img.src = url;
16+
}
17+
img.onload = () => {
18+
// Check if the image loaded successfully
19+
resolve(true);
20+
};
21+
22+
img.onerror = () => {
23+
// Handle the case where the image could not be loaded
24+
resolve(false);
25+
};
26+
});
27+
};
28+
29+
const getValidSvgPaths = async (
30+
technologies: string[],
31+
basePath: string,
32+
subBasePath: string
33+
): Promise<string[]> => {
34+
const validSvgPaths: string[] = [];
35+
for (const technology of technologies) {
36+
const svgIconPath = `${basePath}/${technology.toLowerCase()}/${subBasePath}/${technology.toLowerCase()}-color.svg`;
37+
const isSvgPathValid = await checkImageUrlValidity(svgIconPath as string);
38+
if (isSvgPathValid) {
39+
validSvgPaths.push(technology);
40+
}
41+
}
42+
return validSvgPaths;
43+
};
44+
45+
export const handleImage = async (
46+
technologies: string[],
47+
basePath: string = '',
48+
subBasePath: string = '',
49+
setAvailableTechnologies: (technologies: string[]) => void
50+
) => {
51+
const validSvgPaths = await getValidSvgPaths(technologies, basePath, subBasePath);
52+
setAvailableTechnologies(validSvgPaths);
53+
};
54+
55+
export const DEFAULT_DESIGN_VERSION = '0.0.0';
56+
57+
export const getVersion = (design: Pattern) => {
58+
if (design.visibility === 'published') {
59+
return design?.catalog_data?.published_version || DEFAULT_DESIGN_VERSION;
60+
}
61+
try {
62+
const patternFile = jsyaml.load(design.pattern_file);
63+
return patternFile?.version || DEFAULT_DESIGN_VERSION;
64+
} catch (e) {
65+
console.error(e);
66+
return DEFAULT_DESIGN_VERSION;
67+
}
68+
};

0 commit comments

Comments
 (0)