Skip to content

Commit

Permalink
fix search when change repoID (#6338)
Browse files Browse the repository at this point in the history
* fix search when change repoID

* change function name
  • Loading branch information
Michael18811380328 committed Jul 11, 2024
1 parent 1963e75 commit 10dfe2e
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 29 deletions.
56 changes: 38 additions & 18 deletions frontend/src/components/search/ai-search.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ export default class AISearch extends Component {
path: PropTypes.string,
placeholder: PropTypes.string,
onSearchedClick: PropTypes.func.isRequired,
repoName: PropTypes.string,
currentRepoInfo: PropTypes.object,
isViewFile: PropTypes.bool,
isLibView: PropTypes.bool,
Expand Down Expand Up @@ -64,22 +63,10 @@ export default class AISearch extends Component {
this.inputRef = React.createRef();
this.searchContainer = React.createRef();
this.searchResultListRef = React.createRef();
this.searchResultListContainerRef = React.createRef();
this.indexStateTimer = null;
this.isChineseInput = false;
if (props.isLibView && props.currentRepoInfo) {
this.isRepoOwner = props.currentRepoInfo.owner_email === username;
this.isAdmin = props.currentRepoInfo.is_admin;
} else {
this.isRepoOwner = false;
this.isAdmin = false;
}
this.searchResultListContainerRef = React.createRef();
const { repoID } = props;
let storeKey = 'sfVisitedAISearchItems';
if (repoID) {
storeKey += repoID;
}
this.storeKey = storeKey;
this.calculateStoreKey(props);
}

componentDidMount() {
Expand All @@ -88,12 +75,45 @@ export default class AISearch extends Component {
document.addEventListener('compositionend', this.onCompositionEnd);
document.addEventListener('click', this.handleOutsideClick);
if (this.props.isLibView) {
this.queryLibraryIndexState();
this.queryLibraryIndexState(this.props.repoID);
}
}

UNSAFE_componentWillReceiveProps(nextProps) {
this.calculateStoreKey(nextProps);
if (nextProps.isLibView) {
if (this.props.repoID !== nextProps.repoID) {
this.queryLibraryIndexState(nextProps.repoID);
}
} else {
if (this.indexStateTimer) {
clearInterval(this.indexStateTimer);
this.indexStateTimer = null;
}
this.isChineseInput = false;
this.setState({
indexState: '',
});
}
}

queryLibraryIndexState() {
seafileAPI.queryLibraryIndexState(this.props.repoID).then(res => {
calculateStoreKey = (props) => {
if (props.isLibView && props.currentRepoInfo) {
this.isRepoOwner = props.currentRepoInfo.owner_email === username;
this.isAdmin = props.currentRepoInfo.is_admin;
} else {
this.isRepoOwner = false;
this.isAdmin = false;
}
let storeKey = 'sfVisitedAISearchItems';
if (props.repoID) {
storeKey += props.repoID;
}
this.storeKey = storeKey;
};

queryLibraryIndexState(repoID) {
seafileAPI.queryLibraryIndexState(repoID).then(res => {
const { state: indexState, task_id: taskId } = res.data;
this.setState({ indexState }, () => {
if (indexState === INDEX_STATE.RUNNING) {
Expand Down
21 changes: 15 additions & 6 deletions frontend/src/components/search/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,7 @@ class Search extends Component {
this.searchResultListRef = React.createRef();
this.isChineseInput = false;
this.searchResultListContainerRef = React.createRef();
const { repoID } = props;
let storeKey = 'sfVisitedSearchItems';
if (repoID) {
storeKey += repoID;
}
this.storeKey = storeKey;
this.calculateStoreKey(props);
}

componentDidMount() {
Expand All @@ -72,13 +67,27 @@ class Search extends Component {
document.addEventListener('compositionend', this.onCompositionEnd);
}

UNSAFE_componentWillReceiveProps(nextProps) {
this.calculateStoreKey(nextProps);
this.isChineseInput = false;
}

componentWillUnmount() {
document.removeEventListener('keydown', this.onDocumentKeydown);
document.removeEventListener('compositionstart', this.onCompositionStart);
document.removeEventListener('compositionend', this.onCompositionEnd);
this.isChineseInput = false;
}

calculateStoreKey = (props) => {
const { repoID } = props;
let storeKey = 'sfVisitedSearchItems';
if (repoID) {
storeKey += repoID;
}
this.storeKey = storeKey;
};

onCompositionStart = () => {
this.isChineseInput = true;
};
Expand Down
10 changes: 5 additions & 5 deletions frontend/src/components/toolbar/common-toolbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class CommonToolbar extends React.Component {
isLibView: props.isLibView,
path: props.path,
isViewFile: props.isViewFile,
currentRepoInfo: props.currentRepoInfo,
};
}

Expand All @@ -45,12 +46,12 @@ class CommonToolbar extends React.Component {
this.unsubscribeLibChange && this.unsubscribeLibChange();
}

onRepoChange = ({ repoID, repoName, isLibView, path, isViewFile }) => {
this.setState({ repoID, repoName, isLibView, path, isViewFile });
onRepoChange = ({ repoID, repoName, isLibView, path, isViewFile, currentRepoInfo }) => {
this.setState({ repoID, repoName, isLibView, path, isViewFile, currentRepoInfo });
};

renderSearch = () => {
const { repoID, repoName, isLibView, path, isViewFile } = this.state;
const { repoID, repoName, isLibView, path, isViewFile, currentRepoInfo } = this.state;
const { searchPlaceholder } = this.props;
const placeholder = searchPlaceholder || gettext('Search files');

Expand All @@ -62,9 +63,8 @@ class CommonToolbar extends React.Component {
path={path}
isViewFile={isViewFile}
placeholder={placeholder}
currentRepoInfo={currentRepoInfo}
onSearchedClick={this.props.onSearchedClick}
repoName={repoName}
currentRepoInfo={this.props.currentRepoInfo}
isLibView={isLibView}
/>
);
Expand Down
2 changes: 2 additions & 0 deletions frontend/src/pages/lib-content-view/lib-content-view.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ class LibContentView extends React.Component {
path: '',
isViewFile: false,
isLibView: false,
currentRepoInfo: null,
});
}

Expand All @@ -215,6 +216,7 @@ class LibContentView extends React.Component {
this.props.eventBus.dispatch(EVENT_BUS_TYPE.CURRENT_LIBRARY_CHANGED, {
repoID: this.props.repoID,
repoName: this.state.repoName,
currentRepoInfo: this.state.currentRepoInfo,
path: this.state.path,
isViewFile: this.state.isViewFile,
isLibView: true,
Expand Down

0 comments on commit 10dfe2e

Please sign in to comment.