<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, redux } from "../app-framework";
import {
  path_split,
  should_open_in_foreground,
  trunc_middle,
} from "smc-util/misc";
import { Tip } from "./tip";

interface Props {
  path: string;
  project_id: string;
  display_name?: string; // if provided, show this as the link and show real name in popover
  full?: boolean; // true = show full path, false = show only basename
  trunc?: number; // truncate longer names and show a tooltip with the full name
  style?: React.CSSProperties;
  link?: boolean; // set to false to make it not be a link
}

// Component to attempt opening a cocalc path in a project
export const PathLink: React.FC<Props> = (props) => {
  function handle_click(e): void {
    e.preventDefault();
    const switch_to = should_open_in_foreground(e);
    redux.getProjectActions(props.project_id).open_file({
      path: props.path,
      foreground: switch_to,
      foreground_project: switch_to,
    });
  }

  function render_link(text): JSX.Element {
    if (props.link) {
      return (
        <a onClick={handle_click} style={props.style}>
          {text}
        </a>
      );
    } else {
      return <span style={props.style}>{text}</span>;
    }
  }

  const name = props.full ? props.path : path_split(props.path).tail;
  if (
    (props.trunc != null && name.length > props.trunc) ||
    (props.display_name != null && props.display_name !== name)
  ) {
    let text;
    if (props.trunc != null) {
      text = trunc_middle(
        props.display_name != null ? props.display_name : name,
        props.trunc
      );
    } else {
      text = props.display_name != null ? props.display_name : name;
    }
    return (
      <Tip title="" tip={name}>
        {render_link(text)}
      </Tip>
    );
  } else {
    return render_link(name);
  }
};

PathLink.defaultProps = {
  style: {},
  full: false,
  link: true,
};
<commit_msg>fix #1046 -- in project log make the extensions of files be in grey<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, redux } from "../app-framework";
import {
  endswith,
  path_split,
  separate_file_extension,
  should_open_in_foreground,
  trunc_middle,
} from "smc-util/misc";
import { Tip } from "./tip";

interface Props {
  path: string;
  project_id: string;
  display_name?: string; // if provided, show this as the link and show real name in popover
  full?: boolean; // true = show full path, false = show only basename
  trunc?: number; // truncate longer names and show a tooltip with the full name
  style?: React.CSSProperties;
  link?: boolean; // set to false to make it not be a link
}

// Component to attempt opening a cocalc path in a project
export const PathLink: React.FC<Props> = (props) => {
  function handle_click(e): void {
    e.preventDefault();
    const switch_to = should_open_in_foreground(e);
    redux.getProjectActions(props.project_id).open_file({
      path: props.path,
      foreground: switch_to,
      foreground_project: switch_to,
    });
  }

  function render_link(text): JSX.Element {
    let s;
    if (!endswith(text, "/")) {
      const { name, ext } = separate_file_extension(text);
      if (ext) {
        s = (
          <>
            {name}
            <span style={{ color: "#999" }}>.{ext}</span>
          </>
        );
      } else {
        s = name;
      }
    } else {
      s = text;
    }
    if (props.link) {
      return (
        <a onClick={handle_click} style={props.style}>
          {s}
        </a>
      );
    } else {
      return <span style={props.style}>{s}</span>;
    }
  }

  const name = props.full ? props.path : path_split(props.path).tail;
  if (
    (props.trunc != null && name.length > props.trunc) ||
    (props.display_name != null && props.display_name !== name)
  ) {
    let text;
    if (props.trunc != null) {
      text = trunc_middle(
        props.display_name != null ? props.display_name : name,
        props.trunc
      );
    } else {
      text = props.display_name != null ? props.display_name : name;
    }
    return (
      <Tip title="" tip={name}>
        {render_link(text)}
      </Tip>
    );
  } else {
    return render_link(name);
  }
};

PathLink.defaultProps = {
  style: {},
  full: false,
  link: true,
};

{'fraction_non_alphanumeric': 0.13419240168851365, 'fraction_numerical': 0.0037769384581204176, 'mean_word_length': 2.539308176100629, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 25, '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': '3380898', 'n_tokens_mistral': 1484, 'n_tokens_neox': 1455, 'n_words': 409}