blob: 76a7019e24fa186a4c10eab1fa6dbee48e447488 [file]
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import { platform, signal, Text, View, type JSX } from 'nsh';
import { Button, Input } from 'nsh/base';
import {
parseServerInviteLink,
type ServerEnrollment,
type ServerInvite,
type ServerUiTheme,
} from './makaServer';
function serverHostLabel(rawUrl: string): string {
try {
return new URL(rawUrl).host || rawUrl;
} catch {
return rawUrl;
}
}
function formatFingerprint(hex: string): string {
const clean = hex.trim().toLowerCase();
return clean.length > 24 ? `${clean.slice(0, 12)}…${clean.slice(-12)}` : clean;
}
export type EnrollmentPrimaryAction = {
label: string;
accessibilityLabel: string;
disabled: boolean;
busy: boolean;
submit(): void;
};
export function ServerEnrollmentForm(props: {
labels(): {
link: string;
manual: string;
scan?: string;
invalidLink: string;
url: string;
invite: string;
pin: string;
fingerprint: string;
join: string;
joining: string;
joinFailed: string;
};
theme(): ServerUiTheme<JSX.Element>;
deviceName: string;
invitation?: ServerInvite;
scan?(): void;
formatError?(cause: unknown): string;
enroll(input: ServerEnrollment): Promise<void>;
renderActions(action: EnrollmentPrimaryAction): JSX.Element;
}) {
const touch = platform.os === 'ios' || platform.os === 'android';
const terminal = platform.idiom === 'terminal';
const link = signal('');
const manual = signal(false);
const url = signal('');
const invite = signal('');
const pin = signal('');
const busy = signal(false);
const failure = signal('');
const canJoin = () =>
!busy() &&
Boolean(props.invitation || (manual() ? url().trim() && invite().trim() : link().trim()));
const inputStyle = () => {
const t = props.theme();
return {
minHeight: touch ? 44 : 32,
paddingHorizontal: 12,
paddingVertical: touch ? 8 : 4,
borderRadius: t.radius(8),
backgroundColor: t.colors.field,
borderWidth: t.lineWidth,
borderColor: t.colors.line,
color: t.colors.text,
placeholderColor: t.colors.muted,
...t.type.body,
};
};
const inputFocusStyle = () => {
const t = props.theme();
return terminal ? { backgroundColor: t.colors.raised } : { borderColor: t.colors.accent };
};
const quietFocusStyle = () => {
const t = props.theme();
return terminal
? { backgroundColor: t.colors.raised }
: {
outlineStyle: 'solid' as const,
outlineWidth: 2,
outlineColor: t.colors.accent,
outlineOffset: -2,
};
};
const fieldLabelStyle = () => {
const t = props.theme();
return {
...t.type.caption,
fontWeight: '500' as const,
color: t.colors.secondary,
};
};
const join = async () => {
if (!canJoin()) return;
failure.set('');
let invitation = props.invitation;
if (!invitation) {
if (manual()) {
invitation = {
serverUrl: url().trim(),
inviteToken: invite().trim(),
certSha256: pin().trim() || undefined,
};
} else {
try {
invitation = parseServerInviteLink(link());
} catch {
failure.set(props.labels().invalidLink);
return;
}
}
}
busy.set(true);
try {
await props.enroll({ ...invitation, deviceName: props.deviceName });
link.set('');
invite.set('');
} catch (cause) {
failure.set(
props.formatError
? props.formatError(cause)
: cause instanceof Error && cause.message.trim()
? cause.message
: props.labels().joinFailed,
);
} finally {
busy.set(false);
}
};
return (
<View style={{ gap: 16 }}>
<If
when={!props.invitation}
else={
<View
style={{
backgroundColor: props.theme().colors.card,
borderRadius: props.theme().radius(12),
padding: 16,
gap: 12,
}}
>
<View style={{ gap: 4 }}>
<Text
style={{
...props.theme().type.title,
color: props.theme().colors.text,
}}
>
{serverHostLabel(props.invitation!.serverUrl)}
</Text>
<Text
style={{
...props.theme().type.secondary,
color: props.theme().colors.secondary,
userSelect: 'text',
}}
>
{props.invitation!.serverUrl}
</Text>
</View>
<If when={props.invitation?.certSha256}>
<View
style={{
borderTopWidth: props.theme().lineWidth,
borderTopColor: props.theme().colors.line,
paddingTop: 12,
gap: 4,
}}
>
<Text style={fieldLabelStyle()}>{props.labels().fingerprint}</Text>
<Text
style={{
...props.theme().type.caption,
fontFamily: 'monospace',
color: props.theme().colors.secondary,
userSelect: 'text',
}}
>
{formatFingerprint(props.invitation!.certSha256!)}
</Text>
</View>
</If>
</View>
}
>
<View style={{ gap: 12 }}>
<View
style={{
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'space-between',
gap: 8,
}}
>
<Button
accessibilityLabel={props.labels().manual}
selected={manual()}
disabled={busy()}
onPress={() => {
const openingManual = !manual();
if (openingManual && link().trim()) {
try {
const parsed = parseServerInviteLink(link());
url.set(parsed.serverUrl);
invite.set(parsed.inviteToken);
pin.set(parsed.certSha256 ?? '');
} catch {
// Leave manual fields untouched if the draft is not a complete invite link.
}
}
manual.set(openingManual);
failure.set('');
}}
style={{
minHeight: touch ? 44 : 32,
paddingHorizontal: 8,
marginLeft: terminal ? 0 : -8,
borderRadius: props.theme().radius(8),
flexDirection: 'row',
alignItems: 'center',
gap: 4,
}}
hoverStyle={{ backgroundColor: props.theme().colors.raised }}
activeStyle={{ backgroundColor: props.theme().colors.pressed }}
focusStyle={quietFocusStyle()}
disabledStyle={{ opacity: 0.4 }}
>
{props.theme().chevron(manual())}
<Text
style={{
...props.theme().type.secondary,
fontWeight: '500',
color: props.theme().colors.secondary,
}}
>
{props.labels().manual}
</Text>
</Button>
<If when={props.scan && !manual()}>
<Button
accessibilityLabel={props.labels().scan}
disabled={busy()}
onPress={props.scan}
style={{
minHeight: touch ? 44 : 32,
paddingHorizontal: 8,
borderRadius: props.theme().radius(8),
justifyContent: 'center',
}}
hoverStyle={{ backgroundColor: props.theme().colors.raised }}
activeStyle={{ backgroundColor: props.theme().colors.pressed }}
focusStyle={quietFocusStyle()}
disabledStyle={{ opacity: 0.4 }}
>
<Text
style={{
...props.theme().type.secondary,
fontWeight: '500',
color: props.theme().colors.accentText,
}}
>
{props.labels().scan}
</Text>
</Button>
</If>
</View>
<If when={!manual()}>
<View style={{ gap: 4 }}>
<Text style={fieldLabelStyle()}>{props.labels().link}</Text>
<Input
accessibilityLabel={props.labels().link}
placeholder="maka://server/v1?…"
autoCapitalize="none"
autoComplete="off"
value={link()}
onChange={({ value }) => link.set(value)}
onSubmit={() => void join()}
style={inputStyle()}
focusStyle={inputFocusStyle()}
/>
</View>
</If>
<If when={manual()}>
<View style={{ gap: 12 }}>
<View style={{ gap: 4 }}>
<Text style={fieldLabelStyle()}>{props.labels().url}</Text>
<Input
accessibilityLabel={props.labels().url}
placeholder="https://server.example"
autoCapitalize="none"
autoComplete="off"
value={url()}
onChange={({ value }) => url.set(value)}
style={inputStyle()}
focusStyle={inputFocusStyle()}
/>
</View>
<View style={{ gap: 4 }}>
<Text style={fieldLabelStyle()}>{props.labels().invite}</Text>
<Input
accessibilityLabel={props.labels().invite}
placeholder={props.labels().invite}
autoCapitalize="none"
masked
autoComplete="off"
value={invite()}
onChange={({ value }) => invite.set(value)}
style={inputStyle()}
focusStyle={inputFocusStyle()}
/>
</View>
<View style={{ gap: 4 }}>
<Text style={fieldLabelStyle()}>{props.labels().pin}</Text>
<Input
accessibilityLabel={props.labels().pin}
placeholder={props.labels().pin}
autoCapitalize="none"
autoComplete="off"
value={pin()}
onChange={({ value }) => pin.set(value)}
onSubmit={() => void join()}
style={inputStyle()}
focusStyle={inputFocusStyle()}
/>
</View>
</View>
</If>
</View>
</If>
<If when={failure()}>
<Text
role="alert"
style={{
...props.theme().type.secondary,
color: props.theme().colors.danger,
}}
>
{failure()}
</Text>
</If>
{props.renderActions({
label: busy() ? props.labels().joining : props.labels().join,
accessibilityLabel: props.labels().join,
disabled: !canJoin(),
busy: busy(),
submit: () => void join(),
})}
</View>
);
}