| 1 | "use client"; |
| 2 | |
| 3 | import Link from "next/link"; |
| 4 | import { useState } |
| 5 | import { useQuery, useMutation |
| 6 | import { Plus, Trash2 |
| 7 | import { |
| 8 | Dialog, |
| 9 | DialogPopup, |
| 10 | DialogHeader, |
| 11 | DialogFooter, |
| 12 | DialogTitle, |
| 13 | DialogDescription, |
| 14 | DialogClose, |
| 15 | } from "@struxa/ui/components/dialog"; |
| 16 | import { useTranslations } |
| 17 | import { toast } |
| 18 | import { orpc, queryClient |
| 19 | import { ContextMenu, RowMenu |
| 20 | import { ConfirmDialog } |
| 21 | |
| 22 | function invalidate() { |
| 23 | void queryClient.invalidateQueries( |
| 24 | } |
| 25 | |
| 26 | export default function NestsPage |
| 27 | const t = |
| 28 | const tc = |
| 29 | |
| 30 | const { data: |
| 31 | const createMutation = |
| 32 | onSuccess: () => |
| 33 | })); |
| 34 | const deleteMutation = |
| 35 | onSuccess: () => |
| 36 | })); |
| 37 | |
| 38 | const [search, |
| 39 | const [showCreate, |
| 40 | const [form, |
| 41 | const [confirmDelete, |
| 42 | |
| 43 | const filtered = |
| 44 | const q = |
| 45 | return n.name |
| 46 | }); |
| 47 | |
| 48 | function closeCreate() { |
| 49 | setShowCreate(false); |
| 50 | setForm({ name: |
| 51 | } |
| 52 | |
| 53 | async function handleCreate |
| 54 | if (!form.name |
| 55 | await createMutation.mutateAsync |
| 56 | closeCreate(); |
| 57 | } |
| 58 | |
| 59 | function nestActions(nest |
| 60 | return [ |
| 61 | { |
| 62 | label: tc("delete |
| 63 | icon: Trash2, |
| 64 | onClick: () => |
| 65 | destructive: true, |
| 66 | }, |
| 67 | ]; |
| 68 | } |
| 69 | |
| 70 | return ( |
| 71 | <> |
| 72 | <Dialog open={ |
| 73 | <DialogPopup showCloseButton={ |
| 74 | <DialogHeader> |
| 75 | <DialogTitle>{t( |
| 76 | <DialogDescription>{t( |
| 77 | </DialogHeader> |
| 78 | <div className="flex flex-col gap-4 px-5 py-4 |
| 79 | <div> |
| 80 | <label className="mb-1.5 block text-xs font-medium text-foreground |
| 81 | <input |
| 82 | autoFocus |
| 83 | className="w-full rounded-lg border border-border bg-background px-3 py-2 text-sm text-foreground outline-none placeholder:text-muted-foreground/50 focus:border-ring transition-colors" |
| 84 | placeholder={t(" |
| 85 | value={form.name |
| 86 | onChange={(e) |
| 87 | onKeyDown={(e) |
| 88 | /> |
| 89 | </div> |
| 90 | <div> |
| 91 | <label className="mb-1.5 block text-xs font-medium text-foreground |
| 92 | <input |
| 93 | className="w-full rounded-lg border border-border bg-background px-3 py-2 text-sm text-foreground outline-none placeholder:text-muted-foreground/50 focus:border-ring transition-colors" |
| 94 | placeholder={t(" |
| 95 | value={form.author |
| 96 | onChange={(e) |
| 97 | onKeyDown={(e) |
| 98 | /> |
| 99 | </div> |
| 100 | <div> |
| 101 | <label className="mb-1.5 block text-xs font-medium text-foreground |
| 102 | <input |
| 103 | className="w-full rounded-lg border border-border bg-background px-3 py-2 text-sm text-foreground outline-none placeholder:text-muted-foreground/50 focus:border-ring transition-colors" |
| 104 | placeholder={t(" |
| 105 | value={form.description |
| 106 | onChange={(e) |
| 107 | onKeyDown={(e) |
| 108 | /> |
| 109 | </div> |
| 110 | </div> |
| 111 | <DialogFooter> |
| 112 | <DialogClose |
| 113 | className="rounded-lg px-4 py-1.5 text-xs font-medium text-muted-foreground transition-colors hover:bg-muted hover:text-foreground" |
| 114 | disabled={createMutation.isPending |
| 115 | > |
| 116 | {tc("cancel") |
| 117 | </DialogClose> |
| 118 | <button |
| 119 | type="button" |
| 120 | onClick={() => |
| 121 | disabled={!form. |
| 122 | className="rounded-lg bg-foreground px-4 py-1.5 text-xs font-medium text-background transition-opacity hover:opacity-80 disabled:opacity-40" |
| 123 | > |
| 124 | {createMutation.isPending ? |
| 125 | </button> |
| 126 | </DialogFooter> |
| 127 | </DialogPopup> |
| 128 | </Dialog> |
| 129 | |
| 130 | <div className="flex-1 overflow-auto px-6 py-5 |
| 131 | <div className="mx-auto max-w-5xl |
| 132 | <div className="mb-5 flex items-center justify-between |
| 133 | <div className="flex items-center gap-2.5 |
| 134 | <h1 className="text-sm font-semibold text-foreground |
| 135 | </div> |
| 136 | <div className="flex items-center gap-2 |
| 137 | <div className="relative flex items-center |
| 138 | <Search className=" |
| 139 | <input |
| 140 | className="rounded-lg border border-border bg-background py-1.5 pl-8 pr-3 text-sm text-foreground outline-none placeholder:text-muted-foreground/50 focus:border-ring transition-colors" |
| 141 | placeholder={t(" |
| 142 | value={search} |
| 143 | onChange={(e) |
| 144 | /> |
| 145 | </div> |
| 146 | <button |
| 147 | type="button" |
| 148 | onClick={() => |
| 149 | className="flex items-center gap-1.5 rounded-lg bg-foreground px-3 py-1.5 text-sm font-medium text-background transition-opacity hover:opacity-80" |
| 150 | > |
| 151 | <Plus className=" |
| 152 | {t("newNest") |
| 153 | </button> |
| 154 | </div> |
| 155 | </div> |
| 156 | |
| 157 | <ConfirmDialog |
| 158 | open={confirmDelete !== |
| 159 | onOpenChange={(open) |
| 160 | title={t(" |
| 161 | description={t(" |
| 162 | confirmLabel={tc(" |
| 163 | destructive |
| 164 | loading={deleteMutation.isPending |
| 165 | onConfirm={async () |
| 166 | if (!confirmDelete) return |
| 167 | await deleteMutation.mutateAsync |
| 168 | setConfirmDelete(null); |
| 169 | }} |
| 170 | /> |
| 171 | |
| 172 | <div className="rounded-xl border border-border bg-card overflow-hidden |
| 173 | <div className="grid grid-cols-[1fr_160px_48px] border-b border-border bg-muted/40 px-4 py-2.5 |
| 174 | <span className="text-xs font-medium text-muted-foreground |
| 175 | <span className="text-xs font-medium text-muted-foreground |
| 176 | <span /> |
| 177 | </div> |
| 178 | |
| 179 | {isLoading && ( |
| 180 | <div className="px-4 py-8 text-center text-sm text-muted-foreground |
| 181 | )} |
| 182 | {!isLoading && filtered |
| 183 | <div className="px-4 py-8 text-center text-sm text-muted-foreground |
| 184 | {search ? t |
| 185 | </div> |
| 186 | )} |
| 187 | {filtered.map(( |
| 188 | const actions = |
| 189 | const isLast = |
| 190 | return ( |
| 191 | <ContextMenu key={ |
| 192 | {({ onContextMenu } |
| 193 | <div |
| 194 | onContextMenu={onContextMenu} |
| 195 | className={`grid grid-cols-[1fr_160px_48px] items-center px-4 py-3 hover:bg-muted/40 transition-colors ${! |
| 196 | > |
| 197 | <div className="flex items-center gap-2 |
| 198 | <Link |
| 199 | href={`/admin/nests/${nest |
| 200 | className="flex items-center gap-1 text-sm font-medium text-foreground transition-colors hover:text-blue-500" |
| 201 | onClick={(e) |
| 202 | > |
| 203 | {nest.name} |
| 204 | <ChevronRight className=" |
| 205 | </Link> |
| 206 | {nest.description && |
| 207 | </div> |
| 208 | <span className="text-xs text-muted-foreground |
| 209 | <div className="flex items-center justify-end |
| 210 | <RowMenu items={ |
| 211 | </div> |
| 212 | </div> |
| 213 | )} |
| 214 | </ContextMenu> |
| 215 | ); |
| 216 | })} |
| 217 | </div> |
| 218 | </div> |
| 219 | </div> |
| 220 | </> |
| 221 | ); |
| 222 | } |