1
1
<template >
2
2
<ProjectPage title =" Content" >
3
3
<template #aside >
4
- <FilesTree :files =" files" :selected-file =" selectedFile" @select-file =" selectFile" />
4
+ <FilesTree :files =" files" :selected-file =" file" @select-file =" selectFile" />
5
+ </template >
6
+
7
+ <template #aside-header >
8
+ <UButton
9
+ v-if =" branch"
10
+ icon =" heroicons-solid:switch-horizontal"
11
+ trailing
12
+ :label =" branch.name"
13
+ variant =" transparent"
14
+ size =" sm"
15
+ class =" -mr-3"
16
+ />
5
17
</template >
6
18
7
19
<div class =" whitespace-pre break-words text-sm font-mono focus:outline-none" contenteditable >
12
24
13
25
<script setup lang="ts">
14
26
import type { PropType , Ref } from ' vue'
15
- import type { Team , Project , File } from ' ~/types'
27
+ import type { Team , Project , File , Branch } from ' ~/types'
16
28
17
29
const props = defineProps ({
18
30
team: {
@@ -27,27 +39,68 @@ const props = defineProps({
27
39
28
40
const client = useStrapiClient ()
29
41
30
- const { data : files } = await useAsyncData (' files' , () => client <File []>(` /projects/${props .project .id }/tree ` ))
42
+ const branch: Ref <Branch > = ref (null )
43
+ const file: Ref <File > = ref (null )
44
+ const content: Ref <string > = ref (' ' )
31
45
32
- const selectedFile : Ref < File > = ref ( files . value . find ( file => file . path . toLowerCase (). endsWith ( ' index.md ' )) || files . value . find ( file => file . type === ' file ' ))
46
+ const { data : branches, refresh : refreshBranches } = await useAsyncData ( ' files ' , () => client < Branch []>( ` /projects/${ props . project . id }/branches ` ))
33
47
34
- const content = ref (' ' )
48
+ const { data : files, refresh : refreshFiles } = await useAsyncData (' files' , () => client <File []>(` /projects/${props .project .id }/files ` , {
49
+ params: {
50
+ branch: branch .value ?.name
51
+ }
52
+ }))
35
53
36
- function selectFile (file ) {
37
- selectedFile .value = file
38
- }
54
+ // Select file when files changes
55
+ watch (files , () => {
56
+ const currentFile = file .value ?.path ? findFileFromPath (file .value .path , files .value ) : null
57
+
58
+ selectFile (currentFile || files .value .find (file => file .path .toLowerCase ().endsWith (' index.md' )) || files .value .find (file => file .type === ' file' ))
59
+ }, { immediate: true })
60
+
61
+ // Select branch when branches changes
62
+ watch (branches , () => {
63
+ const branchName = localStorage .getItem (` projects-${props .project .id }-branch ` )
39
64
40
- watch (selectedFile , async () => {
41
- if (! selectedFile .value ) {
65
+ selectBranch ((branchName && branches .value .find (branch => branch .name === branchName )) || branches .value .find (branch => branch .name === props .project .repository .default_branch ) || branches .value [0 ])
66
+ }, { immediate: true })
67
+
68
+ // Fetch content when file changes
69
+ watch (file , async () => {
70
+ if (! file .value ) {
42
71
return
43
72
}
44
73
45
74
const { content : fetchedContent } = await client (` /projects/${props .project .id }/file ` , {
46
75
params: {
47
- path: selectedFile .value .path
76
+ path: file .value .path
48
77
}
49
78
})
50
79
51
80
content .value = fetchedContent
52
81
}, { immediate: true })
82
+
83
+ // Fetch files when branch changes
84
+ watch (branch , async () => await refreshFiles ())
85
+
86
+ function findFileFromPath (path , files ) {
87
+ for (const file of files ) {
88
+ if (file .path === path ) {
89
+ return file
90
+ }
91
+ if (file .children ) {
92
+ const result = findFileFromPath (path , file .children )
93
+ if (result ) { return result }
94
+ }
95
+ }
96
+ }
97
+
98
+ function selectFile (f : File ) {
99
+ file .value = f
100
+ }
101
+
102
+ function selectBranch (b : Branch ) {
103
+ branch .value = b
104
+ localStorage .setItem (` projects-${props .project .id }-branch ` , branch .value ?.name )
105
+ }
53
106
</script >
0 commit comments