mirror of https://github.com/grafana/grafana
Search: Toggle Search based on search query (#23648)
* Search: Toggle Search based on search query * Search: Fix types and closed search param * Search: Remove appEvents from SearchWrapper * Search: Reset folder on close Co-Authored-By: Alexander Zobnin <alexanderzobnin@gmail.com> * Search: Disable reloadOnSearch for manage dashboards urls Co-authored-by: Alexander Zobnin <alexanderzobnin@gmail.com>pull/23675/head
parent
55c306eb6d
commit
8709c9a8a5
@ -1,34 +1,49 @@ |
|||||||
import React, { FC, useState, useEffect } from 'react'; |
import React, { FC, memo } from 'react'; |
||||||
import { appEvents } from 'app/core/core'; |
import { MapDispatchToProps, MapStateToProps } from 'react-redux'; |
||||||
import { CoreEvents } from 'app/types'; |
import { getLocationQuery } from 'app/core/selectors/location'; |
||||||
|
import { updateLocation } from 'app/core/reducers/location'; |
||||||
|
import { connectWithStore } from 'app/core/utils/connectWithReduxStore'; |
||||||
|
import { StoreState } from 'app/types'; |
||||||
import { DashboardSearch } from './DashboardSearch'; |
import { DashboardSearch } from './DashboardSearch'; |
||||||
import { OpenSearchParams } from '../types'; |
|
||||||
|
interface OwnProps { |
||||||
export const SearchWrapper: FC = () => { |
search?: string | null; |
||||||
const [isOpen, setIsOpen] = useState(false); |
folder?: string; |
||||||
const [payload, setPayload] = useState({}); |
queryText?: string; |
||||||
|
filter?: string; |
||||||
useEffect(() => { |
} |
||||||
const openSearch = (payload: OpenSearchParams) => { |
|
||||||
setIsOpen(true); |
interface DispatchProps { |
||||||
setPayload(payload); |
updateLocation: typeof updateLocation; |
||||||
}; |
} |
||||||
|
|
||||||
const closeOnItemClick = (payload: any) => { |
export type Props = OwnProps & DispatchProps; |
||||||
// Detect if the event was emitted by clicking on search item
|
|
||||||
if (payload?.target === 'search-item' && isOpen) { |
export const SearchWrapper: FC<Props> = memo(({ search, folder, updateLocation }) => { |
||||||
setIsOpen(false); |
const isOpen = search === 'open'; |
||||||
} |
|
||||||
}; |
const closeSearch = () => { |
||||||
|
if (search === 'open') { |
||||||
appEvents.on(CoreEvents.showDashSearch, openSearch); |
updateLocation({ |
||||||
appEvents.on(CoreEvents.hideDashSearch, closeOnItemClick); |
query: { |
||||||
|
search: null, |
||||||
return () => { |
folder: null, |
||||||
appEvents.off(CoreEvents.showDashSearch, openSearch); |
}, |
||||||
appEvents.off(CoreEvents.hideDashSearch, closeOnItemClick); |
partial: true, |
||||||
}; |
}); |
||||||
}, [isOpen]); |
} |
||||||
|
}; |
||||||
return isOpen ? <DashboardSearch onCloseSearch={() => setIsOpen(false)} payload={payload} /> : null; |
|
||||||
|
return isOpen ? <DashboardSearch onCloseSearch={closeSearch} folder={folder} /> : null; |
||||||
|
}); |
||||||
|
|
||||||
|
const mapStateToProps: MapStateToProps<{}, OwnProps, StoreState> = (state: StoreState) => { |
||||||
|
const { search, folder } = getLocationQuery(state.location); |
||||||
|
return { search, folder }; |
||||||
}; |
}; |
||||||
|
|
||||||
|
const mapDispatchToProps: MapDispatchToProps<DispatchProps, OwnProps> = { |
||||||
|
updateLocation, |
||||||
|
}; |
||||||
|
|
||||||
|
export default connectWithStore(SearchWrapper, mapStateToProps, mapDispatchToProps); |
||||||
|
|||||||
Loading…
Reference in new issue