Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/src/packages/next/components/share/directory-listing.tsx
Views: 687
/*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";1415interface Props {16id?: string;17relativePath?: string;18listing: FileInfo[];19showHidden?: boolean;20}2122export default function DirectoryListing({23id,24listing,25relativePath,26showHidden,27}: Props): JSX.Element {28return (29<Table30rowKey={"name"}31dataSource={filter(listing, showHidden)}32columns={columns(id, relativePath)}33pagination={{34hideOnSinglePage: true,35defaultPageSize: 50,36showSizeChanger: true,37pageSizeOptions: ["50", "100", "200", "500"],38}}39/>40);41}4243function filter(listing, showHidden): FileInfo[] {44if (showHidden) {45return listing;46}47const v: FileInfo[] = [];48for (const x of listing) {49if (!x.name?.startsWith(".")) {50v.push(x);51}52}53return v;54}5556function columns(id, relativePath) {57return [58{59title: "Name",60dataIndex: "name",61key: "name",62// for style below, see comment in public-paths.tsx.63render: (name, record) => {64return (65<Link66style={{ width: "100%", display: "inline-block" }}67href={68record.url ??69`/share/public_paths/${id}/${encodeURIComponent(70join(relativePath, name)71)}`72}73>74{record.isdir ? <b>{name}/</b> : name}75</Link>76);77},78sorter: field_cmp("name"),79},80{81title: "Size",82dataIndex: "size",83key: "size",84render: (size, record) => renderSize(size, record.isdir),85align: "right" as any,86sorter: field_cmp("size"),87},88{89title: "Last Modified",90dataIndex: "mtime",91key: "mtime",92align: "right" as any,93render: (mtime) => (mtime ? `${new Date(mtime).toLocaleString()}` : ""),94sorter: field_cmp("mtime"),95},96];97}9899function renderSize(size?: number, isdir?: boolean) {100if (size == null) return "-";101if (isdir) return `${size} ${plural(size, "item")}`;102return humanReadableSize(size);103}104105106