|
| 1 | +<script setup lang="ts"> |
| 2 | +import { ref } from "vue"; |
| 3 | +import { useI18n } from "vue-i18n"; |
| 4 | +import gql from "graphql-tag"; |
| 5 | +import { LogOut } from "lucide-vue-next"; |
| 6 | +import { Button } from "~/components/ui/button"; |
| 7 | +import { Input } from "~/components/ui/input"; |
| 8 | +import { Label } from "~/components/ui/label"; |
| 9 | +import { |
| 10 | + Dialog, |
| 11 | + DialogContent, |
| 12 | + DialogDescription, |
| 13 | + DialogFooter, |
| 14 | + DialogHeader, |
| 15 | + DialogTitle, |
| 16 | +} from "~/components/ui/dialog"; |
| 17 | +import { toast } from "@/components/ui/toast"; |
| 18 | +
|
| 19 | +const props = defineProps<{ |
| 20 | + player: Record<string, any>; |
| 21 | + serverId: string; |
| 22 | +}>(); |
| 23 | +
|
| 24 | +const emit = defineEmits<{ (e: "kicked"): void }>(); |
| 25 | +
|
| 26 | +const { t } = useI18n(); |
| 27 | +const nuxtApp = useNuxtApp(); |
| 28 | +
|
| 29 | +const open = ref(false); |
| 30 | +const reason = ref(""); |
| 31 | +const submitting = ref(false); |
| 32 | +
|
| 33 | +const kickMutation = gql` |
| 34 | + mutation KickServerPlayer( |
| 35 | + $serverId: String! |
| 36 | + $steam_id: String! |
| 37 | + $reason: String |
| 38 | + ) { |
| 39 | + kickServerPlayer( |
| 40 | + serverId: $serverId |
| 41 | + steam_id: $steam_id |
| 42 | + reason: $reason |
| 43 | + ) { |
| 44 | + kicked |
| 45 | + message |
| 46 | + } |
| 47 | + } |
| 48 | +`; |
| 49 | +
|
| 50 | +async function kick() { |
| 51 | + if (submitting.value) { |
| 52 | + return; |
| 53 | + } |
| 54 | +
|
| 55 | + submitting.value = true; |
| 56 | +
|
| 57 | + try { |
| 58 | + const { data } = await nuxtApp.$apollo.defaultClient.mutate({ |
| 59 | + mutation: kickMutation, |
| 60 | + variables: { |
| 61 | + serverId: props.serverId, |
| 62 | + steam_id: props.player.steam_id, |
| 63 | + reason: reason.value || null, |
| 64 | + }, |
| 65 | + }); |
| 66 | +
|
| 67 | + const result = data?.kickServerPlayer; |
| 68 | +
|
| 69 | + if (result?.kicked) { |
| 70 | + toast({ title: t("player.kick.kicked", { name: props.player.name }) }); |
| 71 | + } else { |
| 72 | + toast({ |
| 73 | + variant: "destructive", |
| 74 | + title: t("common.error"), |
| 75 | + description: result?.message, |
| 76 | + }); |
| 77 | + } |
| 78 | +
|
| 79 | + open.value = false; |
| 80 | + reason.value = ""; |
| 81 | + emit("kicked"); |
| 82 | + } catch (error: any) { |
| 83 | + toast({ |
| 84 | + variant: "destructive", |
| 85 | + title: t("common.error"), |
| 86 | + description: error?.message, |
| 87 | + }); |
| 88 | + } finally { |
| 89 | + submitting.value = false; |
| 90 | + } |
| 91 | +} |
| 92 | +</script> |
| 93 | + |
| 94 | +<template> |
| 95 | + <button |
| 96 | + type="button" |
| 97 | + :title="$t('player.kick.button')" |
| 98 | + :aria-label="$t('player.kick.button')" |
| 99 | + class="inline-flex h-9 w-9 shrink-0 items-center justify-center rounded border border-amber-500/45 bg-amber-500/10 text-amber-400 transition-colors hover:border-amber-500/80 hover:bg-amber-500/20 hover:text-amber-200 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-amber-500/50 focus-visible:ring-offset-2 focus-visible:ring-offset-background" |
| 100 | + @click="open = true" |
| 101 | + > |
| 102 | + <LogOut class="h-4 w-4" /> |
| 103 | + </button> |
| 104 | + |
| 105 | + <Dialog :open="open" @update:open="open = $event"> |
| 106 | + <DialogContent> |
| 107 | + <DialogHeader> |
| 108 | + <DialogTitle> |
| 109 | + {{ $t("player.kick.title", { name: player.name }) }} |
| 110 | + </DialogTitle> |
| 111 | + <DialogDescription> |
| 112 | + {{ $t("player.kick.description") }} |
| 113 | + </DialogDescription> |
| 114 | + </DialogHeader> |
| 115 | + |
| 116 | + <div class="grid gap-2 py-2"> |
| 117 | + <Label>{{ $t("player.kick.reason_label") }}</Label> |
| 118 | + <Input |
| 119 | + v-model="reason" |
| 120 | + :placeholder="$t('player.kick.reason_placeholder')" |
| 121 | + @keyup.enter="kick" |
| 122 | + /> |
| 123 | + </div> |
| 124 | + |
| 125 | + <DialogFooter> |
| 126 | + <Button variant="outline" @click="open = false"> |
| 127 | + {{ $t("common.cancel") }} |
| 128 | + </Button> |
| 129 | + <Button :loading="submitting" @click="kick"> |
| 130 | + {{ $t("player.kick.confirm") }} |
| 131 | + </Button> |
| 132 | + </DialogFooter> |
| 133 | + </DialogContent> |
| 134 | + </Dialog> |
| 135 | +</template> |
0 commit comments