summaryrefslogtreecommitdiffstats
path: root/src/components/feed-display/FeedItemRow.vue
blob: 34a8d18b897c0a898263b3f105ee238d31d3f2a9 (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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
<template>
	<div class="feed-item-row" @click="select()">
		<div class="link-container">
			<a class="external"
				target="_blank"
				rel="noreferrer"
				:href="item.url"
				:title="t('news', 'Open website')"
				@click="markRead(item); $event.stopPropagation();">
				<EarthIcon />
			</a>
			<RssIcon v-if="!getFeed(item.feedId).faviconLink" />
			<span v-if="getFeed(item.feedId).faviconLink" style="width: 24px; background-size: contain;" :style="{ 'backgroundImage': 'url(' + getFeed(item.feedId).faviconLink + ')' }" />
		</div>
		<div class="title-container" :class="{ 'unread': item.unread }">
			<span style="white-space: nowrap" :dir="item.rtl && 'rtl'">
				{{ item.title }}
				<span class="intro" v-html="item.intro" />
			</span>
		</div>
		<div class="date-container">
			<time class="date" :title="formatDate(item.pubDate*1000, 'yyyy-MM-dd HH:mm:ss')" :datetime="formatDatetime(item.pubDate*1000, 'yyyy-MM-ddTHH:mm:ssZ')">
				{{ getRelativeTimestamp(item.pubDate*1000) }}
			</time>
		</div>
		<div class="button-container" @click="$event.stopPropagation()">
			<StarIcon :class="{'starred': item.starred }" @click="toggleStarred(item)" />
			<EyeIcon v-if="item.unread && !keepUnread" @click="toggleKeepUnread(item)" />
			<EyeCheckIcon v-if="!item.unread && !keepUnread" @click="toggleKeepUnread(item)" />
			<EyeLockIcon v-if="keepUnread" class="keep-unread" @click="toggleKeepUnread(item)" />
			<NcActions :force-menu="true">
				<template #icon>
					<ShareVariant />
				</template>
				<NcActionButton>
					<template #default>
						<!-- TODO: Share Menu --> TODO
					</template>
					<template #icon>
						<ShareVariant />
					</template>
				</NcActionButton>
			</NcActions>
		</div>
	</div>
</template>

<script lang="ts">
import Vue from 'vue'
import { mapState } from 'vuex'

import EarthIcon from 'vue-material-design-icons/Earth.vue'
import StarIcon from 'vue-material-design-icons/Star.vue'
import EyeIcon from 'vue-material-design-icons/Eye.vue'
import EyeCheckIcon from 'vue-material-design-icons/EyeCheck.vue'
import EyeLockIcon from 'vue-material-design-icons/EyeLock.vue'
import RssIcon from 'vue-material-design-icons/Rss.vue'
import ShareVariant from 'vue-material-design-icons/ShareVariant.vue'

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

import { Feed } from '../../types/Feed'
import { FeedItem } from '../../types/FeedItem'
import { ACTIONS, MUTATIONS } from '../../store'

export default Vue.extend({
	name: 'FeedItemRow',
	components: {
		EarthIcon,
		StarIcon,
		EyeIcon,
		EyeCheckIcon,
		EyeLockIcon,
		ShareVariant,
		RssIcon,
		NcActions,
		NcActionButton,
	},
	props: {
		item: {
			type: Object,
			required: true,
		},
	},
	data: () => {
		return {
			keepUnread: false,
		}
	},
	computed: {
		...mapState(['feeds']),
	},
	methods: {
		select(): void {
			this.$store.commit(MUTATIONS.SET_SELECTED_ITEM, { id: this.item.id })
			this.markRead(this.item)
		},
		formatDate(epoch: number): string {
			return new Date(epoch).toLocaleString()
		},
		formatDatetime(epoch: number): string {
			return new Date(epoch).toISOString()
		},
		getRelativeTimestamp(previous: number): string {
			const current = Date.now()

			const msPerMinute = 60 * 1000
			const msPerHour = msPerMinute * 60
			const msPerDay = msPerHour * 24
			const msPerMonth = msPerDay * 30
			const msPerYear = msPerDay * 365

			const elapsed = current - previous

			if (elapsed < msPerMinute) {
				return Math.round(elapsed / 1000) + ' ' + t('news', 'seconds')
			} else if (elapsed < msPerHour) {
				return Math.round(elapsed / msPerMinute) + ' ' + t('news', 'minutes ago')
			} else if (elapsed < msPerDay) {
				return Math.round(elapsed / msPerHour) + ' ' + t('news', 'hours ago')
			} else if (elapsed < msPerMonth) {
				return Math.round(elapsed / msPerDay) + ' ' + t('news', 'days ago')
			} else if (elapsed < msPerYear) {
				return Math.round(elapsed / msPerMonth) + ' ' + t('news', 'months ago')
			} else {
				return Math.round(elapsed / msPerYear) + ' ' + t('news', 'years ago')
			}
		},
		getFeed(id: number): Feed {
			return this.$store.getters.feeds.find((feed: Feed) => feed.id === id) || {}
		},
		markRead(item: FeedItem): void {
			if (!this.keepUnread) {
				this.$store.dispatch(ACTIONS.MARK_READ, { item })
			}
		},
		toggleKeepUnread(item: FeedItem): void {
			this.keepUnread = !this.keepUnread
			this.$store.dispatch(ACTIONS.MARK_UNREAD, { item })
		},
		toggleStarred(item: FeedItem): void {
			this.$store.dispatch(item.starred ? ACTIONS.UNSTAR_ITEM : ACTIONS.STAR_ITEM, { item })
		},
	},
})

</script>

<style>
	.feed-item-container {
		border-bottom: 1px solid #222;
	}

	.feed-item-row {
		display: flex; padding: 5px 10px;
	}

	.feed-item-row:hover {
		background-color: var(--color-background-hover);
	}

	.feed-item-row, .feed-item-row * {
		cursor: pointer;
	}

	.feed-item-row .link-container {
		padding-right: 5px;
		display: flex;
		flex-direction: row;
		align-self: start;
	}

	.feed-item-row .title-container {
		color: var(--color-text-lighter);

		flex-grow: 1;
		overflow: hidden;
		text-overflow: ellipsis;
	}

	.feed-item-row .title-container.unread {
		color: var(--color-main-text);
    font-weight: bold;
	}

	.feed-item-row .intro {
		color: var(--color-text-lighter);
    font-size: 10pt;
    font-weight: normal;
    margin-left: 20px;
	}

	.feed-item-row .date-container {
		color: var(--color-text-lighter);
		padding-left: 4px;
		white-space: nowrap;
	}

	.feed-item-row .button-container {
		display: flex;
		flex-direction: row;
		align-self: start;
	}

	.feed-item-row .button-container .button-vue, .feed-item-row .button-container .button-vue .button-vue__wrapper, .feed-item-row .button-container .material-design-icon {
		width: 30px !important;
    min-width: 30px;
    min-height: 30px;
    height: 30px;
	}

	.feed-item-row .button-container .material-design-icon {
		color: var(--color-text-lighter)
	}

	.feed-item-row .button-container .material-design-icon:hover {
		color: var(--color-text-light);
	}

	.feed-item-row .button-container .material-design-icon.rss-icon:hover {
		color: #555555;
	}

	.material-design-icon.starred {
		color: rgb(255, 204, 0) !important;
	}

	.feed-item-row .button-container .material-design-icon.keep-unread {
		color: var(--color-main-text);
	}

	.material-design-icon.starred:hover {
		color: #555555;
	}

	.feed-item-row .button-container .eye-check-icon {
		color: var(--color-primary-light);
	}
</style>