|
| 1 | +import { batchMoveServer } from "@/api/server" |
| 2 | +import { Button, ButtonProps } from "@/components/ui/button" |
| 3 | +import { |
| 4 | + Dialog, |
| 5 | + DialogClose, |
| 6 | + DialogContent, |
| 7 | + DialogDescription, |
| 8 | + DialogFooter, |
| 9 | + DialogHeader, |
| 10 | + DialogTitle, |
| 11 | + DialogTrigger, |
| 12 | +} from "@/components/ui/dialog" |
| 13 | +import { Input } from "@/components/ui/input" |
| 14 | +import { Label } from "@/components/ui/label" |
| 15 | +import { ScrollArea } from "@/components/ui/scroll-area" |
| 16 | +import { IconButton } from "@/components/xui/icon-button" |
| 17 | +import { useState } from "react" |
| 18 | +import { useTranslation } from "react-i18next" |
| 19 | +import { toast } from "sonner" |
| 20 | +import { Textarea } from "./ui/textarea" |
| 21 | + |
| 22 | +interface BatchMoveServerIconProps extends ButtonProps { |
| 23 | + serverIds: number[] |
| 24 | +} |
| 25 | + |
| 26 | +export const BatchMoveServerIcon: React.FC<BatchMoveServerIconProps> = ({ serverIds, ...props }) => { |
| 27 | + const { t } = useTranslation() |
| 28 | + const [open, setOpen] = useState(false) |
| 29 | + const [toUserId, setToUserId] = useState<number | undefined>(undefined) |
| 30 | + |
| 31 | + const onSubmit = async () => { |
| 32 | + try { |
| 33 | + await batchMoveServer({ |
| 34 | + ids: serverIds, |
| 35 | + to_user: toUserId! |
| 36 | + }) |
| 37 | + } catch (e) { |
| 38 | + console.error(e) |
| 39 | + toast(t("Error"), { |
| 40 | + description: t("Results.UnExpectedError"), |
| 41 | + }) |
| 42 | + return |
| 43 | + } |
| 44 | + toast(t("Done")) |
| 45 | + setOpen(false) |
| 46 | + } |
| 47 | + |
| 48 | + return serverIds.length < 1 ? ( |
| 49 | + <IconButton |
| 50 | + {...props} |
| 51 | + icon="user-pen" |
| 52 | + onClick={() => { |
| 53 | + toast(t("Error"), { |
| 54 | + description: t("Results.NoRowsAreSelected"), |
| 55 | + }) |
| 56 | + }} |
| 57 | + /> |
| 58 | + ) : ( |
| 59 | + <Dialog open={open} onOpenChange={setOpen}> |
| 60 | + <DialogTrigger asChild> |
| 61 | + <IconButton {...props} icon="user-pen" /> |
| 62 | + </DialogTrigger> |
| 63 | + <DialogContent className="sm:max-w-xl"> |
| 64 | + <ScrollArea className="max-h-[calc(100dvh-5rem)] p-3"> |
| 65 | + <div className="items-center mx-1"> |
| 66 | + <DialogHeader> |
| 67 | + <DialogTitle>{t("BatchMoveServer")}</DialogTitle> |
| 68 | + <DialogDescription /> |
| 69 | + </DialogHeader> |
| 70 | + <div className="flex flex-col gap-3 mt-4"> |
| 71 | + <Label>{t("Servers")}</Label> |
| 72 | + <Textarea disabled> |
| 73 | + {serverIds.join(", ")} |
| 74 | + </Textarea> |
| 75 | + <Label>{t("ToUser")}</Label> |
| 76 | + <Input |
| 77 | + type="number" |
| 78 | + placeholder="User ID" |
| 79 | + value={toUserId} |
| 80 | + onChange={(e) => { |
| 81 | + setToUserId(parseInt(e.target.value, 10)) |
| 82 | + }} |
| 83 | + /> |
| 84 | + <DialogFooter className="justify-end"> |
| 85 | + <DialogClose asChild> |
| 86 | + <Button type="button" className="my-2" variant="secondary"> |
| 87 | + {t("Cancel")} |
| 88 | + </Button> |
| 89 | + </DialogClose> |
| 90 | + <Button disabled={!toUserId || toUserId == 0} type="submit" className="my-2" onClick={onSubmit}> |
| 91 | + {t("Move")} |
| 92 | + </Button> |
| 93 | + </DialogFooter> |
| 94 | + </div> |
| 95 | + </div> |
| 96 | + </ScrollArea> |
| 97 | + </DialogContent> |
| 98 | + </Dialog> |
| 99 | + ) |
| 100 | +} |
0 commit comments