github.com/turbot/steampipe@v1.7.0-rc.0.0.20240517123944-7cef272d4458/ui/dashboard/src/components/dashboards/inputs/common/useSelectInputValues.ts (about)

     1  import { DashboardRunState } from "../../../../types";
     2  import { getColumn } from "../../../../utils/data";
     3  import { LeafNodeData } from "../../common";
     4  import { SelectInputOption, SelectOption } from "../types";
     5  import { useMemo } from "react";
     6  
     7  const useSelectInputValues = (
     8    options: SelectInputOption[] | undefined,
     9    data: LeafNodeData | undefined,
    10    status: DashboardRunState
    11  ) => {
    12    // Get the options for the select
    13    return useMemo<SelectOption[]>(() => {
    14      // If no options defined at all
    15      if (
    16        ((!options || options.length === 0) &&
    17          (!data || !data.columns || !data.rows)) ||
    18        // This property is only present in workspaces >=v0.16.x
    19        (status !== undefined && status !== "complete")
    20      ) {
    21        return [];
    22      }
    23  
    24      if (data) {
    25        const labelCol = getColumn(data.columns, "label");
    26        const valueCol = getColumn(data.columns, "value");
    27        const tagsCol = getColumn(data.columns, "tags");
    28  
    29        if (!labelCol || !valueCol) {
    30          return [];
    31        }
    32  
    33        return data.rows.map((row) => {
    34          const label = row[labelCol.name];
    35          const value = row[valueCol.name];
    36          return {
    37            label: !!label ? label.toString() : "",
    38            value: !!value ? value.toString() : null,
    39            tags: tagsCol ? row[tagsCol.name] : {},
    40          };
    41        });
    42      } else if (options) {
    43        return options.map((option) => ({
    44          label: option.label || option.name,
    45          value: option.name,
    46          tags: {},
    47        }));
    48      } else {
    49        return [];
    50      }
    51    }, [options, data, status]);
    52  };
    53  
    54  export default useSelectInputValues;