Files
orca/src/shared/network/server-share-address.ts
T
Jinwoo HongandOrca 9675335da3 refactor(settings): unify network-address picker across mobile & server-share (#6715)
* refactor(settings): unify network-address picker across mobile and server-share

The 'Share this Orca server' form (Settings → Runtime Environments) had the
same uneven dropdown + always-visible custom-text-field layout the mobile
pairing screen used to have.

Extract a generic AddressPicker + CustomAddressDialog (validator and copy
injected) and use it from both surfaces:
- Mobile keeps its IPv4 / Tailscale *.ts.net grammar.
- Server-share gets a plain dropdown (incl. 'This computer') plus an
  'Add custom address…' row opening a dialog that accepts host, host:port,
  or a ws(s):// URL (new parseServerShareAddress validator + tests).

Collapses the server form's separate selectedAddress/customAddress state
into one, dropping the side-by-side text field. Muted (non-red) validation
hint, all strings localized (en/es/ja/ko/zh).

Co-authored-by: Orca <help@stably.ai>

* fix(settings): bound the server-share connection-address dropdown width

Removing the side-by-side custom field left the picker as flex-1, which
stretched the trigger across the whole card for a short value like
'This computer (127.0.0.1)'. Give it min-w-[240px] max-w-full so it sizes to
content and only a long custom URL grows it (then truncates within the card).

Co-authored-by: Orca <help@stably.ai>

---------

Co-authored-by: Orca <help@stably.ai>
2026-06-29 01:36:49 -07:00

43 lines
1.5 KiB
TypeScript

// Why: shared validator for the "Share this Orca server" custom address. The
// field accepts a bare host, host:port, or a full ws(s):// URL — looser than
// the mobile pairing grammar because the target is a transport endpoint, not
// just an IP. Kept pure so the renderer and any future caller agree.
export type ParseServerShareAddressResult = { ok: true; value: string } | { ok: false }
const HOST_LABEL = '[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?'
const HOSTNAME = `${HOST_LABEL}(?:\\.${HOST_LABEL})*`
const IPV4 = '(?:\\d{1,3}\\.){3}\\d{1,3}'
const HOST = `(?:${HOSTNAME}|${IPV4})`
const PORT = '[0-9]{1,5}'
const HOST_OR_HOST_PORT = new RegExp(`^${HOST}(?::${PORT})?$`)
export function parseServerShareAddress(input: string): ParseServerShareAddressResult {
const trimmed = input.trim()
if (trimmed === '' || /\s/.test(trimmed)) {
return { ok: false }
}
// Full ws(s):// URL — defer to the URL parser, which validates host/port/path.
if (/^wss?:\/\//i.test(trimmed)) {
try {
const url = new URL(trimmed)
return url.hostname !== '' ? { ok: true, value: trimmed } : { ok: false }
} catch {
return { ok: false }
}
}
// Bare host or host:port. Reject an out-of-range port early.
const match = trimmed.match(HOST_OR_HOST_PORT)
if (!match) {
return { ok: false }
}
const portPart = trimmed.includes(':') ? trimmed.slice(trimmed.lastIndexOf(':') + 1) : null
if (portPart !== null && Number(portPart) > 65535) {
return { ok: false }
}
return { ok: true, value: trimmed }
}