2022-10-11 14:24:02 +02:00
|
|
|
import React, { useState } from 'react';
|
2022-09-20 18:57:38 +02:00
|
|
|
|
|
|
|
|
import { locationService } from '@grafana/runtime';
|
2022-10-11 14:24:02 +02:00
|
|
|
import { FilterInput, ToolbarButton, useTheme2 } from '@grafana/ui';
|
|
|
|
|
import { useMediaQueryChange } from 'app/core/hooks/useMediaQueryChange';
|
2022-10-11 11:45:58 +01:00
|
|
|
import { t } from 'app/core/internationalization';
|
2022-11-03 08:29:39 +01:00
|
|
|
import { getSearchStateManager } from 'app/features/search/state/SearchStateManager';
|
2022-09-20 18:57:38 +02:00
|
|
|
|
|
|
|
|
export function TopSearchBarInput() {
|
2022-10-11 14:24:02 +02:00
|
|
|
const theme = useTheme2();
|
2022-11-03 08:29:39 +01:00
|
|
|
const stateManager = getSearchStateManager();
|
|
|
|
|
const state = stateManager.useState();
|
2022-10-11 14:24:02 +02:00
|
|
|
const breakpoint = theme.breakpoints.values.sm;
|
|
|
|
|
|
|
|
|
|
const [isSmallScreen, setIsSmallScreen] = useState(window.matchMedia(`(max-width: ${breakpoint}px)`).matches);
|
|
|
|
|
|
|
|
|
|
useMediaQueryChange({
|
|
|
|
|
breakpoint,
|
|
|
|
|
onChange: (e) => {
|
|
|
|
|
setIsSmallScreen(e.matches);
|
|
|
|
|
},
|
|
|
|
|
});
|
2022-09-20 18:57:38 +02:00
|
|
|
|
|
|
|
|
const onOpenSearch = () => {
|
|
|
|
|
locationService.partial({ search: 'open' });
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const onSearchChange = (value: string) => {
|
2022-11-03 08:29:39 +01:00
|
|
|
stateManager.onQueryChange(value);
|
2022-09-20 18:57:38 +02:00
|
|
|
if (value) {
|
|
|
|
|
onOpenSearch();
|
|
|
|
|
}
|
|
|
|
|
};
|
2022-10-11 14:24:02 +02:00
|
|
|
|
|
|
|
|
if (isSmallScreen) {
|
|
|
|
|
return <ToolbarButton iconOnly icon="search" aria-label="Search Grafana" onClick={onOpenSearch} />;
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-20 18:57:38 +02:00
|
|
|
return (
|
|
|
|
|
<FilterInput
|
|
|
|
|
onClick={onOpenSearch}
|
2022-10-11 11:45:58 +01:00
|
|
|
placeholder={t('nav.search.placeholder', 'Search Grafana')}
|
2022-11-03 08:29:39 +01:00
|
|
|
value={state.query ?? ''}
|
2022-09-20 18:57:38 +02:00
|
|
|
onChange={onSearchChange}
|
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
}
|