Skip to content

Commit

Permalink
optimize code
Browse files Browse the repository at this point in the history
  • Loading branch information
zhouwenxuan authored and zhouwenxuan committed Dec 13, 2024
1 parent 7b2750e commit 17f10ab
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 20 deletions.
18 changes: 11 additions & 7 deletions frontend/src/metadata/components/context-menu/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ const ContextMenu = ({ options, getContainerRect, getContentRect, onOptionClick,
return menuStyles;
}, [getContentRect, getContainerRect]);

const handleOptionClick = useCallback((event, option) => {
(event) => {
event.stopPropagation();
onOptionClick(option);
setVisible(false);
};
}, [onOptionClick]);

useEffect(() => {
const handleShow = (event) => {
event.preventDefault();
Expand Down Expand Up @@ -80,18 +88,14 @@ const ContextMenu = ({ options, getContainerRect, getContentRect, onOptionClick,
return (
<div
ref={menuRef}
className='dropdown-menu sf-metadata-contextmenu'
className="dropdown-menu sf-metadata-contextmenu"
style={position}
>
{options.map((option, index) => (
<button
key={index}
className='dropdown-item sf-metadata-contextmenu-item'
onClick={(event) => {
event.stopPropagation();
onOptionClick(option);
setVisible(false);
}}
className="dropdown-item sf-metadata-contextmenu-item"
onClick={(e) => handleOptionClick(e, option)}
>
{option.label}
</button>
Expand Down
10 changes: 6 additions & 4 deletions frontend/src/metadata/utils/row/core.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { siteRoot } from '../../../utils/constants';
import URLDecorator from '../../../utils/url-decorator';
import { Utils } from '../../../utils/utils';
import { PRIVATE_COLUMN_KEY } from '../../constants';
import { getFileNameFromRecord, getParentDirFromRecord } from '../cell';
Expand Down Expand Up @@ -30,7 +29,7 @@ const updateTableRowsWithRowsData = (tables, tableId, recordsData = []) => {
});
};

export const checkIsDir = (record) => {
const checkIsDir = (record) => {
if (!record) return false;
const isDir = record[PRIVATE_COLUMN_KEY.IS_DIR];
if (typeof isDir === 'string') {
Expand All @@ -39,7 +38,7 @@ export const checkIsDir = (record) => {
return isDir;
};

export const openInNewTab = (record) => {
const openInNewTab = (record) => {
const repoID = window.sfMetadataStore.repoId;
const isDir = checkIsDir(record);
const parentDir = getParentDirFromRecord(record);
Expand All @@ -51,7 +50,7 @@ export const openInNewTab = (record) => {
window.open(url, '_blank');
};

export const openParentFolder = (record) => {
const openParentFolder = (record) => {
let parentDir = getParentDirFromRecord(record);

if (window.location.pathname.endsWith('/')) {
Expand All @@ -65,4 +64,7 @@ export const openParentFolder = (record) => {
export {
isTableRows,
updateTableRowsWithRowsData,
checkIsDir,
openInNewTab,
openParentFolder,
};
2 changes: 2 additions & 0 deletions frontend/src/metadata/utils/row/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@ export {
checkIsDir,
isTableRows,
updateTableRowsWithRowsData,
openInNewTab,
openParentFolder,
} from './core';
11 changes: 5 additions & 6 deletions frontend/src/metadata/views/kanban/boards/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,17 @@ import { checkIsPredefinedOption, getCellValueByColumn, isValidCellValue, getRec
getFileNameFromRecord, getParentDirFromRecord
} from '../../../utils/cell';
import { getColumnOptions, getColumnOriginName } from '../../../utils/column';
import { openFile } from '../../../utils/open-file';
import { checkIsDir, openInNewTab, openParentFolder } from '../../../utils/row';
import URLDecorator from '../../../../utils/url-decorator';
import { Utils, validateName } from '../../../../utils/utils';
import { getRowById } from '../../../utils/table';
import AddBoard from '../add-board';
import EmptyTip from '../../../../components/empty-tip';
import Board from './board';
import ImagePreviewer from '../../../components/cell-formatter/image-previewer';
import { openFile } from '../../../utils/open-file';
import { checkIsDir } from '../../../utils/row';
import URLDecorator from '../../../../utils/url-decorator';
import { Utils, validateName } from '../../../../utils/utils';
import toaster from '../../../../components/toast';
import Rename from '../rename';
import { getRowById } from '../../../utils/table';
import { openInNewTab, openParentFolder } from '../../../utils/row/core';
import ContextMenu from '../../../components/context-menu';

import './index.css';
Expand Down
5 changes: 2 additions & 3 deletions frontend/src/metadata/views/table/context-menu/index.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
import React, { useState, useRef, useEffect, useCallback, useMemo } from 'react';
import PropTypes from 'prop-types';
import toaster from '../../../../components/toast';
import { gettext, siteRoot } from '../../../../utils/constants';
import { gettext } from '../../../../utils/constants';
import { Utils } from '../../../../utils/utils';
import { useMetadataView } from '../../../hooks/metadata-view';
import { getColumnByKey, isNameColumn } from '../../../utils/column';
import { checkIsDir } from '../../../utils/row';
import { checkIsDir, openInNewTab, openParentFolder } from '../../../utils/row';
import { EVENT_BUS_TYPE, EVENT_BUS_TYPE as METADATA_EVENT_BUS_TYPE, PRIVATE_COLUMN_KEY } from '../../../constants';
import { getFileNameFromRecord, getParentDirFromRecord, getFileObjIdFromRecord,
getRecordIdFromRecord,
} from '../../../utils/cell';
import FileTagsDialog from '../../../components/dialog/file-tags-dialog';

import './index.css';
import { openInNewTab, openParentFolder } from '../../../utils/row/core';

const OPERATION = {
CLEAR_SELECTED: 'clear-selected',
Expand Down

0 comments on commit 17f10ab

Please sign in to comment.