|
| 1 | +<template> |
| 2 | + <NewModal |
| 3 | + ref="modal" |
| 4 | + :header="formatMessage(messages.header, { name: serverName })" |
| 5 | + :disable-close="isSuspending" |
| 6 | + fade="danger" |
| 7 | + max-width="500px" |
| 8 | + > |
| 9 | + <div class="flex flex-col gap-4"> |
| 10 | + <Admonition type="critical" :header="formatMessage(messages.warningHeader)"> |
| 11 | + {{ formatMessage(messages.warningBody, { name: serverName }) }} |
| 12 | + </Admonition> |
| 13 | + |
| 14 | + <div class="flex flex-col gap-2.5"> |
| 15 | + <span class="text-lg font-semibold text-contrast"> |
| 16 | + {{ formatMessage(messages.reasonLabel) }} |
| 17 | + </span> |
| 18 | + <Chips |
| 19 | + v-model="reason" |
| 20 | + :items="reasonOptions" |
| 21 | + :format-label="reasonLabel" |
| 22 | + :capitalize="false" |
| 23 | + :aria-label="formatMessage(messages.reasonLabel)" |
| 24 | + /> |
| 25 | + </div> |
| 26 | + </div> |
| 27 | + |
| 28 | + <template #actions> |
| 29 | + <div class="flex justify-end gap-2"> |
| 30 | + <Button native-type="button" :disabled="isSuspending" @click="hide"> |
| 31 | + <XIcon /> |
| 32 | + {{ formatMessage(commonMessages.cancelButton) }} |
| 33 | + </Button> |
| 34 | + <Button |
| 35 | + type="colored" |
| 36 | + color="red" |
| 37 | + native-type="button" |
| 38 | + :disabled="isSuspending" |
| 39 | + @click="suspend" |
| 40 | + > |
| 41 | + <SpinnerIcon v-if="isSuspending" class="animate-spin" /> |
| 42 | + <BanIcon v-else /> |
| 43 | + {{ formatMessage(messages.suspendButton) }} |
| 44 | + </Button> |
| 45 | + </div> |
| 46 | + </template> |
| 47 | + </NewModal> |
| 48 | +</template> |
| 49 | + |
| 50 | +<script setup lang="ts"> |
| 51 | +import type { Archon } from '@modrinth/api-client' |
| 52 | +import { BanIcon, SpinnerIcon, XIcon } from '@modrinth/assets' |
| 53 | +import { useQueryClient } from '@tanstack/vue-query' |
| 54 | +import { nextTick, ref } from 'vue' |
| 55 | +
|
| 56 | +import Admonition from '#ui/components/base/Admonition.vue' |
| 57 | +import { Button } from '#ui/components/base/buttons' |
| 58 | +import Chips from '#ui/components/base/Chips.vue' |
| 59 | +import NewModal from '#ui/components/modal/NewModal.vue' |
| 60 | +import { defineMessages, useVIntl } from '#ui/composables/i18n' |
| 61 | +import { injectModrinthClient, injectNotificationManager } from '#ui/providers' |
| 62 | +import { commonMessages } from '#ui/utils/common-messages' |
| 63 | +
|
| 64 | +type Reason = Archon.Servers.Internal.SuspendServerRequest['reason'] |
| 65 | +
|
| 66 | +const props = defineProps<{ |
| 67 | + serverId: string |
| 68 | + serverName: string |
| 69 | +}>() |
| 70 | +
|
| 71 | +const emit = defineEmits<{ |
| 72 | + suspended: [] |
| 73 | +}>() |
| 74 | +
|
| 75 | +const client = injectModrinthClient() |
| 76 | +const { addNotification } = injectNotificationManager() |
| 77 | +const queryClient = useQueryClient() |
| 78 | +const { formatMessage } = useVIntl() |
| 79 | +
|
| 80 | +const messages = defineMessages({ |
| 81 | + header: { |
| 82 | + id: 'servers.suspend.modal.header', |
| 83 | + defaultMessage: 'Suspend {name}', |
| 84 | + }, |
| 85 | + warningHeader: { |
| 86 | + id: 'servers.suspend.modal.warning-header', |
| 87 | + defaultMessage: 'This server will stop running', |
| 88 | + }, |
| 89 | + warningBody: { |
| 90 | + id: 'servers.suspend.modal.warning-body', |
| 91 | + defaultMessage: |
| 92 | + "{name} will stop and its subdomain will be unassigned. Its backups will be deleted in 60 days unless it's unsuspended.", |
| 93 | + }, |
| 94 | + reasonLabel: { |
| 95 | + id: 'servers.suspend.modal.reason-label', |
| 96 | + defaultMessage: 'Reason', |
| 97 | + }, |
| 98 | + reasonModerated: { |
| 99 | + id: 'servers.suspend.modal.reason-moderated', |
| 100 | + defaultMessage: 'Moderation', |
| 101 | + }, |
| 102 | + reasonOther: { |
| 103 | + id: 'servers.suspend.modal.reason-other', |
| 104 | + defaultMessage: 'Other', |
| 105 | + }, |
| 106 | + suspendButton: { |
| 107 | + id: 'servers.suspend.modal.suspend-button', |
| 108 | + defaultMessage: 'Suspend server', |
| 109 | + }, |
| 110 | + successTitle: { |
| 111 | + id: 'servers.suspend.modal.success-title', |
| 112 | + defaultMessage: 'Server suspended', |
| 113 | + }, |
| 114 | + successText: { |
| 115 | + id: 'servers.suspend.modal.success-text', |
| 116 | + defaultMessage: '{name} has been suspended.', |
| 117 | + }, |
| 118 | + errorTitle: { |
| 119 | + id: 'servers.suspend.modal.error-title', |
| 120 | + defaultMessage: 'Failed to suspend server', |
| 121 | + }, |
| 122 | +}) |
| 123 | +
|
| 124 | +const reasonOptions: Reason[] = ['moderated', 'other'] |
| 125 | +
|
| 126 | +const modal = ref<InstanceType<typeof NewModal> | null>(null) |
| 127 | +const reason = ref<Reason>('moderated') |
| 128 | +const isSuspending = ref(false) |
| 129 | +
|
| 130 | +function reasonLabel(option: Reason) { |
| 131 | + return formatMessage(option === 'moderated' ? messages.reasonModerated : messages.reasonOther) |
| 132 | +} |
| 133 | +
|
| 134 | +function show(): void { |
| 135 | + reason.value = 'moderated' |
| 136 | + modal.value?.show() |
| 137 | +} |
| 138 | +
|
| 139 | +function hide(): void { |
| 140 | + modal.value?.hide() |
| 141 | +} |
| 142 | +
|
| 143 | +async function suspend(): Promise<void> { |
| 144 | + if (isSuspending.value) return |
| 145 | +
|
| 146 | + isSuspending.value = true |
| 147 | + try { |
| 148 | + await client.archon.servers_internal.suspend(props.serverId, { reason: reason.value }) |
| 149 | + // The modal can't be hidden until it re-renders with closing enabled. Hide it before refetching, as the new status |
| 150 | + // replaces the page behind it. |
| 151 | + isSuspending.value = false |
| 152 | + await nextTick() |
| 153 | + hide() |
| 154 | + addNotification({ |
| 155 | + type: 'success', |
| 156 | + title: formatMessage(messages.successTitle), |
| 157 | + text: formatMessage(messages.successText, { name: props.serverName }), |
| 158 | + }) |
| 159 | + emit('suspended') |
| 160 | + await Promise.all([ |
| 161 | + queryClient.invalidateQueries({ queryKey: ['servers', 'detail', props.serverId] }), |
| 162 | + queryClient.invalidateQueries({ |
| 163 | + queryKey: ['servers', 'action-log', 'v1', 'infinite', props.serverId], |
| 164 | + }), |
| 165 | + ]) |
| 166 | + } catch (error) { |
| 167 | + addNotification({ |
| 168 | + type: 'error', |
| 169 | + title: formatMessage(messages.errorTitle), |
| 170 | + text: error instanceof Error ? error.message : undefined, |
| 171 | + }) |
| 172 | + } finally { |
| 173 | + isSuspending.value = false |
| 174 | + } |
| 175 | +} |
| 176 | +
|
| 177 | +defineExpose({ |
| 178 | + show, |
| 179 | + hide, |
| 180 | +}) |
| 181 | +</script> |
0 commit comments