|
| 1 | +<script setup lang="ts"> |
| 2 | +const open = defineModel<boolean>('open', { default: false }) |
| 3 | +
|
| 4 | +const { isConnected, isConnecting, npmUser, error, hasOperations, connect, disconnect } = |
| 5 | + useConnector() |
| 6 | +
|
| 7 | +const tokenInput = shallowRef('') |
| 8 | +const portInput = shallowRef('31415') |
| 9 | +const { copied, copy } = useClipboard({ copiedDuring: 2000 }) |
| 10 | +
|
| 11 | +const hasAttemptedConnect = shallowRef(false) |
| 12 | +
|
| 13 | +async function handleConnect() { |
| 14 | + hasAttemptedConnect.value = true |
| 15 | + const port = Number.parseInt(portInput.value, 10) || 31415 |
| 16 | + const success = await connect(tokenInput.value.trim(), port) |
| 17 | + if (success) { |
| 18 | + tokenInput.value = '' |
| 19 | + open.value = false |
| 20 | + } |
| 21 | +} |
| 22 | +
|
| 23 | +function handleDisconnect() { |
| 24 | + disconnect() |
| 25 | +} |
| 26 | +
|
| 27 | +function copyCommand() { |
| 28 | + let command = executeNpmxConnectorCommand.value |
| 29 | + if (portInput.value !== '31415') { |
| 30 | + command += ` --port ${portInput.value}` |
| 31 | + } |
| 32 | + copy(command) |
| 33 | +} |
| 34 | +
|
| 35 | +const selectedPM = useSelectedPackageManager() |
| 36 | +
|
| 37 | +const executeNpmxConnectorCommand = computed(() => { |
| 38 | + return getExecuteCommand({ |
| 39 | + packageName: 'npmx-connector', |
| 40 | + packageManager: selectedPM.value, |
| 41 | + }) |
| 42 | +}) |
| 43 | +
|
| 44 | +// Reset form when modal opens |
| 45 | +watch(open, isOpen => { |
| 46 | + if (isOpen) { |
| 47 | + tokenInput.value = '' |
| 48 | + hasAttemptedConnect.value = false |
| 49 | + } |
| 50 | +}) |
| 51 | +</script> |
| 52 | + |
| 53 | +<template> |
| 54 | + <Teleport to="body"> |
| 55 | + <Transition |
| 56 | + enter-active-class="transition-opacity duration-200" |
| 57 | + leave-active-class="transition-opacity duration-200" |
| 58 | + enter-from-class="opacity-0" |
| 59 | + leave-to-class="opacity-0" |
| 60 | + > |
| 61 | + <div v-if="open" class="fixed inset-0 z-50 flex items-center justify-center p-4"> |
| 62 | + <!-- Backdrop --> |
| 63 | + <button |
| 64 | + type="button" |
| 65 | + class="absolute inset-0 bg-black/60 cursor-default" |
| 66 | + :aria-label="$t('common.close_modal')" |
| 67 | + @click="open = false" |
| 68 | + /> |
| 69 | + |
| 70 | + <!-- Modal --> |
| 71 | + <div |
| 72 | + class="relative w-full bg-bg border border-border rounded-lg shadow-xl max-h-[90vh] overflow-y-auto overscroll-contain" |
| 73 | + :class="isConnected && hasOperations ? 'max-w-2xl' : 'max-w-md'" |
| 74 | + role="dialog" |
| 75 | + aria-modal="true" |
| 76 | + aria-labelledby="connector-modal-title" |
| 77 | + > |
| 78 | + <div class="p-6"> |
| 79 | + <div class="flex items-center justify-between mb-6"> |
| 80 | + <h2 id="connector-modal-title" class="font-mono text-lg font-medium"> |
| 81 | + {{ $t('connector.modal.title') }} |
| 82 | + </h2> |
| 83 | + <button |
| 84 | + type="button" |
| 85 | + class="text-fg-subtle hover:text-fg transition-colors duration-200 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-fg/50 rounded" |
| 86 | + :aria-label="$t('common.close')" |
| 87 | + @click="open = false" |
| 88 | + > |
| 89 | + <span class="i-carbon:close block w-5 h-5" aria-hidden="true" /> |
| 90 | + </button> |
| 91 | + </div> |
| 92 | + |
| 93 | + <!-- Connected state --> |
| 94 | + <div v-if="isConnected" class="space-y-4"> |
| 95 | + <div class="flex items-center gap-3 p-4 bg-bg-subtle border border-border rounded-lg"> |
| 96 | + <span class="w-3 h-3 rounded-full bg-green-500" aria-hidden="true" /> |
| 97 | + <div> |
| 98 | + <p class="font-mono text-sm text-fg">{{ $t('connector.modal.connected') }}</p> |
| 99 | + <p v-if="npmUser" class="font-mono text-xs text-fg-muted"> |
| 100 | + {{ $t('connector.modal.connected_as_user', { user: npmUser }) }} |
| 101 | + </p> |
| 102 | + </div> |
| 103 | + </div> |
| 104 | + |
| 105 | + <!-- Operations Queue --> |
| 106 | + <OperationsQueue /> |
| 107 | + |
| 108 | + <div v-if="!hasOperations" class="text-sm text-fg-muted"> |
| 109 | + {{ $t('connector.modal.connected_hint') }} |
| 110 | + </div> |
| 111 | + |
| 112 | + <button |
| 113 | + type="button" |
| 114 | + class="w-full px-4 py-2 font-mono text-sm text-fg-muted bg-bg-subtle border border-border rounded-md transition-colors duration-200 hover:text-fg hover:border-border-hover focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-fg/50" |
| 115 | + @click="handleDisconnect" |
| 116 | + > |
| 117 | + {{ $t('connector.modal.disconnect') }} |
| 118 | + </button> |
| 119 | + </div> |
| 120 | + |
| 121 | + <!-- Disconnected state --> |
| 122 | + <form v-else class="space-y-4" @submit.prevent="handleConnect"> |
| 123 | + <!-- Contributor-only notice --> |
| 124 | + <div class="p-3 bg-amber-500/10 border border-amber-500/30 rounded-lg"> |
| 125 | + <div class="space-y-2"> |
| 126 | + <span |
| 127 | + class="inline-block px-2 py-0.5 text-xs font-bold uppercase tracking-wider bg-amber-500/20 text-amber-400 rounded" |
| 128 | + > |
| 129 | + {{ $t('connector.modal.contributor_badge') }} |
| 130 | + </span> |
| 131 | + <p class="text-sm text-fg-muted"> |
| 132 | + <i18n-t keypath="connector.modal.contributor_notice"> |
| 133 | + <template #link> |
| 134 | + <a |
| 135 | + href="https://github.com/npmx-dev/npmx.dev/blob/main/CONTRIBUTING.md#local-connector-cli" |
| 136 | + target="_blank" |
| 137 | + rel="noopener noreferrer" |
| 138 | + class="text-amber-400 hover:underline" |
| 139 | + > |
| 140 | + {{ $t('connector.modal.contributor_link') }} |
| 141 | + </a> |
| 142 | + </template> |
| 143 | + </i18n-t> |
| 144 | + </p> |
| 145 | + </div> |
| 146 | + </div> |
| 147 | + |
| 148 | + <p class="text-sm text-fg-muted"> |
| 149 | + {{ $t('connector.modal.run_hint') }} |
| 150 | + </p> |
| 151 | + |
| 152 | + <div |
| 153 | + class="flex items-center p-3 bg-bg-muted border border-border rounded-lg font-mono text-sm" |
| 154 | + > |
| 155 | + <span class="text-fg-subtle">$</span> |
| 156 | + <span class="text-fg-subtle ms-2">pnpm npmx-connector</span> |
| 157 | + <button |
| 158 | + type="button" |
| 159 | + :aria-label=" |
| 160 | + copied ? $t('connector.modal.copied') : $t('connector.modal.copy_command') |
| 161 | + " |
| 162 | + class="ms-auto text-fg-subtle hover:text-fg transition-colors duration-200 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-fg/50 rounded" |
| 163 | + @click="copy('pnpm npmx-connector')" |
| 164 | + > |
| 165 | + <span v-if="!copied" class="i-carbon:copy block w-5 h-5" aria-hidden="true" /> |
| 166 | + <span |
| 167 | + v-else |
| 168 | + class="i-carbon:checkmark block w-5 h-5 text-green-500" |
| 169 | + aria-hidden="true" |
| 170 | + /> |
| 171 | + </button> |
| 172 | + </div> |
| 173 | + |
| 174 | + <!-- TODO: Uncomment when npmx-connector is published to npm |
| 175 | + <div |
| 176 | + class="flex items-center p-3 bg-bg-muted border border-border rounded-lg font-mono text-sm" |
| 177 | + > |
| 178 | + <span class="text-fg-subtle">$</span> |
| 179 | + <span class="text-fg-subtle ms-2">{{ executeNpmxConnectorCommand }}</span> |
| 180 | + <div class="ms-auto flex items-center gap-2"> |
| 181 | + <PackageManagerSelect /> |
| 182 | +
|
| 183 | + <button |
| 184 | + type="button" |
| 185 | + :aria-label=" |
| 186 | + copied ? $t('connector.modal.copied') : $t('connector.modal.copy_command') |
| 187 | + " |
| 188 | + class="ms-auto text-fg-subtle hover:text-fg transition-colors duration-200 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-fg/50 rounded" |
| 189 | + @click="copyCommand" |
| 190 | + > |
| 191 | + <span v-if="!copied" class="i-carbon:copy block w-5 h-5" aria-hidden="true" /> |
| 192 | + <span |
| 193 | + v-else |
| 194 | + class="i-carbon:checkmark block w-5 h-5 text-green-500" |
| 195 | + aria-hidden="true" |
| 196 | + /> |
| 197 | + </button> |
| 198 | + </div> |
| 199 | + </div> |
| 200 | + --> |
| 201 | + |
| 202 | + <p class="text-sm text-fg-muted">{{ $t('connector.modal.paste_token') }}</p> |
| 203 | + |
| 204 | + <div class="space-y-3"> |
| 205 | + <div> |
| 206 | + <label |
| 207 | + for="connector-token" |
| 208 | + class="block text-xs text-fg-subtle uppercase tracking-wider mb-1.5" |
| 209 | + > |
| 210 | + {{ $t('connector.modal.token_label') }} |
| 211 | + </label> |
| 212 | + <input |
| 213 | + id="connector-token" |
| 214 | + v-model="tokenInput" |
| 215 | + type="password" |
| 216 | + name="connector-token" |
| 217 | + :placeholder="$t('connector.modal.token_placeholder')" |
| 218 | + v-bind="noCorrect" |
| 219 | + class="w-full px-3 py-2 font-mono text-sm bg-bg-subtle border border-border rounded-md text-fg placeholder:text-fg-subtle transition-colors duration-200 focus:border-border-hover focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-accent/50" |
| 220 | + /> |
| 221 | + </div> |
| 222 | + |
| 223 | + <details class="text-sm"> |
| 224 | + <summary |
| 225 | + class="text-fg-subtle cursor-pointer hover:text-fg-muted transition-colors duration-200" |
| 226 | + > |
| 227 | + {{ $t('connector.modal.advanced') }} |
| 228 | + </summary> |
| 229 | + <div class="mt-3"> |
| 230 | + <label |
| 231 | + for="connector-port" |
| 232 | + class="block text-xs text-fg-subtle uppercase tracking-wider mb-1.5" |
| 233 | + > |
| 234 | + {{ $t('connector.modal.port_label') }} |
| 235 | + </label> |
| 236 | + <input |
| 237 | + id="connector-port" |
| 238 | + v-model="portInput" |
| 239 | + type="text" |
| 240 | + name="connector-port" |
| 241 | + inputmode="numeric" |
| 242 | + autocomplete="off" |
| 243 | + class="w-full px-3 py-2 font-mono text-sm bg-bg-subtle border border-border rounded-md text-fg transition-colors duration-200 focus:border-border-hover focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-accent/50" |
| 244 | + /> |
| 245 | + </div> |
| 246 | + </details> |
| 247 | + </div> |
| 248 | + |
| 249 | + <!-- Error message (only show after user explicitly clicks Connect) --> |
| 250 | + <div |
| 251 | + v-if="error && hasAttemptedConnect" |
| 252 | + role="alert" |
| 253 | + class="p-3 text-sm text-red-400 bg-red-500/10 border border-red-500/20 rounded-md" |
| 254 | + > |
| 255 | + {{ error }} |
| 256 | + </div> |
| 257 | + |
| 258 | + <!-- Warning message --> |
| 259 | + <div |
| 260 | + role="alert" |
| 261 | + class="p-3 text-sm text-red-400 bg-red-500/10 border border-red-500/20 rounded-md" |
| 262 | + > |
| 263 | + <p class="font-mono text-sm text-fg font-bold"> |
| 264 | + {{ $t('connector.modal.warning') }} |
| 265 | + </p> |
| 266 | + <p class="text-sm text-fg-muted"> |
| 267 | + {{ $t('connector.modal.warning_text') }} |
| 268 | + </p> |
| 269 | + </div> |
| 270 | + |
| 271 | + <button |
| 272 | + type="submit" |
| 273 | + :disabled="!tokenInput.trim() || isConnecting" |
| 274 | + class="w-full px-4 py-2 font-mono text-sm text-bg bg-fg rounded-md transition-all duration-200 hover:bg-fg/90 disabled:opacity-50 disabled:cursor-not-allowed focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-fg/50 focus-visible:ring-offset-2 focus-visible:ring-offset-bg" |
| 275 | + > |
| 276 | + {{ |
| 277 | + isConnecting ? $t('connector.modal.connecting') : $t('connector.modal.connect') |
| 278 | + }} |
| 279 | + </button> |
| 280 | + </form> |
| 281 | + </div> |
| 282 | + </div> |
| 283 | + </div> |
| 284 | + </Transition> |
| 285 | + </Teleport> |
| 286 | +</template> |
0 commit comments