mirror of
https://github.com/ManInDark/HabitTrove.git
synced 2026-01-21 06:34:30 +01:00
25 lines
729 B
TypeScript
25 lines
729 B
TypeScript
// client helpers
|
|
'use-client'
|
|
|
|
import { useSession } from "next-auth/react"
|
|
import { User, UserId } from './types'
|
|
import { useAtom } from 'jotai'
|
|
import { usersAtom } from './atoms'
|
|
import { checkPermission } from './utils'
|
|
|
|
export function useHelpers() {
|
|
const { data: session, status } = useSession()
|
|
const currentUserId = session?.user.id
|
|
const [usersData] = useAtom(usersAtom)
|
|
const currentUser = usersData.users.find((u) => u.id === currentUserId)
|
|
|
|
return {
|
|
currentUserId,
|
|
currentUser,
|
|
usersData,
|
|
status,
|
|
hasPermission: (resource: 'habit' | 'wishlist' | 'coins', action: 'write' | 'interact') => currentUser?.isAdmin ||
|
|
checkPermission(currentUser?.permissions, resource, action)
|
|
}
|
|
}
|