"use strict";
var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var jsx_runtime_1 = require("react/jsx-runtime");
/*
Show directory in a project based on cached information
in the database.
*/
var react_1 = require("react");
var database_1 = __importDefault(require("lib/hooks/database"));
var antd_1 = require("antd");
var loading_1 = __importDefault(require("components/share/loading"));
var edit_url_1 = __importDefault(require("lib/share/edit-url"));
var cmp_1 = require("@cocalc/util/cmp");
var misc_1 = require("@cocalc/util/misc");
var A_1 = __importDefault(require("components/misc/A"));
var path_1 = require("path");
var file_associations_1 = require("@cocalc/frontend/file-associations");
var icon_1 = require("@cocalc/frontend/components/icon");
var misc_2 = require("@cocalc/util/misc");
function getQuery(project_id, path) {
    return {
        listings: { project_id: project_id, path: path, listing: null },
    };
}
function Listing(_a) {
    var _b, _c;
    var project_id = _a.project_id, path = _a.path, title = _a.title, update = _a.update, sort = _a.sort;
    var _d = (0, database_1.default)(getQuery(project_id, path)), error = _d.error, value = _d.value, loading = _d.loading, query = _d.query;
    (0, react_1.useEffect)(function () {
        // update the listing whenever "update" changes.
        query(getQuery(project_id, path));
    }, [update, project_id, path]);
    return ((0, jsx_runtime_1.jsxs)("div", { children: [loading && (0, jsx_runtime_1.jsx)(loading_1.default, {}), error && (0, jsx_runtime_1.jsx)(antd_1.Alert, { type: "error", message: error, showIcon: true }), !loading && !error && ((0, jsx_runtime_1.jsx)(FileList, { listing: (_c = (_b = value.listings) === null || _b === void 0 ? void 0 : _b.listing) !== null && _c !== void 0 ? _c : [], project_id: project_id, path: path, title: title, sort: sort }))] }));
}
exports.default = Listing;
function FileList(_a) {
    var listing = _a.listing, project_id = _a.project_id, path = _a.path, title = _a.title, sort = _a.sort;
    var c;
    if (sort == "time") {
        c = function (a, b) { return (0, cmp_1.cmp)(b.mtime, a.mtime); };
    }
    else if (sort == "size") {
        c = function (a, b) { return (0, cmp_1.cmp)(a.size, b.size); };
    }
    else {
        c = function (a, b) { return (0, cmp_1.cmp)(a.name.toLowerCase(), b.name.toLowerCase()); };
    }
    var dataSource = listing
        .filter(function (entry) { return !entry.name.startsWith("."); })
        .sort(c);
    var columns = [
        {
            title: "Type",
            dataIndex: "isdir",
            key: "isdir",
            render: function (isdir, entry) {
                var _a, _b, _c;
                return ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)(icon_1.Icon, { name: isdir
                                ? "folder"
                                : (_b = (_a = file_associations_1.file_associations[(0, misc_1.filename_extension)(entry.name)]) === null || _a === void 0 ? void 0 : _a.icon) !== null && _b !== void 0 ? _b : (_c = file_associations_1.file_associations[""]) === null || _c === void 0 ? void 0 : _c.icon }), isdir && (0, jsx_runtime_1.jsx)(icon_1.Icon, { name: "caret-right" })] }));
            },
        },
        {
            title: "Name",
            dataIndex: "name",
            key: "name",
            render: function (_, entry) { return ((0, jsx_runtime_1.jsx)(A_1.default, __assign({ href: (0, edit_url_1.default)({ project_id: project_id, path: (0, path_1.join)(path, entry.name) }), external: true }, { children: entry.name }))); },
        },
        {
            title: "Date Modified",
            dataIndex: "mtime",
            key: "mtime",
            render: function (mtime) { return (0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, { children: new Date(mtime * 1000).toLocaleString() }); },
        },
        {
            title: "Size",
            dataIndex: "size",
            key: "size",
            render: function (size, _a) {
                var isdir = _a.isdir;
                return isdir ? "".concat(size, " ").concat((0, misc_2.plural)(size, "item")) : (0, misc_2.human_readable_size)(size);
            },
        },
    ];
    return ((0, jsx_runtime_1.jsxs)(antd_1.Card, __assign({ style: { boxShadow: "4px 4px 2px #ddd" }, title: title ? ((0, jsx_runtime_1.jsxs)("div", { children: ["Title: ", (0, jsx_runtime_1.jsx)(A_1.default, __assign({ external: true, href: (0, edit_url_1.default)({
                        type: "collaborator",
                        project_id: project_id,
                    }) }, { children: title }))] })) : ("Directory Listing") }, { children: [(0, jsx_runtime_1.jsx)("div", __assign({ style: { fontSize: "11pt", marginBottom: "10px" } }, { children: path ? path : "Home Directory" })), (0, jsx_runtime_1.jsx)(antd_1.Table, { dataSource: dataSource, columns: columns, style: { width: "100%" }, pagination: { hideOnSinglePage: true }, rowKey: "name" })] })));
}
//# sourceMappingURL=listing.js.map