import { Head, router } from '@inertiajs/react' import StyledTable from '~/components/StyledTable' import SettingsLayout from '~/layouts/SettingsLayout' import StyledButton from '~/components/StyledButton' import { useModals } from '~/context/ModalContext' import StyledModal from '~/components/StyledModal' import { FileEntry } from '../../../types/files' import { useNotifications } from '~/context/NotificationContext' import { useState } from 'react' import api from '~/lib/api' import DownloadURLModal from '~/components/DownloadURLModal' import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query' import useDownloads from '~/hooks/useDownloads' import StyledSectionHeader from '~/components/StyledSectionHeader' import CuratedCollectionCard from '~/components/CuratedCollectionCard' import type { CollectionWithStatus } from '../../../types/collections' import ActiveDownloads from '~/components/ActiveDownloads' import Alert from '~/components/Alert' const CURATED_COLLECTIONS_KEY = 'curated-map-collections' export default function MapsManager(props: { maps: { baseAssetsExist: boolean; regionFiles: FileEntry[] } }) { const queryClient = useQueryClient() const { openModal, closeAllModals } = useModals() const { addNotification } = useNotifications() const [downloading, setDownloading] = useState(false) const { data: curatedCollections } = useQuery({ queryKey: [CURATED_COLLECTIONS_KEY], queryFn: () => api.listCuratedMapCollections(), refetchOnWindowFocus: false, }) const { invalidate: invalidateDownloads } = useDownloads({ filetype: 'map', enabled: true, }) async function downloadBaseAssets() { try { setDownloading(true) const res = await api.downloadBaseMapAssets() if (!res) { throw new Error('An unknown error occurred while downloading base assets.') } if (res.success) { addNotification({ type: 'success', message: 'Base map assets downloaded successfully.', }) router.reload() } } catch (error) { console.error('Error downloading base assets:', error) addNotification({ type: 'error', message: 'An error occurred while downloading the base map assets. Please try again.', }) } finally { setDownloading(false) } } async function downloadCollection(record: CollectionWithStatus) { try { await api.downloadMapCollection(record.slug) invalidateDownloads() addNotification({ type: 'success', message: `Download for collection "${record.name}" has been queued.`, }) } catch (error) { console.error('Error downloading collection:', error) } } async function downloadCustomFile(url: string) { try { await api.downloadRemoteMapRegion(url) invalidateDownloads() addNotification({ type: 'success', message: 'Download has been queued.', }) } catch (error) { console.error('Error downloading custom file:', error) } } async function confirmDeleteFile(file: FileEntry) { openModal( { closeAllModals() }} onCancel={closeAllModals} open={true} confirmText="Delete" cancelText="Cancel" confirmVariant="danger" >

Are you sure you want to delete {file.name}? This action cannot be undone.

, 'confirm-delete-file-modal' ) } async function confirmDownload(record: CollectionWithStatus) { const isCollection = 'resources' in record openModal( { if (isCollection) { if (record.all_installed) { addNotification({ message: `All resources in the collection "${record.name}" have already been downloaded.`, type: 'info', }) return } downloadCollection(record) } closeAllModals() }} onCancel={closeAllModals} open={true} confirmText="Download" cancelText="Cancel" confirmVariant="primary" >

Are you sure you want to download {isCollection ? record.name : record}? It may take some time for it to be available depending on the file size and your internet connection.

, 'confirm-download-file-modal' ) } async function openDownloadModal() { openModal( closeAllModals()} onPreflightSuccess={async (url) => { await downloadCustomFile(url) closeAllModals() }} />, 'download-map-file-modal' ) } const refreshManifests = useMutation({ mutationFn: () => api.refreshManifests(), onSuccess: () => { addNotification({ message: 'Successfully refreshed map collections.', type: 'success', }) queryClient.invalidateQueries({ queryKey: [CURATED_COLLECTIONS_KEY] }) }, }) return (

Maps Manager

Manage your stored map files and explore new regions!

{!props.maps.baseAssetsExist && ( downloadBaseAssets(), }} /> )}
refreshManifests.mutate()} disabled={refreshManifests.isPending} icon="IconRefresh" > Force Refresh Collections
{curatedCollections?.map((collection) => ( confirmDownload(collection)} /> ))} {curatedCollections && curatedCollections.length === 0 && (

No curated collections available.

)}
Download a Custom Map File
className="font-semibold mt-4" rowLines={true} loading={false} compact columns={[ { accessor: 'name', title: 'Name' }, { accessor: 'actions', title: 'Actions', render: (record) => (
{ confirmDeleteFile(record) }} > Delete
), }, ]} data={props.maps.regionFiles || []} />
) }