forked from QuantumNous/new-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlobe-icon.tsx
More file actions
175 lines (150 loc) · 4.86 KB
/
Copy pathlobe-icon.tsx
File metadata and controls
175 lines (150 loc) · 4.86 KB
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
/*
Copyright (C) 2023-2026 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
*/
/**
* LobeHub Icon Loader
* Dynamically load and render icons from @lobehub/icons
*
* Supports:
* - Basic: "OpenAI", "OpenAI.Color"
* - Chained properties: "OpenAI.Avatar.type={'platform'}"
* - Size parameter: getLobeIcon("OpenAI", 20)
*/
import * as LobeIcons from '@lobehub/icons'
import type React from 'react'
import { IconSub2api } from '@/assets/custom/icon-sub2api'
const CUSTOM_ICONS: Record<string, React.ComponentType<{ size?: number }>> = {
Sub2API: IconSub2api,
}
/**
* Parse a property value from string to appropriate type
* @param raw - Raw string value
* @returns Parsed value (boolean, number, or string)
*/
function parseValue(raw: string | undefined | null): string | number | boolean {
if (raw == null) return true
let v = String(raw).trim()
// Remove curly braces
if (v.startsWith('{') && v.endsWith('}')) {
v = v.slice(1, -1).trim()
}
// Remove quotes
if (
(v.startsWith('"') && v.endsWith('"')) ||
(v.startsWith("'") && v.endsWith("'"))
) {
return v.slice(1, -1)
}
// Boolean
if (v === 'true') return true
if (v === 'false') return false
// Number
if (/^-?\d+(?:\.\d+)?$/.test(v)) return Number(v)
// Return as string
return v
}
/**
* Get LobeHub icon component by name
* @param iconName - Icon name/description (e.g., "OpenAI", "OpenAI.Color", "Claude.Avatar")
* @param size - Icon size (default: 20)
* @returns Icon component or fallback
*
* @example
* getLobeIcon("OpenAI", 24)
* getLobeIcon("OpenAI.Color", 20)
* getLobeIcon("Claude.Avatar.type={'platform'}", 32)
*/
export function getLobeIcon(
iconName: string | undefined | null,
size: number = 20
): React.ReactNode {
if (!iconName || typeof iconName !== 'string') {
return (
<div
className='bg-muted text-muted-foreground flex items-center justify-center rounded-full text-xs font-medium'
style={{ width: size, height: size }}
>
?
</div>
)
}
const trimmedName = iconName.trim()
if (!trimmedName) {
return (
<div
className='bg-muted text-muted-foreground flex items-center justify-center rounded-full text-xs font-medium'
style={{ width: size, height: size }}
>
?
</div>
)
}
// Parse component path and chained properties
const segments = trimmedName.split('.')
const baseKey = segments[0]
const CustomIcon = CUSTOM_ICONS[baseKey]
if (CustomIcon) {
return <CustomIcon size={size} />
}
const BaseIcon = (LobeIcons as Record<string, unknown>)[baseKey] as
| Record<string, unknown>
| undefined
let IconComponent: React.ComponentType<Record<string, unknown>> | undefined
let propStartIndex: number
if (BaseIcon && segments.length > 1 && BaseIcon[segments[1]]) {
IconComponent = BaseIcon[segments[1]] as React.ComponentType<
Record<string, unknown>
>
propStartIndex = 2
} else {
IconComponent = (LobeIcons as Record<string, unknown>)[baseKey] as
| React.ComponentType<Record<string, unknown>>
| undefined
propStartIndex = segments.length > 1 && /^[A-Z]/.test(segments[1]) ? 2 : 1
}
// Fallback if icon not found
if (
!IconComponent ||
(typeof IconComponent !== 'function' && typeof IconComponent !== 'object')
) {
const firstLetter = trimmedName.charAt(0).toUpperCase()
return (
<div
className='bg-muted text-muted-foreground flex items-center justify-center rounded-full text-xs font-medium'
style={{ width: size, height: size }}
>
{firstLetter}
</div>
)
}
// Parse chained properties (e.g., "type={'platform'}", "shape='square'")
const props: Record<string, string | number | boolean> = {}
for (let i = propStartIndex; i < segments.length; i++) {
const seg = segments[i]
if (!seg) continue
const eqIdx = seg.indexOf('=')
if (eqIdx === -1) {
props[seg.trim()] = true
continue
}
const key = seg.slice(0, eqIdx).trim()
const valRaw = seg.slice(eqIdx + 1).trim()
props[key] = parseValue(valRaw)
}
// Set size if not explicitly specified in the string
if (props.size == null && size != null) {
props.size = size
}
return <IconComponent {...props} />
}