|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; |
| 4 | +import { CircleAlertIcon } from "lucide-react"; |
| 5 | +import { useMemo, useState } from "react"; |
| 6 | +import { toast } from "sonner"; |
| 7 | +import { listProjectServerWallets } from "@/actions/project-wallet/list-server-wallets"; |
| 8 | +import type { Project } from "@/api/project/projects"; |
| 9 | +import { WalletAddress } from "@/components/blocks/wallet-address"; |
| 10 | +import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert"; |
| 11 | +import { Button } from "@/components/ui/button"; |
| 12 | +import { Spinner } from "@/components/ui/Spinner"; |
| 13 | +import { |
| 14 | + Select, |
| 15 | + SelectContent, |
| 16 | + SelectItem, |
| 17 | + SelectTrigger, |
| 18 | + SelectValue, |
| 19 | +} from "@/components/ui/select"; |
| 20 | +import { getClientThirdwebClient } from "@/constants/thirdweb-client.client"; |
| 21 | +import { useDashboardRouter } from "@/lib/DashboardRouter"; |
| 22 | +import type { ProjectWalletSummary } from "@/lib/server/project-wallet"; |
| 23 | +import { updateDefaultProjectWallet } from "../../transactions/lib/vault.client"; |
| 24 | +import CreateServerWallet from "../../transactions/server-wallets/components/create-server-wallet.client"; |
| 25 | + |
| 26 | +export function ProjectWalletSetup(props: { |
| 27 | + project: Project; |
| 28 | + teamSlug: string; |
| 29 | + managementAccessToken: string | undefined; |
| 30 | +}) { |
| 31 | + const { project, teamSlug, managementAccessToken } = props; |
| 32 | + const router = useDashboardRouter(); |
| 33 | + const queryClient = useQueryClient(); |
| 34 | + const client = useMemo(() => getClientThirdwebClient(), []); |
| 35 | + const [selectedWalletId, setSelectedWalletId] = useState<string | undefined>( |
| 36 | + undefined, |
| 37 | + ); |
| 38 | + |
| 39 | + const serverWalletsQuery = useQuery({ |
| 40 | + enabled: Boolean(managementAccessToken), |
| 41 | + queryFn: async () => { |
| 42 | + if (!managementAccessToken) { |
| 43 | + return [] as ProjectWalletSummary[]; |
| 44 | + } |
| 45 | + |
| 46 | + return listProjectServerWallets({ |
| 47 | + managementAccessToken, |
| 48 | + projectId: project.id, |
| 49 | + }); |
| 50 | + }, |
| 51 | + queryKey: ["project", project.id, "server-wallets", managementAccessToken], |
| 52 | + staleTime: 60_000, |
| 53 | + }); |
| 54 | + |
| 55 | + const serverWallets = serverWalletsQuery.data ?? []; |
| 56 | + |
| 57 | + const effectiveSelectedWalletId = useMemo(() => { |
| 58 | + if ( |
| 59 | + selectedWalletId && |
| 60 | + serverWallets.some((wallet) => wallet.id === selectedWalletId) |
| 61 | + ) { |
| 62 | + return selectedWalletId; |
| 63 | + } |
| 64 | + |
| 65 | + return serverWallets[0]?.id; |
| 66 | + }, [selectedWalletId, serverWallets]); |
| 67 | + |
| 68 | + const selectedWallet = useMemo(() => { |
| 69 | + if (!effectiveSelectedWalletId) { |
| 70 | + return undefined; |
| 71 | + } |
| 72 | + |
| 73 | + return serverWallets.find( |
| 74 | + (wallet) => wallet.id === effectiveSelectedWalletId, |
| 75 | + ); |
| 76 | + }, [effectiveSelectedWalletId, serverWallets]); |
| 77 | + |
| 78 | + const setDefaultWalletMutation = useMutation({ |
| 79 | + mutationFn: async (wallet: ProjectWalletSummary) => { |
| 80 | + await updateDefaultProjectWallet({ |
| 81 | + project, |
| 82 | + projectWalletAddress: wallet.address, |
| 83 | + }); |
| 84 | + }, |
| 85 | + onError: (error) => { |
| 86 | + const message = |
| 87 | + error instanceof Error |
| 88 | + ? error.message |
| 89 | + : "Failed to update project wallet"; |
| 90 | + toast.error(message); |
| 91 | + }, |
| 92 | + onSuccess: async (_, wallet) => { |
| 93 | + const descriptionLabel = wallet.label ?? wallet.address; |
| 94 | + toast.success("Project wallet updated", { |
| 95 | + description: `Now pointing to ${descriptionLabel}`, |
| 96 | + }); |
| 97 | + await queryClient.invalidateQueries({ |
| 98 | + queryKey: [ |
| 99 | + "project", |
| 100 | + project.id, |
| 101 | + "server-wallets", |
| 102 | + managementAccessToken, |
| 103 | + ], |
| 104 | + }); |
| 105 | + router.refresh(); |
| 106 | + }, |
| 107 | + }); |
| 108 | + |
| 109 | + const canSelectExisting = |
| 110 | + Boolean(managementAccessToken) && serverWallets.length > 0; |
| 111 | + |
| 112 | + return ( |
| 113 | + <Alert variant="info"> |
| 114 | + <CircleAlertIcon className="size-5" /> |
| 115 | + <AlertTitle>No project wallet set</AlertTitle> |
| 116 | + <AlertDescription> |
| 117 | + Set a project wallet to use for dashboard and API integrations. |
| 118 | + </AlertDescription> |
| 119 | + |
| 120 | + <div className="mt-4 space-y-5"> |
| 121 | + {serverWalletsQuery.isLoading ? ( |
| 122 | + <div className="flex items-center gap-2 text-sm text-muted-foreground"> |
| 123 | + <Spinner className="size-4" /> |
| 124 | + <span>Loading existing server wallets…</span> |
| 125 | + </div> |
| 126 | + ) : canSelectExisting ? ( |
| 127 | + <div className="space-y-4"> |
| 128 | + <div className="space-y-2"> |
| 129 | + <p className="text-sm font-medium text-foreground"> |
| 130 | + Choose an existing server wallet |
| 131 | + </p> |
| 132 | + <p className="text-sm text-muted-foreground"> |
| 133 | + These wallets were already created for this project. Pick one to |
| 134 | + set as the default. |
| 135 | + </p> |
| 136 | + <Select |
| 137 | + onValueChange={(value) => setSelectedWalletId(value)} |
| 138 | + value={effectiveSelectedWalletId ?? ""} |
| 139 | + > |
| 140 | + <SelectTrigger className="w-full"> |
| 141 | + <SelectValue placeholder="Select server wallet"> |
| 142 | + {selectedWallet ? ( |
| 143 | + <div className="flex items-center gap-2"> |
| 144 | + <WalletAddress |
| 145 | + address={selectedWallet.address} |
| 146 | + client={client} |
| 147 | + /> |
| 148 | + <span className="text-muted-foreground text-sm"> |
| 149 | + {selectedWallet.label ?? "Unnamed server wallet"} |
| 150 | + </span> |
| 151 | + </div> |
| 152 | + ) : ( |
| 153 | + "Select server wallet" |
| 154 | + )} |
| 155 | + </SelectValue> |
| 156 | + </SelectTrigger> |
| 157 | + <SelectContent> |
| 158 | + {serverWallets.map((wallet) => ( |
| 159 | + <SelectItem key={wallet.id} value={wallet.id}> |
| 160 | + <div className="flex items-center gap-2"> |
| 161 | + <WalletAddress |
| 162 | + address={wallet.address} |
| 163 | + client={client} |
| 164 | + /> |
| 165 | + <span className="text-muted-foreground text-sm"> |
| 166 | + {wallet.label ?? "Unnamed server wallet"} |
| 167 | + </span> |
| 168 | + </div> |
| 169 | + </SelectItem> |
| 170 | + ))} |
| 171 | + </SelectContent> |
| 172 | + </Select> |
| 173 | + </div> |
| 174 | + |
| 175 | + <div className="flex flex-wrap items-center gap-3"> |
| 176 | + <Button |
| 177 | + disabled={!selectedWallet || setDefaultWalletMutation.isPending} |
| 178 | + onClick={() => { |
| 179 | + if (selectedWallet) { |
| 180 | + setDefaultWalletMutation.mutate(selectedWallet); |
| 181 | + } |
| 182 | + }} |
| 183 | + type="button" |
| 184 | + > |
| 185 | + {setDefaultWalletMutation.isPending && ( |
| 186 | + <Spinner className="mr-2 size-4" /> |
| 187 | + )} |
| 188 | + Set as project wallet |
| 189 | + </Button> |
| 190 | + <CreateServerWallet |
| 191 | + managementAccessToken={managementAccessToken} |
| 192 | + project={project} |
| 193 | + teamSlug={teamSlug} |
| 194 | + setAsProjectWallet |
| 195 | + /> |
| 196 | + </div> |
| 197 | + </div> |
| 198 | + ) : ( |
| 199 | + <div className="space-y-3 text-sm text-muted-foreground"> |
| 200 | + <p> |
| 201 | + Create a server wallet to start using transactions and other |
| 202 | + project features. |
| 203 | + </p> |
| 204 | + <CreateServerWallet |
| 205 | + managementAccessToken={managementAccessToken} |
| 206 | + project={project} |
| 207 | + teamSlug={teamSlug} |
| 208 | + setAsProjectWallet |
| 209 | + /> |
| 210 | + </div> |
| 211 | + )} |
| 212 | + </div> |
| 213 | + </Alert> |
| 214 | + ); |
| 215 | +} |
0 commit comments