"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getServerSideProps = void 0;
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
 */
/*
This is simply a list of *all* publicly shared files/directories,
with a simple page.  It is mainly meant to be walked by crawlers
such as Google and for people to browse.
*/
const link_1 = __importDefault(require("next/link"));
const site_name_1 = __importDefault(require("components/share/site-name"));
const pool_1 = __importStar(require("@cocalc/database/pool"));
const public_paths_1 = __importDefault(require("components/share/public-paths"));
const layout_1 = require("components/share/layout");
const with_customize_1 = __importDefault(require("lib/with-customize"));
const customize_1 = require("lib/share/customize");
const google_search_1 = __importDefault(require("components/share/google-search"));
const proxy_input_1 = __importDefault(require("components/share/proxy-input"));
const get_account_1 = __importDefault(require("lib/account/get-account"));
const A_1 = __importDefault(require("components/misc/A"));
const PAGE_SIZE = 100;
function getPage(obj) {
    let { page } = obj ?? {};
    if (page == null) {
        return 1;
    }
    page = parseInt(page);
    if (isFinite(page)) {
        return Math.max(page, 1);
    }
    return 1;
}
function Pager({ page, publicPaths }) {
    return ((0, jsx_runtime_1.jsxs)("div", { children: ["Page ", page, "\u00A0\u00A0", page > 1 ? ((0, jsx_runtime_1.jsx)(link_1.default, { href: `/share/public_paths/page/${page - 1}`, children: (0, jsx_runtime_1.jsx)("a", { children: "Previous" }) })) : ((0, jsx_runtime_1.jsx)("span", { style: { color: "#888" }, children: "Previous" })), "\u00A0\u00A0", publicPaths != null && publicPaths.length >= PAGE_SIZE ? ((0, jsx_runtime_1.jsx)(link_1.default, { href: `/share/public_paths/page/${page + 1}`, children: (0, jsx_runtime_1.jsx)("a", { children: "Next" }) })) : ((0, jsx_runtime_1.jsx)("span", { style: { color: "#888" }, children: "Next" }))] }));
}
function All({ page, publicPaths, customize }) {
    const pager = (0, jsx_runtime_1.jsx)(Pager, { page: page, publicPaths: publicPaths });
    return ((0, jsx_runtime_1.jsx)(customize_1.Customize, { value: customize, children: (0, jsx_runtime_1.jsx)(layout_1.Layout, { title: `Page ${page} of public files`, children: (0, jsx_runtime_1.jsxs)("div", { children: [(0, jsx_runtime_1.jsx)("div", { style: { float: "right", width: "250px" }, children: (0, jsx_runtime_1.jsx)(google_search_1.default, {}) }), (0, jsx_runtime_1.jsxs)("h2", { children: ["Browse Jupyter Notebooks, Computational Whiteboards and much more on", " ", (0, jsx_runtime_1.jsx)(site_name_1.default, {})] }), (0, jsx_runtime_1.jsx)(proxy_input_1.default, {}), "Star items to easily ", (0, jsx_runtime_1.jsx)(A_1.default, { href: "/stars", children: "find them in your list" }), ".", (0, jsx_runtime_1.jsx)("br", {}), (0, jsx_runtime_1.jsx)("br", {}), pager, (0, jsx_runtime_1.jsx)("br", {}), (0, jsx_runtime_1.jsx)(public_paths_1.default, { publicPaths: publicPaths }), (0, jsx_runtime_1.jsx)("br", {}), pager] }) }) }));
}
exports.default = All;
async function getServerSideProps(context) {
    const isAuthenticated = (await (0, get_account_1.default)(context.req)) != null;
    const page = getPage(context.params);
    const pool = (0, pool_1.default)("medium");
    const { rows } = await pool.query(`SELECT public_paths.id, public_paths.path, public_paths.url, public_paths.description, ${(0, pool_1.timeInSeconds)("public_paths.last_edited", "last_edited")}, projects.avatar_image_tiny,
    counter::INT,
     (SELECT COUNT(*)::INT FROM public_path_stars WHERE public_path_id=public_paths.id) AS stars
    FROM public_paths, projects
    WHERE public_paths.project_id = projects.project_id
    AND public_paths.vhost IS NULL AND public_paths.disabled IS NOT TRUE AND public_paths.unlisted IS NOT TRUE AND
    ((public_paths.authenticated IS TRUE AND $1 IS TRUE) OR (public_paths.authenticated IS NOT TRUE))
    ORDER BY stars DESC, public_paths.last_edited DESC LIMIT $2 OFFSET $3`, [isAuthenticated, PAGE_SIZE, PAGE_SIZE * (page - 1)]);
    return await (0, with_customize_1.default)({ context, props: { page, publicPaths: rows } });
}
exports.getServerSideProps = getServerSideProps;
//# sourceMappingURL=%5Bpage%5D.js.map