blowater/UI/setting.tsx

255 lines
9.3 KiB
TypeScript
Raw Normal View History

2023-06-30 14:05:57 +00:00
/** @jsx h */
import { Component, Fragment, h } from "https://esm.sh/preact@10.17.1";
2023-06-30 14:05:57 +00:00
import { tw } from "https://esm.sh/twind@0.16.16";
2023-10-08 17:22:53 +00:00
2023-06-30 14:05:57 +00:00
import {
CenterClass,
inputBorderClass,
InputClass,
LinearGradientsClass,
NoOutlineClass,
} from "./components/tw.ts";
import KeyView from "./key-view.tsx";
2023-08-28 17:58:05 +00:00
import { InMemoryAccountContext, NostrAccountContext } from "../lib/nostr-ts/nostr.ts";
import { PrivateKey } from "../lib/nostr-ts/key.ts";
2023-06-30 14:05:57 +00:00
import {
DividerBackgroundColor,
ErrorColor,
HoverButtonBackgroudColor,
PrimaryTextColor,
2023-09-07 00:37:14 +00:00
SecondaryBackgroundColor,
2023-06-30 14:05:57 +00:00
SuccessColor,
TitleIconColor,
WarnColor,
} from "./style/colors.ts";
2023-11-11 11:19:21 +00:00
import { RelayIcon } from "./icons/relay-icon.tsx";
import { DeleteIcon } from "./icons/delete-icon.tsx";
import { RelayConfig } from "./relay-config.ts";
2023-11-03 13:09:13 +00:00
import { ConnectionPool } from "../lib/nostr-ts/relay-pool.ts";
import { emitFunc } from "../event-bus.ts";
2023-11-14 09:08:20 +00:00
import { sleep } from "https://raw.githubusercontent.com/BlowaterNostr/csp/master/csp.ts";
2023-06-30 14:05:57 +00:00
export interface SettingProps {
logout: () => void;
relayConfig: RelayConfig;
relayPool: ConnectionPool;
2023-06-30 14:05:57 +00:00
myAccountContext: NostrAccountContext;
emit: emitFunc<RelayConfigChange>;
2023-10-26 04:42:24 +00:00
show: boolean;
2023-06-30 14:05:57 +00:00
}
const colors = {
"Connecting": WarnColor,
"Open": SuccessColor,
"Closing": WarnColor,
"Closed": ErrorColor,
};
export const Setting = (props: SettingProps) => {
2023-06-30 14:05:57 +00:00
let priKey: PrivateKey | undefined;
const ctx = props.myAccountContext;
if (ctx instanceof InMemoryAccountContext) {
priKey = ctx.privateKey;
}
2023-10-26 04:42:24 +00:00
if (props.show == false) {
return undefined;
}
2023-06-30 14:05:57 +00:00
return (
2023-10-26 04:42:24 +00:00
<div
class={tw`flex-1 overflow-hidden overflow-y-auto`}
>
<div class={tw`min-w-full min-h-full px-2 bg-[${SecondaryBackgroundColor}]`}>
<div class={tw`max-w-[41rem] m-auto py-[1.5rem]`}>
<div class={tw`px-[1rem] py-[1.5rem] ${inputBorderClass} rounded-lg mt-[1.5rem]`}>
<RelaySetting
emit={props.emit}
relayConfig={props.relayConfig}
relayPool={props.relayPool}
>
</RelaySetting>
</div>
2023-06-30 14:05:57 +00:00
2023-10-26 04:42:24 +00:00
<div
class={tw`px-[1rem] py-[0.5rem] ${inputBorderClass} rounded-lg mt-[1.5rem] text-[${PrimaryTextColor}]`}
>
<KeyView
privateKey={priKey}
publicKey={props.myAccountContext.publicKey}
/>
</div>
<button
class={tw`w-full p-[0.75rem] mt-[1.5rem] rounded-lg ${NoOutlineClass} ${CenterClass} ${LinearGradientsClass} hover:bg-gradient-to-l text-[${PrimaryTextColor}]`}
onClick={props.logout}
>
Logout
</button>
2023-09-07 00:37:14 +00:00
</div>
2023-06-30 14:05:57 +00:00
</div>
</div>
);
};
export type RelayConfigChange = {
type: "RelayConfigChange";
2023-11-15 13:21:45 +00:00
kind: "add" | "remove";
url: string;
};
type RelaySettingProp = {
relayConfig: RelayConfig;
relayPool: ConnectionPool;
emit: emitFunc<RelayConfigChange>;
};
type RelaySettingState = {
error: string;
addRelayInput: string;
relayStatus: { url: string; status: keyof typeof colors }[];
};
export class RelaySetting extends Component<RelaySettingProp, RelaySettingState> {
state: Readonly<RelaySettingState> = {
error: "",
addRelayInput: "",
relayStatus: [],
};
2023-11-14 09:08:20 +00:00
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);
2023-11-14 06:22:50 +00:00
let status: keyof typeof colors = "Closed";
if (relay) {
2023-11-15 13:21:45 +00:00
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) {
2023-11-15 13:21:45 +00:00
const p = props.relayConfig.add(addRelayInput);
this.setState({
addRelayInput: "",
relayStatus: this.computeRelayStatus(props),
});
2023-11-15 13:21:45 +00:00
const relay = await p;
if (relay instanceof Error) {
console.error(relay);
return;
}
props.emit({
type: "RelayConfigChange",
kind: "add",
url: relay.url,
});
}
};
return (
<Fragment>
<p class={tw`text-[1.3125rem] flex text-[${PrimaryTextColor}]`}>
<RelayIcon
class={tw`w-[2rem] h-[2rem] mr-[1rem]`}
style={{
stroke: TitleIconColor,
}}
/>
Relays
</p>
<p class={tw`mt-[1.75rem] text-[${PrimaryTextColor}]`}>
Add Relay
</p>
<div class={tw`mt-[0.5rem] flex text-[${PrimaryTextColor}]`}>
<input
autofocus={true}
onInput={(e) => this.setState({ addRelayInput: e.currentTarget.value })}
value={addRelayInput}
placeholder="wss://"
type="text"
class={tw`${InputClass}`}
/>
<button
class={tw`ml-[0.75rem] w-[5.9375rem] h-[3rem] p-[0.75rem] rounded-lg ${NoOutlineClass} bg-[${DividerBackgroundColor}] hover:bg-[${HoverButtonBackgroudColor}] ${CenterClass} text-[${PrimaryTextColor}]`}
onClick={addRelay}
>
Add
</button>
</div>
{this.state.error
? <p class={tw`mt-2 text-[${ErrorColor}] text-[0.875rem]`}>{this.state.error}</p>
: undefined}
<ul class={tw`mt-[1.5rem] text-[${PrimaryTextColor}]`}>
2023-10-08 17:22:53 +00:00
{relayStatus.map((r) => {
return (
<li
class={tw`w-full px-[1rem] py-[0.75rem] rounded-lg bg-[${DividerBackgroundColor}80] mb-[0.5rem] flex items-center justify-between`}
>
<div class={tw`flex items-center flex-1 overflow-hidden`}>
<span
class={tw`bg-[${
colors[r.status]
}] text-center block py-1 px-2 rounded text-[0.8rem] mr-2 font-bold`}
>
{r.status}
</span>
<span class={tw`truncate`}>{r.url}</span>
</div>
<button
class={tw`w-[2rem] h-[2rem] rounded-lg bg-transparent hover:bg-[${DividerBackgroundColor}] ${CenterClass} ${NoOutlineClass}`}
onClick={async () => {
2023-11-15 13:21:45 +00:00
const p = props.relayConfig.remove(r.url);
2023-10-08 17:22:53 +00:00
this.setState({
relayStatus: this.computeRelayStatus(props),
});
2023-11-15 13:21:45 +00:00
await p;
props.emit({
type: "RelayConfigChange",
kind: "remove",
url: r.url,
});
2023-10-08 17:22:53 +00:00
}}
>
<DeleteIcon
class={tw`w-[1rem] h-[1rem]`}
style={{
stroke: ErrorColor,
}}
2023-10-08 17:22:53 +00:00
/>
</button>
</li>
);
})}
</ul>
</Fragment>
);
}
2023-06-30 14:05:57 +00:00
}