url-shortener-form.tsx266 lines, 8.8 KB
| 1 | "use client"; |
| 2 | |
| 3 | import { useForm } from "react-hook-form"; |
| 4 | import { |
| 5 | Form, |
| 6 | FormControl, |
| 7 | FormField, |
| 8 | FormItem, |
| 9 | FormMessage, |
| 10 | } from "../ui/form"; |
| 11 | import { zodResolver } from "@hookform/resolvers/zod"; |
| 12 | import { UrlFormData, urlSchema } from "@/lib/types"; |
| 13 | import { Input } from "../ui/input"; |
| 14 | import { Button } from "../ui/button"; |
| 15 | import { usePathname, useRouter } from "next/navigation"; |
| 16 | import { useState } from "react"; |
| 17 | import { shortenUrl } from "@/server/actions/urls/shorten-url"; |
| 18 | import { Card, CardContent } from "../ui/card"; |
| 19 | import { AlertTriangle, Copy, QrCode } from "lucide-react"; |
| 20 | import { useSession } from "next-auth/react"; |
| 21 | import { QRCodeModal } from "../modals/qr-code-modal"; |
| 22 | import { boolean } from "drizzle-orm/gel-core"; |
| 23 | import { toast } from "sonner"; |
| 24 | import { SignupSuggestionDialog } from "../dialogs/signup-suggestion-dialog"; |
| 25 | |
| 26 | export function UrlShortenerForm() { |
| 27 | const { data: session } = useSession(); |
| 28 | |
| 29 | const router = useRouter(); |
| 30 | const pathname = usePathname(); |
| 31 | |
| 32 | const [shortUrl, setShortUrl] = useState<string | null>(null); |
| 33 | const [shortCode, setShortCode] = useState<string | null>(null); |
| 34 | const [isLoading, setIsLoading] = useState(false); |
| 35 | const [error, setError] = useState<string | null>(null); |
| 36 | const [showSignupDialog, setShowSignupDialog] = useState(false); |
| 37 | const [isQrCodeModalOpen, setIsQrCodeModalOpen] = useState(false); |
| 38 | const [flaggedInfo, setFlaggedInfo] = useState<{ |
| 39 | flagged: boolean; |
| 40 | reason: string | null; |
| 41 | message: string | undefined; |
| 42 | } | null>(null); |
| 43 | |
| 44 | const form = useForm<UrlFormData>({ |
| 45 | resolver: zodResolver(urlSchema), |
| 46 | defaultValues: { |
| 47 | url: "", |
| 48 | customCode: "", |
| 49 | }, |
| 50 | }); |
| 51 | |
| 52 | const onSubmit = async (data: UrlFormData) => { |
| 53 | setIsLoading(true); |
| 54 | setError(null); |
| 55 | setShortUrl(null); |
| 56 | setShortCode(null); |
| 57 | setFlaggedInfo(null); |
| 58 | |
| 59 | try { |
| 60 | const formData = new FormData(); |
| 61 | formData.append("url", data.url); |
| 62 | |
| 63 | // If a custom code is provided, append it to the form data |
| 64 | if (data.customCode && data.customCode.trim() !== "") { |
| 65 | formData.append("customCode", data.customCode.trim()); |
| 66 | } |
| 67 | |
| 68 | const response = await shortenUrl(formData); |
| 69 | if (response.success && response.data) { |
| 70 | setShortUrl(response.data.shortUrl); |
| 71 | // Extract the short code from the short URL |
| 72 | const shortCodeMatch = response.data.shortUrl.match(/\/r\/([^/]+)$/); |
| 73 | if (shortCodeMatch && shortCodeMatch[1]) { |
| 74 | setShortCode(shortCodeMatch[1]); |
| 75 | } |
| 76 | |
| 77 | if (response.data.flagged) { |
| 78 | setFlaggedInfo({ |
| 79 | flagged: response.data.flagged, |
| 80 | reason: response.data.flagReason || null, |
| 81 | message: response.data.message, |
| 82 | }); |
| 83 | |
| 84 | toast.warning(response.data.message || "This URL is flagged", { |
| 85 | description: response.data.flagReason, |
| 86 | }); |
| 87 | } else { |
| 88 | toast.success("URL shortened successfully"); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | if (session?.user && pathname.includes("/dashboard")) { |
| 93 | router.refresh(); |
| 94 | } |
| 95 | |
| 96 | if (!session?.user) { |
| 97 | setShowSignupDialog(true); |
| 98 | } |
| 99 | } catch (error) { |
| 100 | setError("An error occurred. Please try again."); |
| 101 | console.error(error); |
| 102 | } finally { |
| 103 | setIsLoading(false); |
| 104 | } |
| 105 | }; |
| 106 | |
| 107 | const copyToClipboard = async () => { |
| 108 | if (!shortUrl) return; |
| 109 | |
| 110 | try { |
| 111 | await navigator.clipboard.writeText(shortUrl); |
| 112 | } catch (error) { |
| 113 | console.error(error); |
| 114 | } |
| 115 | }; |
| 116 | |
| 117 | const showQrCode = () => { |
| 118 | if (!shortUrl || !shortCode) return; |
| 119 | setIsQrCodeModalOpen(true); |
| 120 | }; |
| 121 | |
| 122 | return ( |
| 123 | <> |
| 124 | <div className="w-full max-w-2xl mx-auto"> |
| 125 | <Form {...form}> |
| 126 | <form onSubmit={form.handleSubmit(onSubmit)} className="space-y-4"> |
| 127 | <div className="flex flex-col sm:flex-row gap-2"> |
| 128 | <FormField |
| 129 | control={form.control} |
| 130 | name="url" |
| 131 | render={({ field }) => ( |
| 132 | <FormItem className="flex-1"> |
| 133 | <FormControl> |
| 134 | <Input |
| 135 | placeholder="Paste your long URL here" |
| 136 | {...field} |
| 137 | disabled={false} |
| 138 | /> |
| 139 | </FormControl> |
| 140 | <FormMessage /> |
| 141 | </FormItem> |
| 142 | )} |
| 143 | /> |
| 144 | <Button type="submit" disabled={isLoading}> |
| 145 | {isLoading ? ( |
| 146 | <> |
| 147 | <span className="mr-2 size-4 animate-spin rounded-full border-2 border-current border-t-transparent" /> |
| 148 | Shortening... |
| 149 | </> |
| 150 | ) : ( |
| 151 | "Shorten" |
| 152 | )} |
| 153 | </Button> |
| 154 | </div> |
| 155 | |
| 156 | <FormField |
| 157 | control={form.control} |
| 158 | name="customCode" |
| 159 | render={({ field }) => ( |
| 160 | <FormItem> |
| 161 | <FormControl> |
| 162 | <div className="flex items-center"> |
| 163 | <span className="text-sm text-muted-foreground mr-2"> |
| 164 | {process.env.NEXT_PUBLIC_APP_URL || |
| 165 | window.location.origin} |
| 166 | /r/ |
| 167 | </span> |
| 168 | <Input |
| 169 | placeholder="Custom code (optional)" |
| 170 | {...field} |
| 171 | value={field.value || ""} |
| 172 | onChange={(e) => field.onChange(e.target.value || "")} |
| 173 | disabled={isLoading} |
| 174 | className="flex-1" |
| 175 | /> |
| 176 | </div> |
| 177 | </FormControl> |
| 178 | <FormMessage /> |
| 179 | </FormItem> |
| 180 | )} |
| 181 | /> |
| 182 | |
| 183 | {error && ( |
| 184 | <div className="p-3 bg-destructive/10 text-destructive rounded-md text-sm"> |
| 185 | {error} |
| 186 | </div> |
| 187 | )} |
| 188 | |
| 189 | {shortUrl && ( |
| 190 | <Card> |
| 191 | <CardContent className="p-4"> |
| 192 | <p className="text-sm font-medium text-muted-foreground mb-2"> |
| 193 | Your shortened URL: |
| 194 | </p> |
| 195 | <div className="flex items-center gap-2"> |
| 196 | <Input |
| 197 | type="text" |
| 198 | value={shortUrl} |
| 199 | readOnly |
| 200 | className="font-medium" |
| 201 | /> |
| 202 | <Button |
| 203 | type="button" |
| 204 | variant={"outline"} |
| 205 | className="flex-shrink-0" |
| 206 | onClick={copyToClipboard} |
| 207 | > |
| 208 | <Copy className="size-4 mr-1" /> |
| 209 | Copy |
| 210 | </Button> |
| 211 | <Button |
| 212 | type="button" |
| 213 | variant={"outline"} |
| 214 | className="flex-shrink-0" |
| 215 | onClick={showQrCode} |
| 216 | > |
| 217 | <QrCode className="size-4" /> |
| 218 | </Button> |
| 219 | </div> |
| 220 | |
| 221 | {flaggedInfo && flaggedInfo.flagged && ( |
| 222 | <div className="mt-3 p-3 bg-yellow-50 dark:bg-yellow-900/20 border border-yellow-200 dark:border-yellow-800 rounded-md"> |
| 223 | <div className="flex items-start gap-2"> |
| 224 | <AlertTriangle className="size-5 text-yellow-600 dark:text-yellow-400 mt-0.5 flex-shrink-0" /> |
| 225 | <div> |
| 226 | <p className="text-sm font-medium text-yellow-800 dark:text-yellow-300"> |
| 227 | This URL has been flagged for review |
| 228 | </p> |
| 229 | <p className="text-xs text-yellow-700 dark:text-yellow-400 mt-1"> |
| 230 | {flaggedInfo.message || |
| 231 | "This URL will be reviewed by an administrator before it becomes fully active."} |
| 232 | </p> |
| 233 | {flaggedInfo.reason && ( |
| 234 | <p className="text-sm mt-2 text-yellow-600 dark:text-yellow-400"> |
| 235 | <span className="font-medium">Reason:</span>{" "} |
| 236 | {flaggedInfo.reason || "Unknown reason"} |
| 237 | </p> |
| 238 | )} |
| 239 | </div> |
| 240 | </div> |
| 241 | </div> |
| 242 | )} |
| 243 | </CardContent> |
| 244 | </Card> |
| 245 | )} |
| 246 | </form> |
| 247 | </Form> |
| 248 | </div> |
| 249 | |
| 250 | <SignupSuggestionDialog |
| 251 | isOpen={showSignupDialog} |
| 252 | onOpenChange={setShowSignupDialog} |
| 253 | shortUrl={shortUrl || ""} |
| 254 | /> |
| 255 | |
| 256 | {shortUrl && shortCode && ( |
| 257 | <QRCodeModal |
| 258 | isOpen={isQrCodeModalOpen} |
| 259 | onOpenChange={setIsQrCodeModalOpen} |
| 260 | url={shortUrl} |
| 261 | shortCode={shortCode} |
| 262 | /> |
| 263 | )} |
| 264 | </> |
| 265 | ); |
| 266 | } |