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
|
<template>
<NcAppNavigationItem v-if="storageStats"
:aria-label="t('files', 'Storage informations')"
:class="{ 'app-navigation-entry__settings-quota--not-unlimited': storageStats.quota >= 0}"
:loading="loadingStorageStats"
:name="storageStatsTitle"
:title="storageStatsTooltip"
class="app-navigation-entry__settings-quota"
data-cy-files-navigation-settings-quota
@click.stop.prevent="debounceUpdateStorageStats">
<ChartPie slot="icon" :size="20" />
<!-- Progress bar -->
<NcProgressBar v-if="storageStats.quota >= 0"
slot="extra"
:error="storageStats.relative > 80"
:value="Math.min(storageStats.relative, 100)" />
</NcAppNavigationItem>
</template>
<script>
import { formatFileSize } from '@nextcloud/files'
import { generateUrl } from '@nextcloud/router'
import { loadState } from '@nextcloud/initial-state'
import { showError } from '@nextcloud/dialogs'
import { debounce, throttle } from 'throttle-debounce'
import { translate } from '@nextcloud/l10n'
import axios from '@nextcloud/axios'
import ChartPie from 'vue-material-design-icons/ChartPie.vue'
import NcAppNavigationItem from '@nextcloud/vue/dist/Components/NcAppNavigationItem.js'
import NcProgressBar from '@nextcloud/vue/dist/Components/NcProgressBar.js'
import logger from '../logger.js'
import { subscribe } from '@nextcloud/event-bus'
export default {
name: 'NavigationQuota',
components: {
ChartPie,
NcAppNavigationItem,
NcProgressBar,
},
data() {
return {
loadingStorageStats: false,
storageStats: loadState('files', 'storageStats', null),
}
},
computed: {
storageStatsTitle() {
const usedQuotaByte = formatFileSize(this.storageStats?.used)
const quotaByte = formatFileSize(this.storageStats?.quota)
// If no quota set
if (this.storageStats?.quota < 0) {
return this.t('files', '{usedQuotaByte} used', { usedQuotaByte })
}
return this.t('files', '{used} of {quota} used', {
used: usedQuotaByte,
quota: quotaByte,
})
},
storageStatsTooltip() {
if (!this.storageStats.relative) {
return ''
}
return this.t('files', '{relative}% used', this.storageStats)
},
},
beforeMount() {
/**
* Update storage stats every minute
* TODO: remove when all views are migrated to Vue
*/
setInterval(this.throttleUpdateStorageStats, 60 * 1000)
subscribe('files:node:created', this.throttleUpdateStorageStats)
subscribe('files:node:deleted', this.throttleUpdateStorageStats)
subscribe('files:node:moved', this.throttleUpdateStorageStats)
subscribe('files:node:updated', this.throttleUpdateStorageStats)
},
methods: {
// From user input
debounceUpdateStorageStats: debounce(200, function(event) {
this.updateStorageStats(event)
}),
// From interval or event bus
throttleUpdateStorageStats: throttle(1000, function(event) {
this.updateStorageStats(event)
}),
/**
* Update the storage stats
* Throttled at max 1 refresh per minute
*
* @param {Event} [event = null] if user interaction
*/
async updateStorageStats(event = null) {
if (this.loadingStorageStats) {
return
}
this.loadingStorageStats = true
try {
const response = await axios.get(generateUrl('/apps/files/api/v1/stats'))
if (!response?.data?.data) {
throw new Error('Invalid storage stats')
}
this.storageStats = response.data.data
} catch (error) {
logger.error('Could not refresh storage stats', { error })
// Only show to the user if it was manually triggered
if (event) {
showError(t('files', 'Could not refresh storage stats'))
}
} finally {
this.loadingStorageStats = false
}
},
t: translate,
},
}
</script>
<style lang="scss" scoped>
// User storage stats display
.app-navigation-entry__settings-quota {
// Align title with progress and icon
&--not-unlimited::v-deep .app-navigation-entry__name {
margin-top: -6px;
}
progress {
position: absolute;
bottom: 12px;
margin-left: 44px;
width: calc(100% - 44px - 22px);
}
}
</style>
|