File size: 10,417 Bytes
4ef3a0e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*

Copyright (C) 2025 QuantumNous



This program is free software: you can redistribute it and/or modify

it under the terms of the GNU Affero General Public License as

published by the Free Software Foundation, either version 3 of the

License, or (at your option) any later version.



This program is distributed in the hope that it will be useful,

but WITHOUT ANY WARRANTY; without even the implied warranty of

MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the

GNU Affero General Public License for more details.



You should have received a copy of the GNU Affero General Public License

along with this program. If not, see <https://www.gnu.org/licenses/>.



For commercial licensing, please contact support@quantumnous.com

*/

import React, { useState, useEffect } from 'react';
import {
  Empty,
  Skeleton,
  Space,
  Tag,
  Collapsible,
  Tabs,
  TabPane,
  Typography,
  Avatar,
} from '@douyinfe/semi-ui';
import {
  IllustrationNoContent,
  IllustrationNoContentDark,
} from '@douyinfe/semi-illustrations';
import { IconChevronDown, IconChevronUp } from '@douyinfe/semi-icons';
import { Settings } from 'lucide-react';
import { renderModelTag, getModelCategories } from '../../../../helpers';

const ModelsList = ({ t, models, modelsLoading, copyText }) => {
  const [isModelsExpanded, setIsModelsExpanded] = useState(() => {
    // Initialize from localStorage if available
    const savedState = localStorage.getItem('modelsExpanded');
    return savedState ? JSON.parse(savedState) : false;
  });
  const [activeModelCategory, setActiveModelCategory] = useState('all');
  const MODELS_DISPLAY_COUNT = 25; // 默认显示的模型数量

  // Save models expanded state to localStorage whenever it changes
  useEffect(() => {
    localStorage.setItem('modelsExpanded', JSON.stringify(isModelsExpanded));
  }, [isModelsExpanded]);

  return (
    <div className='py-4'>

      {/* 卡片头部 */}

      <div className='flex items-center mb-4'>

        <Avatar size='small' color='green' className='mr-3 shadow-md'>

          <Settings size={16} />

        </Avatar>

        <div>

          <Typography.Text className='text-lg font-medium'>

            {t('可用模型')}

          </Typography.Text>

          <div className='text-xs text-gray-600'>

            {t('查看当前可用的所有模型')}

          </div>

        </div>

      </div>



      {/* 可用模型部分 */}

      <div className='bg-gray-50 dark:bg-gray-800 rounded-xl'>

        {modelsLoading ? (

          // 骨架屏加载状态 - 模拟实际加载后的布局

          <div className='space-y-4'>

            {/* 模拟分类标签 */}

            <div

              className='mb-4'

              style={{ borderBottom: '1px solid var(--semi-color-border)' }}

            >

              <div className='flex overflow-x-auto py-2 gap-2'>

                {Array.from({ length: 8 }).map((_, index) => (

                  <Skeleton.Button

                    key={`cat-${index}`}

                    style={{

                      width: index === 0 ? 130 : 100 + Math.random() * 50,

                      height: 36,

                      borderRadius: 8,

                    }}

                  />

                ))}

              </div>

            </div>



            {/* 模拟模型标签列表 */}

            <div className='flex flex-wrap gap-2'>

              {Array.from({ length: 20 }).map((_, index) => (

                <Skeleton.Button

                  key={`model-${index}`}

                  style={{

                    width: 100 + Math.random() * 100,

                    height: 32,

                    borderRadius: 16,

                    margin: '4px',

                  }}

                />

              ))}

            </div>

          </div>

        ) : models.length === 0 ? (

          <div className='py-8'>

            <Empty

              image={

                <IllustrationNoContent style={{ width: 150, height: 150 }} />

              }

              darkModeImage={

                <IllustrationNoContentDark

                  style={{ width: 150, height: 150 }}

                />

              }

              description={t('没有可用模型')}

              style={{ padding: '24px 0' }}

            />

          </div>

        ) : (

          <>

            {/* 模型分类标签页 */}

            <div className='mb-4'>

              <Tabs

                type='card'

                activeKey={activeModelCategory}

                onChange={(key) => setActiveModelCategory(key)}

                className='mt-2'

                collapsible

              >

                {Object.entries(getModelCategories(t)).map(

                  ([key, category]) => {

                    // 计算该分类下的模型数量

                    const modelCount =

                      key === 'all'

                        ? models.length

                        : models.filter((model) =>

                            category.filter({ model_name: model }),

                          ).length;



                    if (modelCount === 0 && key !== 'all') return null;



                    return (

                      <TabPane

                        tab={

                          <span className='flex items-center gap-2'>

                            {category.icon && (

                              <span className='w-4 h-4'>{category.icon}</span>

                            )}

                            {category.label}

                            <Tag

                              color={

                                activeModelCategory === key ? 'red' : 'grey'

                              }

                              size='small'

                              shape='circle'

                            >

                              {modelCount}

                            </Tag>

                          </span>

                        }

                        itemKey={key}

                        key={key}

                      />

                    );

                  },

                )}

              </Tabs>

            </div>



            <div className='bg-white dark:bg-gray-700 rounded-lg p-3'>

              {(() => {

                // 根据当前选中的分类过滤模型

                const categories = getModelCategories(t);

                const filteredModels =

                  activeModelCategory === 'all'

                    ? models

                    : models.filter((model) =>

                        categories[activeModelCategory].filter({

                          model_name: model,

                        }),

                      );



                // 如果过滤后没有模型,显示空状态

                if (filteredModels.length === 0) {

                  return (

                    <Empty

                      image={

                        <IllustrationNoContent

                          style={{ width: 120, height: 120 }}

                        />

                      }

                      darkModeImage={

                        <IllustrationNoContentDark

                          style={{ width: 120, height: 120 }}

                        />

                      }

                      description={t('该分类下没有可用模型')}

                      style={{ padding: '16px 0' }}

                    />

                  );

                }



                if (filteredModels.length <= MODELS_DISPLAY_COUNT) {

                  return (

                    <Space wrap>

                      {filteredModels.map((model) =>

                        renderModelTag(model, {

                          size: 'small',

                          shape: 'circle',

                          onClick: () => copyText(model),

                        }),

                      )}

                    </Space>

                  );

                } else {

                  return (

                    <>

                      <Collapsible isOpen={isModelsExpanded}>

                        <Space wrap>

                          {filteredModels.map((model) =>

                            renderModelTag(model, {

                              size: 'small',

                              shape: 'circle',

                              onClick: () => copyText(model),

                            }),

                          )}

                          <Tag

                            color='grey'

                            type='light'

                            className='cursor-pointer !rounded-lg'

                            onClick={() => setIsModelsExpanded(false)}

                            icon={<IconChevronUp />}

                          >

                            {t('收起')}

                          </Tag>

                        </Space>

                      </Collapsible>

                      {!isModelsExpanded && (

                        <Space wrap>

                          {filteredModels

                            .slice(0, MODELS_DISPLAY_COUNT)

                            .map((model) =>

                              renderModelTag(model, {

                                size: 'small',

                                shape: 'circle',

                                onClick: () => copyText(model),

                              }),

                            )}

                          <Tag

                            color='grey'

                            type='light'

                            className='cursor-pointer !rounded-lg'

                            onClick={() => setIsModelsExpanded(true)}

                            icon={<IconChevronDown />}

                          >

                            {t('更多')}{' '}

                            {filteredModels.length - MODELS_DISPLAY_COUNT}{' '}

                            {t('个模型')}

                          </Tag>

                        </Space>

                      )}

                    </>

                  );

                }

              })()}

            </div>

          </>
        )}
      </div>
    </div>
  );
};

export default ModelsList;