Skip to content

Commit 2ba7df7

Browse files
committed
whattheduck: Improve performance when changing navigation
1 parent a2a4bb4 commit 2ba7df7

File tree

2 files changed

+36
-21
lines changed

2 files changed

+36
-21
lines changed

apps/whattheduck/src/stores/app.ts

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,17 @@ export interface FabOption {
1414
getTextToCopy?: () => Promise<string>;
1515
}
1616

17+
type NavigationItem =
18+
| { type: 'all'; value: 'all' }
19+
| { type: 'countrycode' | 'publicationcode'; value: string }
20+
| { type: 'issuecodes'; value: string[] };
21+
22+
const navigationItemsEqual = (a: NavigationItem, b: NavigationItem) =>
23+
a.type === b.type &&
24+
(Array.isArray(a.value) && Array.isArray(b.value)
25+
? a.value.every((value, index) => value === b.value[index])
26+
: a.value === b.value);
27+
1728
export const app = defineStore('app', () => {
1829
const offlineBannerHeight = ref(0);
1930
const socket = ref<ReturnType<typeof useDmSocket>>();
@@ -36,11 +47,7 @@ export const app = defineStore('app', () => {
3647

3748
const isCoaView = ref(route.hash.startsWith('#coa-'));
3849

39-
const currentNavigationItem = ref<
40-
| { type: 'all'; value: 'all' }
41-
| { type: 'countrycode' | 'publicationcode'; value: string }
42-
| { type: 'issuecodes'; value: string[] }
43-
>({ type: 'all', value: 'all' });
50+
const currentNavigationItem = ref<NavigationItem>({ type: 'all', value: 'all' });
4451

4552
watch(
4653
() => route.hash,
@@ -52,22 +59,20 @@ export const app = defineStore('app', () => {
5259
.replace(/^#(coa-)?/, '')
5360
.replaceAll('_', ' ')
5461
.split('=');
55-
if (parts.length === 1) {
56-
currentNavigationItem.value = { type: 'all', value: 'all' };
57-
} else {
58-
const [type, value] = parts as ['countrycode' | 'publicationcode' | 'issuecodes', string];
59-
if (type === 'issuecodes') {
60-
currentNavigationItem.value = {
61-
type,
62-
value: value.split(','),
63-
};
64-
} else {
65-
currentNavigationItem.value = {
66-
type,
67-
value,
68-
};
69-
}
62+
const nextItem =
63+
parts.length === 1
64+
? ({ type: 'all', value: 'all' } as const)
65+
: (() => {
66+
const [type, value] = parts as ['countrycode' | 'publicationcode' | 'issuecodes', string];
67+
if (type === 'issuecodes') {
68+
return { type, value: value.split(',') } as const;
69+
}
70+
return { type, value } as const;
71+
})();
72+
if (navigationItemsEqual(currentNavigationItem.value, nextItem)) {
73+
return;
7074
}
75+
currentNavigationItem.value = nextItem;
7176
},
7277
{ immediate: true },
7378
);
@@ -188,6 +193,9 @@ export const app = defineStore('app', () => {
188193
)?.replace(/ /g, '_');
189194
const hash = (isCoaView.value ? '#coa-' : '#') + (navigationItem?.type ? `${navigationItem?.type}=${value}` : '');
190195
if (route.name === 'Collection') {
196+
if (route.hash === hash) {
197+
return;
198+
}
191199
window.location.hash = hash;
192200
} else {
193201
await router.push({

apps/whattheduck/src/views/IssueList.vue

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,14 +211,21 @@ const items = computed(() => {
211211
}
212212
});
213213
214+
const coaIssueIndexByCode = computed(() =>
215+
coaIssuecodes.value
216+
? (Object.fromEntries(coaIssuecodes.value.map((issuecode, index) => [issuecode, index])) as Record<string, number>)
217+
: undefined,
218+
);
219+
214220
const sortedItems = computed(
215221
() =>
216222
coaIssuecodes.value &&
223+
coaIssueIndexByCode.value &&
217224
items.value
218225
?.map(({ key, item }) => ({
219226
key,
220227
item,
221-
indexInCoaList: coaIssuecodes.value.indexOf(item.issuecode),
228+
indexInCoaList: coaIssueIndexByCode.value?.[item.issuecode] ?? -1,
222229
isOwned: (item as (typeof userIssues.value)[0]).condition !== undefined,
223230
}))
224231

0 commit comments

Comments
 (0)