Skip to content

Commit 26d7146

Browse files
committed
Refactor image and icon loading logic
1 parent 25b3a67 commit 26d7146

File tree

2 files changed

+29
-16
lines changed

2 files changed

+29
-16
lines changed

template/src/components/atoms/AssetByVariant/AssetByVariant.tsx

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,21 @@ function AssetByVariant({ path, extension = 'png', ...props }: Props) {
2424
.custom<ImageSourcePropType>()
2525
.parse(images(`./${path}.${extension}`));
2626

27-
const fetchedModule = z
28-
.custom<ImageSourcePropType>()
29-
.parse(images(`./${variant}/${path}.${extension}`));
30-
setImage(fetchedModule ?? defaultSource);
27+
if (variant === 'default') {
28+
setImage(defaultSource);
29+
return;
30+
}
31+
32+
try {
33+
const fetchedModule = z
34+
.custom<ImageSourcePropType>()
35+
.parse(images(`./${variant}/${path}.${extension}`));
36+
setImage(fetchedModule);
37+
} catch (error) {
38+
// eslint-disable-next-line no-console
39+
console.error(`Couldn't load the image: ${path}.${extension} for the variant ${variant}, Fallback to default`, error);
40+
setImage(defaultSource);
41+
}
3142
} catch (error) {
3243
// eslint-disable-next-line no-console
3344
console.error(`Couldn't load the image: ${path}`, error);

template/src/components/atoms/IconByVariant/IconByVariant.tsx

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,23 @@ function IconByVariant({ path, ...props }: Props) {
2424
.object({ default: z.custom<ReactElement<SvgProps>>() })
2525
.parse(icons(`./${path}.${EXTENSION}`));
2626

27-
if (variant !== 'default') {
28-
try {
29-
const fetchedModule = z
30-
.object({ default: z.custom<ReactElement<SvgProps>>() })
31-
.parse(icons(`./${variant}/${path}.${EXTENSION}`));
27+
if (variant === 'default') {
28+
setIcon(defaultSource.default);
29+
return;
30+
}
3231

33-
setIcon(fetchedModule.default);
34-
} catch (error) {
35-
// eslint-disable-next-line no-console
36-
console.error(`Couldn't load the icon: ${path}.${EXTENSION} for the variant ${variant}, Fallback to default`, error);
37-
setIcon(defaultSource.default);
38-
}
39-
} else {
32+
try {
33+
const fetchedModule = z
34+
.object({ default: z.custom<ReactElement<SvgProps>>() })
35+
.parse(icons(`./${variant}/${path}.${EXTENSION}`));
36+
37+
setIcon(fetchedModule.default);
38+
} catch (error) {
39+
// eslint-disable-next-line no-console
40+
console.error(`Couldn't load the icon: ${path}.${EXTENSION} for the variant ${variant}, Fallback to default`, error);
4041
setIcon(defaultSource.default);
4142
}
43+
4244
} catch (error) {
4345
// eslint-disable-next-line no-console
4446
console.error(`Couldn't load the icon: ${path}.${EXTENSION}`, error);

0 commit comments

Comments
 (0)