Skip to content

Commit

Permalink
feat(ServiceNameVariable): Persist last service selected in localStor…
Browse files Browse the repository at this point in the history
…age (#222)
  • Loading branch information
grafakus authored Oct 17, 2024
1 parent a6d0064 commit 3917660
Showing 1 changed file with 11 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
import { Cascader, Icon, Tooltip, useStyles2 } from '@grafana/ui';
import { prepareHistoryEntry } from '@shared/domain/prepareHistoryEntry';
import { reportInteraction } from '@shared/domain/reportInteraction';
import { userStorage } from '@shared/infrastructure/userStorage';
import React, { useMemo } from 'react';
import { lastValueFrom } from 'rxjs';

Expand All @@ -30,6 +31,8 @@ export class ServiceNameVariable extends QueryVariable {
static QUERY_PROFILE_METRIC_DEPENDENT = '$dataSource and only $profileMetricId services';

constructor(state?: ServiceNameVariableState) {
const { serviceName: serviceNameFromStorage } = userStorage.get(userStorage.KEYS.PROFILES_EXPLORER) || {};

super({
key: 'serviceName',
name: 'serviceName',
Expand All @@ -38,16 +41,21 @@ export class ServiceNameVariable extends QueryVariable {
query: ServiceNameVariable.QUERY_DEFAULT,
loading: true,
refresh: VariableRefresh.onTimeRangeChanged,
value: serviceNameFromStorage,
...state,
});

this.addActivationHandler(this.onActivate.bind(this));
}

onActivate() {
if (!this.state.value && this.state.options.length) {
this.setState({ value: this.state.options[0].value });
}
this.subscribeToState((newState, prevState) => {
if (newState.value && newState.value !== prevState.value) {
const storage = userStorage.get(userStorage.KEYS.PROFILES_EXPLORER) || {};
storage.serviceName = newState.value;
userStorage.set(userStorage.KEYS.PROFILES_EXPLORER, storage);
}
});
}

async update() {
Expand Down

1 comment on commit 3917660

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unit test coverage

Lines Statements Branches Functions
Coverage: 11%
11.5% (428/3720) 9.05% (120/1325) 8.6% (102/1185)

Please sign in to comment.