Skip to content

Commit

Permalink
Fix asset state when changing users
Browse files Browse the repository at this point in the history
  • Loading branch information
Kruptein committed Dec 29, 2024
1 parent d83a8df commit 5b05fa0
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 22 deletions.
18 changes: 15 additions & 3 deletions client/src/assets/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { useToast } from "vue-toastification";

import type { ApiAsset, ApiAssetUpload } from "../apiTypes";
import { registerSystem, type System } from "../core/systems";
import type { SystemClearReason } from "../core/systems/models";
import { callbackProvider, uuidv4 } from "../core/utils";
import { router } from "../router";

Expand All @@ -15,12 +17,21 @@ const toast = useToast();

const { raw, mutableReactive: $ } = assetState;

class AssetSystem {
class AssetSystem implements System {
rootCallback = callbackProvider();

clear(): void {
clearLocal(): void {
$.folders = [];
$.files = [];
$.loadingFolder = false;
}

clear(reason: SystemClearReason): void {
if (reason === "logging-out") {
this.clearLocal();
$.idMap.clear();
$.folderPath = [];
}
}

clearFolderPath(): void {
Expand Down Expand Up @@ -82,7 +93,7 @@ class AssetSystem {
if (folder === undefined) return;

const data = typeof folder === "string" ? await getFolderByPath(folder) : await getFolder(folder);
this.clear();
this.clearLocal();
this.setFolderData(data.folder.id, data.folder);
if (data.path) assetSystem.setPath(data.path);
assetState.mutableReactive.sharedParent = data.sharedParent;
Expand Down Expand Up @@ -270,3 +281,4 @@ class AssetSystem {
export const assetSystem = new AssetSystem();
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
(window as any).assetSystem = assetSystem;
registerSystem("asset", assetSystem, false, assetState);
16 changes: 1 addition & 15 deletions client/src/assets/ui/AssetListCore.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script setup lang="ts">
import { computed, nextTick, onMounted, ref } from "vue";
import { computed, nextTick, ref } from "vue";
import type { DeepReadonly } from "vue";
import { assetSystem } from "..";
Expand All @@ -9,7 +9,6 @@ import { baseAdjust } from "../../core/http";
import { ctrlOrCmdPressed } from "../../core/utils";
import { coreStore } from "../../store/core";
import type { AssetId } from "../models";
import { socket } from "../socket";
import { assetState } from "../state";
import { getImageSrcFromAssetId } from "../utils";
Expand Down Expand Up @@ -52,19 +51,6 @@ const files = computed(() => {
return assetState.reactive.files.map((f) => assetState.reactive.idMap.get(f)!);
});
async function load(): Promise<void> {
await assetSystem.loadFolder(assetState.currentFolder.value);
}
onMounted(async () => {
if (socket.connected) {
await load();
} else {
socket.connect();
socket.once("connect", load);
}
});
function dragStart(event: DragEvent, file: AssetId, assetHash: string | null): void {
// emit("onDragStart", event);
drag.startDrag(event, file, assetHash);
Expand Down
4 changes: 4 additions & 0 deletions client/src/auth/logout.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { defineComponent } from "vue";

import { socket } from "../assets/socket";
import { http } from "../core/http";
import { clearSystems } from "../core/systems";
import { coreStore } from "../store/core";

export const Logout = defineComponent({
Expand All @@ -10,6 +12,8 @@ export const Logout = defineComponent({
await http.postJson("/api/logout");
coreStore.setAuthenticated(false);
coreStore.setUsername("");
clearSystems("logging-out");
socket.disconnect();
next({ path: "/auth/login" });
},
});
2 changes: 1 addition & 1 deletion client/src/core/systems/models.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export type SystemClearReason = "full-loading" | "partial-loading" | "leaving";
export type SystemClearReason = "full-loading" | "partial-loading" | "leaving" | "logging-out";
8 changes: 6 additions & 2 deletions client/src/dashboard/Assets.vue
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,12 @@ function getCurrentPath(path?: string): string {
}
async function loadFolder(path: string): Promise<void> {
if (!socket.connected) socket.connect();
await assetSystem.loadFolder(path);
if (socket.connected) {
await assetSystem.loadFolder(path);
} else {
socket.connect();
socket.once("connect", () => assetSystem.loadFolder(path));
}
}
onMounted(async () => {
Expand Down
16 changes: 15 additions & 1 deletion client/src/game/ui/assets/AssetList.vue
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<script setup lang="ts">
import { computed, ref } from "vue";
import { computed, onMounted, ref } from "vue";
import { useI18n } from "vue-i18n";
import { assetSystem } from "../../../assets";
import type { AssetId } from "../../../assets/models";
import { useAssetSearch } from "../../../assets/search";
import { socket } from "../../../assets/socket";
import { assetState } from "../../../assets/state";
import AssetListCore from "../../../assets/ui/AssetListCore.vue";
import AssetUploadProgress from "../../../assets/ui/AssetUploadProgress.vue";
Expand All @@ -19,6 +20,19 @@ const assetsDialogFooter = ref<HTMLDivElement | null>(null);
const searchBar = ref<HTMLInputElement | null>(null);
const search = useAssetSearch(searchBar);
async function load(): Promise<void> {
await assetSystem.loadFolder(assetState.currentFolder.value);
}
onMounted(async () => {
if (socket.connected) {
await load();
} else {
socket.connect();
socket.once("connect", load);
}
});
const shortcuts = computed(() => {
const root = {
name: t("assets.all_assets"),
Expand Down

0 comments on commit 5b05fa0

Please sign in to comment.