import React from 'react'
import { Props } from './global'
import { useAtomValue } from 'jotai'
import { authAtom } from './store/system/user.ts'


export type AuthProps = Props & {
    //是否渲染没有权限的组件
    noAuth?: React.ReactNode
    //权限key
    authKey?: string[]
}

export const Auth: React.FC<AuthProps> = (props) => {

    const auth = useAtomValue(authAtom)

    if (!auth.isLogin) {
        return null
    }

    if (props.authKey && props.authKey.length > 0) {
        if (props.authKey.some(key => !auth.authKey?.includes(key))) {
            return props.noAuth || null
        }
    }

    return (
            <>
                {props!.children}
            </>
    )
}

export default Auth