Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/src/packages/frontend/course/common/folders-tool-bar.tsx
Views: 687
/*1* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45import { Input, Space } from "antd";6import type { Map as iMap } from "immutable";7import { useCallback, useMemo } from "react";89import { SEARCH_STYLE } from "./consts";10import { MultipleAddSearch } from "./multiple-add-search";11import { ItemName } from "./types";1213interface FoldersToolbarProps {14search?: string;15search_change: (search_value: string) => void; // search_change(current_search_value)16num_omitted?: number;17project_id: string;18items: iMap<string, any>;19add_folders: (folders: string[]) => void; // add_folders (Iterable<T>)20item_name: ItemName;21plural_item_name: string;22}2324export function FoldersToolbar({25search_change,26num_omitted,27items,28add_folders,29search: propsSearch,30item_name = "assignment",31plural_item_name = "item",32}: FoldersToolbarProps) {33return (34<Space>35<Input.Search36allowClear37placeholder={`Filter ${plural_item_name}...`}38value={propsSearch}39onChange={(e) => search_change(e.target.value)}40style={SEARCH_STYLE}41/>42{num_omitted ? (43<h544style={{45textAlign: "center",46marginTop: "5px",47}}48>49(Omitting {num_omitted}{" "}50{num_omitted > 1 ? plural_item_name : item_name})51</h5>52) : undefined}53<AddItems addItems={add_folders} itemName={item_name} items={items} />54</Space>55);56}5758export function AddItems({59addItems,60itemName,61items,62defaultOpen,63selectorStyle,64closable = true,65}: {66addItems;67itemName: ItemName;68items;69defaultOpen?;70selectorStyle?;71closable?;72}) {73// Omits any -collect directory (unless explicitly searched for).74// Omits any currently assigned directory or subdirectories.75const pathsToOmit = useMemo(() => {76const omit: Set<string> = new Set([]);77items78.filter((val) => !val.get("deleted"))79.map((val) => {80const path = val.get("path");81if (path != null) {82// path might not be set in case something went wrong83// (this has been hit in production)84omit.add(path);85}86});87return omit;88}, [items]);8990const isExcluded = useCallback(91(path) => {92if (!path) return true;93if (path.includes("-collect")) {94return true;95}96if (pathsToOmit.has(path)) {97return true;98}99// finally check if path is contained in any ommited path.100for (const omit of pathsToOmit) {101if (path.startsWith(omit + "/")) return true;102if (omit.startsWith(path + "/")) return true;103}104105return false;106},107[pathsToOmit],108);109110return (111<MultipleAddSearch112isExcluded={isExcluded}113addSelected={(paths) => {114if (paths != null) {115addItems(paths);116}117}}118itemName={itemName}119defaultOpen={defaultOpen}120selectorStyle={selectorStyle}121closable={closable}122/>123);124}125126127