|
| 1 | +"use client" |
| 2 | +import { useMutation, useQueryClient } from "@tanstack/react-query" |
| 3 | +import { Plus, Search, X } from "lucide-react" |
| 4 | +import { useRouter } from "next/navigation" |
| 5 | +import { useState } from "react" |
| 6 | +import { toast } from "sonner" |
| 7 | +import { Button } from "@/components/ui/button" |
| 8 | +import { |
| 9 | + Dialog, |
| 10 | + DialogClose, |
| 11 | + DialogContent, |
| 12 | + DialogDescription, |
| 13 | + DialogFooter, |
| 14 | + DialogHeader, |
| 15 | + DialogTitle, |
| 16 | + DialogTrigger, |
| 17 | +} from "@/components/ui/dialog" |
| 18 | +import { Input } from "@/components/ui/input" |
| 19 | +import { Label } from "@/components/ui/label" |
| 20 | +import { Select, SelectContent, SelectGroup, SelectItem, SelectTrigger } from "@/components/ui/select" |
| 21 | +import { useSession } from "@/lib/auth" |
| 22 | +import { useTRPC } from "@/lib/trpc/client" |
| 23 | +import type { ApiOutput } from "@/lib/trpc/types" |
| 24 | + |
| 25 | +type Groups = ApiOutput["tg"]["groups"]["search"]["groups"] |
| 26 | +type User = ApiOutput["tg"]["users"]["getByUsername"]["user"] |
| 27 | + |
| 28 | +export function NewGroupAdmin({ user, alreadyIn }: { user: User; alreadyIn: number[] }) { |
| 29 | + const sesh = useSession() |
| 30 | + const adderId = sesh.data?.user.telegramId |
| 31 | + |
| 32 | + const trpc = useTRPC() |
| 33 | + const qc = useQueryClient() |
| 34 | + const router = useRouter() |
| 35 | + |
| 36 | + const [open, setOpen] = useState(false) |
| 37 | + const [groupQuery, setGroupQuery] = useState("") |
| 38 | + const [groups, setGroups] = useState<Groups>([]) |
| 39 | + const [selectedGroup, setSelectedGroup] = useState<Groups[number] | null>(null) |
| 40 | + |
| 41 | + const queryOpts = trpc.tg.groups.search.queryOptions({ query: groupQuery, limit: 20 }) |
| 42 | + |
| 43 | + async function searchGroup(e: React.FormEvent<HTMLFormElement>) { |
| 44 | + e.preventDefault() |
| 45 | + |
| 46 | + const res = await qc.fetchQuery(queryOpts) |
| 47 | + setGroups(res.groups.filter((g) => !alreadyIn.includes(g.telegramId))) |
| 48 | + if (res.count === 0) toast.warning("No groups found with this query") |
| 49 | + else toast.info(`Found ${res.count} groups`) |
| 50 | + } |
| 51 | + |
| 52 | + const submitMutation = useMutation(trpc.tg.permissions.addGroup.mutationOptions()) |
| 53 | + |
| 54 | + async function submit() { |
| 55 | + if (!adderId) return toast.warning("Invalid session, try reloading the page") |
| 56 | + if (!selectedGroup) return toast.warning("No group selected, cannot proceed") |
| 57 | + if (!user) return toast.warning("Invalid user, try restarting the dialog") |
| 58 | + |
| 59 | + try { |
| 60 | + await submitMutation.mutateAsync({ adderId, groupId: selectedGroup?.telegramId, userId: user.id }) |
| 61 | + toast.info(`Group admin added`) |
| 62 | + handleOpenChange(false) |
| 63 | + router.refresh() |
| 64 | + } catch (err) { |
| 65 | + console.error(err) |
| 66 | + handleOpenChange(false) |
| 67 | + toast.error("There was an error, check logs") |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + function reset() { |
| 72 | + setGroups([]) |
| 73 | + setGroupQuery("") |
| 74 | + setSelectedGroup(null) |
| 75 | + } |
| 76 | + |
| 77 | + function handleOpenChange(v: boolean) { |
| 78 | + setOpen(v) |
| 79 | + if (v === false) { |
| 80 | + // closing |
| 81 | + qc.invalidateQueries(trpc.tg.permissions.getRoles.queryOptions({ userId: user?.id ?? 0 })) |
| 82 | + reset() |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + return ( |
| 87 | + <Dialog open={open} onOpenChange={handleOpenChange}> |
| 88 | + <DialogTrigger |
| 89 | + render={ |
| 90 | + <Button variant="outline"> |
| 91 | + <Plus size={20} /> New |
| 92 | + </Button> |
| 93 | + } |
| 94 | + /> |
| 95 | + <DialogContent className="sm:max-w-xl"> |
| 96 | + <DialogHeader> |
| 97 | + <DialogTitle>New Group Admin</DialogTitle> |
| 98 | + <DialogDescription>Make the user admin in a group.</DialogDescription> |
| 99 | + </DialogHeader> |
| 100 | + {user && ( |
| 101 | + <p> |
| 102 | + Target: {user.firstName} {user.username && `@${user.username}`} [{user.id}] |
| 103 | + </p> |
| 104 | + )} |
| 105 | + |
| 106 | + <form onSubmit={searchGroup} className="pt-2 gap-y-4 flex flex-col justify-start items-start"> |
| 107 | + <div className="flex gap-2 flex-col items-start justify-start"> |
| 108 | + <Label htmlFor="email" className="text-base"> |
| 109 | + Search Group |
| 110 | + </Label> |
| 111 | + <div className="flex gap-2 items-center justify-start"> |
| 112 | + <Input |
| 113 | + id="group-query" |
| 114 | + type="text" |
| 115 | + placeholder="Group name" |
| 116 | + className="bg-card w-auto" |
| 117 | + disabled={groups.length > 0} |
| 118 | + required |
| 119 | + onChange={(e) => { |
| 120 | + setGroupQuery(e.target.value) |
| 121 | + }} |
| 122 | + value={groupQuery} |
| 123 | + /> |
| 124 | + <Button type="submit" size="icon"> |
| 125 | + <Search /> |
| 126 | + </Button> |
| 127 | + {groups.length > 0 && ( |
| 128 | + <Button variant="outline" onClick={reset}> |
| 129 | + <X /> |
| 130 | + Reset |
| 131 | + </Button> |
| 132 | + )} |
| 133 | + </div> |
| 134 | + </div> |
| 135 | + </form> |
| 136 | + |
| 137 | + {groups && ( |
| 138 | + <Select |
| 139 | + items={groups.map((g) => ({ value: g, label: g.title }))} |
| 140 | + value={selectedGroup} |
| 141 | + onValueChange={(v) => setSelectedGroup(v)} |
| 142 | + disabled={groups.length === 0} |
| 143 | + > |
| 144 | + <SelectTrigger className="w-full max-w-48">{selectedGroup?.title ?? "Select a group"}</SelectTrigger> |
| 145 | + <SelectContent> |
| 146 | + <SelectGroup> |
| 147 | + {groups.map((item) => ( |
| 148 | + <SelectItem key={item.telegramId} value={item}> |
| 149 | + {item.title} |
| 150 | + </SelectItem> |
| 151 | + ))} |
| 152 | + </SelectGroup> |
| 153 | + </SelectContent> |
| 154 | + </Select> |
| 155 | + )} |
| 156 | + <DialogFooter> |
| 157 | + <DialogClose render={<Button variant="outline">Cancel</Button>} /> |
| 158 | + <Button onClick={submit} disabled={!selectedGroup}> |
| 159 | + Confirm |
| 160 | + </Button> |
| 161 | + </DialogFooter> |
| 162 | + </DialogContent> |
| 163 | + </Dialog> |
| 164 | + ) |
| 165 | +} |
0 commit comments