|
| 1 | +<template> |
| 2 | + <div class="normal-page no-sidebar"> |
| 3 | + <h1>Server lookup</h1> |
| 4 | + <div class="normal-page__content"> |
| 5 | + <form class="card flex flex-col gap-3" @submit.prevent="lookupServer"> |
| 6 | + <div class="flex flex-col gap-2"> |
| 7 | + <label for="server-subdomain"> |
| 8 | + <span class="text-lg font-semibold text-contrast"> |
| 9 | + Server subdomain |
| 10 | + <span class="text-brand-red">*</span> |
| 11 | + </span> |
| 12 | + </label> |
| 13 | + <StyledInput |
| 14 | + id="server-subdomain" |
| 15 | + v-model="subdomainInput" |
| 16 | + :icon="ServerIcon" |
| 17 | + :error="submitted && !!validationError" |
| 18 | + placeholder="example or example.modrinth.gg" |
| 19 | + autocomplete="off" |
| 20 | + autocapitalize="none" |
| 21 | + :spellcheck="false" |
| 22 | + /> |
| 23 | + <span v-if="submitted && validationError" class="text-sm font-medium text-red"> |
| 24 | + {{ validationError }} |
| 25 | + </span> |
| 26 | + </div> |
| 27 | + |
| 28 | + <div> |
| 29 | + <Button type="colored" color="brand" native-type="submit" :loading="isLoading"> |
| 30 | + <SearchIcon aria-hidden="true" /> |
| 31 | + Look up server |
| 32 | + </Button> |
| 33 | + </div> |
| 34 | + |
| 35 | + <Admonition v-if="lookupError" type="critical" header="Lookup failed"> |
| 36 | + {{ lookupError }} |
| 37 | + </Admonition> |
| 38 | + </form> |
| 39 | + </div> |
| 40 | + </div> |
| 41 | +</template> |
| 42 | + |
| 43 | +<script setup lang="ts"> |
| 44 | +import { ModrinthApiError } from '@modrinth/api-client' |
| 45 | +import { SearchIcon, ServerIcon } from '@modrinth/assets' |
| 46 | +import { Admonition, Button, injectModrinthClient, StyledInput } from '@modrinth/ui' |
| 47 | +
|
| 48 | +const client = injectModrinthClient() |
| 49 | +
|
| 50 | +const subdomainInput = ref('') |
| 51 | +const submitted = ref(false) |
| 52 | +const isLoading = ref(false) |
| 53 | +const lookupError = ref('') |
| 54 | +
|
| 55 | +const normalizedSubdomain = computed(() => |
| 56 | + subdomainInput.value |
| 57 | + .trim() |
| 58 | + .replace(/\.modrinth\.gg$/i, '') |
| 59 | + .toLowerCase(), |
| 60 | +) |
| 61 | +const validationError = computed(() => { |
| 62 | + if (!normalizedSubdomain.value) { |
| 63 | + return 'Enter a server subdomain.' |
| 64 | + } |
| 65 | +
|
| 66 | + if (normalizedSubdomain.value.length > 63) { |
| 67 | + return 'Subdomain must be 63 characters or fewer.' |
| 68 | + } |
| 69 | +
|
| 70 | + if (!/^[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?$/.test(normalizedSubdomain.value)) { |
| 71 | + return 'Use only letters, numbers, and hyphens, without a leading or trailing hyphen.' |
| 72 | + } |
| 73 | +
|
| 74 | + return undefined |
| 75 | +}) |
| 76 | +
|
| 77 | +watch(subdomainInput, (value) => { |
| 78 | + const withoutDomain = value.replace(/\.modrinth\.gg$/i, '') |
| 79 | + if (withoutDomain !== value) { |
| 80 | + subdomainInput.value = withoutDomain |
| 81 | + } |
| 82 | + lookupError.value = '' |
| 83 | +}) |
| 84 | +
|
| 85 | +async function lookupServer() { |
| 86 | + if (isLoading.value) { |
| 87 | + return |
| 88 | + } |
| 89 | +
|
| 90 | + submitted.value = true |
| 91 | + lookupError.value = '' |
| 92 | +
|
| 93 | + if (validationError.value) { |
| 94 | + return |
| 95 | + } |
| 96 | +
|
| 97 | + subdomainInput.value = normalizedSubdomain.value |
| 98 | + isLoading.value = true |
| 99 | +
|
| 100 | + try { |
| 101 | + const server = await client.archon.servers_internal.getBySubdomain(normalizedSubdomain.value) |
| 102 | + await navigateTo(`/hosting/manage/${server.id}`) |
| 103 | + } catch (error) { |
| 104 | + if (error instanceof ModrinthApiError && error.statusCode === 404) { |
| 105 | + lookupError.value = `No server was found for ${normalizedSubdomain.value}.modrinth.gg.` |
| 106 | + } else { |
| 107 | + lookupError.value = 'The server could not be looked up. Try again.' |
| 108 | + } |
| 109 | + } finally { |
| 110 | + isLoading.value = false |
| 111 | + } |
| 112 | +} |
| 113 | +</script> |
0 commit comments