<commit_before>/*
 *  This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.
 *  License: AGPLv3 s.t. "Commons Clause" – see LICENSE.md for details
 */

import { React, Component, Rendered } from "../app-framework";
import { CoCalcLogo } from "./cocalc-logo";
import { IsPublicFunction } from "./types";
import { r_join } from "../r_misc/r_join";
import { SiteSearch } from "./search";
import { Settings } from "smc-hub/share/settings";

interface TopBarProps {
  viewer?: string;
  path: string; // The share url. Must have a leading `/`. {base_url}/share{path}
  project_id?: string;
  base_url: string;
  site_name?: string;
  is_public: IsPublicFunction;
  launch_path?: string;
  settings: Settings;
}

export class TopBar extends Component<TopBarProps> {
  private render_logo(top: string): Rendered {
    return (
      <span style={{ marginRight: "10px" }}>
        <a href={top} style={{ textDecoration: "none" }}>
          <CoCalcLogo base_url={this.props.base_url} /> {this.props.settings.site_name}{" "}
          Public Files
        </a>
      </span>
    );
  }

  private render_search(): Rendered {
    if (this.props.project_id != null) return;
    return (
      <div style={{ position: "absolute", top: 0, right: 0, width: "30%" }}>
        <SiteSearch />
      </div>
    );
  }

  public render(): Rendered {
    // TODO: break up this long function!
    const {
      viewer,
      path,
      launch_path,
      project_id,
      site_name,
      is_public,
    } = this.props;
    let path_component: Rendered | Rendered[], top: string;
    let project_link: Rendered = undefined;
    if (path === "/") {
      top = ".";
      path_component = <span />;
    } else {
      let i;
      let v = path.split("/").slice(2);
      top = v.map(() => "..").join("/");
      if (v.length > 0 && v[v.length - 1] === "") {
        v = v.slice(0, v.length - 1);
      }
      const segments: Rendered[] = [];
      let t = "";

      v.reverse();
      for (i = 0; i < v.length; i++) {
        const val = v[i];
        const segment_path = v.slice(i).reverse().join("/");
        if (t && (!project_id || is_public(project_id, segment_path))) {
          const href = `${t}?viewer=share`;
          segments.push(
            <a key={t} href={href}>
              {val}
            </a>
          );
        } else {
          segments.push(<span key={t}>{val}</span>);
        }
        if (!t) {
          if (path.slice(-1) === "/") {
            t = "..";
          } else {
            t = ".";
          }
        } else {
          t += "/..";
        }
      }
      segments.reverse();
      path_component = r_join(
        segments,
        <span style={{ margin: "0 5px" }}> / </span>
      );

      if (project_id) {
        // We put in anonymous=true so that an anonymous account will get created if the
        // user is not already signed in (they usually are if they have an account, so this
        // should cause minimal confusion and friction, but might cause some -- we have to balance
        // friction from asking questions -- which kills like 80% of users -- with friction
        // for existing users).  Also note that path has the leading slash so that's why
        // it isn't "share/" below.
        const cocalc_url = `${top}/../app?anonymous=true&launch=share${
          launch_path ? launch_path : path
        }`;
        project_link = (
          <a
            target="_blank"
            href={cocalc_url}
            className="btn btn-success"
            rel="nofollow"
            style={{ marginLeft: "30px", fontSize: "14pt" }}
          >
            Open in {site_name} with one click!
          </a>
        );
      }
    }
    if (viewer === "embed") {
      return project_link;
    }

    return (
      <div
        key="top"
        style={{
          padding: "5px 5px 0px 5px",
          background: "#efefef",
        }}
      ><commit_msg>fix the "Open" button in the share server, which has somehow degraded<commit_after>/*
 *  This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.
 *  License: AGPLv3 s.t. "Commons Clause" – see LICENSE.md for details
 */

import { React, Component, Rendered } from "../app-framework";
import { CoCalcLogo } from "./cocalc-logo";
import { IsPublicFunction } from "./types";
import { r_join } from "../r_misc/r_join";
import { SiteSearch } from "./search";
import { Settings } from "smc-hub/share/settings";

interface TopBarProps {
  viewer?: string;
  path: string; // The share url. Must have a leading `/`. {base_url}/share{path}
  project_id?: string;
  base_url: string;
  site_name?: string;
  is_public: IsPublicFunction;
  launch_path?: string;
  settings: Settings;
}

export class TopBar extends Component<TopBarProps> {
  private render_logo(top: string): Rendered {
    return (
      <span style={{ marginRight: "10px" }}>
        <a href={top} style={{ textDecoration: "none" }}>
          <CoCalcLogo base_url={this.props.base_url} />{" "}
          {this.props.settings.site_name} Public Files
        </a>
      </span>
    );
  }

  private render_search(): Rendered {
    if (this.props.project_id != null) return;
    return (
      <div style={{ position: "absolute", top: 0, right: 0, width: "30%" }}>
        <SiteSearch />
      </div>
    );
  }

  public render(): Rendered {
    // TODO: break up this long function!
    const {
      viewer,
      path,
      launch_path,
      project_id,
      is_public,
    } = this.props;
    let path_component: Rendered | Rendered[], top: string;
    let project_link: Rendered = undefined;
    if (path === "/") {
      top = ".";
      path_component = <span />;
    } else {
      let i;
      let v = path.split("/").slice(2);
      top = v.map(() => "..").join("/");
      if (v.length > 0 && v[v.length - 1] === "") {
        v = v.slice(0, v.length - 1);
      }
      const segments: Rendered[] = [];
      let t = "";

      v.reverse();
      for (i = 0; i < v.length; i++) {
        const val = v[i];
        const segment_path = v.slice(i).reverse().join("/");
        if (t && (!project_id || is_public(project_id, segment_path))) {
          const href = `${t}?viewer=share`;
          segments.push(
            <a key={t} href={href}>
              {val}
            </a>
          );
        } else {
          segments.push(<span key={t}>{val}</span>);
        }
        if (!t) {
          if (path.slice(-1) === "/") {
            t = "..";
          } else {
            t = ".";
          }
        } else {
          t += "/..";
        }
      }
      segments.reverse();
      path_component = r_join(
        segments,
        <span style={{ margin: "0 5px" }}> / </span>
      );

      if (project_id) {
        // We put in anonymous=true so that an anonymous account will get created if the
        // user is not already signed in (they usually are if they have an account, so this
        // should cause minimal confusion and friction, but might cause some -- we have to balance
        // friction from asking questions -- which kills like 80% of users -- with friction
        // for existing users).  Also note that path has the leading slash so that's why
        // it isn't "share/" below.
        const cocalc_url = `${top}/../app?anonymous=true&launch=share${
          launch_path ? launch_path : path
        }`;
        project_link = (
          <a
            target="_blank"
            href={cocalc_url}
            className="btn btn-success"
            rel="nofollow"
            style={{ marginLeft: "30px", fontSize: "14pt", maxWidth: "400px" }}
          >
            Open with one click!
          </a>
        );
      }
    }
    if (viewer === "embed") {
      return project_link;
    }

    return (
      <div
        key="top"
        style={{
          padding: "5px 5px 0px 5px",
          background: "#efefef",
        }}
      >
{'fraction_non_alphanumeric': 0.15148828372387588, 'fraction_numerical': 0.007979734008866372, 'mean_word_length': 1.8702290076335877, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7037059', 'n_tokens_mistral': 2517, 'n_tokens_neox': 2444, 'n_words': 692}