crush-level-web/src/app/(main)/user/[userId]/context/aiUser/index.tsx

45 lines
1.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'use client'
import { AiUserBaseOutput } from '@/services/user'
import { createContext } from 'react'
import { useGetAIUserBaseInfo } from '@/hooks/aiUser'
import { useParams } from 'next/navigation'
import { useCurrentUser } from '@/hooks/auth'
import Empty from '@/components/ui/empty'
export * from './useAIUser'
const AIUserContext = createContext<{
user: AiUserBaseOutput | undefined
isOwner: boolean
userId: number | undefined
}>({
user: undefined,
isOwner: false,
userId: undefined,
})
export const AIUserProvider = ({ children }: { children: React.ReactNode }) => {
const { userId } = useParams()
const { data, error } = useGetAIUserBaseInfo({ aiId: Number(userId) })
const { data: currentUser } = useCurrentUser()
const isOwner = currentUser?.userId === data?.userId
if (error) {
return (
<div className="flex h-full w-full items-center justify-center">
<Empty title="Oops, theres nothing here…" />
</div>
)
}
return (
<AIUserContext.Provider value={{ user: data, isOwner, userId: Number(userId) }}>
{children}
</AIUserContext.Provider>
)
}
export default AIUserContext