summaryrefslogtreecommitdiffstats
path: root/src/components/feed-display/FeedItemDisplayList.vue
blob: 44e4b070a9c427747ce235b1dd121a9be333fbdc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
<template>
	<div class="feed-item-display-list">
		<div class="header">
			<NcActions class="filter-container" :force-menu="true">
				<template #icon>
					<FilterIcon />
				</template>
				<NcActionButton v-if="cfg.unreadFilter" @click="toggleFilter(unreadFilter)">
					<template #default>
						{{ t("news", "Unread") }}
					</template>
					<template #icon>
						<EyeIcon v-if="filter !== unreadFilter" />
						<EyeCheckIcon v-if="filter === unreadFilter" />
					</template>
				</NcActionButton>
				<NcActionButton v-if="cfg.starFilter" @click="toggleFilter(starFilter)">
					<template #default>
						{{ t("news", "Starred") }}
					</template>
					<template #icon>
						<StarIcon v-if="filter !== starFilter" />
						<StarCheckIcon v-if="filter === starFilter" />
					</template>
				</NcActionButton>
			</NcActions>
		</div>
		<div class="feed-item-display-container">
			<VirtualScroll :reached-end="reachedEnd"
				:fetch-key="fetchKey"
				@load-more="fetchMore()">
				<template v-if="items && items.length > 0">
					<template v-for="item in filterSortedItems()">
						<FeedItemRow :key="item.id" :item="item" />
					</template>
				</template>
			</VirtualScroll>

			<div v-if="selected !== undefined" class="feed-item-container">
				<FeedItemDisplay :item="selected" />
			</div>
		</div>
	</div>
</template>

<script lang="ts">
import Vue from 'vue'
import _ from 'lodash'

import FilterIcon from 'vue-material-design-icons/Filter.vue'
import StarIcon from 'vue-material-design-icons/Star.vue'
import StarCheckIcon from 'vue-material-design-icons/StarCheck.vue'
import EyeIcon from 'vue-material-design-icons/Eye.vue'
import EyeCheckIcon from 'vue-material-design-icons/EyeCheck.vue'

import NcActions from '@nextcloud/vue/dist/Components/NcActions.js'
import NcActionButton from '@nextcloud/vue/dist/Components/NcActionButton.js'

import VirtualScroll from './VirtualScroll.vue'
import FeedItemRow from './FeedItemRow.vue'
import FeedItemDisplay from './FeedItemDisplay.vue'

import { FeedItem } from '../../types/FeedItem'

const DEFAULT_DISPLAY_LIST_CONFIG = {
	starFilter: true,
	unreadFilter: true,
}

export default Vue.extend({
	components: {
		VirtualScroll,
		FeedItemRow,
		FeedItemDisplay,
		FilterIcon,
		StarIcon,
		StarCheckIcon,
		EyeIcon,
		EyeCheckIcon,
		NcActions,
		NcActionButton,
	},
	props: {
		items: {
			type: Array<FeedItem>,
			required: true,
		},
		fetchKey: {
			type: String,
			required: true,
		},
		config: {
			type: Object,
			default: () => {
				return DEFAULT_DISPLAY_LIST_CONFIG
			},
		},
	},
	data() {
		return {
			mounted: false,

			// no filter to start
			filter: () => { return true as boolean },

			// Always want to sort by date (most recent first)
			sort: (a: FeedItem, b: FeedItem) => {
				if (a.pubDate > b.pubDate) {
					return -1
				} else {
					return 1
				}
			},
			cache: [] as FeedItem[] | undefined,
		}
	},
	computed: {
		selected(): FeedItem | undefined {
			return this.$store.getters.selected
		},
		reachedEnd(): boolean {
			return this.mounted && this.$store.state.items.allItemsLoaded[this.fetchKey] === true
		},
		cfg() {
			return _.defaults({ ...this.config }, DEFAULT_DISPLAY_LIST_CONFIG)
		},
	},
	mounted() {
		this.mounted = true
	},
	methods: {
		fetchMore() {
			this.$emit('load-more')
		},
		noFilter(): boolean {
			return true
		},
		starFilter(item: FeedItem): boolean {
			return item.starred
		},
		unreadFilter(item: FeedItem): boolean {
			return item.unread
		},
		toggleFilter(filter: () => boolean) {
			if (this.filter === filter) {
				this.filter = this.noFilter
				if (filter === this.unreadFilter) {
					this.cache = undefined
				}
			} else {
				this.filter = filter as () => boolean
			}
		},
		filterSortedItems(): FeedItem[] {
			let response = [...this.items] as FeedItem[]

			// if we're filtering on unread, we want to cache the unread items when the user presses the filter button
			// that way when the user opens an item, it won't be removed from the displayed list of items (once it's no longer unread)
			if (this.filter === this.unreadFilter) {
				if (!this.cache) {
					if (this.items.length > 0) {
						this.cache = this.items.filter(this.unreadFilter)
					}
				} else if (this.items.length > (this.cache?.length)) {
					for (const item of this.items) {
						if (item.unread && this.cache.find((unread: FeedItem) => unread.id === item.id) === undefined) {
							this.cache.push(item)
						}
					}
				}
				response = [...this.cache as FeedItem[]]
			} else {
				response = response.filter(this.filter)
			}

			return response.sort(this.sort)
		},
	},
})
</script>

<style scoped>
	.feed-item-display-list {
		height: 100%;
		display: flex;
		flex-direction: column;
	}

	.virtual-scroll {
		border-top: 1px solid var(--color-border);
		width: 100%;
	}

	.feed-item-display-container {
		display: flex;
		height: 100%;
	}

	.feed-item-container {
		max-width: 50%;
		overflow-y: hidden;
		height: calc(100vh - 50px - 50px - 10px)
	}

	.header {
		justify-content: right;
		display: flex;
	}

	.filter-container {
		padding: 5px;
	}
</style>