3c9c9e740e
docs: 添加组件文档注释和类型导入 refactor: 统一使用 SnackbarOptions 类型 style: 优化导入语句顺序和格式 refactor: 简化假数据生成器中的faker导入和使用 feat(form-recognizer): 增强表单识别功能并优化UI交互 refactor: 移除未使用的组件文件 refactor(页面头部): 提取通用 PageHeader 组件 style(组件): 调整自动刷新开关和存储选项网格的样式 style(ui): 调整时间戳页面和结果视图的样式 ci(workflow): 移除Firefox测试以简化CI流程 refactor(qrcode): 重构二维码解析功能并提取为独立模块 style(页面样式): 统一页面头部图标颜色并优化表单样式 feat(formMapping): 添加表单映射功能 feat(表单映射): 添加配置导出功能及状态提示 feat(表单填充): 新增智能表单填充功能 ci: 精简触发CI的工作流分支 test: 更新路由测试以匹配新增的路由数量 test: 将测试文件中的描述和断言翻译为中文 test(PageHeader): 添加组件测试用例 refactor(theme): 重构主题样式并优化仪表盘卡片组件 refactor(Button): 统一按钮样式并移除重复样式定义 docs: 更新 README 文件中的目录结构说明 refactor: 优化代码类型声明和UI布局 refactor(消息通信): 使用 @webext-core/messaging 重构消息处理逻辑 fix: 修复依赖项缺失导致的潜在问题 refactor: 重构表单识别和消息处理逻辑 feat(侧边栏): 添加侧边栏状态变化通知功能 refactor: 优化高亮组件渲染逻辑并添加防抖处理 refactor: 统一组件导出方式为默认导出 refactor: 重构样式系统并迁移至 MUI 主题 docs: 更新项目文档以反映新增功能和技术栈细节 feat: 添加错误边界组件以捕获子组件错误 refactor(ui): 使用全局 snackbar 替换本地通知组件 refactor(snackbar): 重构全局消息提示为 SnackbarProvider 组件 refactor(Snackbar): 重构消息提示组件并优化样式 refactor: 重构路由和功能配置系统 refactor(GlobalSnackbar): 合并SnackbarProvider到GlobalSnackbar组件 feat(表单映射): 增强高亮器功能并优化元素定位 refactor: 移除表单映射相关功能 feat: 实现页面组件懒加载并优化加载状态显示 refactor: 移除未使用的数据生成和验证工具类 chore: 更新应用图标文件 refactor(qrcode): 替换二维码生成和解析库以减小体积 feat: 添加在标签页打开功能并优化错误处理 feat: 支持独立标签页模式并重构路由配置 feat(ui): 优化响应式布局和样式设计 refactor: 移除URL工具相关代码 refactor: 简化页面标题和存储选项网格的响应式布局 refactor: 优化样式和错误处理 refactor: 重构页面组件结构 style(DashboardPage): 优化仪表盘布局为响应式网格 refactor(ToolCard): 重构工具卡片组件并添加详细注释 feat: 新增文本统计功能 feat(jwt): 添加 JWT 解析工具功能 docs: 更新 README 以更清晰地描述项目功能和结构 docs: 更新AGENTS.md文档以反映项目最新状态 test: 更新特征计数测试以匹配新增工具 style: 统一导入语句顺序 refactor(pages): 重构工具页面组件结构并优化功能实现 feat(顶部栏): 添加搜索功能和历史记录 feat: 添加多语言支持(i18n) feat(顶部栏): 添加语言切换功能并优化国际化支持 feat(i18n): 增加语言归一化和同步功能 fix(剪贴板): 使用国际化文本替换剪贴板消息 test(顶部栏): 更新测试以匹配国际化标题 refactor: 重构组件目录结构 refactor(Dashboard): 优化卡片组件结构并移除无用属性 refactor(timestamp): 合并输入处理函数并优化样式结构 refactor(dashboard): 重构仪表盘卡片组件并优化布局 refactor(storageCleaner): 重构样式代码并优化国际化处理 refactor(StorageCleaner): 优化条件判断和清理未使用的代码 refactor(QrCode): 重构二维码页面样式和组件结构 refactor(ErrorBoundary): 移除冗余状态更新并调整错误背景色 feat(TopBar): 优化顶部导航栏功能并添加样式配置 chore: 在.gitignore中添加docs目录 feat(JsonDiff): 添加差异引擎类型定义 feat: 新增 JSON 差异比较工具功能 feat(主题): 添加暗色模式支持并重构主题系统
380 lines
14 KiB
TypeScript
380 lines
14 KiB
TypeScript
import { useState, useEffect, useRef, useMemo } from 'react';
|
|
import {
|
|
Box,
|
|
IconButton,
|
|
Stack,
|
|
Tooltip,
|
|
Typography,
|
|
InputBase,
|
|
Paper,
|
|
List,
|
|
ListItemButton,
|
|
ListItemIcon,
|
|
ListItemText,
|
|
ClickAwayListener,
|
|
} from '@mui/material';
|
|
import SettingsIcon from '@mui/icons-material/Settings';
|
|
import OpenInNewIcon from '@mui/icons-material/OpenInNew';
|
|
import ArrowBackIosNewIcon from '@mui/icons-material/ArrowBackIosNew';
|
|
import SearchIcon from '@mui/icons-material/Search';
|
|
import HistoryIcon from '@mui/icons-material/History';
|
|
import CloseIcon from '@mui/icons-material/Close';
|
|
import LanguageIcon from '@mui/icons-material/Language';
|
|
import LightModeIcon from '@mui/icons-material/LightMode';
|
|
import DarkModeIcon from '@mui/icons-material/DarkMode';
|
|
import SettingsBrightnessIcon from '@mui/icons-material/SettingsBrightness';
|
|
import { useRouter } from '@/providers/RouterProvider';
|
|
import { useThemeMode } from '@/providers/ThemeModeProvider';
|
|
import { FEATURES, FeatureConfig } from '@/config/features';
|
|
import { storageUtil } from '@/utils/chromeStorage';
|
|
import { openExtensionPage } from '@/utils/chromeTabs';
|
|
import { useTranslation } from 'react-i18next';
|
|
import { alpha } from '@mui/material/styles';
|
|
import { SUPPORTED_LANGUAGES, normalizeLanguage } from '@/i18n';
|
|
import { topBarStyles } from '@/config/pageTheme';
|
|
|
|
export default function TopBar({ onOpenOptions }: { onOpenOptions: () => void }) {
|
|
const { currentPage, goBack, navigateTo } = useRouter();
|
|
const { mode, setMode } = useThemeMode();
|
|
const { t, i18n } = useTranslation(['common', 'features']);
|
|
const [searchQuery, setSearchQuery] = useState('');
|
|
const [showResults, setShowResults] = useState(false);
|
|
const [searchHistory, setSearchHistory] = useState<string[]>([]);
|
|
const [selectedIndex, setSelectedIndex] = useState(-1);
|
|
const inputRef = useRef<HTMLInputElement>(null);
|
|
|
|
// 加载搜索历史
|
|
useEffect(() => {
|
|
storageUtil
|
|
.get('app/searchHistory', [])
|
|
.then((history) => {
|
|
setSearchHistory(history || []);
|
|
})
|
|
.catch((error) => {
|
|
console.error('加载搜索历史失败:', error);
|
|
});
|
|
}, []);
|
|
|
|
// 模糊搜索逻辑
|
|
const searchResults = useMemo(() => {
|
|
if (!searchQuery.trim()) return [];
|
|
const query = searchQuery.toLowerCase();
|
|
return FEATURES.filter((f) => {
|
|
if (f.key === 'dashboard') return false;
|
|
const label = t(f.labelKey).toLowerCase();
|
|
const desc = t(f.descriptionKey).toLowerCase();
|
|
return label.includes(query) || desc.includes(query);
|
|
});
|
|
}, [searchQuery, t]);
|
|
|
|
const displayedHistory = useMemo(() => {
|
|
if (searchQuery.trim()) return [];
|
|
return searchHistory.slice(0, topBarStyles.SEARCH_HISTORY_DISPLAY);
|
|
}, [searchHistory, searchQuery]);
|
|
|
|
const handleOpenInTab = async () => {
|
|
await openExtensionPage('popup.html', { mode: 'tab' });
|
|
window.close();
|
|
};
|
|
|
|
const handleSearchChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
|
setSearchQuery(e.target.value);
|
|
setShowResults(true);
|
|
setSelectedIndex(-1);
|
|
};
|
|
|
|
const saveToHistory = async (query: string) => {
|
|
if (!query.trim()) return;
|
|
setSearchHistory((prev) => {
|
|
const newHistory = [query, ...prev.filter((h) => h !== query)].slice(
|
|
0,
|
|
topBarStyles.SEARCH_HISTORY_LIMIT,
|
|
);
|
|
storageUtil.set('app/searchHistory', newHistory).catch((error) => {
|
|
console.error('保存搜索历史失败:', error);
|
|
});
|
|
return newHistory;
|
|
});
|
|
};
|
|
|
|
const handleSelectFeature = (feature: FeatureConfig) => {
|
|
navigateTo(feature.key);
|
|
saveToHistory(t(feature.labelKey));
|
|
setSearchQuery('');
|
|
setShowResults(false);
|
|
};
|
|
|
|
const toggleLanguage = async () => {
|
|
const currentLng = normalizeLanguage(i18n.language);
|
|
const currentIndex = SUPPORTED_LANGUAGES.indexOf(currentLng);
|
|
const nextIndex = (currentIndex + 1) % SUPPORTED_LANGUAGES.length;
|
|
const newLng = SUPPORTED_LANGUAGES[nextIndex];
|
|
await i18n.changeLanguage(newLng);
|
|
await storageUtil.set('app/language', newLng);
|
|
};
|
|
|
|
const cycleThemeMode = () => {
|
|
const next = { light: 'dark', dark: 'system', system: 'light' } as const;
|
|
setMode(next[mode]);
|
|
};
|
|
|
|
const ThemeIcon =
|
|
mode === 'light' ? LightModeIcon : mode === 'dark' ? DarkModeIcon : SettingsBrightnessIcon;
|
|
|
|
const handleKeyDown = (e: React.KeyboardEvent) => {
|
|
const totalItems = searchQuery.trim() ? searchResults.length : displayedHistory.length;
|
|
|
|
if (e.key === 'ArrowDown') {
|
|
e.preventDefault();
|
|
setSelectedIndex((prev) => (prev < totalItems - 1 ? prev + 1 : prev));
|
|
} else if (e.key === 'ArrowUp') {
|
|
e.preventDefault();
|
|
setSelectedIndex((prev) => (prev > 0 ? prev - 1 : prev));
|
|
} else if (e.key === 'Enter') {
|
|
if (selectedIndex >= 0) {
|
|
if (searchQuery.trim()) {
|
|
handleSelectFeature(searchResults[selectedIndex]);
|
|
} else {
|
|
const selectedQuery = displayedHistory[selectedIndex];
|
|
setSearchQuery(selectedQuery);
|
|
setSelectedIndex(-1);
|
|
// 触发搜索:如果匹配到功能则跳转,否则保持搜索词展示结果
|
|
const matchedFeature = FEATURES.find(
|
|
(f) => f.key !== 'dashboard' && t(f.labelKey) === selectedQuery,
|
|
);
|
|
if (matchedFeature) {
|
|
handleSelectFeature(matchedFeature);
|
|
}
|
|
}
|
|
} else if (searchQuery.trim() && searchResults.length > 0) {
|
|
handleSelectFeature(searchResults[0]);
|
|
}
|
|
} else if (e.key === 'Escape') {
|
|
setShowResults(false);
|
|
inputRef.current?.blur();
|
|
}
|
|
};
|
|
|
|
const isDashboard = currentPage === 'dashboard';
|
|
|
|
return (
|
|
<Stack
|
|
direction="row"
|
|
justifyContent="space-between"
|
|
alignItems="center"
|
|
sx={{
|
|
px: { xs: 1, sm: 2 },
|
|
py: 1.5,
|
|
borderBottom: '1px solid',
|
|
borderColor: 'divider',
|
|
bgcolor: 'background.paper',
|
|
zIndex: topBarStyles.Z_INDEX,
|
|
position: 'relative',
|
|
}}
|
|
>
|
|
<Box sx={{ width: { xs: 32, sm: 40 } }}>
|
|
{!isDashboard && (
|
|
<IconButton
|
|
size="small"
|
|
onClick={goBack}
|
|
aria-label={t('common:buttons.back')}
|
|
sx={{
|
|
bgcolor: (theme) =>
|
|
theme.palette.mode === 'dark' ? 'rgba(255,255,255,0.05)' : 'grey.50',
|
|
'&:hover': {
|
|
bgcolor: (theme) =>
|
|
theme.palette.mode === 'dark' ? 'rgba(255,255,255,0.1)' : 'grey.200',
|
|
},
|
|
}}
|
|
>
|
|
<ArrowBackIosNewIcon sx={{ fontSize: 14 }} />
|
|
</IconButton>
|
|
)}
|
|
</Box>
|
|
|
|
<Typography
|
|
variant="subtitle2"
|
|
sx={{
|
|
fontWeight: 800,
|
|
letterSpacing: '0.5px',
|
|
textTransform: 'uppercase',
|
|
fontSize: '0.75rem',
|
|
color: 'text.secondary',
|
|
ml: 1,
|
|
display: { xs: 'none', md: 'block' },
|
|
}}
|
|
>
|
|
{t('common:appName')}
|
|
</Typography>
|
|
|
|
<Box sx={{ flex: 1, mx: { xs: 1, sm: 2 }, position: 'relative', maxWidth: 400 }}>
|
|
<ClickAwayListener onClickAway={() => setShowResults(false)}>
|
|
<Box>
|
|
<InputBase
|
|
ref={inputRef}
|
|
placeholder={t('common:buttons.search')}
|
|
value={searchQuery}
|
|
onChange={handleSearchChange}
|
|
onFocus={() => setShowResults(true)}
|
|
onKeyDown={handleKeyDown}
|
|
inputProps={{ 'aria-label': t('common:buttons.search') }}
|
|
startAdornment={<SearchIcon sx={{ color: 'text.disabled', mr: 1, fontSize: 20 }} />}
|
|
endAdornment={
|
|
searchQuery && (
|
|
<IconButton
|
|
size="small"
|
|
onClick={() => {
|
|
setSearchQuery('');
|
|
setSelectedIndex(-1);
|
|
}}
|
|
aria-label={t('common:buttons.clearSearch')}
|
|
>
|
|
<CloseIcon sx={{ fontSize: 16 }} />
|
|
</IconButton>
|
|
)
|
|
}
|
|
sx={{
|
|
width: '100%',
|
|
bgcolor: (theme) =>
|
|
theme.palette.mode === 'dark' ? 'rgba(255,255,255,0.05)' : 'grey.50',
|
|
px: 1.5,
|
|
py: 0.5,
|
|
borderRadius: 2,
|
|
fontSize: '0.875rem',
|
|
border: '1px solid',
|
|
borderColor: (theme) =>
|
|
theme.palette.mode === 'dark' ? 'rgba(255,255,255,0.1)' : 'transparent',
|
|
transition: 'all 0.2s',
|
|
'&:hover': {
|
|
bgcolor: (theme) =>
|
|
theme.palette.mode === 'dark' ? 'rgba(255,255,255,0.08)' : 'grey.100',
|
|
},
|
|
'&.Mui-focused': {
|
|
bgcolor: 'background.paper',
|
|
borderColor: 'primary.main',
|
|
boxShadow: (theme) => `0 0 0 2px ${alpha(theme.palette.primary.main, 0.15)}`,
|
|
},
|
|
}}
|
|
/>
|
|
|
|
{showResults && (searchQuery.trim() || displayedHistory.length > 0) && (
|
|
<Paper
|
|
elevation={8}
|
|
sx={{
|
|
position: 'absolute',
|
|
top: '100%',
|
|
left: 0,
|
|
right: 0,
|
|
mt: 1,
|
|
maxHeight: topBarStyles.DROPDOWN_MAX_HEIGHT,
|
|
overflow: 'auto',
|
|
borderRadius: 2,
|
|
zIndex: topBarStyles.DROPDOWN_Z_INDEX,
|
|
}}
|
|
>
|
|
<List disablePadding role="listbox">
|
|
{searchQuery.trim() ? (
|
|
searchResults.length > 0 ? (
|
|
searchResults.map((feature, index) => (
|
|
<ListItemButton
|
|
key={feature.key}
|
|
selected={selectedIndex === index}
|
|
onClick={() => handleSelectFeature(feature)}
|
|
role="option"
|
|
aria-selected={selectedIndex === index}
|
|
sx={{ py: 1 }}
|
|
>
|
|
<ListItemIcon sx={{ minWidth: 40 }}>{feature.icon}</ListItemIcon>
|
|
<ListItemText
|
|
primary={t(feature.labelKey)}
|
|
secondary={t(feature.descriptionKey)}
|
|
primaryTypographyProps={{ variant: 'body2', fontWeight: 600 }}
|
|
secondaryTypographyProps={{ variant: 'caption', noWrap: true }}
|
|
/>
|
|
</ListItemButton>
|
|
))
|
|
) : (
|
|
<Box sx={{ py: 3, textAlign: 'center' }}>
|
|
<Typography variant="body2" color="text.secondary">
|
|
{t('common:buttons.noResults')}
|
|
</Typography>
|
|
</Box>
|
|
)
|
|
) : (
|
|
<>
|
|
<Box sx={{ px: 2, py: 1 }}>
|
|
<Typography variant="caption" fontWeight={700} color="text.disabled">
|
|
{t('common:buttons.recentSearch')}
|
|
</Typography>
|
|
</Box>
|
|
{displayedHistory.map((item, index) => (
|
|
<ListItemButton
|
|
key={item}
|
|
selected={selectedIndex === index}
|
|
onClick={() => {
|
|
setSearchQuery(item);
|
|
setSelectedIndex(-1);
|
|
}}
|
|
role="option"
|
|
aria-selected={selectedIndex === index}
|
|
>
|
|
<ListItemIcon sx={{ minWidth: 40 }}>
|
|
<HistoryIcon sx={{ fontSize: 18, color: 'text.disabled' }} />
|
|
</ListItemIcon>
|
|
<ListItemText
|
|
primary={item}
|
|
primaryTypographyProps={{ variant: 'body2' }}
|
|
/>
|
|
</ListItemButton>
|
|
))}
|
|
</>
|
|
)}
|
|
</List>
|
|
</Paper>
|
|
)}
|
|
</Box>
|
|
</ClickAwayListener>
|
|
</Box>
|
|
|
|
<Stack direction="row" spacing={0.5} sx={{ justifyContent: 'flex-end', flexShrink: 0 }}>
|
|
<Tooltip title={t('common:buttons.toggleLanguage')}>
|
|
<IconButton
|
|
size="small"
|
|
onClick={toggleLanguage}
|
|
aria-label={t('common:buttons.toggleLanguage')}
|
|
>
|
|
<LanguageIcon sx={{ fontSize: 18 }} />
|
|
</IconButton>
|
|
</Tooltip>
|
|
<Tooltip title={t(`common:buttons.themeMode.${mode}`)}>
|
|
<IconButton
|
|
size="small"
|
|
onClick={cycleThemeMode}
|
|
aria-label={t('common:buttons.toggleTheme')}
|
|
>
|
|
<ThemeIcon sx={{ fontSize: 18 }} />
|
|
</IconButton>
|
|
</Tooltip>
|
|
<Tooltip title={t('common:buttons.openInTab')}>
|
|
<IconButton
|
|
size="small"
|
|
onClick={handleOpenInTab}
|
|
aria-label={t('common:buttons.openInTab')}
|
|
>
|
|
<OpenInNewIcon sx={{ fontSize: 18 }} />
|
|
</IconButton>
|
|
</Tooltip>
|
|
<Tooltip title={t('common:buttons.settings')}>
|
|
<IconButton
|
|
size="small"
|
|
onClick={onOpenOptions}
|
|
aria-label={t('common:buttons.settings')}
|
|
>
|
|
<SettingsIcon sx={{ fontSize: 18 }} />
|
|
</IconButton>
|
|
</Tooltip>
|
|
</Stack>
|
|
</Stack>
|
|
);
|
|
}
|