summaryrefslogtreecommitdiffstats
path: root/apps/files_versions/src/views/VersionTab.vue
blob: 8159415dfc79fe27b0f1cd5a5f2c102121c58819 (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
<!--
 - @copyright 2022 Carl Schwan <carl@carlschwan.eu>
 - @license AGPL-3.0-or-later
 -
 - This program is free software: you can redistribute it and/or modify
 - it under the terms of the GNU Affero General Public License as
 - published by the Free Software Foundation, either version 3 of the
 - License, or (at your option) any later version.
 -
 - This program is distributed in the hope that it will be useful,
 - but WITHOUT ANY WARRANTY; without even the implied warranty of
 - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 - GNU Affero General Public License for more details.
 -
 - You should have received a copy of the GNU Affero General Public License
 - along with this program. If not, see <http://www.gnu.org/licenses/>.
 -->
<template>
	<div>
		<ul>
			<NcListItem v-for="version in versions"
				:key="version.mtime"
				class="version"
				:title="version.title"
				:href="version.url">
				<template #icon>
					<img lazy="true"
						:src="version.preview"
						alt=""
						height="256"
						width="256"
						class="version__image">
				</template>
				<template #subtitle>
					<div class="version__info">
						<span>{{ version.mtime | humanDateFromNow }}</span>
						<!-- Separate dot to improve alignement -->
						<span class="version__info__size">•</span>
						<span class="version__info__size">{{ version.size | humanReadableSize }}</span>
					</div>
				</template>
				<template v-if="!version.isCurrent" #actions>
					<NcActionLink :href="version.url"
						:download="version.url">
						<template #icon>
							<Download :size="22" />
						</template>
						{{ t('files_versions', 'Download version') }}
					</NcActionLink>
					<NcActionButton @click="restoreVersion(version)">
						<template #icon>
							<BackupRestore :size="22" />
						</template>
						{{ t('files_versions', 'Restore version') }}
					</NcActionButton>
				</template>
			</NcListItem>
			<NcEmptyContent v-if="!loading && versions.length === 1"
				:title="t('files_version', 'No versions yet')">
				<!-- length === 1, since we don't want to show versions if there is only the current file -->
				<template #icon>
					<BackupRestore />
				</template>
			</NcEmptyContent>
		</ul>
	</div>
</template>

<script>
import BackupRestore from 'vue-material-design-icons/BackupRestore.vue'
import Download from 'vue-material-design-icons/Download.vue'
import NcActionButton from '@nextcloud/vue/dist/Components/NcActionButton.js'
import NcActionLink from '@nextcloud/vue/dist/Components/NcActionLink.js'
import NcListItem from '@nextcloud/vue/dist/Components/NcListItem.js'
import NcEmptyContent from '@nextcloud/vue/dist/Components/NcEmptyContent.js'
import { showError, showSuccess } from '@nextcloud/dialogs'
import { fetchVersions, restoreVersion } from '../utils/versions.js'
import moment from '@nextcloud/moment'

export default {
	name: 'VersionTab',
	components: {
		NcEmptyContent,
		NcActionLink,
		NcActionButton,
		NcListItem,
		BackupRestore,
		Download,
	},
	filters: {
		humanReadableSize(bytes) {
			return OC.Util.humanFileSize(bytes)
		},
		humanDateFromNow(timestamp) {
			return moment(timestamp * 1000).fromNow()
		},
	},
	data() {
		return {
			fileInfo: null,
			/** @type {import('../utils/versions.js').Version[]} */
			versions: [],
			loading: false,
		}
	},
	methods: {
		/**
		 * Update current fileInfo and fetch new data
		 *
		 * @param {object} fileInfo the current file FileInfo
		 */
		async update(fileInfo) {
			this.fileInfo = fileInfo
			this.resetState()
			this.fetchVersions()
		},

		/**
		 * Get the existing versions infos
		 */
		async fetchVersions() {
			try {
				this.loading = true
				this.versions = await fetchVersions(this.fileInfo)
			} finally {
				this.loading = false
			}
		},

		/**
		 * Restore the given version
		 *
		 * @param version
		 */
		async restoreVersion(version) {
			try {
				await restoreVersion(version, this.fileInfo)
				// File info is not updated so we manually update its size and mtime if the restoration went fine.
				this.fileInfo.size = version.size
				this.fileInfo.mtime = version.lastmod
				showSuccess(t('files_versions', 'Version restored'))
				await this.fetchVersions()
			} catch (exception) {
				showError(t('files_versions', 'Could not restore version'))
			}
		},

		/**
		 * Reset the current view to its default state
		 */
		resetState() {
			this.versions = []
		},
	},
}
</script>

<style scopped lang="scss">
.version {
	display: flex;
	flex-direction: row;

	&__info {
		display: flex;
		flex-direction: row;
		align-items: center;
		gap: 0.5rem;

		&__size {
			color: var(--color-text-lighter);
		}
	}

	&__image {
		width: 3rem;
		height: 3rem;
		border: 1px solid var(--color-border);
		margin-right: 1rem;
		border-radius: var(--border-radius-large);
	}
}
</style>