Files
o2o-weather/admin/src/pages/RenderPage.tsx

40 lines
1.5 KiB
TypeScript
Raw Permalink 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.
import { useEffect, useRef, useState } from 'react'
import { useSearchParams } from 'react-router'
import { BASE_REGION, useCardData } from '../cards/data'
import { decodeCopy, whenPainted } from '../cards/share'
import { templateById } from '../cards/templates'
/** 카드 한 장만 1080×1350으로. 다 그려지면 <html data-ready="1"> */
export function RenderPage() {
const [params] = useSearchParams()
const template = templateById(params.get('template') ?? '')
const slideNo = Number(params.get('slide') ?? '1')
const { data, error } = useCardData(params.get('region') ?? BASE_REGION)
const ref = useRef<HTMLDivElement>(null)
const [ready, setReady] = useState(false)
useEffect(() => {
if (!data || !ref.current) return
let alive = true
whenPainted(ref.current).then(() => alive && setReady(true))
return () => {
alive = false
}
}, [data])
useEffect(() => {
document.documentElement.dataset.ready = ready ? '1' : ''
}, [ready])
if (!template) return <p>template 값이 없거나 틀렸어요: today · ootd · tarot</p>
if (error) return <p>날씨각 API에서 데이터를 받지 못했어요: {String(error)}</p>
if (!data) return null
const copy = { ...template.defaults(data), ...decodeCopy(params.get('copy')) }
const slides = template.slides(data, copy)
return (
<div ref={ref} style={{ width: 1080, height: 1350 }}>
{slides[slideNo - 1] ?? <p>{slides.length}장까지 있어요</p>}
</div>
)
}