The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
Replace usages of deprecated useSafeAsyncFunction (#65406)
Frasera2a31bd9e3b3
diff --git a/.eslintrc.js b/.eslintrc.jsindex 4781fc35500..1c365c2c110 100644--- a/.eslintrc.js+++ b/.eslintrc.js@@ -94,10 +94,7 @@ module.exports = {"react/jsx-no-target-blank": 2,"react/jsx-key": 2,"react/forbid-component-props": [2, { forbid: ["sx"] }],- "react-hooks/exhaustive-deps": [- "warn",- { additionalHooks: "(useSafeAsyncFunction)" },- ],+ "react-hooks/exhaustive-deps": ["warn"],"prefer-const": [1, { destructuring: "all" }],"no-restricted-globals": ["error", "close"],"no-useless-escape": 0,diff --git a/frontend/src/metabase/common/hooks/use-safe-async-function.ts b/frontend/src/metabase/common/hooks/use-safe-async-function.tsdeleted file mode 100644index 8b0f3ad0461..00000000000--- a/frontend/src/metabase/common/hooks/use-safe-async-function.ts+++ /dev/null@@ -1,33 +0,0 @@-import { useCallback } from "react";-import { useMountedState } from "react-use";--type AsyncFn = (...args: any[]) => Promise<any>;--/**- * wraps the given async function in a promise that does not resolve after the component has unmounted- * @deprecated — use `useAsyncFn` from react-use instead- */-export function useSafeAsyncFunction(fn: AsyncFn, deps?: any[]): AsyncFn {- const isMounted = useMountedState();-- const safeFn: AsyncFn = useCallback(- (...args: any[]) =>- new Promise((resolve, reject) => {- return fn(...args)- .then((res: any) => {- if (isMounted()) {- resolve(res);- }- })- .catch((err: Error) => {- if (isMounted()) {- reject(err);- }- });- }),- // eslint-disable-next-line react-hooks/exhaustive-deps- deps ? [...deps, isMounted] : [isMounted],- );-- return safeFn;-}diff --git a/frontend/src/metabase/parameters/components/ValuesSourceModal/ValuesSourceTypeModal.tsx b/frontend/src/metabase/parameters/components/ValuesSourceModal/ValuesSourceTypeModal.tsxindex 52891c864b7..16c8f724cdf 100644--- a/frontend/src/metabase/parameters/components/ValuesSourceModal/ValuesSourceTypeModal.tsx+++ b/frontend/src/metabase/parameters/components/ValuesSourceModal/ValuesSourceTypeModal.tsx@@ -1,5 +1,6 @@import type { ChangeEvent } from "react";-import { useCallback, useLayoutEffect, useMemo, useState } from "react";+import { useCallback, useLayoutEffect, useMemo } from "react";+import { useAsyncFn } from "react-use";import { jt, t } from "ttag";import _ from "underscore";@@ -11,7 +12,6 @@ import Radio from "metabase/common/components/Radio";import type { SelectChangeEvent } from "metabase/common/components/Select";import Select, { Option } from "metabase/common/components/Select";import SelectButton from "metabase/common/components/SelectButton";-import { useSafeAsyncFunction } from "metabase/common/hooks/use-safe-async-function";import Questions from "metabase/entities/questions";import Tables from "metabase/entities/tables";import { connect, useSelector } from "metabase/lib/redux";@@ -509,12 +509,6 @@ const getSourceTypeOptions = (];};-interface ParameterValuesState {- values: ParameterValue[];- isLoading?: boolean;- isError?: boolean;-}-interface UseParameterValuesOpts {parameter: Parameter;sourceType: ValuesSourceType;@@ -530,8 +524,6 @@ const useParameterValues = ({sourceConfig,onFetchParameterValues,}: UseParameterValuesOpts) => {- const [state, setState] = useState<ParameterValuesState>({ values: [] });- const handleFetchValues = useSafeAsyncFunction(onFetchParameterValues);const isValidSource = isValidSourceConfig(sourceType, sourceConfig);const parameter = useMemo(@@ -543,17 +535,22 @@ const useParameterValues = ({[initialParameter, sourceType, sourceConfig],);+ const [{ loading, error, value }, handleFetchValues] = useAsyncFn(+ () => onFetchParameterValues({ parameter }),+ [onFetchParameterValues, parameter],+ );+useLayoutEffect(() => {if (isValidSource) {- setState(({ values }) => ({ values, isLoading: true }));-- handleFetchValues({ parameter })- .then(({ values }) => setState({ values }))- .catch(() => setState({ values: [], isError: true }));+ handleFetchValues();}- }, [parameter, isValidSource, handleFetchValues]);+ }, [isValidSource, handleFetchValues]);- return state;+ return {+ values: value?.values ?? [],+ isLoading: loading,+ isError: !!error,+ };};const mapDispatchToProps = {diff --git a/frontend/src/metabase/public/containers/PublicAction/PublicActionLoader.tsx b/frontend/src/metabase/public/containers/PublicAction/PublicActionLoader.tsxindex 1c153628e44..f4972c0d163 100644--- a/frontend/src/metabase/public/containers/PublicAction/PublicActionLoader.tsx+++ b/frontend/src/metabase/public/containers/PublicAction/PublicActionLoader.tsx@@ -1,13 +1,10 @@-import { useCallback, useState } from "react";-import { useMount } from "react-use";+import { useCallback, useEffect } from "react";+import { useAsyncFn, useMount } from "react-use";-import { useSafeAsyncFunction } from "metabase/common/hooks/use-safe-async-function";import { connect } from "metabase/lib/redux";import { SyncedEmbedFrame } from "metabase/public/components/EmbedFrame";import { setErrorPage } from "metabase/redux/app";import { PublicApi } from "metabase/services";-import type { WritebackAction } from "metabase-types/api";-import type { AppErrorDescriptor } from "metabase-types/store";import PublicAction from "./PublicAction";import {@@ -20,7 +17,7 @@ interface OwnProps {}interface DispatchProps {- setErrorPage: (error: AppErrorDescriptor) => void;+ setErrorPage: (error: any) => void;}type Props = OwnProps & DispatchProps;@@ -30,21 +27,21 @@ const mapDispatchToProps = {};function PublicActionLoader({ params, setErrorPage }: Props) {- const [action, setAction] = useState<WritebackAction | null>(null);- const fetchAction = useSafeAsyncFunction(PublicApi.action);+ const [{ value: action, error }, fetchAction] = useAsyncFn(+ () => PublicApi.action({ uuid: params.uuid }),+ [params.uuid],+ );useMount(() => {- async function loadAction() {- try {- const action = await fetchAction({ uuid: params.uuid });- setAction(action);- } catch (error) {- setErrorPage(error as AppErrorDescriptor);- }- }- loadAction();+ fetchAction();});+ useEffect(() => {+ if (error) {+ setErrorPage(error);+ }+ }, [error, setErrorPage]);+const renderContent = useCallback(() => {if (!action) {return null;