import { HookEnableParam } from '../../../shared/hooks/types';
import { WidgetModel } from './widget-model';
/**
* Parameters for {@link useGetWidgetModel} hook.
*/
export interface GetWidgetModelParams extends HookEnableParam {
/**
* Identifier of the dashboard that contains the widget
*/
dashboardOid: string;
/**
* Identifier of the widget to be retrieved
*/
widgetOid: string;
}
/**
* States of a widget model load.
*/
export type WidgetModelState = WidgetModelLoadingState | WidgetModelErrorState | WidgetModelSuccessState;
/**
* State of a widget model that is loading.
*/
export type WidgetModelLoadingState = {
/** Whether the widget model is loading */
isLoading: true;
/** Whether the widget model load has failed */
isError: false;
/** Whether the widget model load has succeeded */
isSuccess: false;
/** Error, if one occurred */
error: undefined;
/** Widget model, if the load succeeded */
widget: WidgetModel | undefined;
/** Loading status */
status: 'loading';
};
/**
* State of a widget model load that has failed.
*/
export type WidgetModelErrorState = {
/** Whether the widget model is loading */
isLoading: false;
/** Whether the widget model load has failed */
isError: true;
/** Whether the widget model load has succeeded */
isSuccess: false;
/** Error, if one occurred */
error: Error;
/** Widget model, if the load succeeded */
widget: undefined;
/** Loading status */
status: 'error';
};
/**
* State of a widget model load that has succeeded.
*/
export type WidgetModelSuccessState = {
/** Whether the widget model is loading */
isLoading: false;
/** Whether the widget model load has failed */
isError: false;
/** Whether the widget model load has succeeded */
isSuccess: true;
/** Error, if one occurred */
error: undefined;
/** Widget model, if the load succeeded */
widget: WidgetModel;
/** Loading status */
status: 'success';
};
/**
* React hook that retrieves an existing widget model from a Fusion instance.
*
* **Note:** Widget extensions based on JS scripts and add-ons in Fusion are not supported.
*
* @example
* Retrieve a widget model and use it to populate a `ChartWidget` component.
*
* ```tsx
* import { ChartWidget, useGetWidgetModel, widgetModelTranslator } from '@sisense/sdk-ui';
*
* const CodeExample = () => {
* const { widget } = useGetWidgetModel({
* dashboardOid: '65a82171719e7f004018691c',
* widgetOid: '65a82171719e7f004018691f',
* });
*
* const widgetProps = widget ? widgetModelTranslator.toChartWidgetProps(widget) : null;
*
* return (
* <>
* {widgetProps && (
*
* )}
* >
* );
* };
*
* export default CodeExample;
* ```
*
*
*
* Retrieve a widget model and let the user switch its chart type at runtime:
*
* ```tsx
* import Button from '@mui/material/Button';
* import ButtonGroup from '@mui/material/ButtonGroup';
* import { Chart, ChartProps, ChartType, useGetWidgetModel, widgetModelTranslator } from '@sisense/sdk-ui';
* import { useState } from 'react';
*
* const CHART_TYPES: readonly ChartType[] = ['pie', 'line', 'area', 'bar', 'column', 'polar', 'funnel', 'treemap', 'sunburst'];
*
* const CodeExample = () => {
* const { widget } = useGetWidgetModel({
* dashboardOid: '65a82171719e7f004018691c',
* widgetOid: '65a82171719e7f004018691f',
* });
*
* const [chartProps, setChartProps] = useState();
*
* if (widget && !chartProps) setChartProps(widgetModelTranslator.toChartProps(widget));
*
* const changeChartType = (value: ChartType) => {
* if (value && chartProps) {
* setChartProps({
* ...chartProps,
* chartType: value,
* dataOptions: {
* // Fusion widget may not have all required data options.
* ...{ category: [], value: [], breakBy: [] },
* ...chartProps.dataOptions,
* },
* styleOptions: {
* ...chartProps.styleOptions,
* // Changing chart type may invalidate the chart subtype.
* subtype: undefined,
* },
* });
* }
* };
*
* return (
* <>
* {chartProps && (
* <>
*
* {CHART_TYPES.map((chartType) => (
*
* ))}
*
*
* >
* )}
* >
* );
* };
*
* export default CodeExample;
* ```
*
*
*
* @returns Widget load state that contains the status of the execution, the result widget model, or the error if one has occurred
* @group Fusion Assets
* @fusionEmbed
*/
export declare const useGetWidgetModel: (params: GetWidgetModelParams) => WidgetModelState;
/**
* {@link useGetWidgetModel} without tracking to be used inside other hooks or components in Compose SDK.
*
* @param params - Parameters of the widget to be retrieved
* @internal
*/
export declare function useGetWidgetModelInternal(params: GetWidgetModelParams): WidgetModelState;