Files
o2o-weather/frontend/src/b/shareCards.tsx
T

117 lines
5.0 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.
import type { Ref } from 'react'
import { createPortal } from 'react-dom'
import { cx } from '../lib/utils'
import type { TopIndex } from '../api/client/types.gen'
import { MEDALS, type TimeKind } from './lib'
import { Ic } from './ui'
/** 공유 이미지 전용 틀. 화면 밖에 그려 두고 보내기 때 이것만 찍는다 (화면의 버튼은 이미지에서 빠지므로 글자만으로 만든다) */
function Offscreen({ children }: { children: React.ReactNode }) {
return createPortal(
<div aria-hidden="true" className="pointer-events-none fixed top-0 -left-[10000px] w-[380px]">
{children}
</div>,
document.body,
)
}
export type DayShare = {
kind: TimeKind
label: string
now: boolean
icon: string
temp: string
score?: number | null
gak?: { name: string; icon: string } | null
}
function DayLabel({ d, size }: { d: DayShare; size: string }) {
return (
<span className={cx('flex items-center gap-1.5 font-jua leading-none', size)}>
{d.label}
{d.now && <small className="rounded-full bg-[#ff7a45] px-2 py-0.5 font-sans text-[13px] font-extrabold text-white">지금</small>}
</span>
)
}
/** 히어로 공유: 아침·낮·저녁·밤 네 줄. 줄마다 그 시간대 하늘에 날씨·기온, 날씨 점수, 그 시간 1등 각. 로고·지역·날짜는 공유 틀이 그린다 */
export function DayShareCard({ days, ref }: { days: DayShare[]; ref: Ref<HTMLDivElement> }) {
return (
<Offscreen>
<div ref={ref} className="flex aspect-[4/5] flex-col">
{days.map((d) => (
<div key={d.kind} className="b-hero grid flex-1 grid-cols-[96px_1fr_auto] items-center gap-3 px-4" data-t={d.kind}>
<span className="flex flex-col gap-1.5">
<DayLabel d={d} size="text-[28px]" />
<span className="flex items-center gap-1 font-jua text-[22px] leading-none">
<Ic name={d.icon} className="h-7 w-7 object-contain" />
{d.temp}
</span>
</span>
<span className="flex min-w-0 items-center gap-2">
{d.gak && <Ic name={d.gak.icon} className="w-10 shrink-0" />}
<b className="font-jua text-[24px] leading-tight font-normal break-keep">{d.gak?.name ?? '쉬어 가요'}</b>
</span>
<span className="text-right">
<b className="block font-bagel text-[62px] leading-[0.9] font-normal">{d.score ?? '–'}</b>
<small className="text-[13px] font-bold opacity-75">날씨 점수</small>
</span>
</div>
))}
</div>
</Offscreen>
)
}
/** 1:1 공유용: 같은 내용을 2×2 칸으로 */
export function DayShareGrid({ days, ref }: { days: DayShare[]; ref: Ref<HTMLDivElement> }) {
return (
<Offscreen>
<div ref={ref} className="grid aspect-square grid-cols-2 grid-rows-2">
{days.map((d) => (
<div key={d.kind} className="b-hero flex flex-col justify-between p-4" data-t={d.kind}>
<span className="flex items-center justify-between">
<DayLabel d={d} size="text-[26px]" />
<span className="flex items-center gap-1 font-jua text-[20px] leading-none">
<Ic name={d.icon} className="h-7 w-7 object-contain" />
{d.temp}
</span>
</span>
<span>
<b className="block font-bagel text-[64px] leading-[0.9] font-normal">{d.score ?? '–'}</b>
<small className="text-[12px] font-bold opacity-75">날씨 점수</small>
</span>
<span className="flex min-w-0 items-center gap-1.5">
{d.gak && <Ic name={d.gak.icon} className="w-8 shrink-0" />}
<b className="truncate font-jua text-[21px] font-normal">{d.gak?.name ?? '쉬어 가요'}</b>
</span>
</div>
))}
</div>
</Offscreen>
)
}
export function GakShareCard({ items, title, ref }: { items: TopIndex[]; title: string; ref: Ref<HTMLDivElement> }) {
return (
<Offscreen>
<div ref={ref} className="b-band-2 flex aspect-[4/5] flex-col bg-white px-5 pt-5 pb-4">
<p className="m-0 font-jua text-[22px]">{title}</p>
<ol className="m-0 mt-3 grid list-none gap-2 p-0">
{items.map((g, i) => (
<li key={g.index_id} className="flex items-center gap-2.5 rounded-[18px] bg-white px-3 py-2.5 shadow-[inset_0_-4px_0_var(--shade)]">
{i < 3 ? <Ic name={MEDALS[i]} className="w-[24px] shrink-0" /> : <span className="w-[24px] shrink-0 text-center font-jua text-(--ink3)">{i + 1}</span>}
<Ic name={g.icon} className="w-[36px] shrink-0" />
<span className="min-w-0 flex-1">
<b className="block font-jua text-[17px] leading-tight font-normal">{g.name}</b>
{g.summary && <small className="block truncate text-xs font-semibold text-(--ink2)">{g.summary}</small>}
</span>
<em className="font-bagel text-[24px] leading-none text-(--orange) not-italic">{g.score}</em>
</li>
))}
</ol>
</div>
</Offscreen>
)
}