import { AsyncResult } from "@/hooks"; import React from "react"; import { CircularProgress } from "@material-ui/core"; import { Alert } from "@material-ui/lab"; import { Loading } from "@/components/loading"; type AsyncProps = { async: AsyncResult, children: (value: TValue) => JSX.Element, loading?: () => JSX.Element, error?: (error: TError) => JSX.Element, } const defaultLoading = () => ; const defaultError = (error: any) => { error.message }; export function Async( { async, children: render, loading = defaultLoading, error = defaultError }: AsyncProps ) { if (async.isLoading || (!async.error && !async.value)) { return loading(); } if (typeof async.error !== "undefined") { return error(async.error); } return render(async.value as TValue); }