hensei-web/components/LanguageSwitch/index.tsx
Justin Edmund c6b53eb538 Migrate all remaining components to App Router navigation
- Add 'use client' directives to all remaining components
- Update imports from next/router to next/navigation
- Replace router.locale with getCookie('NEXT_LOCALE')
- Update LanguageSwitch to use router.refresh()
- Replace router.asPath with usePathname()

Components migrated:
- Common: SelectWithInput, Editor
- Layout: Header, Layout, MentionList, ElementToggle
- Misc: UpdateToast, SearchModal, RaidCombobox, FilterModal
- Misc: ChangelogUnit, HovercardHeader, LanguageSwitch
- Extra: GuidebookUnit, GuidebookResult
- Reps: GridRep, CharacterRep, WeaponRep, SummonRep

Added migration PRD documentation to track progress.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-09-01 16:31:18 -07:00

54 lines
1.6 KiB
TypeScript

'use client'
import React, { PropsWithChildren, useEffect, useState } from 'react'
import { useRouter } from 'next/navigation'
import { usePathname } from 'next/navigation'
import { setCookie } from 'cookies-next'
import { retrieveLocaleCookies } from '~utils/retrieveCookies'
import * as SwitchPrimitive from '@radix-ui/react-switch'
import styles from './index.module.scss'
interface Props extends SwitchPrimitive.SwitchProps {}
export const LanguageSwitch = React.forwardRef<HTMLButtonElement, Props>(
function LanguageSwitch(
{ children }: PropsWithChildren<Props>,
forwardedRef
) {
// Router and locale data
const router = useRouter()
const pathname = usePathname()
const localeData = retrieveLocaleCookies()
// State
const [languageChecked, setLanguageChecked] = useState(false)
// Hooks
useEffect(() => {
setLanguageChecked(localeData === 'ja' ? true : false)
}, [localeData])
function changeLanguage(value: boolean) {
const language = value ? 'ja' : 'en'
const expiresAt = new Date()
expiresAt.setDate(expiresAt.getDate() + 120)
setCookie('NEXT_LOCALE', language, { path: '/', expires: expiresAt })
router.refresh()
}
return (
<SwitchPrimitive.Root
className={styles.languageSwitch}
onCheckedChange={changeLanguage}
checked={languageChecked}
ref={forwardedRef}
>
<SwitchPrimitive.Thumb className={styles.thumb} />
<span className={styles.left}>JP</span>
<span className={styles.right}>EN</span>
</SwitchPrimitive.Root>
)
}
)
export default LanguageSwitch