-
Notifications
You must be signed in to change notification settings - Fork 374
Expand file tree
/
Copy pathModelName.tsx
More file actions
225 lines (209 loc) · 5.85 KB
/
ModelName.tsx
File metadata and controls
225 lines (209 loc) · 5.85 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
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
import { Box, Check, Copy } from 'lucide-react'
import { useMemo } from 'react'
import { cn, truncate } from '@/utils'
import { Tooltip } from '@/components/Tooltip/Tooltip'
import React from 'react'
import './ModelName.css'
import { CopyButton } from '../CopyButton/CopyButton'
export interface ModelNameProps extends React.HTMLAttributes<HTMLDivElement> {
name: string
hideCatalog?: boolean
hideSchema?: boolean
hideIcon?: boolean
showTooltip?: boolean
showCopy?: boolean
truncateMaxChars?: number
truncateLimitBefore?: number
truncateLimitAfter?: number
grayscale?: boolean
renderLink?: (modelName: React.ReactNode) => React.ReactNode
className?: string
}
const MODEL_NAME_TOOLTIP_SIDE_OFFSET = 6
const MODEL_NAME_ICON_SIZE = 16
export const ModelName = React.forwardRef<HTMLDivElement, ModelNameProps>(
(
{
name,
hideCatalog = false,
hideSchema = false,
hideIcon = false,
showTooltip = true,
showCopy = false,
truncateMaxChars = 25,
truncateLimitBefore = 5,
truncateLimitAfter = 7,
grayscale = false,
renderLink,
className,
...props
},
ref,
) => {
if (!name) throw new Error('Model name should not be empty')
const truncateMaxCharsModel = truncateMaxChars * 2
const { catalog, schema, model, withTooltip } = useMemo(() => {
const [model, schema, catalog] = name.split('.').reverse()
return {
catalog: hideCatalog ? undefined : catalog,
schema: hideSchema ? undefined : schema,
model,
withTooltip:
((hideCatalog && catalog) ||
(hideSchema && schema) ||
[catalog, schema].some(v => v && v.length > truncateMaxChars) ||
model.length > truncateMaxCharsModel) &&
showTooltip,
}
}, [
name,
hideCatalog,
hideSchema,
truncateMaxCharsModel,
showTooltip,
truncateMaxChars,
])
function renderTooltip() {
return (
<Tooltip
trigger={renderName()}
sideOffset={MODEL_NAME_TOOLTIP_SIDE_OFFSET}
side="top"
className="text-xs px-2 py-1 rounded-sm font-semibold"
>
{name}
</Tooltip>
)
}
function renderIcon() {
return (
<Box
size={MODEL_NAME_ICON_SIZE}
className={cn(
'mr-1 flex-shrink-0',
grayscale
? 'text-model-name-grayscale-model'
: 'text-model-name-model',
renderLink && '-mt-[4px]',
)}
/>
)
}
console.assert(name.length > 0, 'Model name should not be empty')
function renderName() {
return (
<span
data-testid="model-name"
className="flex overflow-hidden"
>
{catalog && (
<>
<span
className={cn(
grayscale
? 'text-model-name-grayscale-catalog'
: 'text-model-name-catalog',
)}
>
{_truncate(catalog)}
</span>
.
</>
)}
{schema && (
<>
<span
className={cn(
grayscale
? 'text-model-name-grayscale-schema'
: 'text-model-name-schema',
)}
>
{_truncate(schema)}
</span>
.
</>
)}
<span
className={cn(
'truncate',
grayscale
? 'text-model-name-grayscale-model'
: 'text-model-name-model',
)}
>
{truncate(
model,
truncateMaxCharsModel,
truncateLimitBefore * 2,
'...',
truncateLimitBefore * 2,
)}
</span>
</span>
)
}
function renderNameWithTooltip() {
return withTooltip ? renderTooltip() : renderName()
}
function _truncate(name: string, maxChars: number = truncateMaxChars) {
return truncate(
name,
maxChars,
truncateLimitBefore,
'...',
truncateLimitAfter,
)
}
return (
<span
ref={ref}
data-component="ModelName"
className={cn(
'inline-flex items-center whitespace-nowrap overflow-hidden font-semibold',
className,
)}
{...props}
>
{!hideIcon && renderIcon()}
{renderLink ? (
<span
className={cn(
'flex cursor-pointer border-b -mt-0.5 text-inherit',
grayscale
? 'border-model-name-grayscale-link-underline hover:border-model-name-grayscale-link-underline-hover'
: 'border-model-name-link-underline hover:border-model-name-link-underline-hover',
)}
>
{renderLink(renderNameWithTooltip())}
</span>
) : (
renderNameWithTooltip()
)}
{showCopy && (
<CopyButton
size="2xs"
variant="transparent"
text={name}
className="ml-2 w-6 hover:text-model-name-copy-icon-hover active:text-model-name-copy-icon-hover bg-model-name-copy-icon-background hover:bg-model-name-copy-icon-background-hover active:bg-model-name-copy-icon-background-hover"
>
{copied =>
copied ? (
<Check
size={MODEL_NAME_ICON_SIZE}
className="text-model-name-copy-icon"
/>
) : (
<Copy
size={MODEL_NAME_ICON_SIZE}
className="text-model-name-copy-icon"
/>
)
}
</CopyButton>
)}
</span>
)
},
)
ModelName.displayName = 'ModelName'