-
-
Notifications
You must be signed in to change notification settings - Fork 425
Expand file tree
/
Copy pathDirectoryListing.vue
More file actions
131 lines (120 loc) · 4.02 KB
/
DirectoryListing.vue
File metadata and controls
131 lines (120 loc) · 4.02 KB
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
122
123
124
125
126
127
128
129
130
131
<script setup lang="ts">
import type { RouteLocationRaw } from 'vue-router'
import type { RouteNamedMap } from 'vue-router/auto-routes'
import { ADDITIONAL_ICONS, getFileIcon } from '~/utils/file-icons'
const props = defineProps<{
tree: PackageFileTree[]
currentPath: string
baseUrl: string
baseRoute: Pick<RouteNamedMap['code'], 'params'>
}>()
// Get the current directory's contents
const currentContents = computed(() => {
if (!props.currentPath) {
return props.tree
}
const parts = props.currentPath.split('/')
let current: PackageFileTree[] | undefined = props.tree
for (const part of parts) {
const found: PackageFileTree | undefined = current?.find(n => n.name === part)
if (!found || found.type === 'file') {
return []
}
current = found.children
}
return current ?? []
})
// Get parent directory path
const parentPath = computed(() => {
if (!props.currentPath) return null
const parts = props.currentPath.split('/')
if (parts.length <= 1) return ''
return parts.slice(0, -1).join('/')
})
// Build route object for a path
function getCodeRoute(nodePath?: string): RouteLocationRaw {
return {
name: 'code',
params: {
org: props.baseRoute.params.org,
packageName: props.baseRoute.params.packageName,
version: props.baseRoute.params.version,
filePath: nodePath ?? '',
},
}
}
const bytesFormatter = useBytesFormatter()
</script>
<template>
<div class="directory-listing">
<!-- Empty state -->
<div v-if="currentContents.length === 0" class="py-20 text-center text-fg-muted">
<span class="i-lucide:folder-open w-12 h-12 text-fg-subtle mx-auto mb-4"> </span>
<p>{{ $t('code.no_files') }}</p>
</div>
<!-- File list -->
<table v-else class="w-full">
<thead class="sr-only">
<tr>
<th>{{ $t('code.table.name') }}</th>
<th>{{ $t('code.table.size') }}</th>
</tr>
</thead>
<tbody>
<!-- Parent directory link -->
<tr
v-if="parentPath !== null"
class="border-b border-border hover:bg-bg-subtle transition-[color,background-color] duration-100"
>
<td colspan="2">
<LinkBase
:to="getCodeRoute(parentPath || undefined)"
class="py-2 px-4 font-mono text-sm w-full"
no-underline
>
<svg
class="size-[1em] me-1 shrink-0 text-yellow-600"
viewBox="0 0 16 16"
aria-hidden="true"
>
<use :href="`/file-tree-sprite.svg#${ADDITIONAL_ICONS['folder']}`" />
</svg>
<span class="w-full flex justify-self-stretch items-center gap-2"> .. </span>
</LinkBase>
</td>
</tr>
<!-- Directory/file rows -->
<tr
v-for="node in currentContents"
:key="node.path"
class="border-b border-border hover:bg-bg-subtle transition-[color,background-color] duration-100"
>
<td colspan="2">
<LinkBase
:to="getCodeRoute(node.path)"
class="py-2 px-4 font-mono text-sm w-full"
no-underline
>
<svg
class="size-[1em] me-1 shrink-0"
viewBox="0 0 16 16"
:class="node.type === 'directory' ? 'text-yellow-600' : undefined"
aria-hidden="true"
>
<use
:href="`/file-tree-sprite.svg#${node.type === 'directory' ? ADDITIONAL_ICONS['folder'] : getFileIcon(node.name)}`"
/>
</svg>
<span class="w-full flex justify-self-stretch items-center gap-2">
<span class="flex-1">{{ node.name }}</span>
<span v-if="typeof node.size === 'number'" class="text-end text-xs text-fg-subtle">
{{ bytesFormatter.format(node.size) }}
</span>
</span>
</LinkBase>
</td>
</tr>
</tbody>
</table>
</div>
</template>