Skip to content

Commit 9b13f75

Browse files
committed
feat(launcher): implement a better instances handler
1 parent 61cfa19 commit 9b13f75

File tree

6 files changed

+43
-14
lines changed

6 files changed

+43
-14
lines changed

src/components/home/instance/CurrentInstance.vue

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,33 @@
11
<script setup lang="ts">
2+
import { computed, inject } from "vue";
3+
24
import Image from "@/components/general/base/Image.vue";
35
import MaterialRipple from "@/components/general/base/MaterialRipple.vue";
6+
import { InstanceStatesContextKey } from "@/constants/application.ts";
7+
import Instances from "@/lib/instances";
8+
import type {
9+
InstanceStatesType,
10+
InstanceStateType,
11+
} from "@/types/application/instance-states.type.ts";
12+
13+
const instanceStates = inject<InstanceStatesType>(InstanceStatesContextKey);
14+
15+
const currentInstance = computed((): InstanceStateType | undefined => (
16+
Instances.findCurrent(instanceStates)
17+
));
418
</script>
519

620
<template>
721
<button
22+
v-if="currentInstance"
823
id="__home-page__current-instance-button"
924
class="relative flex flex-nowrap items-center gap-2 rounded-md p-2"
1025
>
1126
<Image
1227
id="__home-page__current-instance-logo"
1328
class-names="rounded-md size-12 p-1"
14-
src="https://media.forgecdn.net/avatars/thumbnails/286/772/64/64/637305737753885398.png"
15-
alt="Fabulously optimized"
29+
:src="currentInstance.icon"
30+
:alt="`${currentInstance.name}'s icon`"
1631
/>
1732
<span
1833
id="__home-page__current-instance-information-wrapper"
@@ -22,13 +37,13 @@ import MaterialRipple from "@/components/general/base/MaterialRipple.vue";
2237
id="__home-page__current-instance-information-title"
2338
class="block font-medium"
2439
>
25-
Fabulously Optimized
40+
{{ currentInstance.name }}
2641
</span>
2742
<span
2843
id="__home-page__current-instance-information-version"
2944
class="block text-neutral-400"
3045
>
31-
1.21.10
46+
{{ currentInstance.version }}
3247
</span>
3348
</span>
3449
<MaterialRipple />

src/components/home/instance/Launch.vue

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { computed, inject, ref } from "vue";
44
import MaterialRipple from "@/components/general/base/MaterialRipple.vue";
55
import { InstanceStatesContextKey } from "@/constants/application.ts";
66
import { LaunchStatus } from "@/constants/launcher.ts";
7+
import Instances from "@/lib/instances";
78
import Launcher from "@/lib/launcher";
89
import { log } from "@/lib/logging/scopes/log.ts";
910
import type {
@@ -14,15 +15,9 @@ import type { LaunchStatusType } from "@/types/launcher/launch-status.type.ts";
1415
1516
const instanceStates = inject<InstanceStatesType>(InstanceStatesContextKey);
1617
17-
const currentInstance = computed((): InstanceStateType | undefined => {
18-
const keys = Object.keys(instanceStates ?? {});
19-
20-
if (keys.length === 0) {
21-
return undefined;
22-
}
23-
24-
return instanceStates?.[keys?.[0]];
25-
});
18+
const currentInstance = computed((): InstanceStateType | undefined => (
19+
Instances.findCurrent(instanceStates)
20+
));
2621
2722
const status = ref<LaunchStatusType>(LaunchStatus.General.Starting);
2823

src/lib/instances/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { ApplicationNamespace } from "@/constants/application.ts";
2+
import { findCurrent } from "@/lib/instances/scopes/find-current.ts";
23
import { getConfigInstancesStates } from "@/lib/instances/scopes/get-config-instances-states.ts";
34
import { readStoredInstances } from "@/lib/instances/scopes/read-stored-instances.ts";
45
import type { InstanceStatesType } from "@/types/application/instance-states.type.ts";
@@ -11,4 +12,5 @@ export default {
1112
): void => window[ApplicationNamespace].__internals.changeInstanceStates(key, value),
1213
"getFromConfig": getConfigInstancesStates,
1314
"readStored" : readStoredInstances,
15+
findCurrent,
1416
} as const;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import type {
2+
InstanceStatesType,
3+
InstanceStateType,
4+
} from "@/types/application/instance-states.type.ts";
5+
6+
export function findCurrent(
7+
instances: InstanceStatesType | undefined,
8+
): InstanceStateType | undefined {
9+
const keys = Object.keys(instances ?? {});
10+
11+
if (keys.length === 0) {
12+
return undefined;
13+
}
14+
15+
return instances?.[keys?.[0]];
16+
}

src/lib/instances/scopes/read-stored-instances.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ export async function readStoredInstances(baseDirectory: string): Promise<Instan
4545

4646
for (const [currentId, currentMetadata] of Object.entries(parsed)) {
4747
const isValid: boolean = Schemas.InstanceMetadataValidator.Check(currentMetadata);
48+
console.log(isValid);
4849

4950
if (!isValid) {
5051
log.info(`The '${currentId}' instances is not valid`);

src/lib/schemas/scopes/instances/instance-metadata.schema.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Type } from "typebox";
22

33
import { MinecraftSchema } from "@/lib/schemas/scopes/config/minecraft.schema.ts";
44

5-
export const InstanceMetadataSchema = Type.Union([
5+
export const InstanceMetadataSchema = Type.Intersect([
66
MinecraftSchema,
77
Type.Object({
88
"id" : Type.String(),

0 commit comments

Comments
 (0)