|
| 1 | +/** |
| 2 | + * Machine catalog UUID display, copy actions, and paste-to-link UI. |
| 3 | + */ |
| 4 | + |
| 5 | +import { useCallback, useState } from 'react'; |
| 6 | +import { useTranslation } from 'react-i18next'; |
| 7 | +import { Check, Copy, Loader2 } from 'lucide-react'; |
| 8 | +import { Button } from '@mcpmux/ui'; |
| 9 | +import { |
| 10 | + buildMcpMachineHeaderSnippet, |
| 11 | + copyTextToClipboard, |
| 12 | + isMachineUuid, |
| 13 | +} from '@/lib/machine-id.helpers'; |
| 14 | + |
| 15 | +export interface MachineIdSectionProps { |
| 16 | + machineId: string | null; |
| 17 | + linkMachineIdDraft?: string; |
| 18 | + onLinkMachineIdDraftChange?: (value: string) => void; |
| 19 | + onLink?: (id: string) => Promise<boolean>; |
| 20 | + isLinking?: boolean; |
| 21 | + linkError?: string | null; |
| 22 | + testIdPrefix?: string; |
| 23 | + compact?: boolean; |
| 24 | +} |
| 25 | + |
| 26 | +type CopiedKind = 'uuid' | 'header' | null; |
| 27 | + |
| 28 | +const COPY_FEEDBACK_MS = 1500; |
| 29 | + |
| 30 | +/** |
| 31 | + * Render machine ID with copy actions when linked, or paste-to-link when unlinked. |
| 32 | + */ |
| 33 | +export function MachineIdSection({ |
| 34 | + machineId, |
| 35 | + linkMachineIdDraft = '', |
| 36 | + onLinkMachineIdDraftChange, |
| 37 | + onLink, |
| 38 | + isLinking = false, |
| 39 | + linkError = null, |
| 40 | + testIdPrefix = 'machine-id', |
| 41 | + compact = false, |
| 42 | +}: MachineIdSectionProps) { |
| 43 | + const { t } = useTranslation('common'); |
| 44 | + const [copiedKind, setCopiedKind] = useState<CopiedKind>(null); |
| 45 | + const [localLinkError, setLocalLinkError] = useState<string | null>(null); |
| 46 | + |
| 47 | + const normalizedId = machineId?.trim() || null; |
| 48 | + const prefix = testIdPrefix; |
| 49 | + |
| 50 | + /** |
| 51 | + * Copy text and show brief inline feedback on the triggering button. |
| 52 | + */ |
| 53 | + const handleCopy = useCallback(async (kind: CopiedKind, text: string) => { |
| 54 | + try { |
| 55 | + await copyTextToClipboard(text); |
| 56 | + setCopiedKind(kind); |
| 57 | + setTimeout(() => setCopiedKind(null), COPY_FEEDBACK_MS); |
| 58 | + } catch { |
| 59 | + /* clipboard may be unavailable */ |
| 60 | + } |
| 61 | + }, []); |
| 62 | + |
| 63 | + /** |
| 64 | + * Validate draft and delegate linking to the parent hook. |
| 65 | + */ |
| 66 | + const handleLink = useCallback(async () => { |
| 67 | + const draft = linkMachineIdDraft.trim(); |
| 68 | + if (!isMachineUuid(draft)) { |
| 69 | + setLocalLinkError('invalidId'); |
| 70 | + return; |
| 71 | + } |
| 72 | + setLocalLinkError(null); |
| 73 | + if (!onLink) { |
| 74 | + return; |
| 75 | + } |
| 76 | + const ok = await onLink(draft); |
| 77 | + if (!ok && !linkError) { |
| 78 | + setLocalLinkError('linkFailed'); |
| 79 | + } |
| 80 | + }, [linkError, linkMachineIdDraft, onLink]); |
| 81 | + |
| 82 | + const resolvedLinkError = |
| 83 | + linkError === 'invalidId' |
| 84 | + ? t('viewerIdentity.invalidId') |
| 85 | + : linkError === 'linkNotFound' |
| 86 | + ? t('viewerIdentity.linkNotFound') |
| 87 | + : linkError === 'linkFailed' |
| 88 | + ? t('viewerIdentity.linkFailed') |
| 89 | + : localLinkError === 'invalidId' |
| 90 | + ? t('viewerIdentity.invalidId') |
| 91 | + : localLinkError === 'linkFailed' |
| 92 | + ? t('viewerIdentity.linkFailed') |
| 93 | + : null; |
| 94 | + |
| 95 | + return ( |
| 96 | + <div |
| 97 | + className={[ |
| 98 | + 'space-y-2 rounded-lg border border-[rgb(var(--border-subtle))] bg-[rgb(var(--surface))]', |
| 99 | + compact ? 'p-3' : 'p-4', |
| 100 | + ].join(' ')} |
| 101 | + data-testid={`${prefix}-section`} |
| 102 | + > |
| 103 | + <p className="text-xs font-medium text-[rgb(var(--muted))]">{t('viewerIdentity.idLabel')}</p> |
| 104 | + |
| 105 | + {normalizedId ? ( |
| 106 | + <> |
| 107 | + <code |
| 108 | + className="block break-all rounded-md border border-[rgb(var(--border))] bg-[rgb(var(--background))] px-3 py-2 font-mono text-xs text-[rgb(var(--foreground))]" |
| 109 | + data-testid={`${prefix}-value`} |
| 110 | + > |
| 111 | + {normalizedId} |
| 112 | + </code> |
| 113 | + <div className="flex flex-wrap gap-2"> |
| 114 | + <Button |
| 115 | + variant="secondary" |
| 116 | + size="sm" |
| 117 | + disabled={isLinking} |
| 118 | + onClick={() => void handleCopy('uuid', normalizedId)} |
| 119 | + data-testid={`${prefix}-copy-uuid`} |
| 120 | + > |
| 121 | + {copiedKind === 'uuid' ? ( |
| 122 | + <Check className="mr-2 h-4 w-4" /> |
| 123 | + ) : ( |
| 124 | + <Copy className="mr-2 h-4 w-4" /> |
| 125 | + )} |
| 126 | + {copiedKind === 'uuid' ? t('viewerIdentity.copied') : t('viewerIdentity.copyUuid')} |
| 127 | + </Button> |
| 128 | + <Button |
| 129 | + variant="secondary" |
| 130 | + size="sm" |
| 131 | + disabled={isLinking} |
| 132 | + onClick={() => |
| 133 | + void handleCopy('header', buildMcpMachineHeaderSnippet(normalizedId)) |
| 134 | + } |
| 135 | + data-testid={`${prefix}-copy-header`} |
| 136 | + > |
| 137 | + {copiedKind === 'header' ? ( |
| 138 | + <Check className="mr-2 h-4 w-4" /> |
| 139 | + ) : ( |
| 140 | + <Copy className="mr-2 h-4 w-4" /> |
| 141 | + )} |
| 142 | + {copiedKind === 'header' ? t('viewerIdentity.copied') : t('viewerIdentity.copyHeader')} |
| 143 | + </Button> |
| 144 | + </div> |
| 145 | + </> |
| 146 | + ) : ( |
| 147 | + <> |
| 148 | + <p className="text-xs text-[rgb(var(--muted))]" data-testid={`${prefix}-pending-hint`}> |
| 149 | + {t('viewerIdentity.idPendingHint')} |
| 150 | + </p> |
| 151 | + {onLink ? ( |
| 152 | + <div className="flex flex-col gap-2 sm:flex-row sm:items-center"> |
| 153 | + <input |
| 154 | + type="text" |
| 155 | + value={linkMachineIdDraft} |
| 156 | + onChange={(e) => { |
| 157 | + setLocalLinkError(null); |
| 158 | + onLinkMachineIdDraftChange?.(e.target.value); |
| 159 | + }} |
| 160 | + placeholder={t('viewerIdentity.linkIdPlaceholder')} |
| 161 | + disabled={isLinking} |
| 162 | + className="min-w-0 flex-1 rounded-lg border border-[rgb(var(--border))] bg-[rgb(var(--background))] px-3 py-2 font-mono text-xs" |
| 163 | + data-testid={`${prefix}-link-input`} |
| 164 | + /> |
| 165 | + <Button |
| 166 | + variant="secondary" |
| 167 | + size="sm" |
| 168 | + disabled={isLinking || !linkMachineIdDraft.trim()} |
| 169 | + onClick={() => void handleLink()} |
| 170 | + data-testid={`${prefix}-link-btn`} |
| 171 | + > |
| 172 | + {isLinking ? <Loader2 className="mr-2 h-4 w-4 animate-spin" /> : null} |
| 173 | + {t('viewerIdentity.linkButton')} |
| 174 | + </Button> |
| 175 | + </div> |
| 176 | + ) : null} |
| 177 | + </> |
| 178 | + )} |
| 179 | + |
| 180 | + {resolvedLinkError ? ( |
| 181 | + <p className="text-xs text-red-500" data-testid={`${prefix}-link-error`}> |
| 182 | + {resolvedLinkError} |
| 183 | + </p> |
| 184 | + ) : null} |
| 185 | + </div> |
| 186 | + ); |
| 187 | +} |
0 commit comments