Path: blob/master/src/packages/next/components/share/directory-listing.tsx
1492 views
/*1* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45import Link from "next/link";6import { Table } from "antd";7import { FileInfo } from "lib/share/get-contents";8import { join } from "path";9import {10human_readable_size as humanReadableSize,11plural,12} from "@cocalc/util/misc";13import { field_cmp } from "@cocalc/util/misc";1415import type { JSX } from "react";1617interface Props {18id?: string;19relativePath?: string;20listing: FileInfo[];21showHidden?: boolean;22}2324export default function DirectoryListing({25id,26listing,27relativePath,28showHidden,29}: Props): JSX.Element {30return (31<Table32rowKey={"name"}33dataSource={filter(listing, showHidden)}34columns={columns(id, relativePath)}35pagination={{36hideOnSinglePage: true,37defaultPageSize: 50,38showSizeChanger: true,39pageSizeOptions: ["50", "100", "200", "500"],40}}41/>42);43}4445function filter(listing, showHidden): FileInfo[] {46if (showHidden) {47return listing;48}49const v: FileInfo[] = [];50for (const x of listing) {51if (!x.name?.startsWith(".")) {52v.push(x);53}54}55return v;56}5758function columns(id, relativePath) {59return [60{61title: "Name",62dataIndex: "name",63key: "name",64// for style below, see comment in public-paths.tsx.65render: (name, record) => {66return (67<Link68style={{ width: "100%", display: "inline-block" }}69href={70record.url ??71`/share/public_paths/${id}/${encodeURIComponent(72join(relativePath, name)73)}`74}75>76{record.isdir ? <b>{name}/</b> : name}77</Link>78);79},80sorter: field_cmp("name"),81},82{83title: "Size",84dataIndex: "size",85key: "size",86render: (size, record) => renderSize(size, record.isdir),87align: "right" as any,88sorter: field_cmp("size"),89},90{91title: "Last Modified",92dataIndex: "mtime",93key: "mtime",94align: "right" as any,95render: (mtime) => (mtime ? `${new Date(mtime).toLocaleString()}` : ""),96sorter: field_cmp("mtime"),97},98];99}100101function renderSize(size?: number, isdir?: boolean) {102if (size == null) return "-";103if (isdir) return `${size} ${plural(size, "item")}`;104return humanReadableSize(size);105}106107108