2019-02-13 10:09:01 +00:00
|
|
|
import React, { Component, Fragment } from 'react';
|
2018-10-16 10:19:51 +00:00
|
|
|
import PropTypes from 'prop-types';
|
2023-10-08 09:20:38 +00:00
|
|
|
import isHotkey from 'is-hotkey';
|
2019-02-13 10:09:01 +00:00
|
|
|
import MediaQuery from 'react-responsive';
|
2019-07-08 08:47:04 +00:00
|
|
|
import { seafileAPI } from '../../utils/seafile-api';
|
2023-10-21 03:38:12 +00:00
|
|
|
import { gettext, siteRoot, username, enableSeafileAI } from '../../utils/constants';
|
2018-09-04 09:16:50 +00:00
|
|
|
import SearchResultItem from './search-result-item';
|
2020-04-01 03:35:40 +00:00
|
|
|
import { Utils } from '../../utils/utils';
|
2023-10-09 09:32:13 +00:00
|
|
|
import { isMac } from '../../utils/extra-attributes';
|
2020-04-01 03:35:40 +00:00
|
|
|
import toaster from '../toast';
|
2018-08-06 10:29:12 +00:00
|
|
|
|
2023-10-21 03:38:12 +00:00
|
|
|
const INDEX_STATE = {
|
|
|
|
RUNNING: 'running',
|
|
|
|
UNCREATED: 'uncreated',
|
|
|
|
FINISHED: 'finished'
|
|
|
|
};
|
|
|
|
|
|
|
|
const SEARCH_MODE = {
|
|
|
|
SIMILARITY: 'similarity',
|
|
|
|
NORMAL: 'normal',
|
|
|
|
};
|
|
|
|
|
2018-10-16 10:19:51 +00:00
|
|
|
const propTypes = {
|
2018-12-07 02:36:59 +00:00
|
|
|
repoID: PropTypes.string,
|
2018-12-13 12:42:51 +00:00
|
|
|
placeholder: PropTypes.string,
|
2018-10-16 10:19:51 +00:00
|
|
|
onSearchedClick: PropTypes.func.isRequired,
|
2023-09-13 00:40:50 +00:00
|
|
|
isPublic: PropTypes.bool,
|
2023-10-21 03:38:12 +00:00
|
|
|
isLibView: PropTypes.bool,
|
|
|
|
repoName: PropTypes.string,
|
2018-10-16 10:19:51 +00:00
|
|
|
};
|
|
|
|
|
2023-10-08 09:20:38 +00:00
|
|
|
const PER_PAGE = 10;
|
2023-10-09 09:32:13 +00:00
|
|
|
const controlKey = isMac() ? '⌘' : 'Ctrl';
|
2023-10-08 09:20:38 +00:00
|
|
|
|
2018-08-06 10:29:12 +00:00
|
|
|
class Search extends Component {
|
2018-08-17 04:23:55 +00:00
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
2021-12-17 03:16:33 +00:00
|
|
|
this.baseSearchPageURL = `${siteRoot}search/`;
|
2018-08-17 04:23:55 +00:00
|
|
|
this.state = {
|
|
|
|
width: 'default',
|
|
|
|
value: '',
|
|
|
|
resultItems: [],
|
2023-10-11 07:58:06 +00:00
|
|
|
highlightIndex: 0,
|
2023-10-08 09:20:38 +00:00
|
|
|
page: 0,
|
|
|
|
isLoading: false,
|
|
|
|
hasMore: true,
|
2018-08-17 04:23:55 +00:00
|
|
|
isMaskShow: false,
|
|
|
|
isResultShow: false,
|
|
|
|
isResultGetted: false,
|
2019-02-13 10:09:01 +00:00
|
|
|
isCloseShow: false,
|
|
|
|
isSearchInputShow: false, // for mobile
|
2023-10-21 03:38:12 +00:00
|
|
|
searchPageUrl: this.baseSearchPageURL,
|
|
|
|
searchMode: SEARCH_MODE.NORMAL,
|
|
|
|
indexState: INDEX_STATE.UNCREATED,
|
2018-08-17 04:23:55 +00:00
|
|
|
};
|
|
|
|
this.inputValue = '';
|
2023-10-11 12:39:41 +00:00
|
|
|
this.highlightRef = null;
|
2018-12-20 07:37:14 +00:00
|
|
|
this.source = null; // used to cancel request;
|
2023-10-08 09:20:38 +00:00
|
|
|
this.inputRef = React.createRef();
|
2023-10-11 12:39:41 +00:00
|
|
|
this.searchContainer = React.createRef();
|
2023-10-08 09:20:38 +00:00
|
|
|
this.searchResultListRef = React.createRef();
|
2023-10-21 03:38:12 +00:00
|
|
|
this.timer = null;
|
|
|
|
this.indexStateTimer = null;
|
2023-10-08 09:20:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
document.addEventListener('keydown', this.onDocumentKeydown);
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
document.removeEventListener('keydown', this.onDocumentKeydown);
|
2023-10-21 03:38:12 +00:00
|
|
|
this.indexStateTimer && clearInterval(this.indexStateTimer);
|
|
|
|
this.timer && clearTimeout(this.timer);
|
2018-08-17 04:23:55 +00:00
|
|
|
}
|
|
|
|
|
2023-10-08 09:20:38 +00:00
|
|
|
onDocumentKeydown = (e) => {
|
|
|
|
if (isHotkey('mod+f')(e)) {
|
|
|
|
e.preventDefault();
|
|
|
|
this.onFocusHandler();
|
|
|
|
if (this.inputRef && this.inputRef.current) {
|
|
|
|
this.inputRef.current.focus();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (isHotkey('esc', e)) {
|
|
|
|
e.preventDefault();
|
2023-10-21 03:38:12 +00:00
|
|
|
this.inputRef && this.inputRef.current && this.inputRef.current.blur();
|
2023-10-08 09:20:38 +00:00
|
|
|
this.resetToDefault();
|
2023-10-11 12:39:41 +00:00
|
|
|
} else if (isHotkey('enter', e)) {
|
|
|
|
this.onEnter(e);
|
|
|
|
} else if (isHotkey('up', e)) {
|
|
|
|
this.onUp(e);
|
|
|
|
} else if (isHotkey('down', e)) {
|
|
|
|
this.onDown(e);
|
2023-10-11 07:58:06 +00:00
|
|
|
}
|
2023-10-08 09:20:38 +00:00
|
|
|
};
|
|
|
|
|
2018-08-17 04:23:55 +00:00
|
|
|
onFocusHandler = () => {
|
2023-10-21 03:38:12 +00:00
|
|
|
const { searchMode, indexState: currentIndexState } = this.state;
|
|
|
|
const { repoID } = this.props;
|
|
|
|
this.setState({ width: '570px', isMaskShow: true, isCloseShow: true }, () => {
|
|
|
|
if (searchMode !== SEARCH_MODE.SIMILARITY) return;
|
|
|
|
if (currentIndexState === INDEX_STATE.FINISHED) return;
|
|
|
|
seafileAPI.queryLibraryIndexState(repoID).then(res => {
|
|
|
|
const { state: indexState, task_id: taskId } = res.data;
|
|
|
|
this.setState({ indexState }, () => {
|
|
|
|
if (indexState !== INDEX_STATE.RUNNING) return;
|
|
|
|
this.queryIndexTaskStatus(taskId);
|
|
|
|
});
|
|
|
|
}).catch(error => {
|
|
|
|
this.setState({ indexState: INDEX_STATE.UNCREATED });
|
|
|
|
});
|
2018-09-29 10:32:53 +00:00
|
|
|
});
|
2023-09-13 00:40:50 +00:00
|
|
|
};
|
2018-08-17 04:23:55 +00:00
|
|
|
|
|
|
|
onCloseHandler = () => {
|
|
|
|
this.resetToDefault();
|
2023-09-13 00:40:50 +00:00
|
|
|
};
|
2018-08-17 04:23:55 +00:00
|
|
|
|
2023-10-11 12:39:41 +00:00
|
|
|
onUp = (e) => {
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
const { highlightIndex } = this.state;
|
|
|
|
if (highlightIndex > 0) {
|
|
|
|
this.setState({ highlightIndex: highlightIndex - 1 }, () => {
|
|
|
|
if (this.highlightRef) {
|
|
|
|
const { top, height } = this.highlightRef.getBoundingClientRect();
|
|
|
|
if (top - height < 0) {
|
|
|
|
this.searchContainer.current.scrollTop -= height;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
onDown = (e) => {
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
const { highlightIndex, resultItems } = this.state;
|
|
|
|
if (highlightIndex < resultItems.length - 1) {
|
|
|
|
this.setState({ highlightIndex: highlightIndex + 1 }, () => {
|
|
|
|
if (this.highlightRef) {
|
|
|
|
const { top, height } = this.highlightRef.getBoundingClientRect();
|
|
|
|
const outerHeight = 300;
|
|
|
|
if (top > outerHeight) {
|
|
|
|
this.searchContainer.current.scrollTop += height;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
onEnter = (e) => {
|
|
|
|
e.preventDefault();
|
|
|
|
let item = this.state.resultItems[this.state.highlightIndex];
|
|
|
|
if (item) {
|
|
|
|
if (document.activeElement) {
|
|
|
|
document.activeElement.blur();
|
|
|
|
}
|
|
|
|
this.onItemClickHandler(item);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-09-18 02:11:37 +00:00
|
|
|
onItemClickHandler = (item) => {
|
2018-08-17 04:23:55 +00:00
|
|
|
this.resetToDefault();
|
2018-09-18 02:11:37 +00:00
|
|
|
this.props.onSearchedClick(item);
|
2023-09-13 00:40:50 +00:00
|
|
|
};
|
2018-08-17 04:23:55 +00:00
|
|
|
|
|
|
|
onChangeHandler = (event) => {
|
2023-10-23 08:03:54 +00:00
|
|
|
const { searchMode } = this.state;
|
2023-10-21 03:38:12 +00:00
|
|
|
const newValue = event.target.value;
|
|
|
|
this.setState({ value: newValue }, () => {
|
|
|
|
if (this.inputValue === newValue.trim()) return;
|
|
|
|
this.inputValue = newValue.trim();
|
2023-10-23 08:03:54 +00:00
|
|
|
this.onSearch(searchMode === SEARCH_MODE.NORMAL);
|
2023-10-21 03:38:12 +00:00
|
|
|
});
|
|
|
|
};
|
2018-08-17 04:23:55 +00:00
|
|
|
|
2023-10-23 08:03:54 +00:00
|
|
|
onKeydownHandler = (event) => {
|
|
|
|
if (isHotkey('enter', event)) {
|
|
|
|
if (this.state.searchMode === SEARCH_MODE.NORMAL) return;
|
|
|
|
this.onSearch(true);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
onSearch = (isGetSearchResult) => {
|
2023-10-21 03:38:12 +00:00
|
|
|
const { value } = this.state;
|
|
|
|
const { repoID } = this.props;
|
|
|
|
const _this = this;
|
|
|
|
this.timer && clearTimeout(this.timer);
|
|
|
|
|
|
|
|
if (_this.inputValue === '' || _this.getValueLength(_this.inputValue) < 3) {
|
2018-08-17 04:23:55 +00:00
|
|
|
this.setState({
|
2023-10-11 07:58:06 +00:00
|
|
|
highlightIndex: 0,
|
2023-10-08 09:20:38 +00:00
|
|
|
resultItems: [],
|
2018-08-17 04:23:55 +00:00
|
|
|
isResultShow: false,
|
|
|
|
isResultGetted: false
|
2018-09-29 10:32:53 +00:00
|
|
|
});
|
2023-10-21 03:38:12 +00:00
|
|
|
return;
|
2018-08-17 04:23:55 +00:00
|
|
|
}
|
2023-10-23 08:03:54 +00:00
|
|
|
if (!isGetSearchResult) return;
|
2023-10-21 03:38:12 +00:00
|
|
|
|
|
|
|
const queryData = {
|
|
|
|
q: value,
|
2018-09-18 02:11:37 +00:00
|
|
|
search_repo: repoID ? repoID : 'all',
|
2018-12-13 12:42:51 +00:00
|
|
|
search_ftypes: 'all',
|
2018-09-29 10:32:53 +00:00
|
|
|
};
|
2018-08-17 04:23:55 +00:00
|
|
|
this.timer = setTimeout(_this.getSearchResult(queryData), 500);
|
2023-09-13 00:40:50 +00:00
|
|
|
};
|
2018-08-17 04:23:55 +00:00
|
|
|
|
2023-10-21 03:38:12 +00:00
|
|
|
getSearchResult = (queryData) => {
|
2023-10-08 09:20:38 +00:00
|
|
|
if (this.source) {
|
|
|
|
this.source.cancel('prev request is cancelled');
|
2018-08-17 04:23:55 +00:00
|
|
|
}
|
|
|
|
this.setState({
|
|
|
|
isResultShow: true,
|
2023-10-08 09:20:38 +00:00
|
|
|
isResultGetted: false,
|
|
|
|
resultItems: [],
|
2023-10-11 07:58:06 +00:00
|
|
|
highlightIndex: 0,
|
2018-09-29 10:32:53 +00:00
|
|
|
});
|
2019-12-31 08:58:45 +00:00
|
|
|
this.source = seafileAPI.getSource();
|
2023-10-08 09:20:38 +00:00
|
|
|
this.sendRequest(queryData, this.source.token, 1);
|
2023-10-21 03:38:12 +00:00
|
|
|
};
|
2018-08-17 04:23:55 +00:00
|
|
|
|
2023-10-08 09:20:38 +00:00
|
|
|
sendRequest = (queryData, cancelToken, page) => {
|
2019-07-08 08:47:04 +00:00
|
|
|
let isPublic = this.props.isPublic;
|
2023-10-08 09:20:38 +00:00
|
|
|
this.queryData = queryData;
|
2019-07-08 08:47:04 +00:00
|
|
|
|
|
|
|
if (isPublic) {
|
2023-10-08 09:20:38 +00:00
|
|
|
seafileAPI.searchFilesInPublishedRepo(queryData.search_repo, queryData.q, page, PER_PAGE).then(res => {
|
|
|
|
this.source = null;
|
|
|
|
if (res.data.total > 0) {
|
|
|
|
this.setState({
|
|
|
|
resultItems: [...this.state.resultItems, this.formatResultItems(res.data.results)],
|
|
|
|
isResultGetted: true,
|
|
|
|
page: page + 1,
|
|
|
|
isLoading: false,
|
|
|
|
hasMore: res.data.has_more,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
this.setState({
|
2023-10-11 07:58:06 +00:00
|
|
|
highlightIndex: 0,
|
2019-07-08 08:47:04 +00:00
|
|
|
resultItems: [],
|
2023-10-08 09:20:38 +00:00
|
|
|
isLoading: false,
|
|
|
|
isResultGetted: true,
|
|
|
|
hasMore: res.data.has_more,
|
2019-07-08 08:47:04 +00:00
|
|
|
});
|
|
|
|
}
|
2019-07-16 02:01:09 +00:00
|
|
|
}).catch(error => {
|
|
|
|
let errMessage = Utils.getErrorMsg(error);
|
|
|
|
toaster.danger(errMessage);
|
2023-10-08 09:20:38 +00:00
|
|
|
this.setState({ isLoading: false });
|
2019-12-31 08:58:45 +00:00
|
|
|
});
|
2019-07-08 08:47:04 +00:00
|
|
|
} else {
|
2021-12-17 03:16:33 +00:00
|
|
|
this.updateSearchPageURL(queryData);
|
2023-10-08 09:20:38 +00:00
|
|
|
queryData['per_page'] = PER_PAGE;
|
|
|
|
queryData['page'] = page;
|
2023-10-21 03:38:12 +00:00
|
|
|
if (this.state.searchMode === SEARCH_MODE.NORMAL) {
|
|
|
|
this.onNormalSearch(queryData, cancelToken, page);
|
|
|
|
} else {
|
|
|
|
this.onSimilaritySearch(queryData, cancelToken, page);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
onNormalSearch = (queryData, cancelToken, page) => {
|
|
|
|
seafileAPI.searchFiles(queryData, cancelToken).then(res => {
|
|
|
|
this.source = null;
|
|
|
|
if (res.data.total > 0) {
|
|
|
|
this.setState({
|
|
|
|
resultItems: [...this.state.resultItems, ...this.formatResultItems(res.data.results)],
|
|
|
|
isResultGetted: true,
|
|
|
|
isLoading: false,
|
|
|
|
page: page + 1,
|
|
|
|
hasMore: res.data.has_more,
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.setState({
|
|
|
|
highlightIndex: 0,
|
|
|
|
resultItems: [],
|
|
|
|
isLoading: false,
|
|
|
|
isResultGetted: true,
|
|
|
|
hasMore: res.data.has_more,
|
2019-07-08 08:47:04 +00:00
|
|
|
});
|
2023-10-21 03:38:12 +00:00
|
|
|
}).catch(error => {
|
|
|
|
/* eslint-disable */
|
|
|
|
console.log(error);
|
|
|
|
this.setState({ isLoading: false });
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
onSimilaritySearch = (queryData, cancelToken, page) => {
|
|
|
|
const { indexState } = this.state;
|
|
|
|
if (indexState === INDEX_STATE.UNCREATED) {
|
|
|
|
toaster.warning(gettext('Please create index first.'));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (indexState === INDEX_STATE.RUNNING) {
|
|
|
|
toaster.warning(gettext('Indexing, please try again later.'));
|
|
|
|
return;
|
2019-07-08 08:47:04 +00:00
|
|
|
}
|
2023-10-21 03:38:12 +00:00
|
|
|
seafileAPI.similaritySearchFiles(queryData, cancelToken).then(res => {
|
|
|
|
this.source = null;
|
|
|
|
if (res.data && res.data.children_list.length > 0) {
|
|
|
|
this.setState({
|
|
|
|
resultItems: [...this.state.resultItems, ...this.formatSimilarityItems(res.data.children_list)],
|
|
|
|
isResultGetted: true,
|
|
|
|
isLoading: false,
|
|
|
|
page: page + 1,
|
|
|
|
hasMore: res.data.has_more,
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.setState({
|
|
|
|
highlightIndex: 0,
|
|
|
|
resultItems: [],
|
|
|
|
isLoading: false,
|
|
|
|
isResultGetted: true,
|
|
|
|
hasMore: res.data.has_more,
|
|
|
|
});
|
|
|
|
}).catch(error => {
|
|
|
|
/* eslint-disable */
|
|
|
|
console.log(error);
|
|
|
|
this.setState({ isLoading: false });
|
|
|
|
});
|
2023-10-08 09:20:38 +00:00
|
|
|
};
|
2018-08-17 04:23:55 +00:00
|
|
|
|
2023-10-08 09:20:38 +00:00
|
|
|
onResultListScroll = (e) => {
|
|
|
|
// Load less than 100 results
|
|
|
|
if (!this.state.hasMore || this.state.isLoading || this.state.resultItems.length > 100) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const listPadding = 20;
|
|
|
|
if (e.target.scrollTop + e.target.clientHeight + listPadding > this.searchResultListRef.current.clientHeight - 10) {
|
|
|
|
this.setState({isLoading: true}, () => {
|
|
|
|
this.source = seafileAPI.getSource();
|
|
|
|
this.sendRequest(this.queryData, this.source.token, this.state.page);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
2018-08-17 04:23:55 +00:00
|
|
|
|
2021-12-17 03:16:33 +00:00
|
|
|
updateSearchPageURL(queryData) {
|
|
|
|
let params = '';
|
|
|
|
for (let key in queryData) {
|
|
|
|
params += key + '=' + encodeURIComponent(queryData[key]) + '&';
|
|
|
|
}
|
|
|
|
this.setState({searchPageUrl: `${this.baseSearchPageURL}?${params.substring(0, params.length - 1)}`});
|
|
|
|
}
|
|
|
|
|
2018-08-17 04:23:55 +00:00
|
|
|
getValueLength(str) {
|
|
|
|
var i = 0, code, len = 0;
|
|
|
|
for (; i < str.length; i++) {
|
|
|
|
code = str.charCodeAt(i);
|
|
|
|
if (code == 10) { //solve enter problem
|
|
|
|
len += 2;
|
|
|
|
} else if (code < 0x007f) {
|
|
|
|
len += 1;
|
|
|
|
} else if (code >= 0x0080 && code <= 0x07ff) {
|
|
|
|
len += 2;
|
|
|
|
} else if (code >= 0x0800 && code <= 0xffff) {
|
|
|
|
len += 3;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
formatResultItems(data) {
|
|
|
|
let items = [];
|
2023-10-08 09:20:38 +00:00
|
|
|
for (let i = 0; i < data.length; i++) {
|
2018-08-17 04:23:55 +00:00
|
|
|
items[i] = {};
|
|
|
|
items[i]['index'] = [i];
|
|
|
|
items[i]['name'] = data[i].name;
|
2018-09-18 02:11:37 +00:00
|
|
|
items[i]['path'] = data[i].fullpath;
|
|
|
|
items[i]['repo_id'] = data[i].repo_id;
|
2018-12-13 12:42:51 +00:00
|
|
|
items[i]['repo_name'] = data[i].repo_name;
|
|
|
|
items[i]['is_dir'] = data[i].is_dir;
|
2018-08-17 04:23:55 +00:00
|
|
|
items[i]['link_content'] = decodeURI(data[i].fullpath).substring(1);
|
|
|
|
items[i]['content'] = data[i].content_highlight;
|
2021-01-21 03:51:16 +00:00
|
|
|
items[i]['thumbnail_url'] = data[i].thumbnail_url;
|
2018-08-17 04:23:55 +00:00
|
|
|
}
|
|
|
|
return items;
|
|
|
|
}
|
|
|
|
|
2023-10-21 03:38:12 +00:00
|
|
|
formatSimilarityItems(data) {
|
|
|
|
let items = [];
|
|
|
|
let repo_id = this.props.repoID;
|
|
|
|
for (let i = 0; i < data.length; i++) {
|
|
|
|
items[i] = {};
|
|
|
|
items[i]['index'] = [i];
|
|
|
|
items[i]['name'] = data[i].path.substring(data[i].path.lastIndexOf('/')+1);
|
|
|
|
items[i]['path'] = data[i].path;
|
|
|
|
items[i]['repo_id'] = repo_id;
|
|
|
|
items[i]['repo_name'] = this.props.repoName;
|
|
|
|
items[i]['is_dir'] = false;
|
|
|
|
items[i]['link_content'] = decodeURI(data[i].path).substring(1);
|
|
|
|
items[i]['content'] = data[i].sentence;
|
|
|
|
items[i]['thumbnail_url'] = '';
|
|
|
|
}
|
|
|
|
return items;
|
|
|
|
}
|
|
|
|
|
2018-08-17 04:23:55 +00:00
|
|
|
resetToDefault() {
|
2023-10-21 03:38:12 +00:00
|
|
|
this.inputValue = '';
|
2018-08-17 04:23:55 +00:00
|
|
|
this.setState({
|
|
|
|
width: '',
|
|
|
|
value: '',
|
|
|
|
isMaskShow: false,
|
|
|
|
isCloseShow: false,
|
|
|
|
isResultShow: false,
|
|
|
|
isResultGetted: false,
|
2019-02-13 10:09:01 +00:00
|
|
|
resultItems: [],
|
2023-10-11 07:58:06 +00:00
|
|
|
highlightIndex: 0,
|
2019-02-13 10:09:01 +00:00
|
|
|
isSearchInputShow: false,
|
2018-09-29 10:32:53 +00:00
|
|
|
});
|
2018-08-17 04:23:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
renderSearchResult() {
|
2023-10-21 03:38:12 +00:00
|
|
|
const { resultItems, highlightIndex, indexState, searchMode, width } = this.state;
|
|
|
|
if (!width) return null;
|
|
|
|
if (searchMode === SEARCH_MODE.SIMILARITY && indexState === INDEX_STATE.UNCREATED) {
|
|
|
|
return (
|
|
|
|
<div className="search-mode-similarity-index-status index-status-uncreated" onClick={this.onCreateIndex}>
|
|
|
|
{gettext('Click create index')}
|
|
|
|
</div>
|
|
|
|
);
|
2018-08-17 04:23:55 +00:00
|
|
|
}
|
2023-10-21 03:38:12 +00:00
|
|
|
|
|
|
|
if (searchMode === SEARCH_MODE.SIMILARITY && indexState === INDEX_STATE.RUNNING) {
|
|
|
|
return (
|
|
|
|
<div className="search-mode-similarity-index-status">
|
|
|
|
{gettext('Indexing...')}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this.state.isResultShow) return null;
|
2018-08-17 04:23:55 +00:00
|
|
|
if (!this.state.isResultGetted || this.getValueLength(this.inputValue) < 3) {
|
|
|
|
return (
|
|
|
|
<span className="loading-icon loading-tip"></span>
|
2018-09-29 10:32:53 +00:00
|
|
|
);
|
2018-08-17 04:23:55 +00:00
|
|
|
}
|
2023-10-08 09:20:38 +00:00
|
|
|
if (!resultItems.length) {
|
2018-08-17 04:23:55 +00:00
|
|
|
return (
|
2019-03-09 04:06:11 +00:00
|
|
|
<div className="search-result-none">{gettext('No results matching.')}</div>
|
2018-09-29 10:32:53 +00:00
|
|
|
);
|
2018-08-17 04:23:55 +00:00
|
|
|
}
|
2023-10-21 03:38:12 +00:00
|
|
|
|
|
|
|
const results = (
|
2023-10-08 09:20:38 +00:00
|
|
|
<ul className="search-result-list" ref={this.searchResultListRef}>
|
|
|
|
{resultItems.map((item, index) => {
|
2023-10-11 12:39:41 +00:00
|
|
|
const isHighlight = index === highlightIndex;
|
2018-08-17 04:23:55 +00:00
|
|
|
return (
|
|
|
|
<SearchResultItem
|
2019-12-31 08:58:45 +00:00
|
|
|
key={index}
|
2018-08-17 04:23:55 +00:00
|
|
|
item={item}
|
2023-10-08 09:20:38 +00:00
|
|
|
onItemClickHandler={this.onItemClickHandler}
|
2023-10-11 12:39:41 +00:00
|
|
|
isHighlight={isHighlight}
|
|
|
|
setRef={isHighlight ? (ref) => {this.highlightRef = ref;} : () => {}}
|
2018-08-17 04:23:55 +00:00
|
|
|
/>
|
2018-09-29 10:32:53 +00:00
|
|
|
);
|
2018-08-17 04:23:55 +00:00
|
|
|
})}
|
|
|
|
</ul>
|
2018-09-29 10:32:53 +00:00
|
|
|
);
|
2023-10-21 03:38:12 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<MediaQuery query="(min-width: 768px)">
|
|
|
|
<div className="search-result-list-container">{results}</div>
|
|
|
|
</MediaQuery>
|
|
|
|
<MediaQuery query="(max-width: 767.8px)">
|
|
|
|
{results}
|
|
|
|
</MediaQuery>
|
|
|
|
</>
|
|
|
|
);
|
2018-08-17 04:23:55 +00:00
|
|
|
}
|
|
|
|
|
2019-02-13 10:09:01 +00:00
|
|
|
onSearchToggle = () => {
|
|
|
|
this.setState({
|
|
|
|
isSearchInputShow: !this.state.isSearchInputShow,
|
|
|
|
isMaskShow: !this.state.isMaskShow,
|
|
|
|
});
|
2023-09-13 00:40:50 +00:00
|
|
|
};
|
2019-02-13 10:09:01 +00:00
|
|
|
|
2023-10-21 03:38:12 +00:00
|
|
|
onChangeSearchMode = (event) => {
|
|
|
|
const searchMode = event.target.getAttribute('mode-type');
|
|
|
|
if (searchMode === this.state.searchMode) return;
|
|
|
|
const { repoID } = this.props;
|
|
|
|
const { indexState: currentIndexState } = this.state;
|
|
|
|
this.timer && clearTimeout(this.timer);
|
|
|
|
this.setState({ searchMode }, () => {
|
|
|
|
if (searchMode === SEARCH_MODE.NORMAL) {
|
|
|
|
this.onSearch();
|
|
|
|
this.indexStateTimer && clearInterval(this.indexStateTimer);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (searchMode === SEARCH_MODE.SIMILARITY) {
|
|
|
|
if (currentIndexState === INDEX_STATE.FINISHED) {
|
2023-10-23 08:03:54 +00:00
|
|
|
this.onSearch(true);
|
2023-10-21 03:38:12 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
seafileAPI.queryLibraryIndexState(repoID).then(res => {
|
|
|
|
const { state: indexState, task_id: taskId } = res.data;
|
|
|
|
this.setState({ indexState }, () => {
|
|
|
|
if (indexState === INDEX_STATE.FINISHED) {
|
2023-10-23 08:03:54 +00:00
|
|
|
this.onSearch(true);
|
2023-10-21 03:38:12 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (indexState === INDEX_STATE.RUNNING) {
|
2023-10-23 08:03:54 +00:00
|
|
|
this.queryIndexTaskStatus(taskId, () => this.onSearch(true));
|
2023-10-21 03:38:12 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}).catch(error => {
|
|
|
|
this.setState({ indexState: INDEX_STATE.UNCREATED });
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
queryIndexTaskStatus = (taskId, callback) => {
|
|
|
|
if (!taskId) return;
|
|
|
|
this.indexStateTimer = setInterval(() => {
|
|
|
|
seafileAPI.queryIndexTaskStatus(taskId).then(res => {
|
|
|
|
const isFinished = res.data.is_finished;
|
|
|
|
if (isFinished) {
|
|
|
|
this.setState({ indexState: INDEX_STATE.FINISHED }, () => {
|
|
|
|
callback && callback();
|
|
|
|
});
|
|
|
|
this.indexStateTimer && clearInterval(this.indexStateTimer);
|
|
|
|
this.indexStateTimer = null;
|
|
|
|
}
|
|
|
|
}).catch(error => {
|
|
|
|
this.indexStateTimer && clearInterval(this.indexStateTimer);
|
|
|
|
this.indexStateTimer = null;
|
|
|
|
const errorMsg = Utils.getErrorMsg(error);
|
|
|
|
toaster.danger(errorMsg);
|
|
|
|
this.setState({ indexState: INDEX_STATE.UNCREATED });
|
|
|
|
});
|
|
|
|
}, 3000);
|
|
|
|
};
|
|
|
|
|
|
|
|
onCreateIndex = () => {
|
|
|
|
this.setState({ indexState: INDEX_STATE.RUNNING });
|
|
|
|
seafileAPI.createLibraryIndex(this.props.repoID).then(res => {
|
|
|
|
const taskId = res.data.task_id;
|
|
|
|
this.queryIndexTaskStatus(taskId);
|
|
|
|
}).catch(error => {
|
|
|
|
const errorMsg = Utils.getErrorMsg(error);
|
|
|
|
toaster.danger(errorMsg);
|
|
|
|
this.setState({ indexState: INDEX_STATE.UNCREATED });
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2018-08-06 10:29:12 +00:00
|
|
|
render() {
|
2018-08-17 04:23:55 +00:00
|
|
|
let width = this.state.width !== 'default' ? this.state.width : '';
|
|
|
|
let style = {'width': width};
|
2023-10-21 03:38:12 +00:00
|
|
|
const { searchPageUrl, isMaskShow, searchMode, indexState, isCloseShow } = this.state;
|
2023-10-09 09:32:13 +00:00
|
|
|
const placeholder = `${this.props.placeholder}${isMaskShow ? '' : ` (${controlKey} + f )`}`;
|
2018-08-06 10:29:12 +00:00
|
|
|
return (
|
2019-02-13 10:09:01 +00:00
|
|
|
<Fragment>
|
|
|
|
<MediaQuery query="(min-width: 768px)">
|
|
|
|
<div className="search">
|
2023-10-09 09:32:13 +00:00
|
|
|
<div className={`search-mask ${isMaskShow ? 'show' : 'hide'}`} onClick={this.onCloseHandler}></div>
|
|
|
|
<div className={`search-container ${isMaskShow ? 'show' : ''}`}>
|
|
|
|
<div className={`input-icon ${isMaskShow ? 'mb-1' : ''}`}>
|
2019-02-13 10:09:01 +00:00
|
|
|
<i className="search-icon-left input-icon-addon fas fa-search"></i>
|
2020-11-02 05:56:35 +00:00
|
|
|
<input
|
|
|
|
type="text"
|
|
|
|
className="form-control search-input"
|
2019-02-13 10:09:01 +00:00
|
|
|
name="query"
|
2023-10-09 09:32:13 +00:00
|
|
|
placeholder={placeholder}
|
2019-02-13 10:09:01 +00:00
|
|
|
style={style}
|
|
|
|
value={this.state.value}
|
|
|
|
onFocus={this.onFocusHandler}
|
|
|
|
onChange={this.onChangeHandler}
|
|
|
|
autoComplete="off"
|
2023-10-08 09:20:38 +00:00
|
|
|
ref={this.inputRef}
|
2023-10-21 03:38:12 +00:00
|
|
|
readOnly={isCloseShow && enableSeafileAI && SEARCH_MODE.SIMILARITY === searchMode && indexState !== INDEX_STATE.FINISHED}
|
2023-10-23 08:03:54 +00:00
|
|
|
onKeyDown={this.onKeydownHandler}
|
2019-02-13 10:09:01 +00:00
|
|
|
/>
|
2019-07-10 03:46:02 +00:00
|
|
|
{(this.state.isCloseShow && username) &&
|
2021-12-17 03:16:33 +00:00
|
|
|
<a href={searchPageUrl} className="search-icon-right input-icon-addon fas fa-external-link-alt search-icon-arrow"></a>
|
2021-09-17 07:13:22 +00:00
|
|
|
}
|
|
|
|
{this.state.isCloseShow &&
|
2023-10-08 09:20:38 +00:00
|
|
|
<button type="button" className="search-icon-right input-icon-addon fas fa-times border-0 bg-transparent mr-4" onClick={this.onCloseHandler} aria-label={gettext('Close')}></button>
|
2019-04-24 03:10:59 +00:00
|
|
|
}
|
2019-02-13 10:09:01 +00:00
|
|
|
</div>
|
2023-10-11 12:39:41 +00:00
|
|
|
<div
|
|
|
|
className="search-result-container dropdown-search-result-container"
|
|
|
|
onScroll={this.onResultListScroll}
|
|
|
|
ref={this.searchContainer}
|
|
|
|
>
|
2023-10-21 03:38:12 +00:00
|
|
|
{isCloseShow && enableSeafileAI && this.props.isLibView &&
|
|
|
|
<div className="search-mode-container">
|
|
|
|
<div className={`search-mode-item ${SEARCH_MODE.NORMAL === searchMode ? 'search-mode-active' : ''}`} mode-type={SEARCH_MODE.NORMAL} onClick={this.onChangeSearchMode}>{gettext('Normal search')}</div>
|
|
|
|
<div className={`search-mode-item ${SEARCH_MODE.SIMILARITY === searchMode ? 'search-mode-active' : ''}`} mode-type={SEARCH_MODE.SIMILARITY} onClick={this.onChangeSearchMode}>{gettext('Similarity search')}</div>
|
|
|
|
</div>
|
|
|
|
}
|
2019-02-13 10:09:01 +00:00
|
|
|
{this.renderSearchResult()}
|
|
|
|
</div>
|
|
|
|
</div>
|
2018-08-17 04:23:55 +00:00
|
|
|
</div>
|
2019-02-13 10:09:01 +00:00
|
|
|
</MediaQuery>
|
2019-07-10 13:51:30 +00:00
|
|
|
<MediaQuery query="(max-width: 767.8px)">
|
2019-02-13 10:09:01 +00:00
|
|
|
<div className="search-icon-container">
|
|
|
|
<i className="search-icon fas fa-search" onClick={this.onSearchToggle}></i>
|
2018-08-17 04:23:55 +00:00
|
|
|
</div>
|
2020-11-02 05:56:35 +00:00
|
|
|
{this.state.isSearchInputShow &&
|
2019-02-13 10:09:01 +00:00
|
|
|
<div className="search">
|
2023-10-09 09:32:13 +00:00
|
|
|
<div className={`search-mask ${isMaskShow ? '' : 'hide'}`} onClick={this.onCloseHandler}></div>
|
2019-02-13 10:09:01 +00:00
|
|
|
<div className="search-container">
|
|
|
|
<div className="input-icon">
|
|
|
|
<i className="search-icon-left input-icon-addon fas fa-search"></i>
|
2020-11-02 05:56:35 +00:00
|
|
|
<input
|
|
|
|
type="text"
|
|
|
|
className="form-control search-input"
|
2019-02-13 10:09:01 +00:00
|
|
|
name="query"
|
2023-10-09 09:32:13 +00:00
|
|
|
placeholder={placeholder}
|
2019-02-13 10:09:01 +00:00
|
|
|
style={style}
|
|
|
|
value={this.state.value}
|
|
|
|
onFocus={this.onFocusHandler}
|
|
|
|
onChange={this.onChangeHandler}
|
|
|
|
autoComplete="off"
|
|
|
|
/>
|
2019-07-10 03:46:02 +00:00
|
|
|
{(this.state.isCloseShow && username) &&
|
2021-12-17 03:16:33 +00:00
|
|
|
<a href={searchPageUrl} className="search-icon-right input-icon-addon fas fa-external-link-alt search-icon-arrow"></a>
|
2021-09-17 07:13:22 +00:00
|
|
|
}
|
|
|
|
{this.state.isCloseShow &&
|
|
|
|
<button type="button" className="search-icon-right input-icon-addon fas fa-times border-0 bg-transparent" onClick={this.onCloseHandler} aria-label={gettext('Close')}></button>
|
2019-04-24 03:10:59 +00:00
|
|
|
}
|
2019-02-13 10:09:01 +00:00
|
|
|
</div>
|
2023-10-08 09:20:38 +00:00
|
|
|
<div className="search-result-container dropdown-search-result-container" onScroll={this.onResultListScroll}>
|
2019-02-13 10:09:01 +00:00
|
|
|
{this.renderSearchResult()}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
}
|
|
|
|
</MediaQuery>
|
|
|
|
</Fragment>
|
2018-09-29 10:32:53 +00:00
|
|
|
);
|
2018-08-06 10:29:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-16 10:19:51 +00:00
|
|
|
Search.propTypes = propTypes;
|
|
|
|
|
2018-08-06 10:29:12 +00:00
|
|
|
export default Search;
|