/** @jsx h */ import { Component, Fragment, h } from "https://esm.sh/preact@10.17.1"; import { CenterClass, inputBorderClass, InputClass, LinearGradientsClass, NoOutlineClass, } from "./components/tw.ts"; import KeyView from "./key-view.tsx"; import { DividerBackgroundColor, ErrorColor, HoverButtonBackgroudColor, PrimaryTextColor, SecondaryBackgroundColor, SuccessColor, TitleIconColor, WarnColor, } from "./style/colors.ts"; import { RelayIcon } from "./icons/relay-icon.tsx"; import { DeleteIcon } from "./icons/delete-icon.tsx"; import { RelayConfig, RemoveBlowaterRelay } from "./relay-config.ts"; import { ConnectionPool } from "../../libs/nostr.ts/relay-pool.ts"; import { emitFunc } from "../event-bus.ts"; import { sleep } from "https://raw.githubusercontent.com/BlowaterNostr/csp/master/csp.ts"; import { blowater } from "../../libs/nostr.ts/relay-list.test.ts"; import { InMemoryAccountContext, NostrAccountContext } from "../../libs/nostr.ts/nostr.ts"; import { PrivateKey } from "../../libs/nostr.ts/key.ts"; export interface SettingProps { logout: () => void; relayConfig: RelayConfig; relayPool: ConnectionPool; myAccountContext: NostrAccountContext; emit: emitFunc; show: boolean; } const colors = { "Connecting": WarnColor, "Open": SuccessColor, "Closing": WarnColor, "Closed": ErrorColor, }; export const Setting = (props: SettingProps) => { let priKey: PrivateKey | undefined; const ctx = props.myAccountContext; if (ctx instanceof InMemoryAccountContext) { priKey = ctx.privateKey; } if (props.show == false) { return undefined; } return (
); }; export type RelayConfigChange = { type: "RelayConfigChange"; kind: "add" | "remove"; url: string; }; export type ViewRelayDetail = { type: "ViewRelayDetail"; url: string; }; type RelaySettingProp = { relayConfig: RelayConfig; relayPool: ConnectionPool; emit: emitFunc; }; type RelaySettingState = { error: string; addRelayInput: string; relayStatus: { url: string; status: keyof typeof colors }[]; }; export class RelaySetting extends Component { state: Readonly = { error: "", addRelayInput: "", relayStatus: [], }; private exit = false; async componentDidMount() { while (this.exit == false) { await sleep(333); const status = this.computeRelayStatus(this.props); this.setState({ relayStatus: status, }); } } componentWillUnmount(): void { this.exit == true; } computeRelayStatus(props: RelaySettingProp) { const _relayStatus: { url: string; status: keyof typeof colors }[] = []; for (const url of props.relayConfig.getRelayURLs()) { const relay = props.relayPool.getRelay(url); let status: keyof typeof colors = "Closed"; if (relay) { status = relay.status(); } _relayStatus.push({ url, status, }); } return _relayStatus; } showRelayDetail = (url: string) => { this.props.emit({ type: "ViewRelayDetail", url: url, }); }; render(props: RelaySettingProp) { const addRelayInput = this.state.addRelayInput; const relayStatus = this.computeRelayStatus(props); const addRelay = async () => { // props.eventBus.emit({ type: "AddRelay" }); console.log("add", addRelayInput); if (addRelayInput.length > 0) { const p = props.relayConfig.add(addRelayInput); this.setState({ addRelayInput: "", relayStatus: this.computeRelayStatus(props), }); const relay = await p; if (relay instanceof Error) { console.error(relay); return; } props.emit({ type: "RelayConfigChange", kind: "add", url: relay.url, }); } }; return (

Relays

Add Relay

this.setState({ addRelayInput: e.currentTarget.value })} value={addRelayInput} placeholder="wss://" type="text" class={`${InputClass}`} />
{this.state.error ?

{this.state.error}

: undefined}
    {relayStatus.map((r) => { return (
  • this.showRelayDetail(r.url)} class={`w-full px-[1rem] py-[0.75rem] rounded-lg bg-[${DividerBackgroundColor}80] mb-[0.5rem] flex items-center justify-between cursor-pointer hover:bg-[${HoverButtonBackgroudColor}]`} >
    {r.status} {r.url}
    {r.url != blowater ? ( ) : undefined}
  • ); })}
); } }