/** @jsx h */ import { Component, h } from "https://esm.sh/preact@10.17.1"; import { ConnectionPool } from "../../libs/nostr.ts/relay-pool.ts"; import { SingleRelayConnection } from "../../libs/nostr.ts/relay-single.ts"; import { emitFunc } from "../event-bus.ts"; import { RelayAvatar } from "./components/avatar.tsx"; import { SelectRelay } from "./nav.tsx"; import { NavigationUpdate } from "./nav.tsx"; import { getRelayInformation, RelayInformation, robohash } from "./relay-detail.tsx"; import { setState } from "./_helper.ts"; import { AddIcon } from "./icons/add-icon.tsx"; type RelaySwitchListProps = { currentRelay?: string; pool: ConnectionPool; emit: emitFunc; }; type RelaySwitchListState = { showRelayList: boolean; relayInformation: Map; searchRelayValue: string; }; export class RelaySwitchList extends Component { state: Readonly = { relayInformation: new Map(), showRelayList: false, searchRelayValue: "", }; async componentDidMount() { for (const relay of this.props.pool.getRelays()) { getRelayInformation(relay.url).then((info) => { if (info instanceof Error) { console.error(info); return; } setState(this, { relayInformation: this.state.relayInformation.set(relay.url, info), }); }); } } handleSearchRelayInput = async (e: Event) => { await setState(this, { searchRelayValue: (e.target as HTMLInputElement).value, }); }; render() { const relayList = []; for (const relay of this.props.pool.getRelays()) { if (!relay.url.includes(this.state.searchRelayValue)) { continue; } relayList.push( this.RelayListItem(relay, this.props.currentRelay == relay.url), ); } return (
{this.props.currentRelay ? ( ) : }
{this.state.showRelayList ? (
{relayList}
Add a relay
) : undefined}
); } RelayListItem(relay: SingleRelayConnection, isCurrentRelay: boolean) { const selected = isCurrentRelay ? " border-[#000] border-2" : ""; return (
{this.state.relayInformation.get(relay.url)?.name}
{new URL(relay.url).hostname}
); } toggleRelayList = async () => { await setState(this, { showRelayList: !this.state.showRelayList, }); }; onRelaySelected = (relay: SingleRelayConnection) => async () => { await setState(this, { showRelayList: false, }); this.props.emit({ type: "SelectRelay", relay, }); }; onAddRelay = async () => { await setState(this, { showRelayList: false, }); this.props.emit({ type: "ChangeNavigation", id: "Setting", }); }; } function getSecondaryDomainName(url: string) { const domain = new URL(url).hostname.split("."); return domain[domain.length - 2]; }