2025-03-15 16:58:02 +08:00
|
|
|
import { isRouteErrorResponse } from 'react-router'
|
2025-03-20 12:31:51 +08:00
|
|
|
import { getClientIPAddress } from 'remix-utils/get-client-ip-address'
|
2025-03-15 16:58:02 +08:00
|
|
|
import { stripHtml } from 'string-strip-html'
|
2025-03-11 23:04:29 +08:00
|
|
|
|
2025-03-09 12:32:36 +08:00
|
|
|
import { getCategories } from '~/apis/common/get-categories'
|
|
|
|
|
import { getNews } from '~/apis/common/get-news'
|
2025-03-08 00:14:30 +07:00
|
|
|
import { getNewsBySlug } from '~/apis/common/get-news-by-slug'
|
2025-03-15 01:00:32 +07:00
|
|
|
import { getUser } from '~/apis/news/get-user'
|
2025-03-08 17:24:53 +08:00
|
|
|
import { APP } from '~/configs/meta'
|
2025-03-08 00:14:30 +07:00
|
|
|
import { handleCookie } from '~/libs/cookies'
|
2025-02-20 07:01:36 +08:00
|
|
|
import { NewsDetailPage } from '~/pages/news-detail'
|
|
|
|
|
|
2025-03-08 00:14:30 +07:00
|
|
|
import type { Route } from './+types/_news.detail.$slug'
|
|
|
|
|
|
2025-03-08 15:03:45 +08:00
|
|
|
export const loader = async ({ request, params }: Route.LoaderArgs) => {
|
2025-03-20 11:49:36 +08:00
|
|
|
const userAgent = request.headers.get('user-agent')
|
2025-03-20 12:31:51 +08:00
|
|
|
const ipAddress = getClientIPAddress(request) || 'localhost'
|
2025-03-12 20:46:54 +08:00
|
|
|
const { userToken: accessToken } = await handleCookie(request)
|
2025-03-15 16:58:02 +08:00
|
|
|
let userData
|
|
|
|
|
if (accessToken) {
|
|
|
|
|
const { data } = await getUser({ accessToken })
|
|
|
|
|
userData = data
|
2025-03-15 01:00:32 +07:00
|
|
|
}
|
2025-03-12 20:46:54 +08:00
|
|
|
const { slug } = params
|
2025-03-20 11:49:36 +08:00
|
|
|
let { data: newsDetailData } = await getNewsBySlug({
|
|
|
|
|
slug,
|
|
|
|
|
accessToken,
|
|
|
|
|
userAgent,
|
|
|
|
|
ipAddress,
|
|
|
|
|
})
|
2025-03-15 16:58:02 +08:00
|
|
|
const shouldSubscribe =
|
|
|
|
|
(!accessToken || userData?.subscribe?.subscribe_plan?.code === 'basic') &&
|
|
|
|
|
newsDetailData?.is_premium
|
|
|
|
|
newsDetailData = {
|
|
|
|
|
...newsDetailData,
|
|
|
|
|
content: shouldSubscribe
|
|
|
|
|
? stripHtml(newsDetailData.content).result.slice(0, 600)
|
|
|
|
|
: newsDetailData.content,
|
2025-03-15 01:08:38 +07:00
|
|
|
}
|
2025-03-09 12:32:36 +08:00
|
|
|
const { data: categoriesData } = await getCategories()
|
|
|
|
|
const beritaCode = 'berita'
|
|
|
|
|
const beritaCategory = categoriesData.find(
|
|
|
|
|
(category) => category.code === beritaCode,
|
|
|
|
|
)
|
2025-03-20 12:10:38 +08:00
|
|
|
const beritaData = getNews({ categories: [beritaCode], active: true })
|
2025-03-08 00:14:30 +07:00
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
newsDetailData,
|
2025-03-09 12:32:36 +08:00
|
|
|
beritaCategory,
|
2025-03-20 12:10:38 +08:00
|
|
|
beritaData,
|
2025-03-15 16:58:02 +08:00
|
|
|
shouldSubscribe,
|
2025-03-08 00:14:30 +07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-08 17:24:53 +08:00
|
|
|
export const meta = ({ data }: Route.MetaArgs) => {
|
2025-03-15 16:58:02 +08:00
|
|
|
const { newsDetailData } = data || {}
|
|
|
|
|
const metaTitle = APP.title
|
|
|
|
|
const title = `${newsDetailData?.title} - ${metaTitle}`
|
2025-03-08 17:24:53 +08:00
|
|
|
|
2025-03-15 16:58:02 +08:00
|
|
|
return [
|
|
|
|
|
{
|
|
|
|
|
title,
|
|
|
|
|
},
|
|
|
|
|
]
|
2025-03-08 17:24:53 +08:00
|
|
|
}
|
|
|
|
|
|
2025-03-11 23:04:29 +08:00
|
|
|
export const ErrorBoundary = ({ error }: Route.ErrorBoundaryProps) => {
|
|
|
|
|
let message = 'Oops!'
|
|
|
|
|
let details = 'An unexpected error occurred.'
|
|
|
|
|
let stack: string | undefined
|
|
|
|
|
|
|
|
|
|
if (isRouteErrorResponse(error)) {
|
|
|
|
|
message = error.status === 404 ? '404' : 'Error'
|
|
|
|
|
details =
|
|
|
|
|
error.status === 404
|
|
|
|
|
? 'The requested page could not be found.'
|
|
|
|
|
: error.statusText || details
|
|
|
|
|
} else if (import.meta.env.DEV && error && error instanceof Error) {
|
|
|
|
|
details = error.message
|
|
|
|
|
stack = error.stack
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div className="container mx-auto p-4">
|
|
|
|
|
<h1>{message}</h1>
|
|
|
|
|
<p>{details}</p>
|
|
|
|
|
{stack && (
|
|
|
|
|
<pre className="w-full p-4 whitespace-pre-wrap">
|
|
|
|
|
<code>{stack}</code>
|
|
|
|
|
</pre>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-03 16:33:30 +08:00
|
|
|
const NewsDetailLayout = () => <NewsDetailPage />
|
2025-02-20 07:01:36 +08:00
|
|
|
|
|
|
|
|
export default NewsDetailLayout
|