Jelajahi Sumber

🌟 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)
t0ng7u 7 bulan lalu
induk
melakukan
56c1fbecea

+ 53 - 16
web/src/components/common/ui/CardPro.js

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

+ 49 - 0
web/src/components/common/ui/CompactModeToggle.js

@@ -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; 

+ 6 - 8
web/src/components/table/channels/ChannelsActions.jsx

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

+ 1 - 0
web/src/components/table/channels/index.jsx

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

+ 7 - 9
web/src/components/table/mj-logs/MjLogsActions.jsx

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

+ 1 - 0
web/src/components/table/mj-logs/index.jsx

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

+ 7 - 9
web/src/components/table/redemptions/RedemptionsDescription.jsx

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

+ 1 - 0
web/src/components/table/redemptions/index.jsx

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

+ 7 - 9
web/src/components/table/task-logs/TaskLogsActions.jsx

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

+ 1 - 0
web/src/components/table/task-logs/index.jsx

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

+ 7 - 9
web/src/components/table/tokens/TokensDescription.jsx

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

+ 1 - 0
web/src/components/table/tokens/index.jsx

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

+ 7 - 9
web/src/components/table/usage-logs/UsageLogsActions.jsx

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

+ 1 - 0
web/src/components/table/usage-logs/index.jsx

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

+ 7 - 9
web/src/components/table/users/UsersDescription.jsx

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

+ 1 - 0
web/src/components/table/users/index.jsx

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

+ 3 - 1
web/src/i18n/locales/en.json

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