useCreateLightningAddress.ts raw
1 import React from "react";
2 import { toast } from "sonner";
3 import { useApp } from "src/hooks/useApp";
4 import { request } from "src/utils/request";
5
6 export function useCreateLightningAddress(appId?: number) {
7 const { data: app, mutate: refetchApp } = useApp(appId);
8 const [creatingLightningAddress, setCreatingLightningAddress] =
9 React.useState(false);
10
11 async function createLightningAddress(intendedLightningAddress: string) {
12 try {
13 if (!app) {
14 throw new Error("app not found");
15 }
16 setCreatingLightningAddress(true);
17 await request("/api/lightning-addresses", {
18 method: "POST",
19 headers: {
20 "Content-Type": "application/json",
21 },
22 body: JSON.stringify({
23 address: intendedLightningAddress,
24 appId: app.id,
25 }),
26 });
27 await refetchApp();
28 toast("Successfully created lightning address");
29 } catch (error) {
30 toast.error("Failed to create lightning address", {
31 description: (error as Error).message.replace(
32 "500 ",
33 ""
34 ) /* remove 500 error code */,
35 });
36 }
37 setCreatingLightningAddress(false);
38 }
39 return { createLightningAddress, creatingLightningAddress };
40 }
41