hensei-web/components/extra/GuidebookUnit/index.tsx
Justin Edmund 3d67622353
Fix i18n migration to next-intl (#430)
## Summary
- Fixed translation key format compatibility with next-intl
- Fixed pluralization format from i18next to next-intl format
- Fixed dynamic translation key error handling
- Updated server components to match API response structure
- Fixed useSearchParams import location

## Changes
- Changed pluralization from `{{count}} items` to `{count} items` format
- Added proper error handling for missing translation keys
- Fixed import paths for next-intl hooks
- Fixed PartyPageClient trying to set non-existent appState.parties

## Test plan
- [x] Verified translations render correctly
- [x] Tested pluralization works with different counts
- [x] Confirmed no console errors about missing translations
- [x] Tested party page functionality

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

---------

Co-authored-by: Claude <noreply@anthropic.com>
2025-09-03 16:25:59 -07:00

204 lines
5.1 KiB
TypeScript

'use client'
import React, { useEffect, useState } from 'react'
import { getCookie } from 'cookies-next'
import { useTranslations } from 'next-intl'
import classNames from 'classnames'
import Alert from '~components/common/Alert'
import SearchModal from '~components/search/SearchModal'
import {
ContextMenu,
ContextMenuTrigger,
ContextMenuContent,
} from '~components/common/ContextMenu'
import ContextMenuItem from '~components/common/ContextMenuItem'
import Button from '~components/common/Button'
import type { SearchableObject } from '~types'
import PlusIcon from '~public/icons/Add.svg'
import SettingsIcon from '~public/icons/Settings.svg'
import styles from './index.module.scss'
interface Props {
guidebook: Guidebook | undefined
position: number
editable: boolean
removeGuidebook: (position: number) => void
updateObject: (object: SearchableObject, position: number) => void
}
const GuidebookUnit = ({
guidebook,
position,
editable,
removeGuidebook: sendGuidebookToRemove,
updateObject,
}: Props) => {
// Translations and locale
const t = useTranslations('common')
const locale = (getCookie('NEXT_LOCALE') as string) || 'en'
// State: UI
const [searchModalOpen, setSearchModalOpen] = useState(false)
const [contextMenuOpen, setContextMenuOpen] = useState(false)
const [alertOpen, setAlertOpen] = useState(false)
// State: Other
const [imageUrl, setImageUrl] = useState('')
// Classes
const classes = classNames({
unit: true,
[styles.unit]: true,
[styles.editable]: editable,
[styles.filled]: guidebook !== undefined,
[styles.empty]: guidebook == undefined,
})
const buttonClasses = classNames({
Options: true,
Clicked: contextMenuOpen,
})
// Hooks
useEffect(() => {
generateImageUrl()
}, [guidebook])
// Methods: Open layer
function openSearchModal() {
if (editable) setSearchModalOpen(true)
}
function openRemoveGuidebookAlert() {
setAlertOpen(true)
}
// Methods: Handle button clicked
function handleButtonClicked() {
setContextMenuOpen(!contextMenuOpen)
}
// Methods: Handle open change
function handleContextMenuOpenChange(open: boolean) {
if (!open) setContextMenuOpen(false)
}
function handleSearchModalOpenChange(open: boolean) {
setSearchModalOpen(open)
}
// Methods: Mutate data
function removeGuidebook() {
if (guidebook) sendGuidebookToRemove(position)
setAlertOpen(false)
}
// Methods: Image string generation
function generateImageUrl() {
let imgSrc = guidebook
? `${process.env.NEXT_PUBLIC_SIERO_IMG_URL}/guidebooks/book_${guidebook.granblue_id}.png`
: ''
setImageUrl(imgSrc)
}
const placeholderImageUrl = '/images/placeholders/placeholder-guidebook.png'
// Methods: Layer element rendering
const contextMenu = () => {
if (editable && guidebook) {
return (
<>
<ContextMenu onOpenChange={handleContextMenuOpenChange}>
<ContextMenuTrigger asChild>
<Button
active={contextMenuOpen}
floating={true}
leftAccessoryIcon={<SettingsIcon />}
className="options"
onClick={handleButtonClicked}
/>
</ContextMenuTrigger>
<ContextMenuContent align="start">
<ContextMenuItem onSelect={openRemoveGuidebookAlert}>
{t('context.remove')}
</ContextMenuItem>
</ContextMenuContent>
</ContextMenu>
{removeAlert()}
</>
)
}
}
const removeAlert = () => {
return (
<Alert
open={alertOpen}
primaryAction={removeGuidebook}
primaryActionText={t('modals.guidebooks.buttons.remove')}
cancelAction={() => setAlertOpen(false)}
cancelActionText={t('buttons.cancel')}
message={
<>
{t.rich('modals.guidebooks.messages.remove', {
guidebook: guidebook?.name[locale],
strong: (chunks) => <strong>{chunks}</strong>
})}
</>
}
/>
)
}
const searchModal = () => {
return (
<SearchModal
placeholderText={t('search.placeholders.guidebook')}
fromPosition={position}
object="guidebooks"
open={searchModalOpen}
onOpenChange={handleSearchModalOpenChange}
send={updateObject}
/>
)
}
// Methods: Core element rendering
const imageElement = (
<div className={styles.guidebookImage} onClick={openSearchModal}>
<img
alt={guidebook?.name[locale]}
className={classNames({
[styles.image]: true,
[styles.placeholder]: imageUrl === '',
})}
src={imageUrl !== '' ? imageUrl : placeholderImageUrl}
/>
{editable ? (
<span className={styles.icon}>
<PlusIcon />
</span>
) : (
''
)}
</div>
)
const unitContent = (
<>
<div className={classes}>
{contextMenu()}
{imageElement}
<h3 className={styles.name}>{guidebook?.name[locale]}</h3>
</div>
{searchModal()}
</>
)
return unitContent
}
export default GuidebookUnit