"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const jsx_runtime_1 = require("react/jsx-runtime");
/*
 *  This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.
 *  License: AGPLv3 s.t. "Commons Clause" – see LICENSE.md for details
 */
const link_1 = __importDefault(require("next/link"));
const antd_1 = require("antd");
const path_1 = require("path");
const misc_1 = require("@cocalc/util/misc");
const misc_2 = require("@cocalc/util/misc");
function DirectoryListing({ id, listing, relativePath, showHidden, }) {
    return ((0, jsx_runtime_1.jsx)(antd_1.Table, { rowKey: "name", dataSource: filter(listing, showHidden), columns: columns(id, relativePath), pagination: {
            hideOnSinglePage: true,
            defaultPageSize: 50,
            showSizeChanger: true,
            pageSizeOptions: ["50", "100", "200", "500"],
        } }));
}
exports.default = DirectoryListing;
function filter(listing, showHidden) {
    if (showHidden) {
        return listing;
    }
    const v = [];
    for (const x of listing) {
        if (!x.name?.startsWith(".")) {
            v.push(x);
        }
    }
    return v;
}
function columns(id, relativePath) {
    return [
        {
            title: "Name",
            dataIndex: "name",
            key: "name",
            // for style below, see comment in public-paths.tsx.
            render: (name, record) => {
                return ((0, jsx_runtime_1.jsx)(link_1.default, { href: record.url ??
                        `/share/public_paths/${id}/${encodeURIComponent((0, path_1.join)(relativePath, name))}`, children: (0, jsx_runtime_1.jsx)("a", { style: { width: "100%", display: "inline-block" }, children: record.isdir ? (0, jsx_runtime_1.jsxs)("b", { children: [name, "/"] }) : name }) }));
            },
            sorter: (0, misc_2.field_cmp)("name"),
        },
        {
            title: "Size",
            dataIndex: "size",
            key: "size",
            render: (size, record) => renderSize(size, record.isdir),
            align: "right",
            sorter: (0, misc_2.field_cmp)("size"),
        },
        {
            title: "Last Modified",
            dataIndex: "mtime",
            key: "mtime",
            align: "right",
            render: (mtime) => (mtime ? `${new Date(mtime).toLocaleString()}` : ""),
            sorter: (0, misc_2.field_cmp)("mtime"),
        },
    ];
}
function renderSize(size, isdir) {
    if (size == null)
        return "-";
    if (isdir)
        return `${size} ${(0, misc_1.plural)(size, "item")}`;
    return (0, misc_1.human_readable_size)(size);
}
//# sourceMappingURL=directory-listing.js.map