2019-12-26 22:10:19 +05:30
|
|
|
<script>
|
2021-11-11 11:23:49 +05:30
|
|
|
import paginatedTreeQuery from 'shared_queries/repository/paginated_tree.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-11-18 22:05:49 +05:30
|
|
|
import { loadCommits, isRequested, resetRequestedCommits } from '../commits_service';
|
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 {
|
2021-11-18 22:05:49 +05:30
|
|
|
commits: [],
|
2019-12-26 22:10:19 +05:30
|
|
|
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 = '';
|
2021-11-18 22:05:49 +05:30
|
|
|
resetRequestedCommits();
|
2019-12-26 22:10:19 +05:30
|
|
|
this.fetchFiles();
|
|
|
|
},
|
|
|
|
},
|
|
|
|
mounted() {
|
|
|
|
// We need to wait for `ref` and `projectPath` to be set
|
2021-11-18 22:05:49 +05:30
|
|
|
this.$nextTick(() => {
|
|
|
|
resetRequestedCommits();
|
|
|
|
this.fetchFiles();
|
|
|
|
});
|
2019-12-26 22:10:19 +05:30
|
|
|
},
|
|
|
|
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({
|
2021-12-11 22:18:48 +05:30
|
|
|
query: paginatedTreeQuery,
|
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
|
|
|
|
2021-12-11 22:18:48 +05:30
|
|
|
const {
|
|
|
|
project: {
|
|
|
|
repository: {
|
|
|
|
paginatedTree: { pageInfo },
|
|
|
|
},
|
|
|
|
},
|
|
|
|
} = data;
|
2019-12-26 22:10:19 +05:30
|
|
|
|
|
|
|
this.isLoadingFiles = false;
|
|
|
|
this.entries = Object.keys(this.entries).reduce(
|
|
|
|
(acc, key) => ({
|
|
|
|
...acc,
|
2021-12-11 22:18:48 +05:30
|
|
|
[key]: this.normalizeData(key, data.project.repository.paginatedTree.nodes[0][key]),
|
2019-12-26 22:10:19 +05:30
|
|
|
}),
|
|
|
|
{},
|
|
|
|
);
|
|
|
|
|
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) {
|
2021-12-11 22:18:48 +05:30
|
|
|
return this.entries[key].concat(data.nodes);
|
2019-12-26 22:10:19 +05:30
|
|
|
},
|
|
|
|
hasNextPage(data) {
|
|
|
|
return []
|
|
|
|
.concat(data.trees.pageInfo, data.submodules.pageInfo, data.blobs.pageInfo)
|
|
|
|
.find(({ hasNextPage }) => hasNextPage);
|
|
|
|
},
|
2021-11-18 22:05:49 +05:30
|
|
|
loadCommitData({ rowNumber = 0, hasCommit } = {}) {
|
|
|
|
if (!this.glFeatures.lazyLoadCommits || hasCommit || isRequested(rowNumber)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
loadCommits(this.projectPath, this.path, this.ref, rowNumber)
|
|
|
|
.then(this.setCommitData)
|
|
|
|
.catch(() => {});
|
|
|
|
},
|
|
|
|
setCommitData(data) {
|
|
|
|
this.commits = this.commits.concat(data);
|
|
|
|
},
|
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"
|
2021-11-18 22:05:49 +05:30
|
|
|
:commits="commits"
|
2020-11-24 15:15:51 +05:30
|
|
|
@showMore="handleShowMore"
|
2021-11-18 22:05:49 +05:30
|
|
|
@row-appear="loadCommitData"
|
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>
|