Skip to content

Commit e25e06c

Browse files
committed
fix(list): only trigger updated when view is active
1 parent 88429b2 commit e25e06c

File tree

4 files changed

+43
-25
lines changed

4 files changed

+43
-25
lines changed

src/components/common/list/ListScroll.vue

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<script lang="ts" setup>
22
import { NFlex, NTimeline, NVirtualList } from 'naive-ui';
33
4-
import { ref, toRefs } from 'vue';
4+
import { computed, ref, toRefs } from 'vue';
55
66
import type { TraktClientPagination } from '@dvcol/trakt-http-client/models';
77
import type { PropType, Ref, Transition } from 'vue';
@@ -141,12 +141,13 @@ const onHover = ({ item, hover }: { item: ListScrollItem; hover: boolean }) => {
141141
const onLoadMore = (payload: { page: number; pageCount: number; pageSize: number }) => {
142142
emits('onloadMore', { listRef, ...payload });
143143
};
144+
const isEmpty = computed(() => !items.value.length && !loading.value);
144145
</script>
145146

146147
<template>
147148
<Transition name="fade" mode="out-in">
148149
<NVirtualList
149-
v-if="items.length || loading"
150+
v-if="!isEmpty"
150151
ref="listRef"
151152
class="list-scroll"
152153
:data-length="items.length"

src/components/common/list/use-list-scroll.ts

+17-2
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,14 @@ export const useListScrollEvents = (
235235
pagination,
236236
loading,
237237
belowThreshold,
238-
}: { data: Ref<ListScrollItem[]>; pagination: Ref<TraktClientPagination | undefined>; loading: Ref<boolean>; belowThreshold?: Ref<boolean> },
238+
active,
239+
}: {
240+
data: Ref<ListScrollItem[]>;
241+
pagination: Ref<TraktClientPagination | undefined>;
242+
loading: Ref<boolean>;
243+
belowThreshold?: Ref<boolean>;
244+
active?: Ref<boolean>;
245+
},
239246
) => {
240247
const onScroll: OnScroll = async listRef => {
241248
const key = data.value[data.value.length - 1].id;
@@ -252,10 +259,18 @@ export const useListScrollEvents = (
252259

253260
/**
254261
* This is a workaround for the onUpdated lifecycle hook not triggering when wrapped in transition.
262+
* It fires when the list is updated and checks if the list is scrolled to the bottom to trigger the next page load.
255263
*/
256264
const onUpdated: OnUpdated = listRef => {
257265
const { scrollHeight, clientHeight } = listRef.value?.$el?.firstElementChild ?? {};
258-
if (scrollHeight !== clientHeight || !belowThreshold?.value || loading.value) return;
266+
// If already loading we don't trigger
267+
if (loading.value) return;
268+
// If the list is not scrolled to the bottom we don't trigger
269+
if (scrollHeight !== clientHeight) return;
270+
// If the list is not below the threshold we don't trigger
271+
if (belowThreshold?.value === false) return;
272+
// If the view is not currently active we don't trigger
273+
if (active?.value === false) return;
259274

260275
return onLoadMore();
261276
};

src/components/views/history/HistoryComponent.vue

+14-13
Original file line numberDiff line numberDiff line change
@@ -25,34 +25,35 @@ const { isWatching } = useWatchingStoreRefs();
2525
2626
const i18n = useI18n('history');
2727
28-
watchUserChange({
28+
const { active } = watchUserChange({
2929
fetch: fetchHistory,
3030
clear: clearState,
3131
});
3232
33-
onMounted(() => {
34-
watch(panelOpen, async value => {
35-
if (!value && panelDirty.value) await fetchHistory();
36-
});
37-
watch(isWatching, async () => {
38-
if (panelOpen.value) return;
39-
await fetchHistory();
40-
});
41-
});
42-
4333
const list = useListScroll(filteredHistory, 'watched_at');
4434
4535
const history = addLoadMore(list, pagination, searchHistory);
4636
37+
const { scrolled, listRef, onClick } = useBackToTop();
38+
const { onItemClick } = usePanelItem();
39+
4740
const { onScroll, onUpdated, onLoadMore } = useListScrollEvents(fetchHistory, {
4841
data: history,
4942
pagination,
5043
loading,
5144
belowThreshold,
45+
active,
5246
});
5347
54-
const { scrolled, listRef, onClick } = useBackToTop();
55-
const { onItemClick } = usePanelItem();
48+
onMounted(() => {
49+
watch(panelOpen, async value => {
50+
if (!value && panelDirty.value) await fetchHistory();
51+
});
52+
watch(isWatching, async () => {
53+
if (panelOpen.value) return;
54+
await fetchHistory();
55+
});
56+
});
5657
</script>
5758

5859
<template>

src/components/views/watchlist/WatchlistComponent.vue

+9-8
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,11 @@ const { activeList } = useListsStoreRefs();
4141
const { scrolled, listRef, onClick } = useBackToTop();
4242
const { onItemClick } = usePanelItem();
4343
44-
watchUserChange({
44+
const { active } = watchUserChange({
4545
fetch: fetchListItems,
4646
clear: clearState,
4747
});
4848
49-
onMounted(() => {
50-
watch(panelOpen, async value => {
51-
if (!value && panelDirty.value) await fetchListItems();
52-
});
53-
watch(activeList, () => onClick());
54-
});
55-
5649
const list = useListScroll<AnyList, AnyListDateTypes>(
5750
filteredListItems,
5851
anyListDateGetter,
@@ -65,6 +58,14 @@ const { onScroll, onUpdated, onLoadMore } = useListScrollEvents(fetchListItems,
6558
pagination,
6659
loading: listLoading,
6760
belowThreshold,
61+
active,
62+
});
63+
64+
onMounted(() => {
65+
watch(panelOpen, async value => {
66+
if (!value && panelDirty.value) await fetchListItems();
67+
});
68+
watch(activeList, () => onClick());
6869
});
6970
</script>
7071

0 commit comments

Comments
 (0)