aboutsummaryrefslogtreecommitdiff
path: root/packages/client/src/views/RepositoryTree.vue
blob: ae4fd6b3e730965df236892959f2fd8020ce0524 (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
<template>
	<div class="row">
		<div class="col fs-5 vld-parent">
			<BaseBreadcrumb
				:items="(pathArr.length === 0) ? [] : [{ name: $router.currentRoute._rawValue.params.repo, path: '/' + $router.currentRoute._rawValue.params.repo + '/tree' }].concat(pathArr.slice(0, -1).map((path_part, index) =>
				{
					return {
						name: path_part,
						path: '/' + $router.currentRoute._rawValue.params.repo + '/tree/' + pathArr.slice(0, index + 1).join('/')
					}
				}))" :active-item="(pathArr.length === 0) ? $router.currentRoute._rawValue.params.repo : pathArr[pathArr.length - 1]" />
			<RepositoryTreeTree
				:repository="$router.currentRoute._rawValue.params.repo" :path="path"
				:tree="tree" v-if="tree" />
			<RepositoryTreeBlob
				:repository="$router.currentRoute._rawValue.params.repo" :path="path"
				:content="blob_content" v-if="blob_content" />
			<BaseErrorMessage :fetch-failed="fetch_failed" />
			<Loading
				:active="is_loading" :height="24"
				:width="24" color="#ffffff"
				:opacity="0" :is-full-page="false" />
		</div>
	</div>
</template>

<script lang="ts">
import { defineComponent, Ref, ref } from "vue";
import fetchData from "../util/fetch";
import { getParam } from "../util/util";

import BaseBreadcrumb from "../components/BaseBreadcrumb.vue";
import RepositoryTreeBlob from "../components/RepositoryTreeBlob.vue";
import RepositoryTreeTree from "../components/RepositoryTreeTree.vue";
import BaseErrorMessage from "../components/BaseErrorMessage.vue";
import Loading from "vue-loading-overlay";

type TreeEntry = {
	name: string,
	type: "tree" | "blob",
	latest_commit: {
		id: string,
		message: string,
		date: number
	}
};

type Tree = {
	type: "tree" | "blob",
	content: string | TreeEntry[]
};

export default defineComponent({
	name: "RepositoryTree",
	components: {
		BaseBreadcrumb,
		RepositoryTreeBlob,
		RepositoryTreeTree,
		Loading,
		BaseErrorMessage
	},
	props: {
		pathArr: {
			type: Array,
			required: true
		}
	},
	watch: {
		pathArr() {
			this.is_loading = true;

			this.fetchTree(getParam(this.$route.params, "repo"));
		}
	},
	setup(props) {
		const tree: Ref<TreeEntry[] | null> = ref(null);
		const blob_content: Ref<string | null> = ref(null);
		const is_loading: Ref<boolean> = ref(true);
		const fetch_failed: Ref<string> = ref("");
		const path: Ref<string | null> = ref(null);

		const fetchTree = async(repository: string) => {
			blob_content.value = null;
			tree.value = null;
			path.value = props.pathArr ? props.pathArr.join("/") : null;

			const tree_data: Tree = await fetchData(`repos/${repository}/tree${path.value ? "?path=" + path.value : ""}`, fetch_failed, is_loading, "tree");

			if(tree_data) {
				if(tree_data.type === "tree" && tree_data.content instanceof Array) {
					let tree_trees = tree_data.content.filter((entry) => entry.type === "tree");
					tree_trees = tree_trees.sort((a, b) => a.name.localeCompare(b.name));

					let tree_blobs = tree_data.content.filter((entry) => entry.type === "blob");
					tree_blobs = tree_blobs.sort((a, b) => a.name.localeCompare(b.name));

					tree.value = tree_trees.concat(tree_blobs);
				} else if(typeof tree_data.content === "string") {
					blob_content.value = tree_data.content;
				}
			}
		};

		return {
			tree,
			blob_content,
			is_loading,
			fetch_failed,
			path,
			fetchTree
		};
	},
	created() {
		this.fetchTree(getParam(this.$route.params, "repo"));
	}
});
</script>

<style lang="scss" scoped>
@import "~vue-loading-overlay/dist/vue-loading.css";
</style>