import { useMemo } from "react"
import { useQuery } from "react-query"
import PageContentSkeleton from "~/common/components/skeleton/PageContentSkeleton"
import { PageContentProps } from "~/common/types/pageContent"
import { getPageContent } from "~/services/pageContent"

type Props = {
  path: string
}

const PageContent = ({ path }: Props) => {
  const { data, isLoading } = useQuery<PageContentProps>(`page-content:${path}`, async () => await getPageContent({ path }))

  const parsedContent = useMemo<string>(() => {
    if (!data) return ''
    return data.content.replaceAll(
      'src="/web/image',
      `src="${process.env.NEXT_PUBLIC_ODOO_API_HOST}/web/image`
    )
  }, [data])

  if (isLoading) return <PageContentSkeleton />

  return (
    <div dangerouslySetInnerHTML={{ __html: parsedContent || '' }}></div>
  )
}

export default PageContent