user-urls-table.tsx229 lines, 7.8 KB
| 1 | "use client"; |
| 2 | |
| 3 | import { Copy, Edit, ExternalLink, QrCode, Trash2Icon } from "lucide-react"; |
| 4 | import { useState } from "react"; |
| 5 | import { Button } from "../ui/button"; |
| 6 | import { formatDistanceToNow } from "date-fns"; |
| 7 | import { toast } from "sonner"; |
| 8 | import { deleteUrl } from "@/server/actions/urls/delete-url"; |
| 9 | import { QRCodeModal } from "../modals/qr-code-modal"; |
| 10 | import { EditUrlModal } from "../modals/edit-url-modal"; |
| 11 | |
| 12 | interface Url { |
| 13 | id: number; |
| 14 | originalUrl: string; |
| 15 | shortCode: string; |
| 16 | createdAt: Date; |
| 17 | clicks: number; |
| 18 | } |
| 19 | |
| 20 | interface UserUrlsTableProps { |
| 21 | urls: Url[]; |
| 22 | } |
| 23 | |
| 24 | export function UserUrlsTable({ urls }: UserUrlsTableProps) { |
| 25 | const [isDeleting, setIsDeleting] = useState<number | null>(null); |
| 26 | const [localUrls, setLocalUrls] = useState<Url[]>(urls); |
| 27 | const [qrCodeUrl, setQrCodeUrl] = useState<string>(""); |
| 28 | const [qrCodeShortCode, setQrCodeShortCode] = useState<string>(""); |
| 29 | const [isQrCodeModalOpen, setIsQrCodeModalOpen] = useState(false); |
| 30 | const [isEditModalOpen, setIsEditModalOpen] = useState(false); |
| 31 | const [urlToEdit, setUrlToEdit] = useState<{ |
| 32 | id: number; |
| 33 | shortCode: string; |
| 34 | } | null>(null); |
| 35 | |
| 36 | const copyToClipboard = async (shortCode: string) => { |
| 37 | const baseUrl = process.env.NEXT_PUBLIC_APP_URL || window.location.origin; |
| 38 | const shortUrl = `${baseUrl}/r/${shortCode}`; |
| 39 | |
| 40 | try { |
| 41 | await navigator.clipboard.writeText(shortUrl); |
| 42 | toast.success("Short URL copied to clipboard", { |
| 43 | description: "The short URL has been copied to your clipboard", |
| 44 | }); |
| 45 | } catch (error) { |
| 46 | console.error("Failed to copy short URL to clipboard", error); |
| 47 | } |
| 48 | }; |
| 49 | |
| 50 | const handleDelete = async (id: number) => { |
| 51 | setIsDeleting(id); |
| 52 | |
| 53 | try { |
| 54 | const response = await deleteUrl(id); |
| 55 | if (response.success) { |
| 56 | setLocalUrls((prev) => prev.filter((url) => url.id !== id)); |
| 57 | toast.success("URL deleted successfully", { |
| 58 | description: "The URL has been deleted successfully", |
| 59 | }); |
| 60 | } else { |
| 61 | toast.error("Failed to delete URL", { |
| 62 | description: response.error || "An error occurred", |
| 63 | }); |
| 64 | } |
| 65 | } catch (error) { |
| 66 | console.error("Failed to delete URL", error); |
| 67 | toast.error("Failed to delete URL", { |
| 68 | description: "An error occurred", |
| 69 | }); |
| 70 | } finally { |
| 71 | setIsDeleting(null); |
| 72 | } |
| 73 | }; |
| 74 | |
| 75 | const showQrCode = (shortCode: string) => { |
| 76 | const baseUrl = process.env.NEXT_PUBLIC_APP_URL || window.location.origin; |
| 77 | const shortUrl = `${baseUrl}/r/${shortCode}`; |
| 78 | setQrCodeUrl(shortUrl); |
| 79 | setQrCodeShortCode(shortCode); |
| 80 | setIsQrCodeModalOpen(true); |
| 81 | }; |
| 82 | |
| 83 | const handleEdit = (id: number, shortCode: string) => { |
| 84 | setUrlToEdit({ id, shortCode }); |
| 85 | setIsEditModalOpen(true); |
| 86 | }; |
| 87 | |
| 88 | const handleEditSuccess = (newShortCode: string) => { |
| 89 | if (!urlToEdit) return; |
| 90 | |
| 91 | // update the short code in the local state |
| 92 | setLocalUrls((prev) => |
| 93 | prev.map((url) => |
| 94 | url.id === urlToEdit.id ? { ...url, shortCode: newShortCode } : url |
| 95 | ) |
| 96 | ); |
| 97 | }; |
| 98 | |
| 99 | if (localUrls.length === 0) { |
| 100 | return ( |
| 101 | <div className="text-center py-8"> |
| 102 | <p className="text-muted-foreground"> |
| 103 | You haven't created any short URLs yet. Click the button below to |
| 104 | create your first short URL. |
| 105 | </p> |
| 106 | </div> |
| 107 | ); |
| 108 | } |
| 109 | |
| 110 | return ( |
| 111 | <> |
| 112 | <div className="overflow-x-auto"> |
| 113 | <table className="w-full"> |
| 114 | <thead> |
| 115 | <tr className="border-b"> |
| 116 | <th className="text-left py-3 px-4 font-medium">Original URL</th> |
| 117 | <th className="text-left py-3 px-4 font-medium">Short URL</th> |
| 118 | <th className="text-left py-3 px-4 font-medium">Clicks</th> |
| 119 | <th className="text-left py-3 px-4 font-medium">Created At</th> |
| 120 | <th className="text-left py-3 px-4 font-medium">Actions</th> |
| 121 | </tr> |
| 122 | </thead> |
| 123 | <tbody> |
| 124 | {localUrls.map((url) => { |
| 125 | const baseUrl = |
| 126 | process.env.NEXT_PUBLIC_APP_URL || window.location.origin; |
| 127 | const shortUrl = `${baseUrl}/r/${url.shortCode}`; |
| 128 | |
| 129 | return ( |
| 130 | <tr key={url.id} className="border-b hover:bg-muted/50"> |
| 131 | <td className="py-3 px-4"> |
| 132 | <div className="flex items-center"> |
| 133 | <div |
| 134 | className="max-w-xs truncate" |
| 135 | title={url.originalUrl} |
| 136 | > |
| 137 | {url.originalUrl} |
| 138 | </div> |
| 139 | <a |
| 140 | href={url.originalUrl} |
| 141 | target="_blank" |
| 142 | className="ml-2 text-muted-foreground hover:text-foreground" |
| 143 | > |
| 144 | <ExternalLink className="size-4" /> |
| 145 | </a> |
| 146 | </div> |
| 147 | </td> |
| 148 | <td className="py-3 px-4"> |
| 149 | <div className="flex items-center"> |
| 150 | <div className="truncate" title={shortUrl}> |
| 151 | {shortUrl} |
| 152 | </div> |
| 153 | <Button |
| 154 | variant={"ghost"} |
| 155 | size={"icon"} |
| 156 | onClick={() => copyToClipboard(url.shortCode)} |
| 157 | className="ml-2 size-8" |
| 158 | > |
| 159 | <Copy className="size-4" /> |
| 160 | </Button> |
| 161 | </div> |
| 162 | </td> |
| 163 | <td className="py-3 px-4 text-muted-foreground"> |
| 164 | {url.clicks} |
| 165 | </td> |
| 166 | <td className="py-3 px-4 text-muted-foreground"> |
| 167 | {formatDistanceToNow(new Date(url.createdAt), { |
| 168 | addSuffix: true, |
| 169 | })} |
| 170 | </td> |
| 171 | <td className="py-3 px-4 text-right"> |
| 172 | <div className="flex justify-end"> |
| 173 | <Button |
| 174 | variant={"ghost"} |
| 175 | size={"icon"} |
| 176 | onClick={() => showQrCode(url.shortCode)} |
| 177 | className="size-8 text-primary hover:text-primary" |
| 178 | > |
| 179 | <QrCode className="size-4" /> |
| 180 | </Button> |
| 181 | <Button |
| 182 | variant={"ghost"} |
| 183 | size={"icon"} |
| 184 | onClick={() => handleEdit(url.id, url.shortCode)} |
| 185 | className="size-8 text-primary hover:text-primary" |
| 186 | > |
| 187 | <Edit className="size-4" /> |
| 188 | </Button> |
| 189 | <Button |
| 190 | variant={"ghost"} |
| 191 | size={"icon"} |
| 192 | onClick={() => handleDelete(url.id)} |
| 193 | disabled={isDeleting === url.id} |
| 194 | className="size-8 text-destructive hover:text-destructive" |
| 195 | > |
| 196 | {isDeleting === url.id ? ( |
| 197 | <div className="size-4 animate-spin rounded-full border-2 border-current border-t-transparent" /> |
| 198 | ) : ( |
| 199 | <Trash2Icon className="size-4" /> |
| 200 | )} |
| 201 | </Button> |
| 202 | </div> |
| 203 | </td> |
| 204 | </tr> |
| 205 | ); |
| 206 | })} |
| 207 | </tbody> |
| 208 | </table> |
| 209 | </div> |
| 210 | |
| 211 | <QRCodeModal |
| 212 | isOpen={isQrCodeModalOpen} |
| 213 | onOpenChange={setIsQrCodeModalOpen} |
| 214 | url={qrCodeUrl} |
| 215 | shortCode={qrCodeShortCode} |
| 216 | /> |
| 217 | |
| 218 | {urlToEdit && ( |
| 219 | <EditUrlModal |
| 220 | isOpen={isEditModalOpen} |
| 221 | onOpenChange={setIsEditModalOpen} |
| 222 | urlId={urlToEdit.id} |
| 223 | currentShortCode={urlToEdit.shortCode} |
| 224 | onSuccess={handleEditSuccess} |
| 225 | /> |
| 226 | )} |
| 227 | </> |
| 228 | ); |
| 229 | } |