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

45 lines
1.1 KiB
TypeScript
Raw Normal View History

2025-11-28 06:31:36 +00:00
'use client'
2025-11-13 08:38:25 +00:00
2025-11-28 06:31:36 +00:00
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'
2025-11-13 08:38:25 +00:00
2025-11-28 06:31:36 +00:00
export * from './useAIUser'
2025-11-13 08:38:25 +00:00
const AIUserContext = createContext<{
2025-11-28 06:31:36 +00:00
user: AiUserBaseOutput | undefined
isOwner: boolean
userId: number | undefined
2025-11-13 08:38:25 +00:00
}>({
user: undefined,
isOwner: false,
userId: undefined,
2025-11-28 06:31:36 +00:00
})
2025-11-13 08:38:25 +00:00
export const AIUserProvider = ({ children }: { children: React.ReactNode }) => {
2025-11-28 06:31:36 +00:00
const { userId } = useParams()
2025-11-13 08:38:25 +00:00
2025-11-28 06:31:36 +00:00
const { data, error } = useGetAIUserBaseInfo({ aiId: Number(userId) })
const { data: currentUser } = useCurrentUser()
const isOwner = currentUser?.userId === data?.userId
2025-11-13 08:38:25 +00:00
if (error) {
return (
2025-11-28 06:31:36 +00:00
<div className="flex h-full w-full items-center justify-center">
2025-11-13 08:38:25 +00:00
<Empty title="Oops, theres nothing here…" />
</div>
)
}
return (
<AIUserContext.Provider value={{ user: data, isOwner, userId: Number(userId) }}>
{children}
</AIUserContext.Provider>
2025-11-28 06:31:36 +00:00
)
2025-11-13 08:38:25 +00:00
}
2025-11-28 06:31:36 +00:00
export default AIUserContext