unpkg/server/middleware/components/DirectoryListing.js

51 lines
1.3 KiB
JavaScript
Raw Normal View History

2017-05-25 18:25:42 +00:00
const React = require('react')
const prettyBytes = require('pretty-bytes')
const { getContentType } = require('../FileUtils')
const e = React.createElement
const formatTime = (time) =>
new Date(time).toISOString()
const DirectoryListing = ({ dir, entries }) => {
const rows = entries.map(({ file, stats }, index) => {
const isDir = stats.isDirectory()
const href = file + (isDir ? '/' : '')
return (
e('tr', { key: file, className: index % 2 ? 'odd' : 'even' },
e('td', null, e('a', { title: file, href }, file)),
e('td', null, isDir ? '-' : getContentType(file)),
e('td', null, isDir ? '-' : prettyBytes(stats.size)),
e('td', null, isDir ? '-' : formatTime(stats.mtime))
)
)
})
if (dir !== '/')
rows.unshift(
e('tr', { key: '..', className: 'odd' },
e('td', null, e('a', { title: 'Parent directory', href: '../' }, '..')),
e('td', null, '-'),
e('td', null, '-'),
e('td', null, '-')
)
)
return (
e('table', null,
e('thead', null,
e('tr', null,
e('th', null, 'Name'),
e('th', null, 'Type'),
e('th', null, 'Size'),
e('th', null, 'Last Modified')
)
),
e('tbody', null, rows)
)
)
}
module.exports = DirectoryListing