Fix favorite sorting

This commit is contained in:
Natsumi
2025-11-22 18:07:39 +11:00
parent e2f41b09d6
commit 22733095d6

View File

@@ -90,31 +90,27 @@ export const useFavoriteStore = defineStore('Favorite', () => {
currentGroup: {} currentGroup: {}
}); });
const favoritesSortOrder = ref([]);
const favoriteFriends = computed(() => { const favoriteFriends = computed(() => {
if (appearanceSettingsStore.sortFavorites) { if (appearanceSettingsStore.sortFavorites) {
return state.favoriteFriends_; return state.favoriteFriends_.sort(compareByFavoriteSortOrder);
} }
const sorted = [...state.favoriteFriends_]; return state.favoriteFriends_.sort(compareByName);
sorted.sort(compareByName);
return sorted;
}); });
const favoriteWorlds = computed(() => { const favoriteWorlds = computed(() => {
if (appearanceSettingsStore.sortFavorites) { if (appearanceSettingsStore.sortFavorites) {
return state.favoriteWorlds_; return state.favoriteWorlds_.sort(compareByFavoriteSortOrder);
} }
const sorted = [...state.favoriteWorlds_]; return state.favoriteWorlds_.sort(compareByName);
sorted.sort(compareByName);
return sorted;
}); });
const favoriteAvatars = computed(() => { const favoriteAvatars = computed(() => {
if (appearanceSettingsStore.sortFavorites) { if (appearanceSettingsStore.sortFavorites) {
return state.favoriteAvatars_; return state.favoriteAvatars_.sort(compareByFavoriteSortOrder);
} }
const sorted = [...state.favoriteAvatars_]; return state.favoriteAvatars_.sort(compareByName);
sorted.sort(compareByName);
return sorted;
}); });
watch( watch(
@@ -268,9 +264,11 @@ export const useFavoriteStore = defineStore('Favorite', () => {
json: args.json, json: args.json,
params: { params: {
favoriteId: args.json.id favoriteId: args.json.id
}, }
sortTop: true
}); });
if (!favoritesSortOrder.value.includes(args.params.favoriteId)) {
favoritesSortOrder.value.unshift(args.params.favoriteId);
}
if ( if (
args.params.type === 'avatar' && args.params.type === 'avatar' &&
@@ -292,7 +290,7 @@ export const useFavoriteStore = defineStore('Favorite', () => {
function handleFavorite(args) { function handleFavorite(args) {
args.ref = applyFavoriteCached(args.json); args.ref = applyFavoriteCached(args.json);
applyFavorite(args.ref.type, args.ref.favoriteId, args.sortTop); applyFavorite(args.ref.type, args.ref.favoriteId);
friendStore.updateFriend(args.ref.favoriteId); friendStore.updateFriend(args.ref.favoriteId);
const { ref } = args; const { ref } = args;
const userDialog = userStore.userDialog; const userDialog = userStore.userDialog;
@@ -358,6 +356,9 @@ export const useFavoriteStore = defineStore('Favorite', () => {
cachedFavorites.delete(ref.id); cachedFavorites.delete(ref.id);
state.favoriteObjects.delete(ref.favoriteId); state.favoriteObjects.delete(ref.favoriteId);
friendStore.localFavoriteFriends.delete(ref.favoriteId); friendStore.localFavoriteFriends.delete(ref.favoriteId);
favoritesSortOrder.value = favoritesSortOrder.value.filter(
(id) => id !== ref.favoriteId
);
friendStore.updateFriend(ref.favoriteId); friendStore.updateFriend(ref.favoriteId);
friendStore.updateSidebarFavorites(); friendStore.updateSidebarFavorites();
@@ -381,10 +382,9 @@ export const useFavoriteStore = defineStore('Favorite', () => {
* *
* @param {'friend' | 'world' | 'avatar'} type * @param {'friend' | 'world' | 'avatar'} type
* @param {string} objectId * @param {string} objectId
* @param {boolean} sortTop
* @returns {Promise<void>} * @returns {Promise<void>}
*/ */
async function applyFavorite(type, objectId, sortTop = false) { async function applyFavorite(type, objectId) {
let ref; let ref;
const favorite = getCachedFavoritesByObjectId(objectId); const favorite = getCachedFavoritesByObjectId(objectId);
let ctx = state.favoriteObjects.get(objectId); let ctx = state.favoriteObjects.get(objectId);
@@ -502,15 +502,7 @@ export const useFavoriteStore = defineStore('Favorite', () => {
} }
} }
if (isTypeChanged) { if (isTypeChanged) {
if (sortTop) { if (type === 'friend') {
if (type === 'friend') {
state.favoriteFriends_.unshift(ctx);
} else if (type === 'world') {
state.favoriteWorlds_.unshift(ctx);
} else if (type === 'avatar') {
state.favoriteAvatars_.unshift(ctx);
}
} else if (type === 'friend') {
state.favoriteFriends_.push(ctx); state.favoriteFriends_.push(ctx);
} else if (type === 'world') { } else if (type === 'world') {
state.favoriteWorlds_.push(ctx); state.favoriteWorlds_.push(ctx);
@@ -676,38 +668,36 @@ export const useFavoriteStore = defineStore('Favorite', () => {
} catch (err) { } catch (err) {
console.error(err); console.error(err);
} }
const previousFavoriteIds = new Set(); let newFavoriteSortOrder = [];
for (const ref of cachedFavorites.values()) {
previousFavoriteIds.add(ref.favoriteId);
}
let newFavoriteIds = new Set();
processBulk({ processBulk({
fn: favoriteRequest.getFavorites, fn: favoriteRequest.getFavorites,
N: -1, N: -1,
params: { params: {
n: 50, n: 300,
offset: 0 offset: 0
}, },
handle(args) { handle(args) {
for (const json of args.json) { for (const json of args.json) {
newFavoriteIds.add(json.favoriteId); newFavoriteSortOrder.push(json.favoriteId);
handleFavorite({ handleFavorite({
json, json,
params: { params: {
favoriteId: json.id favoriteId: json.id
}, }
sortTop: false
}); });
} }
}, },
done(ok) { done(ok) {
if (ok) { if (ok) {
for (const objectId of previousFavoriteIds) { for (const id of favoritesSortOrder.value) {
const fav = getCachedFavoritesByObjectId(objectId); if (!newFavoriteSortOrder.includes(id)) {
if (!newFavoriteIds.has(objectId) && fav) { const fav = cachedFavorites.get(id);
handleFavoriteAtDelete(fav); if (fav) {
handleFavoriteAtDelete(fav);
}
} }
} }
favoritesSortOrder.value = newFavoriteSortOrder;
} }
refreshFavoriteItems(); refreshFavoriteItems();
refreshFavoriteGroups(); refreshFavoriteGroups();
@@ -790,9 +780,8 @@ export const useFavoriteStore = defineStore('Favorite', () => {
* @param tag * @param tag
*/ */
async function refreshFavoriteAvatars(tag) { async function refreshFavoriteAvatars(tag) {
const n = Math.floor(Math.random() * (50 + 1)) + 50;
const params = { const params = {
n, n: 300,
offset: 0, offset: 0,
tag tag
}; };
@@ -824,26 +813,24 @@ export const useFavoriteStore = defineStore('Favorite', () => {
if (N > 0) { if (N > 0) {
if (type === 'avatar') { if (type === 'avatar') {
for (const tag of tags) { for (const tag of tags) {
const n = Math.floor(Math.random() * (50 + 1)) + 50;
processBulk({ processBulk({
fn, fn,
N, N,
handle: (args) => handleFavoriteAvatarList(args), handle: (args) => handleFavoriteAvatarList(args),
params: { params: {
n, n: 300,
offset: 0, offset: 0,
tag tag
} }
}); });
} }
} else { } else {
const n = Math.floor(Math.random() * (36 + 1)) + 64;
processBulk({ processBulk({
fn, fn,
N, N,
handle: (args) => handleFavoriteWorldList(args), handle: (args) => handleFavoriteWorldList(args),
params: { params: {
n, n: 300,
offset: 0 offset: 0
} }
}); });
@@ -1446,6 +1433,12 @@ export const useFavoriteStore = defineStore('Favorite', () => {
getLocalAvatarFavorites(); getLocalAvatarFavorites();
} }
function compareByFavoriteSortOrder(a, b) {
const indexA = favoritesSortOrder.value.indexOf(a.id);
const indexB = favoritesSortOrder.value.indexOf(b.id);
return indexA - indexB;
}
return { return {
state, state,
@@ -1480,6 +1473,7 @@ export const useFavoriteStore = defineStore('Favorite', () => {
selectedFavoriteAvatars, selectedFavoriteAvatars,
localWorldFavGroupLength, localWorldFavGroupLength,
localAvatarFavGroupLength, localAvatarFavGroupLength,
favoritesSortOrder,
initFavorites, initFavorites,
applyFavorite, applyFavorite,