11import * as TabsPrimitive from '@radix-ui/react-tabs' ;
2- import type { FC , ReactElement } from 'react' ;
2+ import { useMemo , type FC , type ReactElement } from 'react' ;
33
44import CodeTabs from '#ui/Common/CodeTabs' ;
55
@@ -21,27 +21,32 @@ const MDXCodeTabs: FC<MDXCodeTabsProps> = ({
2121 defaultTab = '0' ,
2222 ...props
2323} ) => {
24- const languages = rawLanguages . split ( '|' ) ;
25- const displayNames = rawDisplayNames ?. split ( '|' ) ?? [ ] ;
26-
2724 const occurrences : Record < string , number > = { } ;
2825
29- const tabs = languages . map ( ( language , index ) => {
30- const base =
31- displayNames [ index ] ?. trim ( ) ||
32- NAME_OVERRIDES [ language ] ||
33- language . toUpperCase ( ) ;
26+ const { tabs, languages } = useMemo ( ( ) => {
27+ const languages = rawLanguages . split ( '|' ) ;
28+ const displayNames = rawDisplayNames ?. split ( '|' ) ?? [ ] ;
29+
30+ const tabs = languages . map ( ( language , index ) => {
31+ const base =
32+ displayNames [ index ] ?. trim ( ) ||
33+ NAME_OVERRIDES [ language ] ||
34+ language . toUpperCase ( ) ;
35+
36+ const count = occurrences [ base ] ?? 0 ;
37+
38+ occurrences [ base ] = count + 1 ;
3439
35- const count = occurrences [ base ] ?? 0 ;
36- occurrences [ base ] = count + 1 ;
40+ const label = count > 0 ? `${ base } (${ count + 1 } )` : base ;
3741
38- const label = count > 0 ? `${ base } (${ count + 1 } )` : base ;
42+ return {
43+ key : `${ language } -${ index } ` ,
44+ label : label ,
45+ } ;
46+ } ) ;
3947
40- return {
41- key : `${ language } -${ index } ` ,
42- label : label ,
43- } ;
44- } ) ;
48+ return { tabs, languages } ;
49+ } , [ rawLanguages , rawDisplayNames ] ) ;
4550
4651 return (
4752 < CodeTabs
0 commit comments