Skip to content

Commit

Permalink
Fixed initial profile loading
Browse files Browse the repository at this point in the history
  • Loading branch information
XyperCode committed Apr 2, 2024
1 parent 416edea commit 297c44f
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 37 deletions.
2 changes: 1 addition & 1 deletion .run/Tauri Build.run.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Tauri Build" type="CargoCommandRunConfiguration" factoryName="Cargo Command">
<option name="command" value="tauri build --target x86_64-pc-windows-msvc" />
<option name="command" value="tauri build" />
<option name="workingDirectory" value="file://$PROJECT_DIR$/src-tauri" />
<envs />
<option name="emulateTerminal" value="true" />
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ultreon-appcenter",
"version": "0.1.0",
"version": "0.1.1",
"private": true,
"type": "module",
"scripts": {
Expand Down
3 changes: 2 additions & 1 deletion src-tauri/tauri.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
},
"package": {
"productName": "Ultreon App Center",
"version": "0.1.0"
"version": "0.1.1"
},
"tauri": {
"allowlist": {
Expand Down Expand Up @@ -109,6 +109,7 @@
"resizable": true,
"title": "Ultreon AppCenter",
"width": 1024,
"visible": true,
"transparent": true,
"decorations": false,
"acceptFirstMouse": true,
Expand Down
2 changes: 1 addition & 1 deletion src/components/SideBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import './SideBar.css';
export default function SideBar() {
return (
<div id="SideBar">
<a href="#"><MdHome/></a>
<a href="#home"><MdHome/></a>
<a href="#apps"><MdApps/></a>
</div>
)
Expand Down
4 changes: 4 additions & 0 deletions src/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -276,3 +276,7 @@ a:hover {
bottom: 35px;
color: black;
}

.RefreshButton {
margin: 8px;
}
10 changes: 5 additions & 5 deletions src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {window as appWindow} from "@tauri-apps/api";

const router = createHashRouter([
{
path: "/",
path: "home",
element: <HomePage/>,
errorElement: <h1>Error 404</h1>,
},
Expand All @@ -22,10 +22,10 @@ const router = createHashRouter([
element: <AppsPage/>,
errorElement: <h1>Error 404</h1>
},
// {
// path: "instances",
// element: <InstancesPage/>
// },
{
path: "/",
element: <>{window.location.href = "#home"}</>
},
{
path: "project",
element: <ProjectPage/>
Expand Down
72 changes: 46 additions & 26 deletions src/routes/home/HomePage.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import {MouseEvent, ReactElement, useEffect, useState} from 'react';
import {
MouseEvent,
ReactElement,
useEffect,
useState
} from 'react';
import './HomePage.css';
import {invoke} from '@tauri-apps/api'
import {listen} from '@tauri-apps/api/event'
import {load, Profile, PROFILES} from '../../util/Profiles.tsx';
import {load, Profile, getProfiles} from '../../util/Profiles.tsx';
import {toast} from 'react-toastify';
import 'react-toastify/dist/ReactToastify.css';
import ToastComponent from "../../components/CustomToast.tsx";
import {MdRefresh} from "react-icons/md";

let selectedProfile: Profile | null = null;

Expand Down Expand Up @@ -82,8 +88,7 @@ function ProfileEntry(element: Profile, selected: boolean, onClick: (profile: Pr
if (event.currentTarget.classList.contains('Disabled')) return;

const elem = event.currentTarget
if (PROFILES.length === 0) await load();
const app = PROFILES.find(value => value.app == elem.ariaLabel)
const app = (getProfiles()).find(value => value.app == elem.ariaLabel)
selectedProfile = app === undefined ? null : app;

if (selectedProfile === null) {
Expand All @@ -97,7 +102,8 @@ function ProfileEntry(element: Profile, selected: boolean, onClick: (profile: Pr
}

return (
<button className={selected ? "ProfileEntry Selected" : "ProfileEntry"} aria-label={element.app} key={element.app} type="button"
<button className={selected ? "ProfileEntry Selected" : "ProfileEntry"} aria-label={element.app}
key={element.app} type="button"
onClick={SelectProfile}>
{element.name}
</button>
Expand Down Expand Up @@ -141,42 +147,46 @@ export class DownloadInfo {
}
}

let loaded = false

export default function HomePage() {
const [items, setItems] = useState<Profile[]>(PROFILES);
const [items, setItems] = useState<Profile[]>(getProfiles());
const [newItem, setNewItem] = useState<Profile | null>(null);
const [progress, setProgress] = useState<DownloadInfo>(new DownloadInfo());
const [selectedProfile, setSelectedProfile] = useState<Profile | null>(null);

useEffect(() => {
const loadProfiles = async () => {
try {
if (PROFILES.length === 0) await load();

setItems(PROFILES);
} catch (error) {
console.error('Error loading profiles:', error);
}
};

loadProfiles().then(() => {
});
}, []);

useEffect(() => {
if (newItem !== null) {
setItems((prevItems) => {
return [...prevItems, newItem];
});
setNewItem(null);
}

setItems(getProfiles())
}, [newItem]);

const LIST = (
<div id="SidePanel">
{items.filter((item, pos, self) => self.findIndex(it => item.name == it.name) == pos).map((profile) => ProfileEntry(profile, selectedProfile === profile, setSelectedProfile))}
{
items.length > 0
? <div>
{items.filter((item, pos, self) => self.findIndex(it => item.name == it.name) == pos).map((profile) => {
if (profile === null) return <></>;
return ProfileEntry(profile, selectedProfile === profile, setSelectedProfile)
})
}
</div> : <>
<p>No profiles</p>
<button id="RefreshButtonHome" type="button" className="RefreshButton" onClick={async () => {
setItems([])
await load();
setItems(getProfiles());
}}><MdRefresh/></button>
</>
}
</div>
)

async function importProfile(name: string) {
try {
console.log("Attempting to import profile:" + name);
Expand All @@ -187,9 +197,8 @@ export default function HomePage() {
}

console.log("Imported profile: %s", profile.name);
if (PROFILES.length === 0) await load();
PROFILES.push(profile);
console.log(PROFILES);
(getProfiles()).push(profile);
console.log(getProfiles());

setNewItem(profile)
hideModal();
Expand Down Expand Up @@ -225,6 +234,17 @@ export default function HomePage() {
</div>
)

useEffect(() => {
(async () => {
if (loaded) return
setItems([])
await load().then(profiles => {
setItems(profiles)
loaded = true
});
})();
});

return (
<>
{Content(LIST, MODAL, PROGRESS)}
Expand Down
10 changes: 8 additions & 2 deletions src/util/Profiles.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,22 @@ export class Profile {
}

// eslint-disable-next-line react-refresh/only-export-components
export let PROFILES: Array<Profile> = [];
const PROFILES: Array<Profile> = [];

export async function load() {
try {
PROFILES = []
PROFILES.length = 0
PROFILES.push(...(await invoke("load_profiles") as Array<Profile>));
console.log(PROFILES)
return PROFILES
} catch(error) {
console.error(error)
return []
}
}

export function getProfiles() {
return PROFILES
}

console.log(PROFILES)

0 comments on commit 297c44f

Please sign in to comment.