debian-mirror-gitlab/app/assets/javascripts/repository/components/tree_content.vue

164 lines
4.4 KiB
Vue
Raw Normal View History

2019-12-26 22:10:19 +05:30
<script>
2021-01-29 00:20:46 +05:30
import filesQuery from 'shared_queries/repository/files.query.graphql';
2021-09-04 01:27:46 +05:30
import createFlash from '~/flash';
2021-09-30 23:02:18 +05:30
import glFeatureFlagMixin from '~/vue_shared/mixins/gl_feature_flags_mixin';
2019-12-26 22:10:19 +05:30
import { __ } from '../../locale';
2021-09-30 23:02:18 +05:30
import { TREE_PAGE_SIZE, TREE_INITIAL_FETCH_COUNT, TREE_PAGE_LIMIT } from '../constants';
2019-12-26 22:10:19 +05:30
import getRefMixin from '../mixins/get_ref';
2020-10-24 23:57:45 +05:30
import projectPathQuery from '../queries/project_path.query.graphql';
2019-12-26 22:10:19 +05:30
import { readmeFile } from '../utils/readme';
2021-03-11 19:13:27 +05:30
import FilePreview from './preview/index.vue';
import FileTable from './table/index.vue';
2019-12-26 22:10:19 +05:30
export default {
components: {
FileTable,
FilePreview,
},
2021-09-30 23:02:18 +05:30
mixins: [getRefMixin, glFeatureFlagMixin()],
2019-12-26 22:10:19 +05:30
apollo: {
projectPath: {
2020-10-24 23:57:45 +05:30
query: projectPathQuery,
2019-12-26 22:10:19 +05:30
},
},
props: {
path: {
type: String,
required: false,
default: '/',
},
2020-03-13 15:44:24 +05:30
loadingPath: {
type: String,
required: false,
default: '',
},
2019-12-26 22:10:19 +05:30
},
data() {
return {
projectPath: '',
nextPageCursor: '',
2021-09-30 23:02:18 +05:30
pagesLoaded: 1,
2019-12-26 22:10:19 +05:30
entries: {
trees: [],
submodules: [],
blobs: [],
},
isLoadingFiles: false,
2020-10-24 23:57:45 +05:30
isOverLimit: false,
clickedShowMore: false,
fetchCounter: 0,
2019-12-26 22:10:19 +05:30
};
},
computed: {
2021-09-30 23:02:18 +05:30
pageSize() {
// we want to exponentially increase the page size to reduce the load on the frontend
const exponentialSize = (TREE_PAGE_SIZE / TREE_INITIAL_FETCH_COUNT) * (this.fetchCounter + 1);
return exponentialSize < TREE_PAGE_SIZE && this.glFeatures.increasePageSizeExponentially
? exponentialSize
: TREE_PAGE_SIZE;
},
totalEntries() {
return Object.values(this.entries).flat().length;
},
2019-12-26 22:10:19 +05:30
readme() {
return readmeFile(this.entries.blobs);
},
2021-09-30 23:02:18 +05:30
pageLimitReached() {
return this.totalEntries / this.pagesLoaded >= TREE_PAGE_LIMIT;
},
2020-10-24 23:57:45 +05:30
hasShowMore() {
2021-09-30 23:02:18 +05:30
return !this.clickedShowMore && this.pageLimitReached;
2020-10-24 23:57:45 +05:30
},
2019-12-26 22:10:19 +05:30
},
watch: {
$route: function routeChange() {
this.entries.trees = [];
this.entries.submodules = [];
this.entries.blobs = [];
this.nextPageCursor = '';
this.fetchFiles();
},
},
mounted() {
// We need to wait for `ref` and `projectPath` to be set
this.$nextTick(() => this.fetchFiles());
},
methods: {
fetchFiles() {
2021-01-03 14:25:43 +05:30
const originalPath = this.path || '/';
2019-12-26 22:10:19 +05:30
this.isLoadingFiles = true;
return this.$apollo
.query({
2020-10-24 23:57:45 +05:30
query: filesQuery,
2019-12-26 22:10:19 +05:30
variables: {
projectPath: this.projectPath,
ref: this.ref,
2021-01-03 14:25:43 +05:30
path: originalPath,
2019-12-26 22:10:19 +05:30
nextPageCursor: this.nextPageCursor,
2020-10-24 23:57:45 +05:30
pageSize: this.pageSize,
2019-12-26 22:10:19 +05:30
},
})
.then(({ data }) => {
2020-03-13 15:44:24 +05:30
if (data.errors) throw data.errors;
2021-01-03 14:25:43 +05:30
if (!data?.project?.repository || originalPath !== (this.path || '/')) return;
2019-12-26 22:10:19 +05:30
const pageInfo = this.hasNextPage(data.project.repository.tree);
this.isLoadingFiles = false;
this.entries = Object.keys(this.entries).reduce(
(acc, key) => ({
...acc,
[key]: this.normalizeData(key, data.project.repository.tree[key].edges),
}),
{},
);
2020-03-13 15:44:24 +05:30
if (pageInfo?.hasNextPage) {
2019-12-26 22:10:19 +05:30
this.nextPageCursor = pageInfo.endCursor;
2020-10-24 23:57:45 +05:30
this.fetchCounter += 1;
2021-09-30 23:02:18 +05:30
if (!this.pageLimitReached || this.clickedShowMore) {
2020-10-24 23:57:45 +05:30
this.fetchFiles();
this.clickedShowMore = false;
}
2019-12-26 22:10:19 +05:30
}
})
2021-03-08 18:12:59 +05:30
.catch((error) => {
2021-09-04 01:27:46 +05:30
createFlash({
message: __('An error occurred while fetching folder content.'),
});
2020-03-13 15:44:24 +05:30
throw error;
});
2019-12-26 22:10:19 +05:30
},
normalizeData(key, data) {
return this.entries[key].concat(data.map(({ node }) => node));
},
hasNextPage(data) {
return []
.concat(data.trees.pageInfo, data.submodules.pageInfo, data.blobs.pageInfo)
.find(({ hasNextPage }) => hasNextPage);
},
2020-11-24 15:15:51 +05:30
handleShowMore() {
2020-10-24 23:57:45 +05:30
this.clickedShowMore = true;
2021-09-30 23:02:18 +05:30
this.pagesLoaded += 1;
2020-10-24 23:57:45 +05:30
this.fetchFiles();
},
2019-12-26 22:10:19 +05:30
},
};
</script>
<template>
<div>
2020-03-13 15:44:24 +05:30
<file-table
:path="path"
:entries="entries"
:is-loading="isLoadingFiles"
:loading-path="loadingPath"
2020-11-24 15:15:51 +05:30
:has-more="hasShowMore"
@showMore="handleShowMore"
2020-03-13 15:44:24 +05:30
/>
2019-12-26 22:10:19 +05:30
<file-preview v-if="readme" :blob="readme" />
</div>
</template>