Skip to content

Commit 4a7f752

Browse files
committed
chore(project): fetch branches
1 parent a19cd9e commit 4a7f752

File tree

5 files changed

+74
-15
lines changed

5 files changed

+74
-15
lines changed

components/molecules/FilesTree.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
class="group border-r-2 py-2 pr-6 flex items-center text-sm font-medium focus:u-bg-gray-50 focus:outline-none w-full cursor-pointer"
66
:class="{
77
[`pl-${6 + (level * 3)}`]: true,
8-
'u-bg-gray-50 u-border-gray-800 u-text-gray-900': isSelected(file),
8+
'u-bg-gray-100 u-border-gray-800 u-text-gray-900': isSelected(file),
99
'border-transparent u-text-gray-500 hover:u-text-gray-900 hover:u-bg-gray-50': !isSelected(file)
1010
}"
1111
@click="selectFile(file)"

components/organisms/project/ProjectPage.vue

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<template>
22
<main class="flex-1 flex overflow-hidden">
33
<!-- Primary column -->
4-
<section class="min-w-0 flex-1 h-full overflow-y-auto lg:order-last">
4+
<section class="min-w-0 flex-1 h-full overflow-y-auto lg:order-last bg-gray-50 dark:bg-gray-900">
55
<div class="h-full p-4 sm:p-6 lg:p-8 xl:p-10 overflow-y-auto">
66
<slot />
77
</div>
@@ -10,10 +10,12 @@
1010
<!-- Secondary column (hidden on smaller screens) -->
1111
<aside v-if="$slots.aside" class="hidden lg:block lg:flex-shrink-0 lg:order-first">
1212
<div class="h-full relative flex flex-col w-72 border-r u-border-gray-200 u-bg-white overflow-y-auto">
13-
<div class="flex-shrink-0 h-16 px-6 flex items-center sticky top-0 u-bg-white">
13+
<div class="flex-shrink-0 h-16 px-6 flex items-center justify-between sticky top-0 u-bg-white">
1414
<p class="text-lg font-semibold text-blue-gray-900">
1515
{{ title }}
1616
</p>
17+
18+
<slot name="aside-header" />
1719
</div>
1820

1921
<slot name="aside" />

nuxt.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ export default defineNuxtConfig({
6767
tailwindcss: {
6868
config: {
6969
content: ['presets/*.ts'],
70-
safelist: ['pl-6', 'pl-9', 'pl-12', 'pl-15', 'pl-18']
70+
safelist: [3, 6, 9, 12, 15, 18, 21, 24, 27, 30].map(number => `pl-${number}`)
7171
}
7272
}
7373
})

pages/@[team]/[project]/content.vue

Lines changed: 64 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,19 @@
11
<template>
22
<ProjectPage title="Content">
33
<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+
/>
517
</template>
618

719
<div class="whitespace-pre break-words text-sm font-mono focus:outline-none" contenteditable>
@@ -12,7 +24,7 @@
1224

1325
<script setup lang="ts">
1426
import type { PropType, Ref } from 'vue'
15-
import type { Team, Project, File } from '~/types'
27+
import type { Team, Project, File, Branch } from '~/types'
1628
1729
const props = defineProps({
1830
team: {
@@ -27,27 +39,68 @@ const props = defineProps({
2739
2840
const client = useStrapiClient()
2941
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('')
3145
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`))
3347
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+
}))
3553
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`)
3964
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) {
4271
return
4372
}
4473
4574
const { content: fetchedContent } = await client(`/projects/${props.project.id}/file`, {
4675
params: {
47-
path: selectedFile.value.path
76+
path: file.value.path
4877
}
4978
})
5079
5180
content.value = fetchedContent
5281
}, { 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+
}
53106
</script>

types/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@ export interface File {
9393
isDeleted?: boolean
9494
}
9595

96+
export interface Branch {
97+
name: string
98+
}
99+
96100
export interface GitHubAccount {
97101
login: string
98102
avatar_url: string

0 commit comments

Comments
 (0)