forked from TheSoftwareDevGuild/TheGuildGenesis
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreateProfileDialog.tsx
More file actions
180 lines (170 loc) · 5.67 KB
/
Copy pathCreateProfileDialog.tsx
File metadata and controls
180 lines (170 loc) · 5.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import { Plus } from "lucide-react";
import {
Dialog,
DialogClose,
DialogContent,
DialogDescription,
DialogHeader,
DialogTitle,
DialogTrigger,
} from "@/components/ui/dialog";
import { useState } from "react";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { useCreateProfile } from "@/hooks/profiles/use-create-profile";
import { useQueryClient } from "@tanstack/react-query";
import {
Form,
FormControl,
FormField,
FormItem,
FormLabel,
FormMessage,
} from "@/components/ui/form";
import { useForm } from "react-hook-form";
import { z } from "zod";
import { zodResolver } from "@hookform/resolvers/zod";
import { useGetNonce } from "@/hooks/profiles/use-get-nonce";
import { generateSiweMessage } from "@/lib/utils/siwe";
import { useAccount, useSignMessage } from "wagmi";
const formSchema = z.object({
name: z.string().min(2, { message: "Name must be at least 2 characters." }),
description: z.string().optional(),
githubLogin: z.string().optional(),
});
type FormValues = z.infer<typeof formSchema>;
export function CreateProfileButton() {
const [open, setOpen] = useState(false);
const createProfile = useCreateProfile();
const queryClient = useQueryClient();
const { address } = useAccount();
const { signMessageAsync } = useSignMessage();
const { data: nonceData, isLoading: isLoadingNonce } = useGetNonce(address);
const siweMessage = nonceData ? generateSiweMessage(nonceData) : "";
const form = useForm<FormValues>({
resolver: zodResolver(formSchema),
defaultValues: { name: "", description: "" },
});
const onSubmit = async (values: FormValues) => {
if (!siweMessage) {
throw new Error("SIWE message not available");
}
// Sign the SIWE message
const signature = await signMessageAsync({ message: siweMessage });
await createProfile.mutateAsync({
input: {
name: values.name,
description: values.description || "",
github_login: values.githubLogin || "",
},
signature,
});
await queryClient.invalidateQueries({ queryKey: ["profiles"] });
setOpen(false);
form.reset();
};
return (
<Dialog open={open} onOpenChange={setOpen}>
<DialogTrigger asChild>
<Button className="flex items-center space-x-2">
<Plus className="h-4 w-4" />
<span>Create Profile</span>
</Button>
</DialogTrigger>
<DialogContent>
<DialogHeader>
<DialogTitle>Create Profile</DialogTitle>
<DialogDescription>
Provide a name and an optional description for your profile.
</DialogDescription>
</DialogHeader>
<Form {...form}>
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-4">
<FormField
control={form.control}
name="name"
render={({ field }) => (
<FormItem>
<FormLabel>Name</FormLabel>
<FormControl>
<Input placeholder="My profile" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="description"
render={({ field }) => (
<FormItem>
<FormLabel>Description</FormLabel>
<FormControl>
<Input
placeholder="Write a short introduction..."
{...field}
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="githubLogin"
render={({ field }) => (
<FormItem>
<FormLabel>GitHub Handle</FormLabel>
<FormControl>
<div className="flex items-center gap-2">
<span className="text-sm text-gray-500">@</span>
<Input placeholder="username" {...field} />
</div>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
{siweMessage && (
<div className="space-y-2">
<FormLabel>Message to Sign</FormLabel>
<div className="p-3 bg-gray-50 rounded-md text-sm font-mono break-all">
{siweMessage}
</div>
<p className="text-xs text-gray-600">
This message will be signed with your wallet to authenticate
your profile creation.
</p>
</div>
)}
<div className="flex justify-end gap-2 pt-2">
<DialogClose asChild>
<Button type="button" variant="secondary">
Cancel
</Button>
</DialogClose>
<Button
type="submit"
disabled={
createProfile.isPending || isLoadingNonce || !siweMessage
}
>
{isLoadingNonce
? "Loading..."
: createProfile.isPending
? "Creating..."
: "Create"}
</Button>
</div>
{createProfile.isError ? (
<p className="text-sm text-red-600">
{(createProfile.error as Error).message}
</p>
) : null}
</form>
</Form>
</DialogContent>
</Dialog>
);
}
export default CreateProfileButton;