/** @jsx h */ import { Component, Fragment, h } from "preact"; import { CenterClass, inputBorderClass, InputClass, LinearGradientsClass, NoOutlineClass, } from "./components/tw.ts"; import KeyView from "./key-view.tsx"; import { DividerBackgroundColor, ErrorColor, HoverButtonBackgroundColor, PrimaryTextColor, SecondaryBackgroundColor, SuccessColor, TitleIconColor, WarnColor, } from "./style/colors.ts"; import { RelayIcon } from "./icons/relay-icon.tsx"; import { DeleteIcon } from "./icons/delete-icon.tsx"; import { default_blowater_relay, RelayConfig } from "./relay-config.ts"; import { emitFunc } from "../event-bus.ts"; import { sleep } from "@blowater/csp"; import { ConnectionPool, InMemoryAccountContext, NostrAccountContext, PrivateKey } from "@blowater/nostr-sdk"; 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 ViewSpaceSettings = { type: "ViewSpaceSettings"; url: string; }; export type ViewRecommendedRelaysList = { type: "ViewRecommendedRelaysList"; }; export type func_GetRelayURLs = () => Set; type RelaySettingProp = { getRelayURLs: func_GetRelayURLs; 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.getRelayURLs()) { const relay = props.relayPool.getRelay(url); let status: keyof typeof colors = "Closed"; if (relay) { status = relay.status(); } _relayStatus.push({ url, status, }); } return _relayStatus; } viewSpaceSettings = (url: string) => { this.props.emit({ type: "ViewSpaceSettings", url: url, }); }; showRecommendedRelaysList = () => { this.props.emit({ type: "ViewRecommendedRelaysList", }); }; render(props: RelaySettingProp) { const addRelayInput = this.state.addRelayInput; const relayStatus = this.computeRelayStatus(props); const addRelay = async () => { console.log("add", addRelayInput); if (addRelayInput.length < 0) { return; } this.setState({ addRelayInput: "", relayStatus: this.computeRelayStatus(props), }); props.emit({ type: "RelayConfigChange", kind: "add", url: addRelayInput, }); }; 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.viewSpaceSettings(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-[${HoverButtonBackgroundColor}]`} >
    {r.status} {r.url}
    {r.url != default_blowater_relay ? ( ) : undefined}
  • ); })}
); } removeRelay = (props: RelaySettingProp, url: string) => async (e: Event) => { e.stopPropagation(); this.setState({ relayStatus: this.computeRelayStatus(props), }); props.emit({ type: "RelayConfigChange", kind: "remove", url: url, }); }; }