🌟 feat(ui): reusable CompactModeToggle & mobile-friendly CardPro

Summary
-------
Introduce a reusable compact-mode toggle component and greatly improve the CardPro header for small screens.  Removes duplicated code, adds i18n support, and refines overall responsiveness.

Details
-------
🎨  UI / Components
• Create `common/ui/CompactModeToggle.js`
  – Provides a single source of truth for switching between “Compact list” and “Adaptive list”
  – Automatically hides itself on mobile devices via `useIsMobile()`

• Refactor table modules to use the new component
  – `Users`, `Tokens`, `Redemptions`, `Channels`, `TaskLogs`, `MjLogs`, `UsageLogs`
  – Deletes legacy in-file toggle buttons & reduces repetition

📱  CardPro improvements
• Hide `actionsArea` and `searchArea` on mobile, showing a single “Show Actions / Hide Actions” toggle button
• Add i18n: texts are now pulled from injected `t()` function (`显示操作项` / `隐藏操作项` etc.)
• Extend PropTypes to accept the `t` prop; supply a safe fallback
• Minor cleanup: remove legacy DOM observers & flag CSS, simplify logic

🔧  Integration
• Pass the `t` translation function to every `CardPro` usage across table pages
• Remove temporary custom class hooks after logic simplification

Benefits
--------
✓ Consistent, DRY compact-mode handling across the entire dashboard
✓ Better mobile experience with decluttered headers
✓ Full translation support for newly added strings
✓ Easier future maintenance (single compact toggle, unified CardPro API)
This commit is contained in:
t0ng7u
2025-07-19 01:34:59 +08:00
parent de9d18a2fe
commit 56c1fbecea
17 changed files with 160 additions and 79 deletions

View File

@@ -1,6 +1,8 @@
import React from 'react'; import React, { useState } from 'react';
import { Card, Divider, Typography } from '@douyinfe/semi-ui'; import { Card, Divider, Typography, Button } from '@douyinfe/semi-ui';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import { useIsMobile } from '../../../hooks/common/useIsMobile';
import { IconEyeOpened, IconEyeClosed } from '@douyinfe/semi-icons';
const { Text } = Typography; const { Text } = Typography;
@@ -34,8 +36,21 @@ const CardPro = ({
bordered = false, bordered = false,
// 自定义样式 // 自定义样式
style, style,
// 国际化函数
t = (key) => key, // 默认函数直接返回key
...props ...props
}) => { }) => {
const isMobile = useIsMobile();
const [showMobileActions, setShowMobileActions] = useState(false);
// 切换移动端操作项显示状态
const toggleMobileActions = () => {
setShowMobileActions(!showMobileActions);
};
// 检查是否有需要在移动端隐藏的内容
const hasMobileHideableContent = actionsArea || searchArea;
// 渲染头部内容 // 渲染头部内容
const renderHeader = () => { const renderHeader = () => {
const hasContent = statsArea || descriptionArea || tabsArea || actionsArea || searchArea; const hasContent = statsArea || descriptionArea || tabsArea || actionsArea || searchArea;
@@ -70,22 +85,42 @@ const CardPro = ({
</> </>
)} )}
{/* 操作按钮和搜索表单的容器 */} {/* 移动端操作切换按钮 */}
<div className="flex flex-col gap-2"> {isMobile && hasMobileHideableContent && (
{/* 操作按钮区域 - 用于type1和type3 */} <>
{(type === 'type1' || type === 'type3') && actionsArea && ( <div className="w-full mb-2">
<div className="w-full"> <Button
{actionsArea} onClick={toggleMobileActions}
icon={showMobileActions ? <IconEyeClosed /> : <IconEyeOpened />}
type="tertiary"
size="small"
block
>
{showMobileActions ? t('隐藏操作项') : t('显示操作项')}
</Button>
</div> </div>
)} </>
)}
{/* 搜索表单区域 - 所有类型都可能有 */} {/* 操作按钮和搜索表单的容器 */}
{searchArea && ( {/* 在移动端时根据showMobileActions状态控制显示在桌面端时始终显示 */}
<div className="w-full"> {(!isMobile || showMobileActions) && (
{searchArea} <div className="flex flex-col gap-2">
</div> {/* 操作按钮区域 - 用于type1和type3 */}
)} {(type === 'type1' || type === 'type3') && actionsArea && (
</div> <div className="w-full">
{actionsArea}
</div>
)}
{/* 搜索表单区域 - 所有类型都可能有 */}
{searchArea && (
<div className="w-full">
{searchArea}
</div>
)}
</div>
)}
</div> </div>
); );
}; };
@@ -122,6 +157,8 @@ CardPro.propTypes = {
searchArea: PropTypes.node, searchArea: PropTypes.node,
// 表格内容 // 表格内容
children: PropTypes.node, children: PropTypes.node,
// 国际化函数
t: PropTypes.func,
}; };
export default CardPro; export default CardPro;

View File

@@ -0,0 +1,49 @@
import React from 'react';
import { Button } from '@douyinfe/semi-ui';
import PropTypes from 'prop-types';
import { useIsMobile } from '../../../hooks/common/useIsMobile';
/**
* 紧凑模式切换按钮组件
* 用于在自适应列表和紧凑列表之间切换
* 在移动端时自动隐藏,因为移动端使用"显示操作项"按钮来控制内容显示
*/
const CompactModeToggle = ({
compactMode,
setCompactMode,
t,
size = 'small',
type = 'tertiary',
className = '',
...props
}) => {
const isMobile = useIsMobile();
// 在移动端隐藏紧凑列表切换按钮
if (isMobile) {
return null;
}
return (
<Button
type={type}
size={size}
className={`w-full md:w-auto ${className}`}
onClick={() => setCompactMode(!compactMode)}
{...props}
>
{compactMode ? t('自适应列表') : t('紧凑列表')}
</Button>
);
};
CompactModeToggle.propTypes = {
compactMode: PropTypes.bool.isRequired,
setCompactMode: PropTypes.func.isRequired,
t: PropTypes.func.isRequired,
size: PropTypes.string,
type: PropTypes.string,
className: PropTypes.string,
};
export default CompactModeToggle;

View File

@@ -7,6 +7,7 @@ import {
Typography, Typography,
Select Select
} from '@douyinfe/semi-ui'; } from '@douyinfe/semi-ui';
import CompactModeToggle from '../../common/ui/CompactModeToggle';
const ChannelsActions = ({ const ChannelsActions = ({
enableBatchDelete, enableBatchDelete,
@@ -150,14 +151,11 @@ const ChannelsActions = ({
</Button> </Button>
</Dropdown> </Dropdown>
<Button <CompactModeToggle
size='small' compactMode={compactMode}
type='tertiary' setCompactMode={setCompactMode}
className="w-full md:w-auto" t={t}
onClick={() => setCompactMode(!compactMode)} />
>
{compactMode ? t('自适应列表') : t('紧凑列表')}
</Button>
</div> </div>
{/* 右侧:设置开关区域 */} {/* 右侧:设置开关区域 */}

View File

@@ -39,6 +39,7 @@ const ChannelsPage = () => {
tabsArea={<ChannelsTabs {...channelsData} />} tabsArea={<ChannelsTabs {...channelsData} />}
actionsArea={<ChannelsActions {...channelsData} />} actionsArea={<ChannelsActions {...channelsData} />}
searchArea={<ChannelsFilters {...channelsData} />} searchArea={<ChannelsFilters {...channelsData} />}
t={channelsData.t}
> >
<ChannelsTable {...channelsData} /> <ChannelsTable {...channelsData} />
</CardPro> </CardPro>

View File

@@ -1,6 +1,7 @@
import React from 'react'; import React from 'react';
import { Button, Skeleton, Typography } from '@douyinfe/semi-ui'; import { Skeleton, Typography } from '@douyinfe/semi-ui';
import { IconEyeOpened } from '@douyinfe/semi-icons'; import { IconEyeOpened } from '@douyinfe/semi-icons';
import CompactModeToggle from '../../common/ui/CompactModeToggle';
const { Text } = Typography; const { Text } = Typography;
@@ -32,14 +33,11 @@ const MjLogsActions = ({
</Text> </Text>
)} )}
</div> </div>
<Button <CompactModeToggle
type='tertiary' compactMode={compactMode}
className="w-full md:w-auto" setCompactMode={setCompactMode}
onClick={() => setCompactMode(!compactMode)} t={t}
size="small" />
>
{compactMode ? t('自适应列表') : t('紧凑列表')}
</Button>
</div> </div>
); );
}; };

View File

@@ -22,6 +22,7 @@ const MjLogsPage = () => {
type="type2" type="type2"
statsArea={<MjLogsActions {...mjLogsData} />} statsArea={<MjLogsActions {...mjLogsData} />}
searchArea={<MjLogsFilters {...mjLogsData} />} searchArea={<MjLogsFilters {...mjLogsData} />}
t={mjLogsData.t}
> >
<MjLogsTable {...mjLogsData} /> <MjLogsTable {...mjLogsData} />
</CardPro> </CardPro>

View File

@@ -1,6 +1,7 @@
import React from 'react'; import React from 'react';
import { Button, Typography } from '@douyinfe/semi-ui'; import { Typography } from '@douyinfe/semi-ui';
import { Ticket } from 'lucide-react'; import { Ticket } from 'lucide-react';
import CompactModeToggle from '../../common/ui/CompactModeToggle';
const { Text } = Typography; const { Text } = Typography;
@@ -12,14 +13,11 @@ const RedemptionsDescription = ({ compactMode, setCompactMode, t }) => {
<Text>{t('兑换码可以批量生成和分发,适合用于推广活动或批量充值。')}</Text> <Text>{t('兑换码可以批量生成和分发,适合用于推广活动或批量充值。')}</Text>
</div> </div>
<Button <CompactModeToggle
type="tertiary" compactMode={compactMode}
className="w-full md:w-auto" setCompactMode={setCompactMode}
onClick={() => setCompactMode(!compactMode)} t={t}
size="small" />
>
{compactMode ? t('自适应列表') : t('紧凑列表')}
</Button>
</div> </div>
); );
}; };

View File

@@ -80,6 +80,7 @@ const RedemptionsPage = () => {
</div> </div>
</div> </div>
} }
t={t}
> >
<RedemptionsTable {...redemptionsData} /> <RedemptionsTable {...redemptionsData} />
</CardPro> </CardPro>

View File

@@ -1,6 +1,7 @@
import React from 'react'; import React from 'react';
import { Button, Typography } from '@douyinfe/semi-ui'; import { Typography } from '@douyinfe/semi-ui';
import { IconEyeOpened } from '@douyinfe/semi-icons'; import { IconEyeOpened } from '@douyinfe/semi-icons';
import CompactModeToggle from '../../common/ui/CompactModeToggle';
const { Text } = Typography; const { Text } = Typography;
@@ -15,14 +16,11 @@ const TaskLogsActions = ({
<IconEyeOpened className="mr-2" /> <IconEyeOpened className="mr-2" />
<Text>{t('任务记录')}</Text> <Text>{t('任务记录')}</Text>
</div> </div>
<Button <CompactModeToggle
type='tertiary' compactMode={compactMode}
className="w-full md:w-auto" setCompactMode={setCompactMode}
onClick={() => setCompactMode(!compactMode)} t={t}
size="small" />
>
{compactMode ? t('自适应列表') : t('紧凑列表')}
</Button>
</div> </div>
); );
}; };

View File

@@ -22,6 +22,7 @@ const TaskLogsPage = () => {
type="type2" type="type2"
statsArea={<TaskLogsActions {...taskLogsData} />} statsArea={<TaskLogsActions {...taskLogsData} />}
searchArea={<TaskLogsFilters {...taskLogsData} />} searchArea={<TaskLogsFilters {...taskLogsData} />}
t={taskLogsData.t}
> >
<TaskLogsTable {...taskLogsData} /> <TaskLogsTable {...taskLogsData} />
</CardPro> </CardPro>

View File

@@ -1,6 +1,7 @@
import React from 'react'; import React from 'react';
import { Button, Typography } from '@douyinfe/semi-ui'; import { Typography } from '@douyinfe/semi-ui';
import { Key } from 'lucide-react'; import { Key } from 'lucide-react';
import CompactModeToggle from '../../common/ui/CompactModeToggle';
const { Text } = Typography; const { Text } = Typography;
@@ -12,14 +13,11 @@ const TokensDescription = ({ compactMode, setCompactMode, t }) => {
<Text>{t('令牌用于API访问认证可以设置额度限制和模型权限。')}</Text> <Text>{t('令牌用于API访问认证可以设置额度限制和模型权限。')}</Text>
</div> </div>
<Button <CompactModeToggle
type="tertiary" compactMode={compactMode}
className="w-full md:w-auto" setCompactMode={setCompactMode}
onClick={() => setCompactMode(!compactMode)} t={t}
size="small" />
>
{compactMode ? t('自适应列表') : t('紧凑列表')}
</Button>
</div> </div>
); );
}; };

View File

@@ -82,6 +82,7 @@ const TokensPage = () => {
</div> </div>
</div> </div>
} }
t={t}
> >
<TokensTable {...tokensData} /> <TokensTable {...tokensData} />
</CardPro> </CardPro>

View File

@@ -1,6 +1,7 @@
import React from 'react'; import React from 'react';
import { Button, Tag, Space, Spin } from '@douyinfe/semi-ui'; import { Tag, Space, Spin } from '@douyinfe/semi-ui';
import { renderQuota } from '../../../helpers'; import { renderQuota } from '../../../helpers';
import CompactModeToggle from '../../common/ui/CompactModeToggle';
const LogsActions = ({ const LogsActions = ({
stat, stat,
@@ -49,14 +50,11 @@ const LogsActions = ({
</Tag> </Tag>
</Space> </Space>
<Button <CompactModeToggle
type='tertiary' compactMode={compactMode}
className="w-full md:w-auto" setCompactMode={setCompactMode}
onClick={() => setCompactMode(!compactMode)} t={t}
size="small" />
>
{compactMode ? t('自适应列表') : t('紧凑列表')}
</Button>
</div> </div>
</Spin> </Spin>
); );

View File

@@ -21,6 +21,7 @@ const LogsPage = () => {
type="type2" type="type2"
statsArea={<LogsActions {...logsData} />} statsArea={<LogsActions {...logsData} />}
searchArea={<LogsFilters {...logsData} />} searchArea={<LogsFilters {...logsData} />}
t={logsData.t}
> >
<LogsTable {...logsData} /> <LogsTable {...logsData} />
</CardPro> </CardPro>

View File

@@ -1,6 +1,7 @@
import React from 'react'; import React from 'react';
import { Button, Typography } from '@douyinfe/semi-ui'; import { Typography } from '@douyinfe/semi-ui';
import { IconUserAdd } from '@douyinfe/semi-icons'; import { IconUserAdd } from '@douyinfe/semi-icons';
import CompactModeToggle from '../../common/ui/CompactModeToggle';
const { Text } = Typography; const { Text } = Typography;
@@ -11,14 +12,11 @@ const UsersDescription = ({ compactMode, setCompactMode, t }) => {
<IconUserAdd className="mr-2" /> <IconUserAdd className="mr-2" />
<Text>{t('用户管理页面,可以查看和管理所有注册用户的信息、权限和状态。')}</Text> <Text>{t('用户管理页面,可以查看和管理所有注册用户的信息、权限和状态。')}</Text>
</div> </div>
<Button <CompactModeToggle
type='tertiary' compactMode={compactMode}
className="w-full md:w-auto" setCompactMode={setCompactMode}
onClick={() => setCompactMode(!compactMode)} t={t}
size="small" />
>
{compactMode ? t('自适应列表') : t('紧凑列表')}
</Button>
</div> </div>
); );
}; };

View File

@@ -85,6 +85,7 @@ const UsersPage = () => {
/> />
</div> </div>
} }
t={t}
> >
<UsersTable {...usersData} /> <UsersTable {...usersData} />
</CardPro> </CardPro>

View File

@@ -1780,5 +1780,7 @@
"启用全部密钥": "Enable all keys", "启用全部密钥": "Enable all keys",
"以充值价格显示": "Show with recharge price", "以充值价格显示": "Show with recharge price",
"美元汇率(非充值汇率,仅用于定价页面换算)": "USD exchange rate (not recharge rate, only used for pricing page conversion)", "美元汇率(非充值汇率,仅用于定价页面换算)": "USD exchange rate (not recharge rate, only used for pricing page conversion)",
"美元汇率": "USD exchange rate" "美元汇率": "USD exchange rate",
"隐藏操作项": "Hide actions",
"显示操作项": "Show actions"
} }