import Taro, { ENV_TYPE } from "@tarojs/taro";
import { useContext, useEffect } from "react"
import RouteContext, { RouteComponentContext } from "../provider/route";


/**展示的时候 */
export const useDidShow = (fun: () => void) => {
    if (Taro.getEnv() === ENV_TYPE.WEB) {
        const context = useContext(RouteContext);
        const routeContext = useContext(RouteComponentContext);
        useEffect(() => {
            const filter = context.history.filter(p => p.visible);
            const find = filter[filter.length - 1];
            if (find?.url === routeContext.data.url) {
                fun();
            }
        }, [context.history])
    }
    else {
        Taro.useDidShow(() => {
            fun();
        });
    }
}