# Conflicts: # backend/common/enums.py # backend/router/v1/auth/account.py # frontend/.dockerignore # frontend/package-lock.json # frontend/package.json # frontend/public/terms.html # frontend/src/api/client/index.ts # frontend/src/b/BLayout.tsx # frontend/src/b/Sheets.tsx # shared/api/client/@tanstack/react-query.gen.ts # shared/api/client/sdk.gen.ts # shared/api/client/types.gen.ts
565 lines
28 KiB
TypeScript
565 lines
28 KiB
TypeScript
import { useEffect, useRef, useState, type CSSProperties, type MouseEvent, type ReactNode, type RefObject } from 'react'
|
|
import { createPortal } from 'react-dom'
|
|
import { PostType, SlotTone, type GakSlide, type HoursSlide, type ImageSlide, type PlanSlide, type PostData, type PostReaction, type ResFeed, type RowsSlide, type SunsetSlide, type WeekSlide } from '@nalssigak/shared/api/client/types.gen'
|
|
import { API_BASE_URL } from '../api/setup'
|
|
import { cx } from '../lib/utils'
|
|
import { cardBg, clockShort } from '@nalssigak/shared/lib'
|
|
import { fall } from './lib'
|
|
import type { SlideBg } from '@nalssigak/shared/lib/types'
|
|
import { deg } from '@nalssigak/shared/lib/weatherText'
|
|
import { useMutation, useQueryClient, type QueryClient } from '@tanstack/react-query'
|
|
import { likePostMutation, unlikePostMutation } from '@nalssigak/shared/api/client/@tanstack/react-query.gen'
|
|
import { usePinnedLike, type PinnedKey } from './pinnedLike'
|
|
import { useSheet } from './sheetContext'
|
|
import { CommentIcon, HeartIcon, SendIcon } from './svg'
|
|
import { useCarousel } from './carousel'
|
|
import { Arrows, Credit, Ic, Rows, Skel, TAP44, Thumb } from './ui'
|
|
|
|
const ACCOUNTS: Record<PostType, readonly [string, string]> = {
|
|
[PostType.WEATHER]: ['오늘 날씨', 'sun'],
|
|
[PostType.GAK]: ['오늘의 각', 'rabbit'],
|
|
[PostType.SUNSET]: ['노을', 'sunset'],
|
|
[PostType.SEASON]: ['계절 소식', 'maple'],
|
|
[PostType.EVENT]: ['가까운 행사', 'fireworks'],
|
|
[PostType.TIP]: ['날씨 상식', 'bulb'],
|
|
[PostType.WEEK]: ['주말 예보', 'calendar'],
|
|
[PostType.ALERT]: ['생활 알림', 'umbrella'],
|
|
[PostType.TOMORROW]: ['내일 미리보기', 'moon'],
|
|
[PostType.QUOTE]: ['오늘의 한 문장', 'letter'],
|
|
[PostType.WEEKEND_PLAN]: ['주말 플랜', 'calendar'],
|
|
[PostType.AIR]: ['미세먼지', 'vent'],
|
|
[PostType.DAY_TRIP]: ['당일치기', 'picnic'],
|
|
[PostType.MOON]: ['오늘 밤 달', 'moon'],
|
|
[PostType.UV]: ['자외선', 'glasses'],
|
|
[PostType.HERO]: ['오늘 날씨', 'sun'],
|
|
[PostType.TODAY_GAK]: ['오늘의 각', 'rabbit'],
|
|
[PostType.FOLIAGE]: ['단풍 지도', 'maple'],
|
|
[PostType.EVENTS_BAND]: ['이번 주 가까운 행사', 'fireworks'],
|
|
[PostType.SEASON_BAND]: ['이번 주 계절 이야기', 'fallen_leaf'],
|
|
[PostType.CARD]: ['날씨각 카드', 'stars'],
|
|
}
|
|
|
|
type Slide = HoursSlide | RowsSlide | GakSlide | SunsetSlide | WeekSlide | PlanSlide | ImageSlide
|
|
|
|
const GAK_BG: SlideBg[] = ['butter', 'peach', 'lilac']
|
|
const ROW_ICON_BG: Record<string, SlideBg> = { umbrella: 'lilac', vent: 'mint', sunset: 'peach', thermo: 'sky', maple: 'peach', moon: 'lilac', glasses: 'butter' }
|
|
|
|
/** 배경색은 API에 없어 글 종류·아이콘으로 고른다 */
|
|
function slideBg(post: PostData, slide: Slide): SlideBg {
|
|
if (slide.kind === 'hours') return 'sky'
|
|
if (slide.kind === 'sunset') return 'dusk'
|
|
if (slide.kind === 'week') return 'mint'
|
|
if (slide.kind === 'plan') return slide.eyebrow.startsWith('일') ? 'peach' : 'butter'
|
|
if (slide.kind === 'gak') return GAK_BG[post.post_id % GAK_BG.length]
|
|
if (slide.kind === 'image') return 'sky'
|
|
if (post.post_type === PostType.EVENT) return 'night'
|
|
if (post.post_type === PostType.TIP) return 'sky'
|
|
if (post.post_type === PostType.AIR) return slide.icon === 'vent' ? 'mint' : 'lilac'
|
|
if (post.post_type === PostType.DAY_TRIP) return slide.image ? 'night' : 'mint'
|
|
return ROW_ICON_BG[slide.icon] ?? cardBg(slide.icon, post.post_id)
|
|
}
|
|
|
|
const DARK = new Set<SlideBg>(['dusk', 'night'])
|
|
|
|
// 그림 배경이 있는 각(code)과 장 수. 여러 장이면 글마다 돌려 쓴다
|
|
const BG_IMAGES: Record<string, number> = { bike: 4, picnic: 1, hiking: 1, camping: 1, stars: 1, walk: 1, hangang: 1 }
|
|
const EVENT_BG_N = 4
|
|
|
|
function bgImage(name: string, n: number, seed: number): CSSProperties {
|
|
const file = n > 1 ? `${name}-${'abcd'[seed % n]}` : name
|
|
return { backgroundImage: `url(/post-bg/${file}.webp)`, backgroundSize: 'cover', backgroundPosition: 'center' }
|
|
}
|
|
|
|
/** 그림 배경: 각은 각마다, 사진 없는 행사는 행사용. 없으면 색 배경만 */
|
|
function slideImage(post: PostData, slide: Slide): CSSProperties | undefined {
|
|
if (slide.kind === 'gak') return BG_IMAGES[slide.code] ? bgImage(slide.code, BG_IMAGES[slide.code], post.post_id) : undefined
|
|
if (post.post_type === PostType.EVENT && slide.kind === 'rows' && !slide.image) return bgImage('event', EVENT_BG_N, post.post_id)
|
|
return undefined
|
|
}
|
|
|
|
function Lines({ text }: { text: string }) {
|
|
return text.split('\n').map((t, i) => (
|
|
<span key={i}>
|
|
{i > 0 && <br />}
|
|
{t}
|
|
</span>
|
|
))
|
|
}
|
|
|
|
function Eyebrow({ children, className }: { children: ReactNode; className?: string }) {
|
|
return <p className={cx('m-0 text-[13px] font-extrabold opacity-75', className)}>{children}</p>
|
|
}
|
|
|
|
function Big({ children, className }: { children: ReactNode; className?: string }) {
|
|
return <p className={cx('mt-1.5 font-jua text-[29px] leading-tight', className)}>{children}</p>
|
|
}
|
|
|
|
function Pic({ name, className }: { name: string; className?: string }) {
|
|
return (
|
|
<span className={cx('b-fall pointer-events-none absolute top-[30%] right-3.5 w-[42%]', className)} style={fall('.1s', '5s')}>
|
|
<Ic name={name} className="b-bob w-full drop-shadow-[0_16px_18px_rgb(40_48_74/0.18)]" />
|
|
</span>
|
|
)
|
|
}
|
|
|
|
const slideBase = 'relative flex h-full w-full shrink-0 flex-col px-5 py-[22px]'
|
|
|
|
function SlideView({ slide, bg, img }: { slide: Slide; bg: SlideBg; img?: CSSProperties }) {
|
|
const { open } = useSheet()
|
|
if (slide.kind === 'hours') {
|
|
const temps = slide.hours.flatMap((h) => (h.temp_c == null ? [] : [h.temp_c]))
|
|
const lo = Math.min(...temps)
|
|
const span = Math.max(...temps) - lo || 1
|
|
return (
|
|
<div className={cx(slideBase, 'b-bg-sky')}>
|
|
<Eyebrow>{slide.eyebrow}</Eyebrow>
|
|
<Big>
|
|
<Lines text={slide.line} />
|
|
</Big>
|
|
<div className="mt-3.5 grid flex-1 grid-cols-4 gap-2">
|
|
{slide.hours.map((h) => (
|
|
<div
|
|
key={h.label}
|
|
className={cx(
|
|
'relative flex flex-col items-center justify-between overflow-hidden rounded-[18px] px-1 pt-2.5 pb-[9px] text-xs font-bold text-(--ink2) [&>*]:relative',
|
|
h.tone === SlotTone.NIGHT ? 'bg-[#e3e7ff] shadow-[inset_0_-5px_0_#c7cff5]' : h.tone === SlotTone.SUNSET ? 'bg-[#ffe3d2] shadow-[inset_0_-5px_0_#ffc19f]' : 'bg-white shadow-[inset_0_-5px_0_var(--shade)]',
|
|
)}
|
|
>
|
|
{h.temp_c != null && (
|
|
<i className="absolute! inset-x-0 bottom-0 bg-linear-to-t from-[rgb(255_122_69/0.34)] to-[rgb(255_122_69/0.1)]" style={{ height: `${20 + ((h.temp_c - lo) / span) * 70}%` }} aria-hidden="true" />
|
|
)}
|
|
<span>{h.label}</span>
|
|
<Ic name={h.icon} className="w-[34px]" />
|
|
<b className="font-bagel text-[21px] font-normal text-(--ink)">{deg(h.temp_c)}</b>
|
|
</div>
|
|
))}
|
|
</div>
|
|
</div>
|
|
)
|
|
}
|
|
if (slide.kind === 'gak')
|
|
return (
|
|
<div className={cx(slideBase, `b-bg-${bg}`)} style={img}>
|
|
<Eyebrow>오늘의 각 · {slide.rank}위</Eyebrow>
|
|
<Big>{slide.line}</Big>
|
|
<Pic name={slide.code} />
|
|
{slide.say && <p className="relative z-[1] m-0 mt-2.5 w-[56%] rounded-[16px] rounded-tl-[4px] bg-white/80 px-3 py-2 text-[14px] leading-snug font-bold">{slide.say}</p>}
|
|
{/* 점수 근거: 비중 큰 조건과 지금 값. 오른쪽 아이콘과 겹치지 않게 왼쪽 반만 */}
|
|
{!!slide.rows?.length && (
|
|
<div className="relative z-[1] mt-2 w-[56%] rounded-[16px] bg-white/70 px-3 text-[14px] [&_b]:text-[15px] [&>div>div]:py-1.5 [&>div]:mt-0">
|
|
<Rows rows={slide.rows} />
|
|
</div>
|
|
)}
|
|
<b className="mt-auto font-bagel text-[82px] leading-[0.9] font-normal text-(--orange)">{slide.score}</b>
|
|
<p className="m-0 font-jua text-[29px] leading-tight">{slide.name}</p>
|
|
<Eyebrow className="mt-1">{slide.summary}</Eyebrow>
|
|
<button type="button" onClick={() => open({ type: 'gak', indexId: slide.index_id })} className="mt-3 cursor-pointer self-start rounded-full bg-(--orange) px-4 py-[9px] font-jua text-[15px] text-white shadow-[inset_0_-5px_0_var(--orange-deep)]">
|
|
계산 근거 보기
|
|
</button>
|
|
</div>
|
|
)
|
|
if (slide.kind === 'sunset')
|
|
return (
|
|
<div className={cx(slideBase, slide.situation ? `b-sit-${slide.situation}` : 'b-bg-dusk')}>
|
|
<Eyebrow>{slide.eyebrow}</Eyebrow>
|
|
{/* 노을 탭 히어로와 같은 순서: 결론 → 가장 붉을 때 → 쉬운 이유. 점수는 노을 탭 '자세히'에 */}
|
|
<Big>{slide.headline ?? slide.grade_label}</Big>
|
|
{slide.line && <p className="m-0 mt-0.5 font-jua text-[18px]">{slide.line}</p>}
|
|
<div className="mt-3 flex flex-1 flex-col gap-2">
|
|
{slide.cuts.map((c, i) => (
|
|
<div key={c.kind} className={cx('b-skyline relative flex-1 overflow-hidden rounded-2xl px-3 py-2 font-bagel text-[21px] text-white', `b-cut-${Math.min(i + 1, 3)}`)}>
|
|
<span className="relative z-[1]">
|
|
{clockShort(c.at)}
|
|
<small className="ml-2 font-jua text-sm">{c.label}</small>
|
|
</span>
|
|
</div>
|
|
))}
|
|
</div>
|
|
</div>
|
|
)
|
|
if (slide.kind === 'week')
|
|
return (
|
|
<div className={cx(slideBase, 'b-bg-mint')}>
|
|
<Eyebrow>{slide.eyebrow}</Eyebrow>
|
|
<Big>
|
|
<Lines text={slide.line} />
|
|
</Big>
|
|
{/* 칸은 제목 바로 아래, 팁만 맨 아래로. 칸을 바닥에 붙이면 가운데가 비어 덜 그려진 화면처럼 보인다 */}
|
|
<div className="mt-4 grid grid-cols-2 gap-2.5">
|
|
{slide.days.map((d) => (
|
|
<div key={d.day} className="flex flex-col gap-0.5 rounded-[18px] bg-white/85 px-3 py-2.5 shadow-[inset_0_-5px_0_rgb(217_229_244/0.9)]">
|
|
<Ic name={d.icon} className="w-[34px]" />
|
|
<b className="font-jua text-lg font-normal">
|
|
{d.weekday} {d.day.split('-').slice(1).map(Number).join('.')}
|
|
</b>
|
|
<span className="font-jua text-[22px]">
|
|
<span className="text-(--rain)">{deg(d.temp_min_c)}</span> → <span className="text-(--orange)">{deg(d.temp_max_c)}</span>
|
|
</span>
|
|
<small className="text-xs font-bold text-(--ink2)">
|
|
비 {d.rain_prob ?? '?'}%{d.temp_min_c != null && d.temp_max_c != null && ` · 일교차 ${Math.round(d.temp_max_c - d.temp_min_c)}°`}
|
|
</small>
|
|
{d.gaks && d.gaks.length > 0 && (
|
|
<ul className="mt-2 flex flex-col gap-1.5 border-t border-[rgb(217_229_244)] pt-2" aria-label={`${d.weekday}요일 대표 각`}>
|
|
<li className="text-[11px] font-extrabold text-(--ink2)">이날 할 만한 각</li>
|
|
{d.gaks.map((g) => (
|
|
<li key={g.index_id} className="flex items-center gap-1.5">
|
|
<Ic name={g.icon} className="w-[26px] shrink-0" />
|
|
<span className="min-w-0 flex-1 truncate text-[14px] font-bold">{g.name}</span>
|
|
<em className="font-bagel text-[16px] text-(--orange) not-italic">{g.score}</em>
|
|
</li>
|
|
))}
|
|
</ul>
|
|
)}
|
|
</div>
|
|
))}
|
|
</div>
|
|
{slide.tip && <p className="mt-auto rounded-[14px] bg-(--ink) px-3 py-2.5 text-[13px] leading-snug font-bold text-white">{slide.tip}</p>}
|
|
</div>
|
|
)
|
|
if (slide.kind === 'plan') {
|
|
const credits = [...new Set(slide.steps.flatMap((st) => (st.image && st.image_credit ? [st.image_credit] : [])))]
|
|
return (
|
|
<div className={cx(slideBase, `b-bg-${bg}`)}>
|
|
<Eyebrow>{slide.eyebrow}</Eyebrow>
|
|
<Big className="text-[24px]!">
|
|
<Lines text={slide.line} />
|
|
</Big>
|
|
<ol className="mt-auto flex flex-col gap-2">
|
|
{slide.steps.map((st, i) => (
|
|
<li key={i} className="grid grid-cols-[52px_26px_minmax(0,1fr)_auto] items-center gap-1.5 rounded-[14px] bg-white/80 px-2 py-1.5">
|
|
<time className="font-jua text-sm text-(--ink2) tabular-nums">{st.time}</time>
|
|
<Ic name={st.icon} className="w-[22px] justify-self-center" />
|
|
<span className="min-w-0">
|
|
<b className="block truncate text-[13.5px]">{st.title}</b>
|
|
<small className="block truncate text-[11.5px] font-semibold text-(--ink2)">{st.sub}</small>
|
|
</span>
|
|
{st.image && <Thumb image={st.image} icon={st.icon} className="size-[46px] rounded-[10px]" iconClassName="w-[26px]" />}
|
|
</li>
|
|
))}
|
|
</ol>
|
|
{credits.length > 0 && <small className="mt-1.5 block text-right text-[10px] font-semibold text-(--ink2)">사진 {credits.join(' · ')}</small>}
|
|
</div>
|
|
)
|
|
}
|
|
// 카드뉴스: 관리자가 만든 4:5 이미지 한 장 (/v1/card/... 는 API 서버 경로)
|
|
if (slide.kind === 'image')
|
|
return <img src={slide.url.startsWith('/') ? `${API_BASE_URL}${slide.url}` : slide.url} alt={slide.alt ?? ''} className="h-full w-full shrink-0 object-cover" loading="lazy" draggable={false} />
|
|
if (slide.kind === 'rows' && slide.image)
|
|
return (
|
|
<div className={'relative flex h-full w-full shrink-0 flex-col justify-end bg-cover bg-center text-white'} style={{ backgroundImage: `url(${slide.image})` }}>
|
|
{slide.image_credit && <Credit text={slide.image_credit} className="top-2.5 right-2.5" />}
|
|
<div className="bg-linear-to-b from-transparent via-[rgb(16_20_34/0.55)] via-35% to-[rgb(16_20_34/0.85)] px-5 pt-20 pb-3.5">
|
|
<Eyebrow>{slide.eyebrow}</Eyebrow>
|
|
<Big>
|
|
<Lines text={slide.line} />
|
|
</Big>
|
|
<Rows rows={slide.rows ?? []} dark />
|
|
</div>
|
|
</div>
|
|
)
|
|
return (
|
|
<div className={cx(slideBase, `b-bg-${bg}`)} style={img}>
|
|
<Eyebrow className={DARK.has(bg) ? 'opacity-85' : undefined}>{slide.eyebrow}</Eyebrow>
|
|
<Big className="relative z-[1]">
|
|
<Lines text={slide.line} />
|
|
</Big>
|
|
<Pic name={slide.icon} className="w-[36%]" />
|
|
<Rows rows={slide.rows ?? []} dark={DARK.has(bg)} />
|
|
</div>
|
|
)
|
|
}
|
|
|
|
type PostProps = { post: PostData }
|
|
|
|
const HEART = 'M12 20.5s-8-4.9-8-11A4.6 4.6 0 0 1 12 6.8a4.6 4.6 0 0 1 8 2.7c0 6.1-8 11-8 11z'
|
|
// 좋아요 때 튀어 나가는 작은 하트: [x, y, 회전, 색]
|
|
const BURST: [number, number, number, string][] = [
|
|
[-86, -60, -24, '#ff3b5c'],
|
|
[84, -66, 22, '#ff8e4e'],
|
|
[-104, 18, -40, '#ffc86e'],
|
|
[102, 24, 36, '#ff3b5c'],
|
|
[-48, -104, -8, '#ff8aa0'],
|
|
[52, -100, 12, '#ffc86e'],
|
|
]
|
|
|
|
/** 받아 둔 피드 데이터에도 좋아요를 반영한다. 안 하면 다른 탭에 다녀왔을 때 예전 값으로 다시 그려진다 */
|
|
function patchFeed(queryClient: QueryClient, postId: number, patch: Partial<PostData>) {
|
|
queryClient.setQueriesData<ResFeed>({ predicate: (q) => (q.queryKey[0] as { _id?: string } | undefined)?._id === 'getFeed' }, (feed) =>
|
|
feed?.posts ? { ...feed, posts: feed.posts.map((p) => (p.post_id === postId ? { ...p, ...patch } : p)) } : feed,
|
|
)
|
|
}
|
|
|
|
/** 한 줄 카드 (흐린 날 노을처럼 큰 그림이 필요 없는 소식) */
|
|
function LineCard({ post }: PostProps) {
|
|
const [account, avatar] = ACCOUNTS[post.post_type]
|
|
return (
|
|
<article className="mx-3 mt-3.5 flex items-center gap-3 rounded-[22px] bg-white px-4 py-3 shadow-[inset_0_-4px_0_var(--shade)]" aria-label={post.title}>
|
|
<Ic name={post.icon || avatar} className="w-10 shrink-0" />
|
|
<p className="m-0 min-w-0 text-sm">
|
|
<small className="block text-xs font-bold text-(--ink3)">{account}</small>
|
|
<b className="font-jua text-base font-normal">{post.title}</b> <span className="text-(--ink2)">{post.caption}</span>
|
|
</p>
|
|
</article>
|
|
)
|
|
}
|
|
|
|
/** 인스타식 게시물: 옆으로 넘기는 슬라이드, 두 번 눌러 좋아요. 모르는 종류(서버에 새로 생긴 것)는 건너뛴다 */
|
|
export function PostCard({ post }: PostProps) {
|
|
if (!(post.post_type in ACCOUNTS)) return null
|
|
return post.size === 'line' ? <LineCard post={post} /> : <FullCard post={post} />
|
|
}
|
|
|
|
/** FullCard 와 같은 틀(머리줄·4:5 그림·버튼줄·글 한 줄)의 스켈레톤 */
|
|
export function PostCardSkel() {
|
|
return (
|
|
<div className="mt-3.5 border-y border-(--line) bg-white" aria-hidden="true">
|
|
<div className="flex items-center gap-2.5 px-3 py-2.5">
|
|
<Skel shape className="size-[34px] shrink-0 rounded-full" />
|
|
<span>
|
|
<b className="block text-sm leading-tight font-extrabold">
|
|
<Skel className="w-12" />
|
|
</b>
|
|
<small className="text-xs font-semibold">
|
|
<Skel className="w-16" />
|
|
</small>
|
|
</span>
|
|
</div>
|
|
<Skel shape className="aspect-[4/5]" />
|
|
<div className="flex items-center gap-3.5 px-3 pt-2.5 pb-1">
|
|
<Skel shape className="size-7 rounded-full" />
|
|
<Skel shape className="size-7 rounded-full" />
|
|
<Skel shape className="size-7 rounded-full" />
|
|
</div>
|
|
<p className="px-3 py-0.5 text-sm">
|
|
<Skel className="w-20" />
|
|
</p>
|
|
<p className="px-3 py-0.5 text-sm">
|
|
<Skel className="w-4/5" />
|
|
</p>
|
|
<p className="px-3 pt-[3px] text-sm">
|
|
<Skel className="w-32" />
|
|
</p>
|
|
<p className="px-3 pt-1 pb-3 text-[11.5px]">
|
|
<Skel className="w-8" />
|
|
</p>
|
|
</div>
|
|
)
|
|
}
|
|
|
|
function FullCard({ post }: PostProps) {
|
|
const { open } = useSheet()
|
|
const liked = !!post.liked
|
|
const likeCount = post.like_count
|
|
const queryClient = useQueryClient()
|
|
const like = useMutation(likePostMutation())
|
|
const unlike = useMutation(unlikePostMutation())
|
|
const { ref: sliderRef, api: slider, prev: canPrev, next: canNext, index } = useCarousel({ align: 'start', active: post.slides.length > 1 })
|
|
const [pop, setPop] = useState(0)
|
|
const [likeFailed, setLikeFailed] = useState(0)
|
|
const lastTap = useRef(0)
|
|
const trackRef = useRef<HTMLDivElement>(null)
|
|
const [account, avatar] = ACCOUNTS[post.post_type]
|
|
const n = post.slides.length
|
|
const bgs = post.slides.map((sl) => slideBg(post, sl))
|
|
const share = () => open({ type: 'share', share: { title: post.title, slides: Array.from(trackRef.current?.children ?? []).filter((c) => c instanceof HTMLElement) } })
|
|
const comments = () => open({ type: 'comments', postId: post.post_id })
|
|
const patch = (values: Partial<PostData>) => patchFeed(queryClient, post.post_id, values)
|
|
|
|
useEffect(() => {
|
|
if (!likeFailed) return
|
|
const t = setTimeout(() => setLikeFailed(0), 2500)
|
|
return () => clearTimeout(t)
|
|
}, [likeFailed])
|
|
|
|
// 화면을 먼저 바꾸고, 실패하면 되돌린다 (서버는 실패도 200 + result.success=false 로 준다)
|
|
const setLike = (on: boolean) => {
|
|
if (on === liked || like.isPending || unlike.isPending) return
|
|
if (on) setPop((p) => p + 1)
|
|
setLikeFailed(0)
|
|
patch({ liked: on, like_count: likeCount + (on ? 1 : -1) })
|
|
const revert = () => {
|
|
patch({ liked, like_count: likeCount })
|
|
setLikeFailed(Date.now())
|
|
}
|
|
;(on ? like : unlike).mutate(
|
|
{ path: { post_id: post.post_id } },
|
|
{
|
|
onSuccess: (res) =>
|
|
res.result?.success
|
|
? patch({ liked: res.liked ?? on, ...(res.like_count != null ? { like_count: res.like_count } : {}) })
|
|
: revert(),
|
|
onError: revert,
|
|
},
|
|
)
|
|
}
|
|
|
|
const onTap = (e: MouseEvent<HTMLDivElement>) => {
|
|
if ((e.target as HTMLElement).closest('button')) return
|
|
const now = Date.now()
|
|
if (now - lastTap.current < 320) {
|
|
if (liked) setPop((p) => p + 1)
|
|
else setLike(true)
|
|
}
|
|
lastTap.current = now
|
|
}
|
|
|
|
return (
|
|
<article className="mt-3.5 border-y border-(--line) bg-white" aria-label={post.title}>
|
|
<div className="flex items-center gap-2.5 px-3 py-2.5">
|
|
<span className="b-ring size-[34px] shrink-0 rounded-full p-0.5">
|
|
<i className="grid size-full place-items-center rounded-full bg-white">
|
|
<Ic name={avatar} className="w-[22px]" />
|
|
</i>
|
|
</span>
|
|
<span>
|
|
<b className="block text-sm leading-tight font-extrabold">날씨각</b>
|
|
<small className="text-xs font-semibold text-(--ink2)">{account}</small>
|
|
</span>
|
|
</div>
|
|
<div className="relative aspect-[4/5] overflow-hidden" onClick={onTap}>
|
|
{n > 1 && (
|
|
<span data-noshot className="absolute top-3 right-3 z-[2] rounded-full bg-[rgb(30_36_56/0.55)] px-[9px] py-[3px] text-xs font-extrabold text-white" aria-hidden="true">
|
|
{index + 1}/{n}
|
|
</span>
|
|
)}
|
|
<div
|
|
ref={sliderRef}
|
|
className="h-full overflow-hidden"
|
|
tabIndex={n > 1 ? 0 : undefined}
|
|
aria-label={n > 1 ? `슬라이드 ${n}장, 옆으로 넘겨 보세요` : undefined}
|
|
role={n > 1 ? 'region' : undefined}
|
|
onKeyDown={(e) => {
|
|
if (e.key === 'ArrowLeft') slider?.scrollPrev()
|
|
if (e.key === 'ArrowRight') slider?.scrollNext()
|
|
}}
|
|
>
|
|
<div ref={trackRef} className="flex h-full">
|
|
{post.slides.map((s, i) => (
|
|
<SlideView key={i} slide={s} bg={bgs[i]} img={slideImage(post, s)} />
|
|
))}
|
|
</div>
|
|
</div>
|
|
{n > 1 && <Arrows api={slider} prev={canPrev} next={canNext} />}
|
|
{/* 좋아요 효과·알림은 카드가 아니라 화면 위에 띄운다 */}
|
|
{pop > 0 && createPortal(
|
|
<div key={pop} className="pointer-events-none fixed inset-0 z-50 grid place-items-center" aria-hidden="true">
|
|
{BURST.map((b, k) => (
|
|
<svg key={k} className="b-like-bit absolute w-5" style={{ '--dx': `${b[0]}px`, '--dy': `${b[1]}px`, '--rot': `${b[2]}deg` } as CSSProperties} viewBox="0 0 24 24" fill={b[3]}>
|
|
<path d={HEART} />
|
|
</svg>
|
|
))}
|
|
<svg className="b-heart-pop w-28 opacity-0 drop-shadow-[0_10px_18px_rgb(255_59_92/0.45)]" viewBox="0 0 24 24" fill="url(#b-like-grad)">
|
|
<defs>
|
|
<linearGradient id="b-like-grad" x1="0" y1="0" x2="0" y2="1">
|
|
<stop offset="0" stopColor="#ff8aa0" />
|
|
<stop offset="1" stopColor="#ff3b5c" />
|
|
</linearGradient>
|
|
</defs>
|
|
<path d={HEART} />
|
|
</svg>
|
|
<span className="b-like-toast absolute top-[58%] rounded-full bg-white px-4 py-2 font-jua text-[17px] text-(--heart) opacity-0 shadow-[0_8px_18px_rgb(40_48_74/0.18)]">좋아요했어요!</span>
|
|
</div>,
|
|
document.body,
|
|
)}
|
|
{likeFailed > 0 && createPortal(
|
|
<p role="alert" className="pointer-events-none fixed inset-x-0 bottom-[calc(88px+env(safe-area-inset-bottom,0px))] z-50 m-0 flex justify-center px-4">
|
|
<span className="rounded-full bg-[rgb(30_36_56/0.82)] px-3.5 py-2 text-[13px] font-bold text-white">{liked ? '좋아요를 취소하지 못했어요' : '좋아요를 반영하지 못했어요'}. 다시 해 주세요.</span>
|
|
</p>,
|
|
document.body,
|
|
)}
|
|
</div>
|
|
<div className="flex items-center gap-3.5 px-3 pt-2.5 pb-1 [&_svg]:size-[26px] [&_svg]:stroke-[1.9]">
|
|
<button type="button" onClick={() => setLike(!liked)} aria-pressed={liked} aria-label="좋아요" className={cx('relative grid size-7 cursor-pointer place-items-center', TAP44)}>
|
|
<HeartIcon className={liked ? 'fill-(--heart) stroke-(--heart)' : 'fill-none stroke-(--ink)'} />
|
|
</button>
|
|
<button type="button" onClick={comments} aria-label="댓글 보기" className={cx('relative grid size-7 cursor-pointer place-items-center', TAP44)}>
|
|
<CommentIcon className="fill-none stroke-(--ink) [stroke-linejoin:round]" />
|
|
</button>
|
|
<button type="button" onClick={share} aria-label="보내기" className={cx('relative grid size-7 cursor-pointer place-items-center', TAP44)}>
|
|
<SendIcon className="fill-none stroke-(--ink) [stroke-linecap:round] [stroke-linejoin:round]" />
|
|
</button>
|
|
</div>
|
|
<p className="px-3 py-0.5 text-sm font-extrabold">좋아요 {likeCount.toLocaleString()}개</p>
|
|
<p className="px-3 py-0.5 text-sm">
|
|
<b className="mr-[5px] font-extrabold">날씨각</b>
|
|
{post.caption}
|
|
</p>
|
|
<button type="button" onClick={comments} className="block cursor-pointer px-3 pt-[3px] text-sm font-semibold text-(--ink3)">
|
|
{post.comment_count ? `댓글 ${post.comment_count}개 모두 보기` : '첫 댓글 남기기'}
|
|
</button>
|
|
<p className="px-3 pt-1 pb-3 text-[11.5px] font-semibold text-(--ink3)">오늘</p>
|
|
</article>
|
|
)
|
|
}
|
|
|
|
type FrameProps = {
|
|
title: string
|
|
account: string
|
|
avatar: string
|
|
caption?: ReactNode
|
|
more?: ReactNode
|
|
shot?: RefObject<HTMLDivElement | null>
|
|
pinned?: { key: PinnedKey; post?: PostReaction | null }
|
|
children: ReactNode
|
|
}
|
|
|
|
/** 피드 글과 같은 틀(계정 줄·본문·보내기 줄)에 날씨·각 같은 화면 조각을 담는다. 보내기는 shot(공유용 틀)이 있으면 그것을, 없으면 본문을 이미지로 */
|
|
export function PostFrame({ title, account, avatar, caption, more, shot, pinned, children }: FrameProps) {
|
|
const { open } = useSheet()
|
|
const bodyRef = useRef<HTMLDivElement>(null)
|
|
const like = usePinnedLike(pinned?.key ?? 'gak_post', pinned?.post)
|
|
const postId = pinned?.post?.post_id
|
|
const share = () => {
|
|
const el = shot?.current ?? bodyRef.current
|
|
if (el) open({ type: 'share', share: { title, slides: [el] } })
|
|
}
|
|
return (
|
|
<article className="mt-3.5 border-y border-(--line) bg-white first:mt-1" aria-label={title}>
|
|
<div className="flex items-center gap-2.5 px-3 py-2.5">
|
|
<span className="b-ring size-[34px] shrink-0 rounded-full p-0.5">
|
|
<i className="grid size-full place-items-center rounded-full bg-white">
|
|
<Ic name={avatar} className="w-[22px]" />
|
|
</i>
|
|
</span>
|
|
<span>
|
|
<b className="block text-sm leading-tight font-extrabold">날씨각</b>
|
|
<small className="text-xs font-semibold text-(--ink2)">{account}</small>
|
|
</span>
|
|
</div>
|
|
<div ref={bodyRef}>{children}</div>
|
|
<div className="flex items-center gap-3.5 px-3 pt-2.5 pb-1 [&_svg]:size-[26px] [&_svg]:stroke-[1.9]">
|
|
{postId != null && (
|
|
<>
|
|
<button type="button" onClick={like.toggle} aria-pressed={like.liked} aria-label="좋아요" className={cx('relative grid size-7 cursor-pointer place-items-center', TAP44)}>
|
|
<HeartIcon className={like.liked ? 'fill-(--heart) stroke-(--heart)' : 'fill-none stroke-(--ink)'} />
|
|
</button>
|
|
<button type="button" onClick={() => open({ type: 'comments', postId })} aria-label="댓글 보기" className={cx('relative grid size-7 cursor-pointer place-items-center', TAP44)}>
|
|
<CommentIcon className="fill-none stroke-(--ink) [stroke-linejoin:round]" />
|
|
</button>
|
|
</>
|
|
)}
|
|
<button type="button" onClick={share} aria-label="보내기" className={cx('relative grid size-7 cursor-pointer place-items-center', TAP44)}>
|
|
<SendIcon className="fill-none stroke-(--ink) [stroke-linecap:round] [stroke-linejoin:round]" />
|
|
</button>
|
|
{more && <span className="ml-auto text-[13px] font-bold text-(--ink2)">{more}</span>}
|
|
</div>
|
|
{postId != null && (
|
|
<p className="px-3 py-0.5 text-sm font-extrabold">
|
|
{like.failed ? <span className="text-(--heart)" role="alert">좋아요를 반영하지 못했어요. 다시 해 주세요.</span> : `좋아요 ${like.count.toLocaleString()}개`}
|
|
</p>
|
|
)}
|
|
{caption && (
|
|
<p className="px-3 pt-0.5 pb-3 text-sm">
|
|
<b className="mr-[5px] font-extrabold">날씨각</b>
|
|
{caption}
|
|
</p>
|
|
)}
|
|
{postId != null && (
|
|
<button type="button" onClick={() => open({ type: 'comments', postId })} className="block cursor-pointer px-3 pb-3 text-sm font-semibold text-(--ink3)">
|
|
{pinned?.post?.comment_count ? `댓글 ${pinned.post.comment_count}개 모두 보기` : '첫 댓글 남기기'}
|
|
</button>
|
|
)}
|
|
</article>
|
|
)
|
|
}
|