refactor: store state

This commit is contained in:
pa
2025-10-11 15:30:44 +09:00
committed by Natsumi
parent 1e18d89b61
commit 86f7847c46
31 changed files with 2719 additions and 4029 deletions

View File

@@ -1,5 +1,5 @@
import { defineStore } from 'pinia';
import { computed, reactive, watch, nextTick } from 'vue';
import { ref, watch, nextTick } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import { avatarRequest, miscRequest } from '../api';
import { database } from '../service/database';
@@ -28,63 +28,47 @@ export const useAvatarStore = defineStore('Avatar', () => {
const advancedSettingsStore = useAdvancedSettingsStore();
const userStore = useUserStore();
const state = reactive({
avatarDialog: {
visible: false,
loading: false,
id: '',
memo: '',
ref: {},
isFavorite: false,
isBlocked: false,
isQuestFallback: false,
hasImposter: false,
imposterVersion: '',
isPC: false,
isQuest: false,
isIos: false,
bundleSizes: [],
platformInfo: {},
galleryImages: [],
galleryLoading: false,
lastUpdated: '',
inCache: false,
cacheSize: '',
cacheLocked: false,
cachePath: '',
fileAnalysis: []
},
avatarHistory: new Set(),
avatarHistoryArray: []
});
let cachedAvatarModerations = new Map();
let cachedAvatars = new Map();
let cachedAvatarNames = new Map();
const avatarDialog = computed({
get: () => state.avatarDialog,
set: (value) => {
state.avatarDialog = value;
}
});
const avatarHistory = state.avatarHistory;
const avatarHistoryArray = computed({
get: () => state.avatarHistoryArray,
set: (value) => {
state.avatarHistoryArray = value;
}
const avatarDialog = ref({
visible: false,
loading: false,
id: '',
memo: '',
ref: {},
isFavorite: false,
isBlocked: false,
isQuestFallback: false,
hasImposter: false,
imposterVersion: '',
isPC: false,
isQuest: false,
isIos: false,
bundleSizes: [],
platformInfo: {},
galleryImages: [],
galleryLoading: false,
lastUpdated: '',
inCache: false,
cacheSize: '',
cacheLocked: false,
cachePath: '',
fileAnalysis: []
});
const avatarHistory = ref(new Set());
const avatarHistoryArray = ref([]);
watch(
() => watchState.isLoggedIn,
(isLoggedIn) => {
state.avatarDialog.visible = false;
avatarDialog.value.visible = false;
cachedAvatars.clear();
cachedAvatarNames.clear();
cachedAvatarModerations.clear();
state.avatarHistory.clear();
state.avatarHistoryArray = [];
avatarHistory.value.clear();
avatarHistoryArray.value = [];
if (isLoggedIn) {
getAvatarHistory();
}
@@ -183,7 +167,7 @@ export const useAvatarStore = defineStore('Avatar', () => {
* @returns
*/
function showAvatarDialog(avatarId) {
const D = state.avatarDialog;
const D = avatarDialog.value;
D.visible = true;
D.loading = true;
D.id = avatarId;
@@ -266,7 +250,7 @@ export const useAvatarStore = defineStore('Avatar', () => {
* @returns {Promise<string[]>}
*/
async function getAvatarGallery(avatarId) {
const D = state.avatarDialog;
const D = avatarDialog.value;
const args = await avatarRequest
.getAvatarGallery(avatarId)
.finally(() => {
@@ -318,7 +302,7 @@ export const useAvatarStore = defineStore('Avatar', () => {
}
// update avatar dialog
const D = state.avatarDialog;
const D = avatarDialog.value;
if (
D.visible &&
ref.avatarModerationType === 'block' &&
@@ -331,7 +315,7 @@ export const useAvatarStore = defineStore('Avatar', () => {
}
function updateVRChatAvatarCache() {
const D = state.avatarDialog;
const D = avatarDialog.value;
if (D.visible) {
D.inCache = false;
D.cacheSize = '';
@@ -353,7 +337,7 @@ export const useAvatarStore = defineStore('Avatar', () => {
* @returns {Promise<void>}
*/
async function getAvatarHistory() {
state.avatarHistory = new Set();
avatarHistory.value = new Set();
const historyArray = await database.getAvatarHistory(
userStore.currentUser.id
);
@@ -363,9 +347,9 @@ export const useAvatarStore = defineStore('Avatar', () => {
continue;
}
applyAvatar(avatar);
state.avatarHistory.add(avatar.id);
avatarHistory.value.add(avatar.id);
}
state.avatarHistoryArray = historyArray;
avatarHistoryArray.value = historyArray;
}
/**
@@ -384,16 +368,16 @@ export const useAvatarStore = defineStore('Avatar', () => {
return;
}
const historyArray = state.avatarHistoryArray;
const historyArray = avatarHistoryArray.value;
for (let i = 0; i < historyArray.length; ++i) {
if (historyArray[i].id === ref.id) {
historyArray.splice(i, 1);
}
}
state.avatarHistoryArray.unshift(ref);
state.avatarHistory.delete(ref.id);
state.avatarHistory.add(ref.id);
avatarHistoryArray.value.unshift(ref);
avatarHistory.value.delete(ref.id);
avatarHistory.value.add(ref.id);
})
.catch((err) => {
console.error('Failed to add avatar to history:', err);
@@ -401,8 +385,8 @@ export const useAvatarStore = defineStore('Avatar', () => {
}
function clearAvatarHistory() {
state.avatarHistory = new Set();
state.avatarHistoryArray = [];
avatarHistory.value = new Set();
avatarHistoryArray.value = [];
database.clearAvatarHistory();
}
@@ -687,8 +671,6 @@ export const useAvatarStore = defineStore('Avatar', () => {
}
return {
state,
avatarDialog,
avatarHistory,
avatarHistoryArray,