forked from npmx-dev/npmx.dev
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDirectoryListing.vue
More file actions
122 lines (111 loc) · 3.53 KB
/
DirectoryListing.vue
File metadata and controls
122 lines (111 loc) · 3.53 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
<script setup lang="ts">
import type { PackageFileTree } from '#shared/types'
import type { RouteLocationRaw } from 'vue-router'
import { getFileIcon } from '~/utils/file-icons'
const props = defineProps<{
tree: PackageFileTree[]
currentPath: string
baseUrl: string
/** Base path segments for the code route (e.g., ['nuxt', 'v', '4.2.0']) */
basePath: string[]
}>()
// 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 {
if (!nodePath) {
return { name: 'code', params: { path: props.basePath as [string, ...string[]] } }
}
const pathSegments = [...props.basePath, ...nodePath.split('/')]
return {
name: 'code',
params: { path: pathSegments as [string, ...string[]] },
}
}
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">
<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-colors"
>
<td colspan="2">
<LinkBase
:to="getCodeRoute(parentPath || undefined)"
class="py-2 px-4 font-mono text-sm w-full"
no-underline
classicon="i-carbon:folder text-yellow-600"
>
<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-colors"
>
<td colspan="2">
<LinkBase
:to="getCodeRoute(node.path)"
class="py-2 px-4 font-mono text-sm w-full"
no-underline
:classicon="
node.type === 'directory'
? 'i-carbon:folder text-yellow-600'
: getFileIcon(node.name)
"
>
<span class="w-full flex justify-self-stretch items-center gap-2">
<span class="flex-1">{{ node.name }}</span>
<span
v-if="node.type === 'file' && node.size"
class="text-end text-xs text-fg-subtle"
>
{{ bytesFormatter.format(node.size) }}
</span>
</span>
</LinkBase>
</td>
</tr>
</tbody>
</table>
</div>
</template>