From 8758116520d7fa51d5a6d4178fcca86ecb4e168c Mon Sep 17 00:00:00 2001 From: kieran Date: Fri, 21 Feb 2025 11:43:16 +0000 Subject: [PATCH] feat: notification settings --- src/api.ts | 22 +++++++++- src/hooks/login.tsx | 6 +-- src/index.css | 4 ++ src/pages/account.tsx | 94 ++++++++++++++++++++++++++++++++----------- 4 files changed, 98 insertions(+), 28 deletions(-) diff --git a/src/api.ts b/src/api.ts index 26514c8..91d01a2 100644 --- a/src/api.ts +++ b/src/api.ts @@ -9,6 +9,12 @@ export type ApiResponse = ApiResponseBase & { data: T; }; +export interface AccountDetail { + email?: string; + contact_nip17: boolean; + contact_email: boolean; +} + export interface VmCostPlan { id: number; name: string; @@ -108,7 +114,21 @@ export class LNVpsApi { constructor( readonly url: string, readonly publisher: EventPublisher | undefined, - ) {} + ) { } + + async getAccount() { + const { data } = await this.#handleResponse>( + await this.#req("/api/v1/account", "GET"), + ); + return data; + } + + async updateAccount(acc: AccountDetail) { + const { data } = await this.#handleResponse>( + await this.#req("/api/v1/account", "PATCH", acc), + ); + return data; + } async listVms() { const { data } = await this.#handleResponse>>( diff --git a/src/hooks/login.tsx b/src/hooks/login.tsx index 28d9b4a..e188f48 100644 --- a/src/hooks/login.tsx +++ b/src/hooks/login.tsx @@ -1,4 +1,4 @@ -import { useContext, useSyncExternalStore } from "react"; +import { useContext, useMemo, useSyncExternalStore } from "react"; import { LoginState } from "../login"; import { SnortContext } from "@snort/system-react"; import { LNVpsApi } from "../api"; @@ -10,12 +10,12 @@ export default function useLogin() { () => LoginState.snapshot(), ); const system = useContext(SnortContext); - return session + return useMemo(() => session ? { type: session.type, publicKey: session.publicKey, system, api: new LNVpsApi(ApiUrl, LoginState.getSigner()), } - : undefined; + : undefined, [session, system]); } diff --git a/src/index.css b/src/index.css index 36d5efa..49d3b0d 100644 --- a/src/index.css +++ b/src/index.css @@ -41,3 +41,7 @@ textarea, select { @apply border-none rounded-xl bg-neutral-900 p-2; } + +input:disabled { + @apply text-neutral-200/50; +} \ No newline at end of file diff --git a/src/pages/account.tsx b/src/pages/account.tsx index 2e70d0e..177657e 100644 --- a/src/pages/account.tsx +++ b/src/pages/account.tsx @@ -1,45 +1,91 @@ import { useEffect, useState } from "react"; -import { VmInstance } from "../api"; +import { AccountDetail, LNVpsApi, VmInstance } from "../api"; import useLogin from "../hooks/login"; import VpsInstanceRow from "../components/vps-instance"; import { hexToBech32 } from "@snort/shared"; +import { Icon } from "../components/icon"; +import { AsyncButton } from "../components/button"; export default function AccountPage() { const login = useLogin(); + const [acc, setAcc] = useState(); + const [editEmail, setEditEmail] = useState(false); const [vms, setVms] = useState>([]); - async function loadVms() { - if (!login?.api) return; - const vms = await login?.api.listVms(); + async function loadVms(api: LNVpsApi) { + const vms = await api.listVms(); setVms(vms); } useEffect(() => { - loadVms(); - const t = setInterval(() => loadVms(), 5_000); - return () => clearInterval(t); + if (login?.api) { + loadVms(login.api); + login.api.getAccount().then(setAcc); + const t = setInterval(() => { + loadVms(login.api); + }, 5_000); + return () => clearInterval(t); + } }, [login]); - const npub = hexToBech32("npub", login?.publicKey); - return ( - <> -
- Your Public Key: -
{npub}
+ function notifications() { + return <> +

Notification Settings

+
+ { + setAcc((s) => (s ? { ...s, contact_email: e.target.checked } : undefined)); + }} /> + Email + { + setAcc((s) => (s ? { ...s, contact_nip17: e.target.checked } : undefined)); + }} /> + Nostr DM
-

My Resources

-
- Something doesnt look right?
- Please contact support on: {" "} - - sales@lnvps.net - +
+

Email

+ setAcc(s => (s ? { ...s, email: e.target.value } : undefined))} /> + {!editEmail && setEditEmail(true)} />}
-
- {vms.map((a) => ( - - ))} +
+ { + if (login?.api && acc) { + await login.api.updateAccount(acc); + const newAcc = await login.api.getAccount(); + setAcc(newAcc); + setEditEmail(false); + } + }}> + Save +
+ } + + const npub = hexToBech32("npub", login?.publicKey); + const subjectLine = `[${npub}] Account Query`; + return ( +
+ Your Public Key: +
{npub}
+ {notifications()} +

My Resources

+
+ Something doesnt look right?
+ Please contact support on: {" "} + + sales@lnvps.net + +
+ Please include your public key in all communications. +
+ {vms.map((a) => ( + { + if (login?.api) { + loadVms(login.api); + } + }} /> + ))} + +
); }