|
| 1 | +<script setup lang="ts"> |
| 2 | +import { |
| 3 | + ClipboardCopyIcon, |
| 4 | + ClockIcon, |
| 5 | + FolderSearchIcon, |
| 6 | + RefreshCwIcon, |
| 7 | + XIcon, |
| 8 | +} from '@modrinth/assets' |
| 9 | +import { |
| 10 | + Admonition, |
| 11 | + Button, |
| 12 | + type ButtonMenuOption, |
| 13 | + commonMessages, |
| 14 | + defineMessages, |
| 15 | + NewModal, |
| 16 | + SplitButton, |
| 17 | + useVIntl, |
| 18 | +} from '@modrinth/ui' |
| 19 | +import { useQuery } from '@tanstack/vue-query' |
| 20 | +import { computed, ref } from 'vue' |
| 21 | +
|
| 22 | +import { get_all as getRunningProcesses } from '@/helpers/process.js' |
| 23 | +import { pickDirectory } from '@/platform/dialogs' |
| 24 | +
|
| 25 | +import { isDriveRoot, isNestedPath, normalizePath } from './paths' |
| 26 | +import { useCopyPath } from './use-copy-path' |
| 27 | +
|
| 28 | +const props = defineProps<{ |
| 29 | + currentDir: string |
| 30 | + caseInsensitive: boolean |
| 31 | +}>() |
| 32 | +
|
| 33 | +const emit = defineEmits<{ |
| 34 | + confirm: [directory: string, restartNow: boolean] |
| 35 | +}>() |
| 36 | +
|
| 37 | +const { formatMessage } = useVIntl() |
| 38 | +const copyPath = useCopyPath() |
| 39 | +
|
| 40 | +const messages = defineMessages({ |
| 41 | + header: { |
| 42 | + id: 'app.settings.resource-management.app-directory.modal.header', |
| 43 | + defaultMessage: 'Change app directory', |
| 44 | + }, |
| 45 | + admonitionHeader: { |
| 46 | + id: 'app.settings.resource-management.app-directory.modal.admonition-header', |
| 47 | + defaultMessage: 'All your files will be moved', |
| 48 | + }, |
| 49 | + admonitionBody: { |
| 50 | + id: 'app.settings.resource-management.app-directory.modal.admonition-body', |
| 51 | + defaultMessage: |
| 52 | + "You won't be able to use Modrinth App while we move your data. This may take a while.", |
| 53 | + }, |
| 54 | + currentLabel: { |
| 55 | + id: 'app.settings.resource-management.app-directory.modal.current', |
| 56 | + defaultMessage: 'Current', |
| 57 | + }, |
| 58 | + newLabel: { |
| 59 | + id: 'app.settings.resource-management.app-directory.modal.new', |
| 60 | + defaultMessage: 'New', |
| 61 | + }, |
| 62 | + choose: { |
| 63 | + id: 'app.settings.resource-management.app-directory.modal.choose', |
| 64 | + defaultMessage: 'Choose a folder', |
| 65 | + }, |
| 66 | + pickerTitle: { |
| 67 | + id: 'app.settings.resource-management.app-directory.modal.picker-title', |
| 68 | + defaultMessage: 'Select a new app directory', |
| 69 | + }, |
| 70 | + instancesRunning: { |
| 71 | + id: 'app.settings.resource-management.app-directory.modal.instances-running', |
| 72 | + defaultMessage: 'Close all running instances before changing the app directory.', |
| 73 | + }, |
| 74 | + nested: { |
| 75 | + id: 'app.settings.resource-management.app-directory.modal.nested', |
| 76 | + defaultMessage: "The new folder can't be inside your current app directory, or contain it.", |
| 77 | + }, |
| 78 | + driveRoot: { |
| 79 | + id: 'app.settings.resource-management.app-directory.modal.drive-root', |
| 80 | + defaultMessage: |
| 81 | + "Choose a folder rather than the top of a drive, so your files don't mix with everything else on it.", |
| 82 | + }, |
| 83 | + moveAndRestart: { |
| 84 | + id: 'app.settings.resource-management.app-directory.modal.move-and-restart', |
| 85 | + defaultMessage: 'Move and restart', |
| 86 | + }, |
| 87 | + moveLater: { |
| 88 | + id: 'app.settings.resource-management.app-directory.modal.move-later', |
| 89 | + defaultMessage: 'Move on next launch', |
| 90 | + }, |
| 91 | + moreOptions: { |
| 92 | + id: 'app.settings.resource-management.app-directory.modal.more-options', |
| 93 | + defaultMessage: 'More move options', |
| 94 | + }, |
| 95 | +}) |
| 96 | +
|
| 97 | +const modal = ref<InstanceType<typeof NewModal>>() |
| 98 | +const newDir = ref<string | null>(null) |
| 99 | +const locked = ref(false) |
| 100 | +
|
| 101 | +const processesQuery = useQuery({ |
| 102 | + queryKey: ['processes', 'list'], |
| 103 | + networkMode: 'always', |
| 104 | + queryFn: (): Promise<unknown[]> => getRunningProcesses(), |
| 105 | + staleTime: 0, |
| 106 | +}) |
| 107 | +const instancesRunning = computed(() => (processesQuery.data.value?.length ?? 0) > 0) |
| 108 | +
|
| 109 | +const normalizedNew = computed(() => |
| 110 | + newDir.value === null ? null : normalizePath(newDir.value, props.caseInsensitive), |
| 111 | +) |
| 112 | +const normalizedCurrent = computed(() => normalizePath(props.currentDir, props.caseInsensitive)) |
| 113 | +const sameAsCurrent = computed(() => normalizedNew.value === normalizedCurrent.value) |
| 114 | +const nested = computed( |
| 115 | + () => |
| 116 | + normalizedNew.value !== null && |
| 117 | + !sameAsCurrent.value && |
| 118 | + isNestedPath(normalizedNew.value, normalizedCurrent.value), |
| 119 | +) |
| 120 | +const driveRoot = computed(() => newDir.value !== null && isDriveRoot(newDir.value)) |
| 121 | +const canMove = computed( |
| 122 | + () => newDir.value !== null && !sameAsCurrent.value && !nested.value && !instancesRunning.value, |
| 123 | +) |
| 124 | +
|
| 125 | +const moveOptions = computed<ButtonMenuOption[]>(() => [ |
| 126 | + { |
| 127 | + id: 'move-later', |
| 128 | + label: formatMessage(messages.moveLater), |
| 129 | + icon: ClockIcon, |
| 130 | + action: () => confirm(false), |
| 131 | + }, |
| 132 | +]) |
| 133 | +
|
| 134 | +function show(event?: MouseEvent, directory?: string) { |
| 135 | + newDir.value = directory ?? null |
| 136 | + locked.value = directory !== undefined |
| 137 | + void processesQuery.refetch() |
| 138 | + modal.value?.show(event) |
| 139 | +} |
| 140 | +
|
| 141 | +async function browse() { |
| 142 | + const directory = await pickDirectory(formatMessage(messages.pickerTitle)) |
| 143 | + if (directory) { |
| 144 | + newDir.value = directory |
| 145 | + } |
| 146 | +} |
| 147 | +
|
| 148 | +async function confirm(restartNow: boolean) { |
| 149 | + await processesQuery.refetch() |
| 150 | + if (!canMove.value || newDir.value === null) return |
| 151 | + modal.value?.hide() |
| 152 | + emit('confirm', newDir.value, restartNow) |
| 153 | +} |
| 154 | +
|
| 155 | +defineExpose({ show }) |
| 156 | +</script> |
| 157 | + |
| 158 | +<template> |
| 159 | + <NewModal ref="modal" :header="formatMessage(messages.header)" fade="danger" max-width="560px"> |
| 160 | + <div class="flex flex-col gap-6"> |
| 161 | + <Admonition |
| 162 | + type="critical" |
| 163 | + :header="formatMessage(messages.admonitionHeader)" |
| 164 | + :body="formatMessage(messages.admonitionBody)" |
| 165 | + /> |
| 166 | + |
| 167 | + <div class="flex flex-col gap-2"> |
| 168 | + <span class="font-semibold text-contrast">{{ formatMessage(messages.currentLabel) }}</span> |
| 169 | + <Button size="lg" class="w-full !justify-between text-left" @click="copyPath(currentDir)"> |
| 170 | + <span class="min-w-0 truncate text-base font-semibold text-primary"> |
| 171 | + {{ currentDir }} |
| 172 | + </span> |
| 173 | + <ClipboardCopyIcon class="size-5 shrink-0 text-secondary" aria-hidden="true" /> |
| 174 | + </Button> |
| 175 | + </div> |
| 176 | + |
| 177 | + <div class="flex flex-col gap-2"> |
| 178 | + <span class="font-semibold text-contrast">{{ formatMessage(messages.newLabel) }}</span> |
| 179 | + <Button |
| 180 | + size="lg" |
| 181 | + class="w-full !justify-between text-left" |
| 182 | + :disabled="locked" |
| 183 | + @click="browse" |
| 184 | + > |
| 185 | + <span |
| 186 | + class="min-w-0 truncate font-semibold" |
| 187 | + :class="newDir ? 'text-primary' : 'text-secondary'" |
| 188 | + :title="newDir ?? undefined" |
| 189 | + > |
| 190 | + {{ newDir ?? formatMessage(messages.choose) }} |
| 191 | + </span> |
| 192 | + <FolderSearchIcon class="size-5 shrink-0 text-secondary" aria-hidden="true" /> |
| 193 | + </Button> |
| 194 | + <div aria-live="polite" class="flex flex-col gap-1 empty:hidden"> |
| 195 | + <p v-if="instancesRunning" class="m-0 text-sm text-red"> |
| 196 | + {{ formatMessage(messages.instancesRunning) }} |
| 197 | + </p> |
| 198 | + <p v-if="nested" class="m-0 text-sm text-red"> |
| 199 | + {{ formatMessage(messages.nested) }} |
| 200 | + </p> |
| 201 | + <p v-else-if="driveRoot" class="m-0 text-sm text-orange"> |
| 202 | + {{ formatMessage(messages.driveRoot) }} |
| 203 | + </p> |
| 204 | + </div> |
| 205 | + </div> |
| 206 | + </div> |
| 207 | + |
| 208 | + <template #actions> |
| 209 | + <div class="flex justify-end gap-2"> |
| 210 | + <Button type="outlined" @click="modal?.hide()"> |
| 211 | + <XIcon aria-hidden="true" /> |
| 212 | + {{ formatMessage(commonMessages.cancelButton) }} |
| 213 | + </Button> |
| 214 | + <SplitButton |
| 215 | + type="colored" |
| 216 | + color="red" |
| 217 | + :menu-label="formatMessage(messages.moreOptions)" |
| 218 | + :options="moveOptions" |
| 219 | + :disabled="!canMove" |
| 220 | + @click="confirm(true)" |
| 221 | + > |
| 222 | + <RefreshCwIcon aria-hidden="true" /> |
| 223 | + {{ formatMessage(messages.moveAndRestart) }} |
| 224 | + </SplitButton> |
| 225 | + </div> |
| 226 | + </template> |
| 227 | + </NewModal> |
| 228 | +</template> |
0 commit comments