File size: 9,201 Bytes
4c2a557
 
 
 
 
 
a572854
4c2a557
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
"use client";

import { useState } from "react";
import { Table, TablePaginationConfig, Select } from "antd";
import type { FilterValue } from "antd/es/table/interface";
import type { SorterResult } from "antd/es/table/interface";
import dayjs from "@/lib/dayjs";
import { useTranslation } from "react-i18next";
interface UsageRecord {
  id: number;
  nickname: string;
  use_time: string;
  model_name: string;
  input_tokens: number;
  output_tokens: number;
  cost: number;
  balance_after: number;
}

interface TableParams {
  pagination: TablePaginationConfig;
  sortField?: string;
  sortOrder?: string;
  filters?: Record<string, FilterValue | null>;
}

interface Props {
  loading: boolean;
  records: UsageRecord[];
  tableParams: TableParams;
  models: { model_name: string }[];
  users: { nickname: string }[];
  onTableChange: (
    pagination: TablePaginationConfig,
    filters: Record<string, FilterValue | null>,
    sorter: SorterResult<UsageRecord> | SorterResult<UsageRecord>[]
  ) => void;
}

const MobileCard = ({
  record,
  t,
}: {
  record: UsageRecord;
  t: (key: string) => string;
}) => {
  return (
    <div className="p-4 bg-white rounded-xl border border-gray-100/80 shadow-sm transition-all duration-200 hover:shadow-md hover:border-gray-200/80">
      <div className="flex justify-between items-start mb-4">
        <div className="space-y-1">
          <div className="font-medium text-gray-900">{record.nickname}</div>
          <div className="text-xs text-gray-500 flex items-center gap-1.5">
            <div className="w-1 h-1 rounded-full bg-gray-300" />
            {dayjs(record.use_time).format("YYYY-MM-DD HH:mm:ss")}
          </div>
        </div>
        <div className="text-right">
          <div className="font-medium text-primary">
            ¥{Number(record.cost).toFixed(4)}
          </div>
          <div className="text-xs text-gray-500 mt-1">
            {t("panel.usageDetails.table.balance")}: ¥
            {Number(record.balance_after).toFixed(4)}
          </div>
        </div>
      </div>

      <div className="flex items-center gap-4 bg-gray-50/70 rounded-lg p-3">
        <div className="flex-1 min-w-0">
          <div className="text-xs text-gray-500 mb-1.5">
            {t("panel.usageDetails.table.model")}
          </div>
          <div className="text-sm text-gray-700 font-medium truncate">
            {record.model_name}
          </div>
        </div>
        <div className="shrink-0">
          <div className="text-xs text-gray-500 mb-1.5">Tokens</div>
          <div className="text-sm text-gray-700 font-medium tabular-nums">
            {(record.input_tokens + record.output_tokens).toLocaleString()}
          </div>
        </div>
      </div>
    </div>
  );
};

export default function UsageRecordsTable({
  loading,
  records,
  tableParams,
  models,
  users,
  onTableChange,
}: Props) {
  const { t } = useTranslation("common");

  const [filters, setFilters] = useState<Record<string, FilterValue | null>>(
    tableParams.filters || {}
  );

  const handleFilterChange = (field: string, value: string[] | null) => {
    const newFilters = {
      ...filters,
      [field]: value,
    };
    setFilters(newFilters);
    onTableChange(tableParams.pagination, newFilters, {});
  };

  const columns = [
    {
      title: t("panel.usageDetails.table.user"),
      dataIndex: "nickname",
      key: "nickname",
      width: 120,
      filters: users.map((user) => ({
        text: user.nickname,
        value: user.nickname,
      })),
      filterMode: "menu" as const,
      filtered: filters.nickname ? filters.nickname.length > 0 : false,
      filteredValue: filters.nickname || null,
    },
    {
      title: t("panel.usageDetails.table.time"),
      dataIndex: "use_time",
      key: "use_time",
      width: 180,
      sorter: true,
      render: (time: string) => dayjs(time).format("YYYY-MM-DD HH:mm:ss"),
    },
    {
      title: t("panel.usageDetails.table.model"),
      dataIndex: "model_name",
      key: "model_name",
      width: 150,
      filters: models.map((model) => ({
        text: model.model_name,
        value: model.model_name,
      })),
      filterMode: "menu" as const,
      filtered: filters.model_name ? filters.model_name.length > 0 : false,
      filteredValue: filters.model_name || null,
    },
    {
      title: t("panel.usageDetails.table.tokens"),
      key: "tokens",
      width: 120,
      sorter: true,
      render: (_: unknown, record: UsageRecord) =>
        (record.input_tokens + record.output_tokens).toLocaleString(),
    },
    {
      title: t("panel.usageDetails.table.cost"),
      dataIndex: "cost",
      key: "cost",
      width: 100,
      sorter: true,
      render: (_: unknown, record: UsageRecord) =>
        `${t("common.currency")}${Number(record.cost).toFixed(4)}`,
    },
    {
      title: t("panel.usageDetails.table.balance"),
      dataIndex: "balance_after",
      key: "balance_after",
      width: 100,
      sorter: true,
      render: (_: unknown, record: UsageRecord) =>
        `${t("common.currency")}${Number(record.balance_after).toFixed(2)}`,
    },
  ];

  return (
    <div className="space-y-4">
      <div className="sm:hidden space-y-3">
        <Select
          mode="multiple"
          placeholder={t("panel.usageDetails.table.user")}
          className="w-full"
          value={filters.nickname as string[]}
          onChange={(value) => handleFilterChange("nickname", value)}
          options={users.map((user) => ({
            label: user.nickname,
            value: user.nickname,
          }))}
          maxTagCount="responsive"
        />
        <Select
          mode="multiple"
          placeholder={t("panel.usageDetails.table.model")}
          className="w-full"
          value={filters.model_name as string[]}
          onChange={(value) => handleFilterChange("model_name", value)}
          options={models.map((model) => ({
            label: model.model_name,
            value: model.model_name,
          }))}
          maxTagCount="responsive"
        />
      </div>

      <div className="hidden sm:block">
        <Table
          columns={columns}
          dataSource={records}
          loading={loading}
          onChange={onTableChange}
          pagination={{
            ...tableParams.pagination,
            className: "px-2",
            showTotal: (total) => `${t("common.total")} ${total}`,
            itemRender: (page, type, originalElement) => {
              if (type === "prev") {
                return (
                  <button className="px-2 py-0.5 hover:text-primary">
                    {t("common.prev")}
                  </button>
                );
              }
              if (type === "next") {
                return (
                  <button className="px-2 py-0.5 hover:text-primary">
                    {t("common.next")}
                  </button>
                );
              }
              return originalElement;
            },
          }}
          rowKey="id"
          scroll={{ x: 800 }}
          className="bg-background rounded-md border [&_.ant-table-thead]:bg-muted [&_.ant-table-thead>tr>th]:bg-transparent [&_.ant-table-thead>tr>th]:text-muted-foreground [&_.ant-table-tbody>tr>td]:border-muted [&_.ant-table-tbody>tr:last-child>td]:border-b-0 [&_.ant-table-tbody>tr:hover>td]:bg-muted/50 [&_.ant-pagination]:flex [&_.ant-pagination]:items-center [&_.ant-pagination]:px-2 [&_.ant-pagination]:py-4 [&_.ant-pagination]:border-t [&_.ant-pagination]:border-muted [&_.ant-pagination-item]:border-muted [&_.ant-pagination-item]:bg-transparent [&_.ant-pagination-item]:hover:border-primary [&_.ant-pagination-item]:hover:text-primary [&_.ant-pagination-item-active]:border-primary [&_.ant-pagination-item-active]:text-primary [&_.ant-pagination-item-active]:bg-transparent [&_.ant-pagination-prev]:hover:text-primary [&_.ant-pagination-next]:hover:text-primary [&_.ant-pagination-prev>button]:hover:border-primary [&_.ant-pagination-next>button]:hover:border-primary [&_.ant-pagination-options]:ml-auto [&_.ant-select]:border-muted [&_.ant-select]:hover:border-primary [&_.ant-select-focused]:border-primary"
        />
      </div>

      <div className="sm:hidden space-y-4">
        {loading ? (
          <div className="flex justify-center py-8">
            <div className="w-6 h-6 border-2 border-primary/30 border-t-primary animate-spin rounded-full" />
          </div>
        ) : (
          <>
            <div className="space-y-3">
              {records.map((record) => (
                <MobileCard key={record.id} record={record} t={t} />
              ))}
            </div>
            <Table
              dataSource={[]}
              loading={loading}
              onChange={onTableChange}
              pagination={{
                ...tableParams.pagination,
                size: "small",
                className:
                  "flex justify-center [&_.ant-pagination-options]:hidden",
              }}
              className="[&_.ant-pagination]:!mt-0 [&_.ant-table]:hidden [&_.ant-pagination-item]:!bg-white"
            />
          </>
        )}
      </div>
    </div>
  );
}