2018-09-19 21:19:11 -05:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2024-07-25 14:17:24 +08:00
|
|
|
import { isPro, gettext, showLogoutIcon, enableSeasearch, enableElasticsearch } from '../../utils/constants';
|
2018-09-19 21:19:11 -05:00
|
|
|
import Search from '../search/search';
|
2024-06-13 11:34:51 +08:00
|
|
|
import AISearch from '../search/ai-search';
|
2020-11-16 10:45:19 +08:00
|
|
|
import SearchByName from '../search/search-by-name';
|
2018-09-19 21:19:11 -05:00
|
|
|
import Notification from '../common/notification';
|
|
|
|
import Account from '../common/account';
|
2020-03-25 18:03:04 +08:00
|
|
|
import Logout from '../common/logout';
|
2024-07-11 17:45:30 +08:00
|
|
|
import { EVENT_BUS_TYPE } from '../common/event-bus-type';
|
2018-09-19 21:19:11 -05:00
|
|
|
|
|
|
|
const propTypes = {
|
2018-12-07 10:36:59 +08:00
|
|
|
repoID: PropTypes.string,
|
2024-06-06 12:11:49 +08:00
|
|
|
path: PropTypes.string,
|
2020-11-16 10:45:19 +08:00
|
|
|
repoName: PropTypes.string,
|
|
|
|
isLibView: PropTypes.bool,
|
2024-06-25 21:48:57 +08:00
|
|
|
onSearchedClick: PropTypes.func,
|
2024-01-09 18:23:11 +08:00
|
|
|
searchPlaceholder: PropTypes.string,
|
|
|
|
currentRepoInfo: PropTypes.object,
|
2024-07-11 17:45:30 +08:00
|
|
|
eventBus: PropTypes.object,
|
2024-06-13 11:34:51 +08:00
|
|
|
isViewFile: PropTypes.bool,
|
2024-06-25 21:48:57 +08:00
|
|
|
showSearch: PropTypes.bool
|
2018-09-19 21:19:11 -05:00
|
|
|
};
|
|
|
|
|
2020-11-16 10:45:19 +08:00
|
|
|
class CommonToolbar extends React.Component {
|
|
|
|
|
2024-07-11 17:45:30 +08:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.state = {
|
|
|
|
repoID: props.repoID,
|
|
|
|
repoName: props.repoName,
|
|
|
|
isLibView: props.isLibView,
|
|
|
|
path: props.path,
|
|
|
|
isViewFile: props.isViewFile,
|
2024-07-11 20:37:57 +08:00
|
|
|
currentRepoInfo: props.currentRepoInfo,
|
2024-07-11 17:45:30 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
if (this.props.eventBus) {
|
|
|
|
this.unsubscribeLibChange = this.props.eventBus.subscribe(EVENT_BUS_TYPE.CURRENT_LIBRARY_CHANGED, this.onRepoChange);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
this.unsubscribeLibChange && this.unsubscribeLibChange();
|
|
|
|
}
|
|
|
|
|
2024-07-11 20:37:57 +08:00
|
|
|
onRepoChange = ({ repoID, repoName, isLibView, path, isViewFile, currentRepoInfo }) => {
|
|
|
|
this.setState({ repoID, repoName, isLibView, path, isViewFile, currentRepoInfo });
|
2024-07-11 17:45:30 +08:00
|
|
|
};
|
|
|
|
|
2024-07-13 21:03:31 +08:00
|
|
|
onSearchedClick = (searchedItem) => {
|
|
|
|
// If search result is current library, use libContentView.onSearchedClick; else use app.onSearchedClick
|
|
|
|
if (this.state.isLibView && this.state.repoID === searchedItem.repo_id) {
|
|
|
|
this.props.eventBus.dispatch(EVENT_BUS_TYPE.SEARCH_LIBRARY_CONTENT, searchedItem);
|
|
|
|
} else {
|
|
|
|
this.props.onSearchedClick(searchedItem);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-11-20 18:22:01 +08:00
|
|
|
renderSearch = () => {
|
2024-07-11 20:37:57 +08:00
|
|
|
const { repoID, repoName, isLibView, path, isViewFile, currentRepoInfo } = this.state;
|
2024-07-11 17:45:30 +08:00
|
|
|
const { searchPlaceholder } = this.props;
|
2023-11-20 18:22:01 +08:00
|
|
|
const placeholder = searchPlaceholder || gettext('Search files');
|
|
|
|
|
|
|
|
if (isPro) {
|
2024-07-25 14:17:24 +08:00
|
|
|
if (enableSeasearch && !enableElasticsearch) {
|
2024-06-13 11:34:51 +08:00
|
|
|
return (
|
|
|
|
<AISearch
|
|
|
|
repoID={repoID}
|
|
|
|
path={path}
|
|
|
|
isViewFile={isViewFile}
|
|
|
|
placeholder={placeholder}
|
2024-07-11 20:37:57 +08:00
|
|
|
currentRepoInfo={currentRepoInfo}
|
2024-07-13 21:03:31 +08:00
|
|
|
onSearchedClick={this.onSearchedClick}
|
2024-06-13 11:34:51 +08:00
|
|
|
isLibView={isLibView}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return (
|
|
|
|
<Search
|
|
|
|
repoID={repoID}
|
|
|
|
placeholder={placeholder}
|
2024-07-13 21:03:31 +08:00
|
|
|
onSearchedClick={this.onSearchedClick}
|
2024-06-13 11:34:51 +08:00
|
|
|
isViewFile={isViewFile}
|
|
|
|
isPublic={false}
|
|
|
|
path={path}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
2023-11-20 18:22:01 +08:00
|
|
|
} else {
|
|
|
|
if (isLibView) {
|
|
|
|
return (
|
|
|
|
<SearchByName repoID={repoID} repoName={repoName} />
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
render() {
|
2024-06-25 21:48:57 +08:00
|
|
|
const { showSearch = true } = this.props;
|
2023-11-20 18:22:01 +08:00
|
|
|
return (
|
|
|
|
<div className="common-toolbar">
|
2024-06-25 21:48:57 +08:00
|
|
|
{showSearch && this.renderSearch()}
|
2019-12-31 16:58:45 +08:00
|
|
|
<Notification />
|
2018-09-19 21:19:11 -05:00
|
|
|
<Account />
|
2020-03-25 20:20:56 +08:00
|
|
|
{showLogoutIcon && (<Logout />)}
|
2018-09-19 21:19:11 -05:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CommonToolbar.propTypes = propTypes;
|
|
|
|
|
2019-12-31 16:58:45 +08:00
|
|
|
export default CommonToolbar;
|