Loki Context UI: Do not disable last label (#62866)

* improved text

* prevent all labels removed

* use `structuredClone` instead of lodash

* update test

* get `structuredClone` in test?

* fix using structuredClone
This commit is contained in:
Sven Grossmann 2023-02-10 14:23:17 +01:00 committed by GitHub
parent f0333ac41f
commit 00edc1c6b1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 2 deletions

View File

@ -5998,6 +5998,11 @@ exports[`better eslint`] = {
"public/app/plugins/datasource/loki/LiveStreams.ts:5381": [
[0, 0, 0, "Unexpected any. Specify a different type.", "0"]
],
"public/app/plugins/datasource/loki/components/LokiContextUi.test.tsx:5381": [
[0, 0, 0, "Unexpected any. Specify a different type.", "0"],
[0, 0, 0, "Unexpected any. Specify a different type.", "1"],
[0, 0, 0, "Unexpected any. Specify a different type.", "2"]
],
"public/app/plugins/datasource/loki/components/LokiLabelBrowser.tsx:5381": [
[0, 0, 0, "Do not use any type assertions.", "0"],
[0, 0, 0, "Do not use any type assertions.", "1"]

View File

@ -15,6 +15,20 @@ jest.mock('@grafana/runtime', () => ({
}));
describe('LokiContextUi', () => {
const savedGlobal = global;
beforeAll(() => {
// TODO: `structuredClone` is not yet in jsdom https://github.com/jsdom/jsdom/issues/3363
if (!(global as any).structuredClone) {
(global as any).structuredClone = function structuredClone(objectToClone: any) {
const stringified = JSON.stringify(objectToClone);
const parsed = JSON.parse(stringified);
return parsed;
};
}
});
afterAll(() => {
global = savedGlobal;
});
const setupProps = (): LokiContextUiProps => {
const mockLanguageProvider = {
start: jest.fn().mockImplementation(() => Promise.resolve()),
@ -66,7 +80,7 @@ describe('LokiContextUi', () => {
render(<LokiContextUi {...props} />);
// Initial set of labels is available and not selected
expect(await screen.findByText(/Select labels to include in the context query/)).toBeInTheDocument();
expect(await screen.findByText(/Select labels to be included in the context query/)).toBeInTheDocument();
});
it('starts the languageProvider', async () => {

View File

@ -49,9 +49,11 @@ export function LokiContextUi(props: LokiContextUiProps) {
const styles = useStyles2(getStyles);
const [contextFilters, setContextFilters] = useState<ContextFilter[]>([]);
const [initialized, setInitialized] = useState(false);
const timerHandle = React.useRef<number>();
const previousInitialized = React.useRef<boolean>(false);
const previousContextFilters = React.useRef<ContextFilter[]>([]);
useEffect(() => {
if (!initialized) {
return;
@ -63,6 +65,13 @@ export function LokiContextUi(props: LokiContextUiProps) {
return;
}
if (contextFilters.filter(({ enabled, fromParser }) => enabled && !fromParser).length === 0) {
setContextFilters(previousContextFilters.current);
return;
}
previousContextFilters.current = structuredClone(contextFilters);
if (timerHandle.current) {
clearTimeout(timerHandle.current);
}
@ -143,7 +152,7 @@ export function LokiContextUi(props: LokiContextUiProps) {
colorIndex={1}
/>
</Tooltip>{' '}
Select labels to include in the context query:
Select labels to be included in the context query:
</div>
<div>
<MultiSelect