Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix DataSourceVariable doesn't use options from initial state #984

Closed
wants to merge 2 commits into from

Conversation

svennergr
Copy link
Contributor

@svennergr svennergr commented Dec 2, 2024

Since DataSourceVariable is a MultiSelectVariable the initial state can contain options like this:

  const dataSourceList = getDataSourceSrv()
    .getList({
      filter: (datasource) => {
        if (
          [
            'mysql',
          ].includes(datasource.type)
        ) {
          return true;
        }
        return false;
      },
    })
    .map((ds) => {
      return { label: ds.name, value: ds.uid };
    });
  const dsVar = new DataSourceVariable({ label: 'Data Source', options: dataSourceList });

However, these options are not valued in its implementation of getValueOptions.

@svennergr svennergr added the type/bug Something isn't working label Dec 2, 2024
Copy link

@cyriltovena cyriltovena left a comment

Choose a reason for hiding this comment

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

LGTM

@svennergr svennergr requested review from torkelo and dprokop December 2, 2024 15:26
Comment on lines +48 to +51
if (this.state.options?.length) {
return of(this.state.options);
}

Copy link
Member

Choose a reason for hiding this comment

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

not sure about this, this will make it never update the options list (based on the pluginId), making it just a statically defined variable (might as well use CustomVariable) ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In my opinion we should then at least remove options from initialState in

public constructor(initialState: Partial<DataSourceVariableState>) {

I needed a way to use multiple pluginId and thought options might be a good property, but it wasn't applied in the constructor. Instead I'm just setting the DataSourceVariable state later than the constructor now.

Copy link
Member

Choose a reason for hiding this comment

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

but it wasn't applied in the constructor.

what do you mean?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The example like this, where you pass options in the DataSourceVariable constructor does not work right now:

  const dataSourceList = getDataSourceSrv()
    .getList({
      filter: (datasource) => {
        if (
          [
            'mysql',
          ].includes(datasource.type)
        ) {
          return true;
        }
        return false;
      },
    })
    .map((ds) => {
      return { label: ds.name, value: ds.uid };
    });
  const dsVar = new DataSourceVariable({ label: 'Data Source', options: dataSourceList });

What I needed to do was to deliberately call dsVar.setState({options: dataSourceList}); after I created the var.

Copy link
Member

@torkelo torkelo Dec 5, 2024

Choose a reason for hiding this comment

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

@svennergr confused,

 super({
      type: 'datasource',
      value: '',
      text: '',
      options: [],
      name: '',
      regex: '',
      pluginId: '',
      ...initialState,
    });

looks like options passed in via initialState is set, of course it will be cleared when variable activates (or the set activates). But that is it's correct behavior, options is not to be set externally really it's something it looks up using the pluginId

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yea, if it's the foreseen behavior it makes sense. From just looking at the types when using the variable as a "consumer"/developer, you see options and think it could be used.

What do you think about omitting that type in initalState like constructor(initialState: Omit<Partial<DataSourceVariableState>, 'options'>);

@svennergr svennergr closed this Dec 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type/bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants