feat: 10/20 회의 반영 — 이야기 탭을 오늘 피드로 합침(글쓰기 칸·사진 올리기·위치 정보 삭제), 오늘의 각 카드와 히어로·각 공유 템플릿, 나만의 각을 이름+날씨 조건 저장으로, 로그인 없이 좋아요(IP 해시), 피드 순서·불쾌지수 상시·포스트 그림 배경, 새 각 아이콘
This commit is contained in:
1 parent
017022a0b0
commit
d0213f41a6
81 files changed
+3503
-593
No files matched your search
+89
-45
@@ -1,24 +1,26 @@
|
||||
import { useInfiniteQuery, useMutation, useQueryClient } from '@tanstack/react-query'
|
||||
import { useState, type FormEvent } from 'react'
|
||||
import { useMutation, useQueryClient } from '@tanstack/react-query'
|
||||
import { useEffect, useRef, useState, type ChangeEvent, type FormEvent } from 'react'
|
||||
import {
|
||||
addTalkMutation,
|
||||
blockTalkAuthorMutation,
|
||||
deleteTalkMutation,
|
||||
likeTalkMutation,
|
||||
listTalksInfiniteOptions,
|
||||
reportTalkMutation,
|
||||
unlikeTalkMutation,
|
||||
} from '../api/client/@tanstack/react-query.gen'
|
||||
import { uploadTalkImage } from '../api/client/sdk.gen'
|
||||
import { TalkReport, type TalkData } from '../api/client/types.gen'
|
||||
import { API_BASE_URL } from '../api/setup'
|
||||
import { cx } from '../lib/utils'
|
||||
import { useAuth } from './authContext'
|
||||
import { ago, kst, loadOf } from './lib'
|
||||
import { ago, kst } from './lib'
|
||||
import { useRegion } from './regionContext'
|
||||
import { useSheet } from './sheetContext'
|
||||
import { HeartIcon } from './svg'
|
||||
import { Avatar, Sec, StateBox, TAP44 } from './ui'
|
||||
import { Avatar, TAP44 } from './ui'
|
||||
|
||||
const BODY_MAX = 100
|
||||
const PHOTO_MAX = 10 * 1024 * 1024
|
||||
|
||||
// 서버 ErrorType 이름 → 안내
|
||||
const WRITE_ERRORS: Record<string, string> = {
|
||||
@@ -45,7 +47,7 @@ function useRefreshTalks() {
|
||||
return () => queryClient.invalidateQueries({ queryKey: [{ _id: 'listTalks' }] })
|
||||
}
|
||||
|
||||
function Composer() {
|
||||
function Composer({ onDone }: { onDone?: (t: TalkData) => void }) {
|
||||
const { region } = useRegion()
|
||||
const { loggedIn, me } = useAuth()
|
||||
const { open } = useSheet()
|
||||
@@ -53,22 +55,62 @@ function Composer() {
|
||||
const add = useMutation(addTalkMutation())
|
||||
const [text, setText] = useState('')
|
||||
const [error, setError] = useState<string | null>(null)
|
||||
const [photo, setPhoto] = useState<{ file: File; src: string } | null>(null)
|
||||
const [uploading, setUploading] = useState(false)
|
||||
const fileRef = useRef<HTMLInputElement>(null)
|
||||
const busy = add.isPending || uploading
|
||||
|
||||
useEffect(() => {
|
||||
if (!photo) return
|
||||
return () => URL.revokeObjectURL(photo.src)
|
||||
}, [photo])
|
||||
|
||||
const pick = (e: ChangeEvent<HTMLInputElement>) => {
|
||||
const file = e.target.files?.[0]
|
||||
e.target.value = ''
|
||||
if (!file) return
|
||||
if (file.size > PHOTO_MAX) return setError('사진은 10MB까지 올릴 수 있어요.')
|
||||
setError(null)
|
||||
setPhoto({ file, src: URL.createObjectURL(file) })
|
||||
}
|
||||
|
||||
// 사진이 있으면 먼저 올려 주소를 받고, 그 주소를 글에 붙인다
|
||||
const upload = async (): Promise<string | undefined> => {
|
||||
if (!photo) return undefined
|
||||
setUploading(true)
|
||||
try {
|
||||
const { data } = await uploadTalkImage({ body: { file: photo.file } })
|
||||
if (!data.result?.success || !data.image) throw new Error(data.result?.desc ?? 'upload')
|
||||
return data.image
|
||||
} finally {
|
||||
setUploading(false)
|
||||
}
|
||||
}
|
||||
|
||||
const submit = (e: FormEvent) => {
|
||||
e.preventDefault()
|
||||
if (!loggedIn) return open({ type: 'login' })
|
||||
const body = text.trim()
|
||||
if (!body || add.isPending) return
|
||||
if (!body || busy) return
|
||||
setError(null)
|
||||
add.mutate(
|
||||
{ body: { region_code: region.code, body } },
|
||||
{
|
||||
onSuccess: (res) => {
|
||||
if (!res.result?.success) return setError(WRITE_ERRORS[res.result?.desc ?? ''] ?? '글을 올리지 못했어요. 다시 해 주세요.')
|
||||
setText('')
|
||||
refresh()
|
||||
},
|
||||
onError: () => setError('글을 올리지 못했어요. 다시 해 주세요.'),
|
||||
upload().then(
|
||||
(image) =>
|
||||
add.mutate(
|
||||
{ body: { region_code: region.code, body, image } },
|
||||
{
|
||||
onSuccess: (res) => {
|
||||
if (!res.result?.success) return setError(WRITE_ERRORS[res.result?.desc ?? ''] ?? '글을 올리지 못했어요. 다시 해 주세요.')
|
||||
setText('')
|
||||
setPhoto(null)
|
||||
refresh()
|
||||
if (res.talk) onDone?.(res.talk)
|
||||
},
|
||||
onError: () => setError('글을 올리지 못했어요. 다시 해 주세요.'),
|
||||
},
|
||||
),
|
||||
(e: unknown) => {
|
||||
console.error('[talk] photo upload', e)
|
||||
setError('사진을 올리지 못했어요. 사진 파일인지 확인해 주세요.')
|
||||
},
|
||||
)
|
||||
}
|
||||
@@ -79,20 +121,32 @@ function Composer() {
|
||||
value={text}
|
||||
onChange={(e) => setText(e.target.value)}
|
||||
onFocus={() => !loggedIn && open({ type: 'login' })}
|
||||
readOnly={add.isPending}
|
||||
readOnly={busy}
|
||||
maxLength={BODY_MAX}
|
||||
rows={2}
|
||||
placeholder={`지금 ${region.name.split(' ').at(-1)} 날씨 어때요?`}
|
||||
aria-label="동네 이야기 쓰기"
|
||||
className="w-full resize-none rounded-2xl bg-[#f3f6fb] px-3 py-2.5 text-[14.5px]"
|
||||
/>
|
||||
{photo && (
|
||||
<div className="relative mt-2 w-[96px]">
|
||||
<img src={photo.src} alt="올릴 사진" className="aspect-[4/5] w-full rounded-xl object-cover" />
|
||||
<button type="button" onClick={() => setPhoto(null)} aria-label="사진 빼기" className="absolute -top-1.5 -right-1.5 grid size-6 cursor-pointer place-items-center rounded-full bg-(--ink) text-[13px] font-extrabold text-white">
|
||||
×
|
||||
</button>
|
||||
</div>
|
||||
)}
|
||||
<input ref={fileRef} type="file" accept="image/*" onChange={pick} hidden />
|
||||
<div className="mt-1.5 flex items-center gap-2 text-xs font-bold text-(--ink3)">
|
||||
<span>{me ? `${me.nickname} · ${region.name.split(' ').at(-1)} · 지금 날씨가 함께 올라가요` : '로그인하면 쓸 수 있어요'}</span>
|
||||
<button type="button" onClick={() => (loggedIn ? fileRef.current?.click() : open({ type: 'login' }))} disabled={busy} className="shrink-0 cursor-pointer rounded-full bg-[#eef4fc] px-3 py-1.5 text-[13px] font-extrabold text-(--ink2) disabled:opacity-40">
|
||||
사진
|
||||
</button>
|
||||
<span className="min-w-0 truncate">{me ? `${me.nickname} · ${region.name.split(' ').at(-1)} · 지금 날씨가 함께 올라가요` : '로그인하면 쓸 수 있어요'}</span>
|
||||
<span className="ml-auto tabular-nums">
|
||||
{text.length}/{BODY_MAX}
|
||||
</span>
|
||||
<button type="submit" disabled={add.isPending || (loggedIn && !text.trim())} className="cursor-pointer rounded-full bg-(--orange) px-3.5 py-1.5 text-[13px] font-extrabold text-white disabled:opacity-40">
|
||||
{add.isPending ? '올리는 중…' : '올리기'}
|
||||
<button type="submit" disabled={busy || (loggedIn && !text.trim())} className="shrink-0 cursor-pointer rounded-full bg-(--orange) px-3.5 py-1.5 text-[13px] font-extrabold text-white disabled:opacity-40">
|
||||
{busy ? '올리는 중…' : '올리기'}
|
||||
</button>
|
||||
</div>
|
||||
{error && (
|
||||
@@ -119,7 +173,6 @@ function TalkRow({ t }: { t: TalkData }) {
|
||||
const [failed, setFailed] = useState(false)
|
||||
|
||||
const toggle = () => {
|
||||
if (!loggedIn) return open({ type: 'login' })
|
||||
if (like.isPending || unlike.isPending) return
|
||||
const next = !liked
|
||||
setLiked(next)
|
||||
@@ -160,6 +213,7 @@ function TalkRow({ t }: { t: TalkData }) {
|
||||
{[t.region_name, slotWord(t.created_at), t.weather].filter(Boolean).join(' · ')}
|
||||
</small>
|
||||
<p className="m-0 mt-0.5 text-[14.5px] break-words whitespace-pre-wrap">{t.body}</p>
|
||||
{t.image && <img src={t.image.startsWith('/v1/') ? API_BASE_URL + t.image : t.image} alt="" loading="lazy" className="mt-2 aspect-[4/5] w-full rounded-2xl bg-[#eef4fc] object-cover" />}
|
||||
<small className="mt-0.5 block text-xs font-bold text-(--ink3)">
|
||||
{ago(t.created_at)} · 좋아요 {count}개 ·{' '}
|
||||
<button type="button" onClick={() => setMenu(menu === 'closed' ? 'open' : 'closed')} aria-expanded={menu !== 'closed'} className="cursor-pointer font-bold text-(--ink3)">
|
||||
@@ -206,30 +260,20 @@ function TalkRow({ t }: { t: TalkData }) {
|
||||
}
|
||||
|
||||
/** 이야기 탭: 쓰기 칸과 전국 최신 글 (아래로 더 받기) */
|
||||
export function TalkPage() {
|
||||
const list = useInfiniteQuery({
|
||||
...listTalksInfiniteOptions(),
|
||||
initialPageParam: {},
|
||||
getNextPageParam: (last) => last.next_cursor ?? undefined,
|
||||
})
|
||||
const talks = list.data?.pages.flatMap((p) => p.talks ?? []) ?? []
|
||||
const load = loadOf(list)
|
||||
/** 피드 위 쓰는 칸. 늘 펼쳐 두고, 방금 올린 글은 바로 아래에 보인다 (피드 사이 글은 새로 받을 때 섞인다) */
|
||||
export function FeedComposer({ onPosted }: { onPosted: (t: TalkData) => void }) {
|
||||
return (
|
||||
<section aria-label="동네 이야기" aria-busy={load === 'loading'}>
|
||||
<Sec title="지금 날씨 이야기" sub="전국 사람들이 쓰는 동네 날씨 이야기" />
|
||||
<Composer />
|
||||
<div className="relative mx-3 mt-2 min-h-[120px] rounded-[22px] bg-white px-3 py-1 shadow-[inset_0_-5px_0_var(--shade)]">
|
||||
{talks.length ? (
|
||||
talks.map((t) => <TalkRow key={`${t.talk_id}:${t.liked}:${t.like_count}`} t={t} />)
|
||||
) : (
|
||||
<StateBox load={load} onRetry={() => list.refetch()} empty="아직 글이 없어요. 첫 이야기를 남겨 보세요." className="inset-0" />
|
||||
)}
|
||||
{list.hasNextPage && (
|
||||
<button type="button" onClick={() => list.fetchNextPage()} disabled={list.isFetchingNextPage} className="w-full cursor-pointer border-t border-(--line) py-2.5 text-center text-sm font-bold text-(--ink2) disabled:opacity-50">
|
||||
{list.isFetchingNextPage ? '불러오는 중…' : '더 보기'}
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
</section>
|
||||
<div className="mt-3.5">
|
||||
<Composer onDone={onPosted} />
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
/** 피드에 끼우는 글 한 장 */
|
||||
export function TalkCard({ t }: { t: TalkData }) {
|
||||
return (
|
||||
<article className="mt-3.5 border-y border-(--line) bg-white px-3" aria-label={`${t.nickname}의 글`}>
|
||||
<TalkRow key={`${t.talk_id}:${t.liked}:${t.like_count}`} t={t} />
|
||||
</article>
|
||||
)
|
||||
}
|
||||
Reference in new issue
Block a user