Added altarik manifest to frontend, with the game version of the selected chapter given back to backend when calling download command

This commit is contained in:
Quentin Legot 2023-10-03 23:03:53 +02:00
parent 3122d324bf
commit 6ec89497cb
7 changed files with 111 additions and 14 deletions

View File

@ -16,7 +16,7 @@ use reqwest::Client;
use tauri::Manager;
use tokio::sync::mpsc;
struct CustomState (Option<GameProfile>);
struct CustomState (Option<GameProfile>, Option<AltarikManifest>);
#[tauri::command]
async fn login(app: tauri::AppHandle, _window: tauri::Window, state: tauri::State<'_, Mutex<CustomState>>) -> Result<String, String> {
@ -39,14 +39,14 @@ async fn login(app: tauri::AppHandle, _window: tauri::Window, state: tauri::Stat
}
#[tauri::command]
async fn load_altarik_manifest(state: tauri::State<'_, Mutex<Option<AltarikManifest>>>) -> Result<AltarikManifest, String> {
async fn load_altarik_manifest(state: tauri::State<'_, Mutex<CustomState>>) -> Result<AltarikManifest, String> {
let reqwest_client = Client::new();
let altarik_manifest = AltarikManifest::get_altarik_manifest(&reqwest_client).await;
match altarik_manifest {
Ok(val) => {
match state.lock() {
Ok(mut global_manifest) => {
let _ = global_manifest.insert(val.clone());
let _ = global_manifest.1.insert(val.clone());
Ok(val)
},
Err(err) => {
@ -62,7 +62,7 @@ async fn load_altarik_manifest(state: tauri::State<'_, Mutex<Option<AltarikManif
}
#[tauri::command]
async fn download(app: tauri::AppHandle, state: tauri::State<'_, Mutex<CustomState>>) -> Result<String, String> {
async fn download(game_version: String, app: tauri::AppHandle, state: tauri::State<'_, Mutex<CustomState>>) -> Result<String, String> {
if let Some(base_dir) = BaseDirs::new() {
let data_folder = base_dir.data_dir().join(".altarik_test");
let root_path = data_folder.as_path();
@ -80,7 +80,7 @@ async fn download(app: tauri::AppHandle, state: tauri::State<'_, Mutex<CustomSta
log_channel: sender.clone(),
root_path,
java_path: &java_path.as_path(),
version_number: "1.19.3".to_string(),
version_number: game_version,
version_type: launcher::VersionType::Release,
memory_min: "2G".to_string(),
memory_max: "4G".to_string(),
@ -129,7 +129,7 @@ async fn read_channel(mut receiver: mpsc::Receiver<ProgressMessage>, app: tauri:
#[tokio::main]
async fn main() {
tauri::Builder::default()
.manage(Mutex::new(CustomState(None)))
.manage(Mutex::new(CustomState(None, None)))
.invoke_handler(tauri::generate_handler![login, download, load_altarik_manifest])
.run(tauri::generate_context!())
.expect("error while running tauri application");

View File

@ -0,0 +1,18 @@
import { AltarikManifest } from "../models/manifest/AltarikManifest";
import ChapterList from "./ChapterList";
interface Props {
manifest: AltarikManifest | undefined,
selectedChapter: number,
onClickFunction: Function
}
export default function AltarikManifestComponent({manifest, selectedChapter, onClickFunction} : Props) {
return (
<>
{manifest != undefined ? <ChapterList chapters={manifest.chapters} selectedChapter={selectedChapter} onClickFunction={onClickFunction} /> : <></>}
</>
)
}

View File

@ -0,0 +1,16 @@
import { title } from "process"
import { Chapter } from "../models/manifest/AltarikManifest"
import { MouseEventHandler } from "react"
interface Props {
chapter: Chapter,
isSelected: boolean,
onClickFunction: MouseEventHandler<HTMLButtonElement>
}
export default function ChapterItem({ chapter, isSelected, onClickFunction } : Props) {
return (
<button className={isSelected ? "selected": ""} onClick={onClickFunction}>{chapter.title} -- {chapter.minecraftVersion}</button>
)
}

View File

@ -0,0 +1,25 @@
import { AltarikManifest, Chapter } from "../models/manifest/AltarikManifest"
import ChapterItem from "./ChapterItem"
interface Props {
chapters: Chapter[],
selectedChapter: number,
onClickFunction: Function
}
export default function ChapterList({ chapters, selectedChapter, onClickFunction }: Props) {
return (
<div id="chaptersList">
{
chapters.map((chapter, key) => (
<ChapterItem chapter={chapter} key={key} isSelected={key === selectedChapter} onClickFunction={() => onClickFunction(key)}/>
))
}
</div>
)
}

View File

@ -1,5 +1,7 @@
import { useEffect, useState } from "react"
import { invoke, event } from "@tauri-apps/api";
import { AltarikManifest } from "../models/manifest/AltarikManifest";
import AltarikManifestComponent from "./AltarikManifestComponent";
interface ProgressMessage {
p_type: String,
@ -14,6 +16,8 @@ export default function LoginPage() {
const [greetMessage, setGreetMessage] = useState<String>("");
const [isLogged, setIsLogged] = useState<boolean>(false);
const [loginButtonDisabled, setLoginButtonDisabled] = useState<boolean>(false);
const [altarikManifest, setAltarikManifest] = useState<AltarikManifest>();
const [selectedChapter, setSelectChapter] = useState<number>(-1);
useEffect(() => {
event.listen('progress', (e) => {
@ -21,9 +25,20 @@ export default function LoginPage() {
setGreetMessage("{type: " + v.p_type + ", current: " + v.current + ", total: " + v.total + "}");
// setGreetMessage(String(e.payload));
});
}, [])
useEffect(() => {
if(isLogged) {
invoke('load_altarik_manifest', {}).then(val => {
setAltarikManifest(val as AltarikManifest)
}).catch(err => {
setGreetMessage("Cannot load altarik manifest: " + err)
})
} else {
setAltarikManifest(undefined)
}
}, [isLogged])
async function login () {
if(!isLogged && !loginButtonDisabled) {
setLoginButtonDisabled(true);
@ -39,15 +54,22 @@ export default function LoginPage() {
async function download() {
if(isLogged) {
invoke("download", {}).then(value => {
if(selectedChapter !== -1 && altarikManifest !== undefined) {
invoke("download", { gameVersion: altarikManifest?.chapters[selectedChapter].minecraftVersion }).then(value => {
setGreetMessage(String(value))
// this.greet_message = value
}).catch(err => {
console.log("An error occured")
setGreetMessage("Error: " + err)
})
} else {
setGreetMessage("Please select a chapter first")
}
}
}
async function select_chapter(key: number) {
setSelectChapter(key)
}
return (
<>
@ -60,6 +82,9 @@ export default function LoginPage() {
</div>
<p id="greet-msg">{ greetMessage }</p>
<hr />
<AltarikManifestComponent manifest={altarikManifest} selectedChapter={selectedChapter} onClickFunction={select_chapter} />
</>
)

View File

@ -0,0 +1,9 @@
export interface AltarikManifest {
chapters: Chapter[]
}
export interface Chapter {
title: String,
minecraftVersion: String,
}

View File

@ -88,6 +88,10 @@ button {
display: none;
}
button.selected {
color: red;
}
@media (prefers-color-scheme: dark) {
:root {
color: #f6f6f6;