From eada7ea6e2ea85af5fef813a4d4abf105debdc5f Mon Sep 17 00:00:00 2001 From: Michael An <2331806369@qq.com> Date: Thu, 11 Jul 2024 18:26:14 +0800 Subject: [PATCH] fix search when change repoID --- frontend/src/components/search/ai-search.js | 56 +++++++++++++------ frontend/src/components/search/search.js | 20 +++++-- .../src/components/toolbar/common-toolbar.js | 1 - 3 files changed, 52 insertions(+), 25 deletions(-) diff --git a/frontend/src/components/search/ai-search.js b/frontend/src/components/search/ai-search.js index 83c9778e1ed..30fc215adf4 100644 --- a/frontend/src/components/search/ai-search.js +++ b/frontend/src/components/search/ai-search.js @@ -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, @@ -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.calculateProperty(props); } componentDidMount() { @@ -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.calculateProperty(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 => { + calculateProperty = (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) { diff --git a/frontend/src/components/search/search.js b/frontend/src/components/search/search.js index c0218752020..40955eeccec 100644 --- a/frontend/src/components/search/search.js +++ b/frontend/src/components/search/search.js @@ -58,12 +58,6 @@ 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; } componentDidMount() { @@ -72,6 +66,10 @@ class Search extends Component { document.addEventListener('compositionend', this.onCompositionEnd); } + UNSAFE_componentWillReceiveProps(nextProps) { + this.calculateProperty(nextProps); + } + componentWillUnmount() { document.removeEventListener('keydown', this.onDocumentKeydown); document.removeEventListener('compositionstart', this.onCompositionStart); @@ -79,6 +77,16 @@ class Search extends Component { this.isChineseInput = false; } + calculateProperty = (props) => { + const { repoID } = props; + let storeKey = 'sfVisitedSearchItems'; + if (repoID) { + storeKey += repoID; + } + this.storeKey = storeKey; + this.isChineseInput = false; + }; + onCompositionStart = () => { this.isChineseInput = true; }; diff --git a/frontend/src/components/toolbar/common-toolbar.js b/frontend/src/components/toolbar/common-toolbar.js index 28532cfaaa9..e71d0b6611e 100644 --- a/frontend/src/components/toolbar/common-toolbar.js +++ b/frontend/src/components/toolbar/common-toolbar.js @@ -63,7 +63,6 @@ class CommonToolbar extends React.Component { isViewFile={isViewFile} placeholder={placeholder} onSearchedClick={this.props.onSearchedClick} - repoName={repoName} currentRepoInfo={this.props.currentRepoInfo} isLibView={isLibView} />