import { useMemo, useState } from 'react'; import { alpha, Box, Container, Grid, Paper, Typography } from '@mui/material'; import PageHeader from '@/components/PageHeader'; import TextInputArea from '@/components/TextInputArea'; import DescriptionIcon from '@mui/icons-material/Description'; import { formatByteSize, getTextStats } from '@/utils/textStatistics'; import { textStatisticsPageStyles } from '@/config/pageTheme'; import { useTranslation } from 'react-i18next'; /** * 文本统计页面组件 * * 提供实时的文本分析功能,包括字符数、单词数、行数和字节大小。 */ export default function Index() { const { t } = useTranslation(['textStatistics']); const [text, setText] = useState(''); // 实时计算统计信息,使用 useMemo 优化性能 // 对于 10,000 字符以上的文本,Intl.Segmenter 也能保持良好的性能 const stats = useMemo(() => getTextStats(text), [text]); const statItems = [ { label: t('textStatistics:characters'), value: stats.characters }, { label: t('textStatistics:words'), value: stats.words }, { label: t('textStatistics:lines'), value: stats.lines }, { label: t('textStatistics:bytes'), value: formatByteSize(stats.bytes) }, ]; return ( {/* 头部区域 */} } iconColor={textStatisticsPageStyles.primaryColor} /> {/* 文本输入区域 */} {/* 统计结果展示区域 */} {statItems.map((item) => ( `0 4px 12px ${alpha(textStatisticsPageStyles.primaryColor, 0.15)}`, borderColor: textStatisticsPageStyles.primaryColor, }, lineHeight: 1.6, fontSize: '0.9rem', fontWeight: 600, }} > {item.label} {item.value} ))} ); }