|
| 1 | +import { Box, Check, Copy } from 'lucide-react' |
| 2 | +import { useMemo } from 'react' |
| 3 | + |
| 4 | +import { ClipboardCopy } from '@/components/ClipboardCopy/ClipboardCopy' |
| 5 | +import { cn } from '@/utils' |
| 6 | +import { EnumSize } from '@/types/enums' |
| 7 | +import { isNilOrEmptyString, truncate } from '@/utils' |
| 8 | +import Tooltip from '@/components/Tooltip/Tooltip' |
| 9 | + |
| 10 | +const MODEL_NAME_TOOLTIP_SIDE_OFFSET = 6 |
| 11 | +const MODEL_NAME_ICON_SIZE = 16 |
| 12 | + |
| 13 | +export function ModelName({ |
| 14 | + name, |
| 15 | + hideCatalog = false, |
| 16 | + hideSchema = false, |
| 17 | + hideIcon = false, |
| 18 | + showTooltip = false, |
| 19 | + showCopy = false, |
| 20 | + truncateMaxChars = 25, |
| 21 | + truncateLimitBefore = 5, |
| 22 | + truncateLimitAfter = 7, |
| 23 | + grayscale = false, |
| 24 | + link, |
| 25 | + className, |
| 26 | +}: { |
| 27 | + name: string |
| 28 | + hideCatalog?: boolean |
| 29 | + hideSchema?: boolean |
| 30 | + hideIcon?: boolean |
| 31 | + showTooltip?: boolean |
| 32 | + showCopy?: boolean |
| 33 | + truncateMaxChars?: number |
| 34 | + truncateLimitBefore?: number |
| 35 | + truncateLimitAfter?: number |
| 36 | + grayscale?: boolean |
| 37 | + link?: string |
| 38 | + className?: string |
| 39 | +}) { |
| 40 | + if (isNilOrEmptyString(name)) |
| 41 | + throw new Error('Model name should not be empty') |
| 42 | + |
| 43 | + const truncateMaxCharsModel = truncateMaxChars * 2 |
| 44 | + |
| 45 | + const { catalog, schema, model, withTooltip } = useMemo(() => { |
| 46 | + const [model, schema, catalog] = name.split('.').reverse() |
| 47 | + |
| 48 | + return { |
| 49 | + catalog: hideCatalog ? undefined : catalog, |
| 50 | + schema: hideSchema ? undefined : schema, |
| 51 | + model, |
| 52 | + withTooltip: |
| 53 | + ((hideCatalog && catalog) || |
| 54 | + (hideSchema && schema) || |
| 55 | + [catalog, schema].some(v => v && v.length > truncateMaxChars) || |
| 56 | + model.length > truncateMaxCharsModel) && |
| 57 | + showTooltip, |
| 58 | + } |
| 59 | + }, [ |
| 60 | + name, |
| 61 | + hideCatalog, |
| 62 | + hideSchema, |
| 63 | + truncateMaxCharsModel, |
| 64 | + showTooltip, |
| 65 | + truncateMaxChars, |
| 66 | + ]) |
| 67 | + |
| 68 | + function renderTooltip() { |
| 69 | + return ( |
| 70 | + <Tooltip |
| 71 | + trigger={renderName()} |
| 72 | + sideOffset={MODEL_NAME_TOOLTIP_SIDE_OFFSET} |
| 73 | + side="top" |
| 74 | + className="text-xs px-2 py-1 rounded-sm font-semibold" |
| 75 | + > |
| 76 | + {name} |
| 77 | + </Tooltip> |
| 78 | + ) |
| 79 | + } |
| 80 | + |
| 81 | + function renderIcon() { |
| 82 | + return ( |
| 83 | + <Box |
| 84 | + size={MODEL_NAME_ICON_SIZE} |
| 85 | + className={cn( |
| 86 | + 'mr-1 flex-shrink-0', |
| 87 | + grayscale |
| 88 | + ? 'text-model-name-grayscale-model' |
| 89 | + : 'text-model-name-model', |
| 90 | + link && '-mt-[4px]', |
| 91 | + )} |
| 92 | + /> |
| 93 | + ) |
| 94 | + } |
| 95 | + |
| 96 | + console.assert(model, 'Model name should not be empty') |
| 97 | + |
| 98 | + function renderName() { |
| 99 | + return ( |
| 100 | + <span |
| 101 | + data-testid="model-name" |
| 102 | + className="overflow-hidden" |
| 103 | + > |
| 104 | + {catalog && ( |
| 105 | + <> |
| 106 | + <span |
| 107 | + className={cn( |
| 108 | + grayscale |
| 109 | + ? 'text-model-name-grayscale-catalog' |
| 110 | + : 'text-model-name-catalog', |
| 111 | + )} |
| 112 | + > |
| 113 | + {_truncate(catalog)} |
| 114 | + </span> |
| 115 | + . |
| 116 | + </> |
| 117 | + )} |
| 118 | + {schema && ( |
| 119 | + <> |
| 120 | + <span |
| 121 | + className={cn( |
| 122 | + grayscale |
| 123 | + ? 'text-model-name-grayscale-schema' |
| 124 | + : 'text-model-name-schema', |
| 125 | + )} |
| 126 | + > |
| 127 | + {_truncate(schema)} |
| 128 | + </span> |
| 129 | + . |
| 130 | + </> |
| 131 | + )} |
| 132 | + <span |
| 133 | + className={cn( |
| 134 | + grayscale |
| 135 | + ? 'text-model-name-grayscale-model' |
| 136 | + : 'text-model-name-model', |
| 137 | + )} |
| 138 | + > |
| 139 | + {truncate(model, truncateMaxCharsModel, 15)} |
| 140 | + </span> |
| 141 | + </span> |
| 142 | + ) |
| 143 | + } |
| 144 | + |
| 145 | + function renderNameWithTooltip() { |
| 146 | + return withTooltip ? renderTooltip() : renderName() |
| 147 | + } |
| 148 | + |
| 149 | + function _truncate(name: string, maxChars: number = truncateMaxChars) { |
| 150 | + return truncate( |
| 151 | + name, |
| 152 | + maxChars, |
| 153 | + truncateLimitBefore, |
| 154 | + '...', |
| 155 | + truncateLimitAfter, |
| 156 | + ) |
| 157 | + } |
| 158 | + |
| 159 | + return ( |
| 160 | + <span |
| 161 | + data-component="ModelName" |
| 162 | + className={cn( |
| 163 | + 'inline-flex items-center whitespace-nowrap overflow-hidden font-semibold', |
| 164 | + className, |
| 165 | + )} |
| 166 | + > |
| 167 | + {!hideIcon && renderIcon()} |
| 168 | + {link ? ( |
| 169 | + <a |
| 170 | + href={link} |
| 171 | + className={cn( |
| 172 | + 'flex cursor-pointer border-b -mt-0.5 text-inherit', |
| 173 | + grayscale |
| 174 | + ? 'border-model-name-grayscale-link hover:border-model-name-grayscale-link-hover' |
| 175 | + : 'border-model-name-link hover:border-model-name-link-hover', |
| 176 | + )} |
| 177 | + > |
| 178 | + {renderNameWithTooltip()} |
| 179 | + </a> |
| 180 | + ) : ( |
| 181 | + renderNameWithTooltip() |
| 182 | + )} |
| 183 | + {showCopy && ( |
| 184 | + <ClipboardCopy |
| 185 | + size={EnumSize.XXS} |
| 186 | + text={name} |
| 187 | + className="ml-2 w-6 hover:text-model-name-copy-icon-hover active:text-model-name-copy-icon-hover" |
| 188 | + > |
| 189 | + {copied => |
| 190 | + copied ? ( |
| 191 | + <Check |
| 192 | + size={MODEL_NAME_ICON_SIZE} |
| 193 | + className="text-model-name-copy-icon" |
| 194 | + /> |
| 195 | + ) : ( |
| 196 | + <Copy |
| 197 | + size={MODEL_NAME_ICON_SIZE} |
| 198 | + className="text-model-name-copy-icon" |
| 199 | + /> |
| 200 | + ) |
| 201 | + } |
| 202 | + </ClipboardCopy> |
| 203 | + )} |
| 204 | + </span> |
| 205 | + ) |
| 206 | +} |
0 commit comments