'use client' import { useEffect } from 'react' import '@/app/Installs.css' import { invoke } from '@tauri-apps/api/core' import { useGlobal } from '@/providers/GlobalProvider' import { useRouter, useSearchParams } from 'next/navigation' import { platform } from '@tauri-apps/plugin-os' import { faWarning } from '@fortawesome/free-solid-svg-icons' import { FontAwesomeIcon } from '@fortawesome/react-fontawesome' import { ask } from '@tauri-apps/plugin-dialog' import { BaseDirectory, exists, remove } from '@tauri-apps/plugin-fs' import { openFolder } from '@/lib/util' export default function Installs () { const { versionsList, serverVersionList, showPopup, setShowPopup, setPopupMode, setFadeOut, setSelectedVersionList, setManagingVersion, setSelectedGame, category, setCategory, downloadVersions, downloadProgress, linuxUseWine, linuxWineCommand, versions } = useGlobal() const params = useSearchParams() const router = useRouter() const id = Number(params.get('id') || 0) const game = serverVersionList?.games.find(g => g.id === id) useEffect(() => { if (!showPopup) return setSelectedVersionList([]) }, [setSelectedVersionList, showPopup]) if (!id || !game) return

Invalid game

const needsRevisionUpdate = ( lastRevision: number | undefined, version: string ) => { if (!lastRevision) return false return ( lastRevision > 0 && (versionsList == undefined ? 0 : versionsList[version]) / 1000 <= lastRevision ) } return (

{game.name} Installs

{category == -1 && Object.entries(game.categoryNames) .filter(([key]) => { const count = Object.keys(versionsList).filter(v => { const info = serverVersionList?.versions.find( vf => vf.id == v ) if (!info) return false if (platform() == 'linux' && info.wine && !linuxUseWine) return false return info.game === id && info.category === Number(key) }).length return count >= 1 }) .sort(([a], [b]) => Number(b) - Number(a)) .map(([key, value]) => { return (
setCategory(Number(key))} >

{value}

e.stopPropagation()} >

{(() => { const count = Object.keys(versionsList).filter(v => { const info = serverVersionList?.versions.find( vf => vf.id == v ) if (!info) return false if ( platform() == 'linux' && info.wine && !linuxUseWine ) return false return ( info.game === id && info.category == Number(key) ) }).length ?? 0 return `${count} install${count === 1 ? '' : 's'}` })()}

) })} {Object.keys(versionsList) .filter(v => { const info = serverVersionList?.versions.find(vf => vf.id == v) if (!info) return false if (platform() == 'linux' && info.wine && !linuxUseWine) return false return ( info.game === id && (category == -1 ? info.category == -1 : info.category == category) ) }) .sort((a, b) => { const infoA = serverVersionList?.versions.find(vf => vf.id == a) const infoB = serverVersionList?.versions.find(vf => vf.id == b) if (!infoA || !infoB) return 0 return infoB.place - infoA.place }) .map(v => { const versionInfo = serverVersionList?.versions.find( vf => vf.id == v ) if (!versionInfo) return return (
{ if (needsRevisionUpdate(versionInfo.lastRevision, v)) { const answer = await ask( 'Before proceeding, if you do not want your installation directory wiped just yet, please backup the files to another directory. When you click "Yes", it will be wiped. Click "No" if you want to open the installation folder instead.', { title: 'Revision Update', kind: 'warning' } ) if (answer) { const answer2 = await ask( 'Are you sure you want to update? If you did not read the last popup, please go back and read it.', { title: 'Revision Update', kind: 'warning' } ) if (!answer2) return //open downloads popup setPopupMode(1) setShowPopup(true) setFadeOut(false) //uninstall versions?.set( 'list', Object.fromEntries( Object.entries(versionsList).filter( ([k]) => k !== v ) ) ) if ( await exists('game/' + v, { baseDir: BaseDirectory.AppLocalData }) ) await remove('game/' + v, { baseDir: BaseDirectory.AppLocalData, recursive: true }) //reinstall setSelectedVersionList([v]) downloadVersions([ { id: v, type: 0 } ]) } else { openFolder(v) } return } invoke('launch_game', { name: versionInfo.id, executable: versionInfo.executable, displayName: versionInfo.displayName, useWine: !!( platform() == 'linux' && versionInfo.wine && linuxUseWine ), wineCommand: linuxWineCommand }) }} onContextMenu={e => { e.preventDefault() setManagingVersion(v) setPopupMode(2) setShowPopup(true) setFadeOut(false) }} >

{versionInfo.displayName}

Installed{' '} {new Intl.DateTimeFormat(undefined).format( versionsList[v] )}

) })}
) }