|
| 1 | +import { useState } from 'react' |
| 2 | +import type { ProviderId } from '../../types' |
| 3 | +import { CheckIcon, CopyIcon, CustomSelect, Panel, PROVIDER_IDS, ProviderTag, getProviderLabel } from '../../ui/shared' |
| 4 | + |
| 5 | +function buildEndpoint(baseOrigin: string, provider: ProviderId) { |
| 6 | + return baseOrigin ? `${baseOrigin}/${provider}` : `/${provider}` |
| 7 | +} |
| 8 | + |
| 9 | +function buildRouteEntries(baseOrigin: string, provider: ProviderId) { |
| 10 | + if (provider === 'jina') { |
| 11 | + const jinaBase = buildEndpoint(baseOrigin, provider) |
| 12 | + |
| 13 | + return [ |
| 14 | + { |
| 15 | + id: 'reader', |
| 16 | + label: 'Reader', |
| 17 | + endpoint: `${jinaBase}/r`, |
| 18 | + }, |
| 19 | + { |
| 20 | + id: 'search', |
| 21 | + label: 'Search', |
| 22 | + endpoint: `${jinaBase}/s`, |
| 23 | + }, |
| 24 | + ] |
| 25 | + } |
| 26 | + |
| 27 | + return [ |
| 28 | + { |
| 29 | + id: provider, |
| 30 | + label: getProviderLabel(provider), |
| 31 | + endpoint: buildEndpoint(baseOrigin, provider), |
| 32 | + }, |
| 33 | + ] |
| 34 | +} |
| 35 | + |
| 36 | +export function ProxyEndpointsPanel({ |
| 37 | + baseOrigin, |
| 38 | + copiedId, |
| 39 | + onCopyEndpoint, |
| 40 | + t, |
| 41 | +}: { |
| 42 | + baseOrigin: string |
| 43 | + copiedId: string | null |
| 44 | + onCopyEndpoint: (endpoint: string, copyId: string) => void |
| 45 | + t: (key: string, values?: Record<string, string | number>) => string |
| 46 | +}) { |
| 47 | + const normalizedOrigin = baseOrigin.trim().replace(/\/+$/, '') |
| 48 | + const [selectedProvider, setSelectedProvider] = useState<ProviderId>('firecrawl') |
| 49 | + const description = normalizedOrigin |
| 50 | + ? t('api_keys.endpoints_desc', { origin: normalizedOrigin }) |
| 51 | + : t('api_keys.endpoints_desc_fallback') |
| 52 | + const routeEntries = buildRouteEntries(normalizedOrigin, selectedProvider) |
| 53 | + |
| 54 | + return ( |
| 55 | + <Panel title={t('api_keys.endpoints_title')} description={description}> |
| 56 | + <div className="api-endpoints-toolbar"> |
| 57 | + <div className="api-endpoints-provider"> |
| 58 | + <span className="api-key-hint">{t('api_keys.provider_label')}</span> |
| 59 | + <CustomSelect |
| 60 | + value={selectedProvider} |
| 61 | + onChange={(value) => setSelectedProvider(value as ProviderId)} |
| 62 | + ariaLabel={t('api_keys.provider_label')} |
| 63 | + options={PROVIDER_IDS.map((provider) => ({ |
| 64 | + value: provider, |
| 65 | + label: getProviderLabel(provider), |
| 66 | + }))} |
| 67 | + /> |
| 68 | + </div> |
| 69 | + </div> |
| 70 | + <div className="mini-table api-endpoints-list"> |
| 71 | + {routeEntries.map((entry) => { |
| 72 | + const copyId = `endpoint:${selectedProvider}:${entry.id}` |
| 73 | + |
| 74 | + return ( |
| 75 | + <div key={entry.id} className="mini-row api-endpoint-row"> |
| 76 | + <div className="api-endpoint-meta"> |
| 77 | + <ProviderTag provider={selectedProvider} /> |
| 78 | + <strong>{entry.label}</strong> |
| 79 | + </div> |
| 80 | + <code className="api-endpoint-url">{entry.endpoint}</code> |
| 81 | + <button |
| 82 | + type="button" |
| 83 | + className={`copy-btn copy-btn-compact${copiedId === copyId ? ' is-copied' : ''}`} |
| 84 | + onClick={() => onCopyEndpoint(entry.endpoint, copyId)} |
| 85 | + > |
| 86 | + {copiedId === copyId ? ( |
| 87 | + <> |
| 88 | + <CheckIcon /> |
| 89 | + {t('common.copied')} |
| 90 | + </> |
| 91 | + ) : ( |
| 92 | + <> |
| 93 | + <CopyIcon /> |
| 94 | + {t('common.copy')} |
| 95 | + </> |
| 96 | + )} |
| 97 | + </button> |
| 98 | + </div> |
| 99 | + ) |
| 100 | + })} |
| 101 | + </div> |
| 102 | + <p className="api-key-hint"> |
| 103 | + {selectedProvider === 'jina' ? t('api_keys.endpoints_hint_jina') : t('api_keys.endpoints_hint')} |
| 104 | + </p> |
| 105 | + </Panel> |
| 106 | + ) |
| 107 | +} |
0 commit comments