Skip to content

Commit 14a341b

Browse files
committed
fix(website): improve randomization in LanguageSection and update key usage in LocalCardList for consistency
1 parent 9d36ffb commit 14a341b

File tree

3 files changed

+74
-12
lines changed

3 files changed

+74
-12
lines changed

apps/website/src/components/LandingPage/LanguageSection/index.tsx

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,13 @@ import {
1313

1414
const shuffleArray = (array: string[], limit?: number) => {
1515
const shuffled = [...array];
16+
1617
for (let i = shuffled.length - 1; i > 0; i--) {
17-
const j = Math.floor(Math.random() * (i + 1));
18-
[shuffled[i], shuffled[j]] = [shuffled[j], shuffled[i]];
18+
const randomIndex = Math.floor(Math.random() * (i + 1));
19+
20+
[shuffled[i], shuffled[randomIndex]] = [shuffled[randomIndex], shuffled[i]];
1921
}
22+
2023
return limit ? shuffled.slice(0, limit) : shuffled;
2124
};
2225

@@ -56,12 +59,12 @@ const LocalCardList: FC<{ localeList: string[]; className?: string }> = ({
5659
className={cn('inline-flex shrink-0 will-change-transform', className)}
5760
>
5861
{/* First set of cards */}
59-
{localeList.map((locale, idx) => (
60-
<LocalCard key={`${locale}-first-${idx}`} locale={locale} />
62+
{localeList.map((locale, index) => (
63+
<LocalCard key={`${locale}-first-${index}`} locale={locale} />
6164
))}
6265
{/* Duplicate set for seamless loop */}
63-
{localeList.map((locale, idx) => (
64-
<LocalCard key={`${locale}-second-${idx}`} locale={locale} />
66+
{localeList.map((locale, index) => (
67+
<LocalCard key={`${locale}-second-${index}`} locale={locale} />
6568
))}
6669
</div>
6770
</div>

packages/@intlayer/design-system/src/components/Flags/Flag.tsx

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import { type Locale, Locales } from '@intlayer/types';
2-
import type { FC, ImgHTMLAttributes, JSX } from 'react';
2+
import type {
3+
ComponentType,
4+
FC,
5+
ImgHTMLAttributes,
6+
JSX,
7+
SVGProps,
8+
} from 'react';
39

410
// import andorra from './ad.svg';
511
import unitedArabEmirates from './ae.svg';
@@ -490,9 +496,30 @@ const flagRecord: Partial<Record<Locale, typeof unknown>> = {
490496
[Locales.NEPALI_NEPAL]: nepal,
491497
};
492498

493-
export const Flag: FC<FlagProps> = ({ locale, ...props }): JSX.Element => {
494-
const flagSrc = flagRecord[locale];
495-
const src = typeof flagSrc === 'string' ? flagSrc : (flagSrc as any)?.src;
499+
export const Flag: FC<FlagProps> = ({ locale, alt, ...props }): JSX.Element => {
500+
const asset = flagRecord[locale] ?? unknown;
496501

497-
return <img src={src} alt={`${locale} flag`} {...props} />;
502+
// Case 1: bundler returns a URL string or an object with a .src field
503+
if (typeof asset === 'string' || (asset as any)?.src) {
504+
const src =
505+
typeof asset === 'string' ? (asset as string) : (asset as any).src;
506+
return <img src={src} alt={alt ?? `${locale} flag`} {...props} />;
507+
}
508+
509+
// Case 2: bundler returns a React component (SVGR)
510+
const Svg = asset as ComponentType<SVGProps<SVGSVGElement>>;
511+
const { width, height, className, style, onClick } = props;
512+
513+
return (
514+
<Svg
515+
aria-label={alt ?? `${locale} flag`}
516+
width={width as any}
517+
height={height as any}
518+
className={className}
519+
style={style}
520+
onClick={onClick as any}
521+
role="img"
522+
focusable={false as any}
523+
/>
524+
);
498525
};

packages/@intlayer/design-system/tsdown.config.ts

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,37 @@ import svgr from '@svgr/rollup';
22
import { getOptions } from '@utils/tsdown-config';
33
import { defineConfig, type Options } from 'tsdown';
44

5+
// Plugin to inject React import into SVG files that use React.createElement
6+
const injectReactPlugin = () => ({
7+
name: 'inject-react',
8+
renderChunk(code: string, chunk: { fileName: string }) {
9+
// Check if this is an SVG file and uses React.createElement
10+
if (
11+
chunk.fileName.match(/\.svg\.(mjs|js)$/) &&
12+
code.includes('React.createElement')
13+
) {
14+
// Add React import at the top if not already present
15+
if (
16+
!code.includes('import React') &&
17+
!code.includes('from "react"') &&
18+
!code.includes("from 'react'")
19+
) {
20+
const regionMatch = code.match(/^(\/\/#region [^\n]+\n)/);
21+
if (regionMatch) {
22+
// Insert after #region comment
23+
return code.replace(
24+
regionMatch[0],
25+
`${regionMatch[0]}import * as React from "react";\n`
26+
);
27+
}
28+
// Insert at the top
29+
return `import * as React from "react";\n${code}`;
30+
}
31+
}
32+
return null;
33+
},
34+
});
35+
536
const options: Options[] = getOptions({
637
all: {
738
platform: 'neutral',
@@ -11,11 +42,12 @@ const options: Options[] = getOptions({
1142
svgo: true,
1243
icon: true, // scale to 1em
1344
exportType: 'default', // default export a React component
14-
jsxRuntime: 'automatic', // no need to import React
45+
jsxRuntime: 'classic', // Use classic runtime to ensure React import
1546
// ref: true, // enable if you need refs
1647
// include/exclude if you want to limit where it runs:
1748
// include: '**/*.svg',
1849
}),
50+
injectReactPlugin(),
1951
],
2052
},
2153
types: {

0 commit comments

Comments
 (0)