1
1
<script lang="ts" setup>
2
- import { NTimeline , NVirtualList } from ' naive-ui ' ;
2
+ import { onActivated , onDeactivated , onMounted , ref , watch } from ' vue ' ;
3
3
4
- import { onActivated , onMounted , ref , Transition , watch } from ' vue' ;
4
+ import type { WatchStopHandle } from ' vue' ;
5
5
6
- import type { VirtualListInst } from ' naive-ui' ;
7
- import type { TraktHistory } from ' ~/models/trakt/trakt-history.model' ;
6
+ import type { OnScroll , OnUpdated } from ' ~/components/common/list/ListScroll.model' ;
8
7
9
- import HistoryEmpty from ' ~/components/views/history/HistoryEmpty .vue' ;
8
+ import ListScroll from ' ~/components/common/list/ListScroll .vue' ;
10
9
import HistoryItem from ' ~/components/views/history/HistoryItem.vue' ;
11
10
12
11
import { useHistoryStore , useHistoryStoreRefs } from ' ~/stores/data/history.store' ;
@@ -23,93 +22,52 @@ const { fetchHistory } = useHistoryStore();
23
22
24
23
const { user } = useUserSettingsStoreRefs ();
25
24
26
- const virtualList = ref <VirtualListInst & typeof NVirtualList >();
25
+ onMounted (() => {
26
+ fetchHistory ();
27
+ });
27
28
28
- const onScroll = async (e : Event ) => {
29
- if (loading .value ) return ;
30
- if (! e ?.target ) return ;
31
- const { scrollTop, scrollHeight, clientHeight } = e .target as HTMLDivElement ;
32
- if (! scrollTop || scrollHeight !== scrollTop + clientHeight ) return ;
33
- if (pagination .value ?.page === pagination .value ?.pageCount ) return ;
29
+ const watcher = ref <WatchStopHandle >();
30
+
31
+ onActivated (() => {
32
+ watcher .value = watch (user , () => fetchHistory ());
33
+ });
34
34
35
+ onDeactivated (() => {
36
+ watcher .value ?.();
37
+ });
38
+
39
+ const onScroll: OnScroll = async listRef => {
35
40
const key = history .value [history .value .length - 1 ].id ;
36
41
await fetchHistory ({
37
42
page: pagination .value ?.page ? pagination .value .page + 1 : 0 ,
38
43
});
39
- virtualList .value ?.scrollTo ({ key , debounce: true });
44
+ listRef .value ?.scrollTo ({ key , debounce: true });
40
45
};
41
46
42
- onMounted (() => {
43
- console .info (' History mounted' );
44
- fetchHistory ();
45
-
46
- watch (user , () => {
47
- console .info (' User Change - re fetching' );
48
- fetchHistory ();
49
- });
50
- });
51
-
52
- onActivated (() => {
53
- console .info (' History activated' );
54
- });
55
-
56
47
/**
57
48
* This is a workaround for the onUpdated lifecycle hook not triggering when wrapped in transition.
58
49
*/
59
- const onUpdated = () => {
60
- const { scrollHeight, clientHeight } = virtualList .value ?.$el ?.firstElementChild ?? {};
50
+ const onUpdated: OnUpdated = listRef => {
51
+ const { scrollHeight, clientHeight } = listRef .value ?.$el ?.firstElementChild ?? {};
61
52
if (scrollHeight !== clientHeight || ! belowThreshold .value || loading .value ) return ;
62
53
63
54
return fetchHistory ({
64
55
page: pagination .value ?.page ? pagination .value .page + 1 : 0 ,
65
56
});
66
57
};
67
-
68
- const getTitle = (media : TraktHistory ) => {
69
- if (' movie' in media ) return media .movie .title ;
70
- const number = media .episode ?.number ?.toString ().padStart (2 , ' 0' );
71
- return ` ${media .episode ?.season }x${number } - ${media ?.episode ?.title } ` ;
72
- };
73
58
</script >
74
59
75
60
<template >
76
- <Transition name =" fade" mode =" out-in" >
77
- <NVirtualList
78
- v-if =" history.length || loading"
79
- ref =" virtualList"
80
- class =" history-list"
81
- :item-size =" 80"
82
- :data-length =" history.length"
83
- :data-page-size =" pageSize"
84
- :items =" history"
85
- :visible-items-tag =" NTimeline"
86
- :visible-items-tag-props =" { size: 'large' }"
87
- :padding-top =" 56"
88
- :padding-bottom =" 16"
89
- @scroll =" onScroll"
90
- @vue:updated =" onUpdated"
91
- >
92
- <template #default =" { item , index } " >
93
- <HistoryItem :item =" item" :index =" index" />
94
- </template >
95
- </NVirtualList >
96
- <HistoryEmpty
97
- v-else
98
- :page =" pagination?.page"
99
- :page-count =" pagination?.pageCount"
100
- :page-size =" pageSize"
101
- />
102
- </Transition >
61
+ <ListScroll
62
+ :items =" history"
63
+ :loading =" loading"
64
+ :pagination =" pagination"
65
+ :page-size =" pageSize"
66
+ @on-scroll =" onScroll"
67
+ @on-updated =" onUpdated"
68
+ >
69
+ <template #default =" { item , index } " >
70
+ <HistoryItem :item =" item" :index =" index" />
71
+ </template >
72
+ </ListScroll >
103
73
</template >
104
-
105
- <style lang="scss" scoped>
106
- @use ' ~/styles/layout' as layout ;
107
- @use ' ~/styles/transition' as transition ;
108
- @include transition .fade ;
109
-
110
- .history-list {
111
- height : calc (100 dvh - 8px );
112
- margin-top : - #{layout .$header-navbar-height } ;
113
- margin-bottom : 8px ;
114
- }
115
- </style >
0 commit comments