Path: blob/master/src/packages/frontend/editors/file-info-dropdown.tsx
1678 views
/*1* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45// info button inside the editor when editing a file. links you back to the file listing with the action prompted67import { createRoot } from "react-dom/client";89import { CSS, React, useActions } from "@cocalc/frontend/app-framework";10import { DropdownMenu, Icon, IconName } from "@cocalc/frontend/components";11import { MenuItems } from "@cocalc/frontend/components/dropdown-menu";12import { useStudentProjectFunctionality } from "@cocalc/frontend/course";13import { file_actions } from "@cocalc/frontend/project_store";14import { capitalize, filename_extension } from "@cocalc/util/misc";1516interface Props {17filename: string; // expects the full path name18project_id: string;19is_public?: boolean;20style?: CSS;21button?: boolean;22mode?: "explorer" | "flyout";23}2425const EditorFileInfoDropdown: React.FC<Props> = React.memo(26(props: Props) => {27const {28filename,29project_id,30is_public,31style,32button,33mode = "explorer",34} = props;35const actions = useActions({ project_id });36const student_project_functionality =37useStudentProjectFunctionality(project_id);38if (student_project_functionality.disableActions) {39return <span></span>;40}4142function handle_click(name) {43if (actions == null) {44console.warn("file click -- actions not available");45return;46}47if (name === "new") {48let new_ext: string | undefined = filename_extension(filename);49if (new_ext == "") {50// otherwise 'foo' leads to 'random.'51new_ext = undefined;52}53// Special calse -- not an action on this one file54actions.set_active_tab("new", { new_ext });55return;56}57for (const key in file_actions) {58if (key === name) {59actions.show_file_action_panel({60path: filename,61action: key,62});63break;64}65}66}6768function render_menu_item(key: string, icon: IconName): MenuItems[0] {69return {70key,71onClick: () => handle_click(key),72label: (73<>74<Icon name={icon} style={{ width: "25px" }} /> {capitalize(key)}75</>76),77};78}7980function render_menu_items(): MenuItems {81let items: { [key: string]: IconName };82const v: MenuItems = [];83if (is_public) {84// Fewer options when viewing the action dropdown in public mode:85items = {86download: "cloud-download",87copy: "files",88};89} else {90if (mode !== "flyout") {91v.push(render_menu_item("new", "plus-circle"));92}93// create a map from name to icon94items = {};95for (const key in file_actions) {96const { icon, hideFlyout } = file_actions[key];97if (mode === "flyout" && hideFlyout) continue;98items[key] = icon;99}100}101102for (let key in items) {103const icon = items[key];104v.push(render_menu_item(key, icon));105}106return v;107}108109return (110<DropdownMenu111button={button}112style={{ ...{ height: "100%" }, ...style }}113id="file_info_button"114title={"File"}115items={render_menu_items()}116/>117);118},119(prev, next) =>120prev.filename == next.filename && prev.is_public == next.is_public,121);122123// This is for sage worksheets...124export function render_file_info_dropdown(125filename: string,126project_id: string,127dom_node,128is_public?,129) {130const root = createRoot(dom_node);131root.render(132<EditorFileInfoDropdown133filename={filename}134project_id={project_id}135is_public={is_public}136style={{ height: "34px" }}137/>,138);139}140141142