import { h, Component } from 'preact' import { bindActionCreators } from 'redux' import { connect } from 'react-redux' import { Link } from 'react-router-dom'; import moment from 'moment' import * as util from '../util' const defaultFields = new Set(['date', 'size']) export const FileList = props => { const { files, fields, sort, title, linkFiles, onClick, orderBy='name asc', className='', fileListClassName='filelist', rowClassName='row file' } = props const { mapFn, sortFn } = orderByFn(orderBy) const fileList = (files || []) .map(mapFn) .sort(sortFn) .map(pair => ) return (
{(files && files.length) ? (title &&

{title}

) :

No files

}
{fileList}
) } const numericSort = { asc: (a,b) => a[0] - b[0], desc: (a,b) => b[0] - a[0], } const stringSort = { asc: (a,b) => a[0].localeCompare(b[0]), desc: (a,b) => b[0].localeCompare(a[0]), } export const orderByFn = (s='name asc') => { const [field='name', direction='asc'] = s.split(' ') let mapFn, sortFn switch (field) { case 'epoch': mapFn = a => [a.epoch || a.epochs, a] sortFn = numericSort[direction] break case 'size': mapFn = a => [a.size, a] sortFn = numericSort[direction] break case 'date': mapFn = a => [+new Date(a.date || a.created_at), a] sortFn = numericSort[direction] break case 'name': default: mapFn = a => [a.id || a.name, a] sortFn = stringSort[direction] break } return { mapFn, sortFn } } export const fieldSet = fields => { if (fields) { if (fields instanceof Set) { return fields } return new Set(fields.split(' ')) } return defaultFields } export const FileRow = props => { const { file, linkFiles, onClick, className='row file', username='' } = props const fields = fieldSet(props.fields) const size = util.hush_size(file.size) const date = file.date || file.created_at return (
{file.persisted === false ? {file.name || file.url} : (linkFiles && file.url) ? {file.name || file.url} : onClick(file)}>{file.name || file.url} }
{fields.has('age') &&
{util.get_age(date)}
} {fields.has('username') &&
{username}
} {fields.has('date') &&
{moment(date).format("YYYY-MM-DD")}
} {fields.has('datetime') &&
{moment(date).format("YYYY-MM-DD h:mm a")}
} {fields.has('size') &&
{size[1]}
} {fields.has('epoch') &&
{file.epoch > 0 ? 'epoch ' + file.epoch : ' '}
} {(fields.has('activity') || fields.has('module')) &&
{fields.has('activity') && file.activity} {fields.has('module') && file.module}
} {props.options && props.options(file)}
) }