/** @jsx h */ import { Component, Fragment, h } from "https://esm.sh/preact@10.17.1"; import { tw } from "https://esm.sh/twind@0.16.16"; import { CenterClass, inputBorderClass, InputClass, LinearGradientsClass, NoOutlineClass, } from "./components/tw.ts"; import KeyView from "./key-view.tsx"; import { InMemoryAccountContext, NostrAccountContext } from "../lib/nostr-ts/nostr.ts"; import { PrivateKey } from "../lib/nostr-ts/key.ts"; 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 } from "./relay-config.ts"; import { ConnectionPool } from "../lib/nostr-ts/relay-pool.ts"; import { emitFunc } from "../event-bus.ts"; import { sleep } from "https://raw.githubusercontent.com/BlowaterNostr/csp/master/csp.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; }; 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(1000); 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; } 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={tw`${InputClass}`} />
{this.state.error ?

{this.state.error}

: undefined}
    {relayStatus.map((r) => { return (
  • {r.status} {r.url}
  • ); })}
); } }