11import styles from './tabs.module.sass'
22
3- import { Children , ReactElement , ReactNode , useState } from 'react'
3+ import { Children , ReactElement , ReactNode } from 'react'
44
55import Code from '@components/Code'
66import CopyButton from '@components/CopyButton'
@@ -9,7 +9,7 @@ import SvgPython from '@icons/Python'
99import SvgJavascript from '@/icons/Javascript'
1010import SvgCpp from '@icons/Cpp'
1111import SvgJava from '@/icons/Java'
12-
12+ import { useLanguage } from '@components/Language/context'
1313
1414interface TabsProps {
1515 title : string
@@ -21,22 +21,25 @@ interface TabProps {
2121 language : 'python' | 'javascript' | 'java' | 'cpp'
2222}
2323
24-
2524const LANGUAGE_ICONS = {
2625 python : SvgPython ,
2726 javascript : SvgJavascript ,
2827 cpp : SvgCpp ,
29- java : SvgJava
28+ java : SvgJava ,
3029}
3130
32-
3331export default function Tabs ( { title, children } : TabsProps ) {
34- const [ activeIndex , setActiveIndex ] = useState ( 0 )
32+ const { selectedLanguage , setSelectedLanguage } = useLanguage ( )
3533 const tabs = Children . toArray ( children ) as ReactElement < TabProps > [ ]
34+ const activeIndex = tabs . findIndex (
35+ ( tab ) => tab . props . language === selectedLanguage
36+ )
3637 const code = tabs [ activeIndex ] . props . code
3738 const language = tabs [ activeIndex ] . props . language
3839
39- const changeTab = ( index : number ) => setActiveIndex ( index )
40+ const changeTab = ( index : number ) => {
41+ setSelectedLanguage ( tabs [ index ] . props . language )
42+ }
4043 const handleCopy = ( ) => navigator . clipboard . writeText ( code )
4144
4245 const renderTabButtons = ( ) => {
0 commit comments