Skip to content

Commit ab9d02f

Browse files
refactor(vue3): migrate VirtualList to script setup ts
Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com>
1 parent 1a3b6fb commit ab9d02f

1 file changed

Lines changed: 72 additions & 60 deletions

File tree

src/views/FilesList/VirtualList.vue

Lines changed: 72 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,9 @@
5555
</div>
5656
</template>
5757

58-
<script>
58+
<script setup lang="ts">
5959
import { t } from '@nextcloud/l10n'
60+
import { onBeforeUnmount, onMounted, ref, useTemplateRef } from 'vue'
6061
6162
import debounce from 'debounce'
6263
@@ -65,67 +66,78 @@ import { subscribe, unsubscribe } from '@nextcloud/event-bus'
6566
import { useFilesStore } from '../../store/files.js'
6667
import { useUserConfigStore } from '../../store/userconfig.js'
6768
68-
export default {
69+
defineOptions({
6970
name: 'VirtualList',
70-
props: {
71-
dataComponent: {
72-
type: [Object, Function],
73-
required: true,
74-
},
75-
loading: {
76-
type: Boolean,
77-
required: true,
78-
},
79-
/**
80-
* Visually hidden caption for the table accessibility
81-
*/
82-
caption: {
83-
type: String,
84-
default: '',
85-
},
86-
},
87-
setup() {
88-
const filesStore = useFilesStore()
89-
const userConfigStore = useUserConfigStore()
90-
return {
91-
t,
92-
filesStore,
93-
userConfigStore,
94-
}
95-
},
96-
data() {
97-
return {
98-
observer: null,
99-
}
100-
},
101-
mounted() {
102-
this.observer = new IntersectionObserver(debounce(([entry]) => {
103-
if (entry && entry.isIntersecting) {
104-
this.getFilesIfNotLoading()
105-
}
106-
}, 100))
107-
subscribe('libresign:files:updated', this.updateObserver)
108-
},
109-
beforeUnmount() {
110-
this.observer.disconnect()
111-
unsubscribe('libresign:files:updated')
112-
},
113-
methods: {
114-
getFilesIfNotLoading() {
115-
if (this.filesStore.loading) {
116-
setTimeout(this.getFilesIfNotLoading, 100)
117-
} else {
118-
this.filesStore.getAllFiles()
119-
}
120-
},
121-
updateObserver() {
122-
const endOfListElement = this.$refs?.endOfList
123-
if (!endOfListElement) return
124-
this.observer.disconnect()
125-
this.observer.observe(endOfListElement)
126-
},
127-
},
71+
})
72+
73+
type FileItem = {
74+
id: string | number
75+
}
76+
77+
type FilesStore = {
78+
loading: boolean
79+
filesSorted: () => FileItem[]
80+
getAllFiles: () => void
12881
}
82+
83+
type UserConfigStore = {
84+
files_list_grid_view: boolean
85+
}
86+
87+
const props = withDefaults(defineProps<{
88+
dataComponent: object | (() => unknown)
89+
loading: boolean
90+
caption?: string
91+
}>(), {
92+
caption: '',
93+
})
94+
95+
const filesStore = useFilesStore() as FilesStore
96+
const userConfigStore = useUserConfigStore() as UserConfigStore
97+
const endOfList = useTemplateRef<HTMLElement>('endOfList')
98+
const observer = ref<IntersectionObserver | null>(null)
99+
100+
function getFilesIfNotLoading() {
101+
if (filesStore.loading) {
102+
setTimeout(getFilesIfNotLoading, 100)
103+
} else {
104+
filesStore.getAllFiles()
105+
}
106+
}
107+
108+
function updateObserver() {
109+
const endOfListElement = endOfList.value
110+
if (!endOfListElement || !observer.value) {
111+
return
112+
}
113+
observer.value.disconnect()
114+
observer.value.observe(endOfListElement)
115+
}
116+
117+
onMounted(() => {
118+
observer.value = new IntersectionObserver(debounce(([entry]) => {
119+
if (entry && entry.isIntersecting) {
120+
getFilesIfNotLoading()
121+
}
122+
}, 100))
123+
subscribe('libresign:files:updated', updateObserver)
124+
})
125+
126+
onBeforeUnmount(() => {
127+
observer.value?.disconnect()
128+
unsubscribe('libresign:files:updated')
129+
})
130+
131+
defineExpose({
132+
filesStore,
133+
userConfigStore,
134+
observer,
135+
endOfList,
136+
getFilesIfNotLoading,
137+
updateObserver,
138+
props,
139+
t,
140+
})
129141
</script>
130142

131143
<style scoped lang="scss">

0 commit comments

Comments
 (0)