{"version":3,"file":"DatasetSelector.mjs","sources":["../../../src/components/DatasetSelector.tsx"],"sourcesContent":["import { useAsync } from 'react-use';\n\nimport { type SelectableValue } from '@grafana/data';\nimport { t } from '@grafana/i18n';\nimport { Select } from '@grafana/ui';\n\nimport { type DB, type ResourceSelectorProps, type SQLDialect, toOption } from '../types';\n\nexport interface DatasetSelectorProps extends ResourceSelectorProps {\n  db: DB;\n  dataset: string | undefined;\n  preconfiguredDataset: string;\n  dialect: SQLDialect;\n  onChange: (v: SelectableValue) => void;\n  inputId?: string | undefined;\n}\n\nexport const DatasetSelector = ({\n  dataset,\n  db,\n  dialect,\n  onChange,\n  inputId,\n  preconfiguredDataset,\n}: DatasetSelectorProps) => {\n  /*\n    The behavior of this component - for MSSQL and MySQL datasources - is based on whether the user chose to create a datasource\n    with or without a default database (preconfiguredDataset). If the user configured a default database, this selector\n    should only allow that single preconfigured database option to be selected. If the user chose to NOT assign/configure a default database,\n    then the user should be able to use this component to choose between multiple databases available to the datasource.\n  */\n  // `hasPreconfigCondition` is true if either 1) the sql datasource has a preconfigured default database,\n  // OR if 2) the datasource is Postgres. In either case the only option available to the user is the preconfigured database.\n  const hasPreconfigCondition = !!preconfiguredDataset || dialect === 'postgres';\n\n  const state = useAsync(async () => {\n    // If a default database is already configured for a MSSQL or MySQL data source, OR the data source is Postgres, no need to fetch other databases.\n    if (hasPreconfigCondition) {\n      // Set the current database to the preconfigured database.\n      onChange(toOption(preconfiguredDataset));\n      return [toOption(preconfiguredDataset)];\n    }\n\n    // If there is no preconfigured database, but there is a selected dataset, set the current database to the selected dataset.\n    if (dataset) {\n      onChange(toOption(dataset));\n    }\n\n    // Otherwise, fetch all databases available to the datasource.\n    const datasets = await db.datasets();\n    return datasets.map(toOption);\n  }, []);\n\n  return (\n    <Select\n      aria-label={t('grafana-sql.components.dataset-selector.aria-label-dataset-selector', 'Dataset selector')}\n      inputId={inputId}\n      value={dataset}\n      options={state.value}\n      onChange={onChange}\n      disabled={state.loading}\n      isLoading={state.loading}\n      menuShouldPortal={true}\n    />\n  );\n};\n"],"names":[],"mappings":";;;;;;;AAiBO,MAAM,kBAAkB,CAAC;AAAA,EAC9B,OAAA;AAAA,EACA,EAAA;AAAA,EACA,OAAA;AAAA,EACA,QAAA;AAAA,EACA,OAAA;AAAA,EACA;AACF,CAAA,KAA4B;AAS1B,EAAA,MAAM,qBAAA,GAAwB,CAAC,CAAC,oBAAA,IAAwB,OAAA,KAAY,UAAA;AAEpE,EAAA,MAAM,KAAA,GAAQ,SAAS,YAAY;AAEjC,IAAA,IAAI,qBAAA,EAAuB;AAEzB,MAAA,QAAA,CAAS,QAAA,CAAS,oBAAoB,CAAC,CAAA;AACvC,MAAA,OAAO,CAAC,QAAA,CAAS,oBAAoB,CAAC,CAAA;AAAA,IACxC;AAGA,IAAA,IAAI,OAAA,EAAS;AACX,MAAA,QAAA,CAAS,QAAA,CAAS,OAAO,CAAC,CAAA;AAAA,IAC5B;AAGA,IAAA,MAAM,QAAA,GAAW,MAAM,EAAA,CAAG,QAAA,EAAS;AACnC,IAAA,OAAO,QAAA,CAAS,IAAI,QAAQ,CAAA;AAAA,EAC9B,CAAA,EAAG,EAAE,CAAA;AAEL,EAAA,uBACE,GAAA;AAAA,IAAC,MAAA;AAAA,IAAA;AAAA,MACC,YAAA,EAAY,CAAA,CAAE,qEAAA,EAAuE,kBAAkB,CAAA;AAAA,MACvG,OAAA;AAAA,MACA,KAAA,EAAO,OAAA;AAAA,MACP,SAAS,KAAA,CAAM,KAAA;AAAA,MACf,QAAA;AAAA,MACA,UAAU,KAAA,CAAM,OAAA;AAAA,MAChB,WAAW,KAAA,CAAM,OAAA;AAAA,MACjB,gBAAA,EAAkB;AAAA;AAAA,GACpB;AAEJ;;;;"}