feat: 앱 나머지 화면 이전 — 게시물 카드·시트·각/노을/주말/계절 탭·움직임
- 게시물 카드 슬라이드 7종, 좋아요·댓글, 텍스트 공유 - 시트 7종(RN Modal), 위 막대 지역·계절·로그인/아바타 - 각·노을·주말·행사·계절·상식 탭, 당겨서 새로고침 - 히어로 해·구름·비·눈 움직임, 스켈레톤 깜빡임, 첫 로딩 덮개, 하늘색 전환
This commit is contained in:
1 parent
28ff032160
commit
f71dd2cebb
33 files changed
+2786
-75
No files matched your search
@@ -0,0 +1,222 @@
|
||||
import {
|
||||
addCommentMutation,
|
||||
deleteCommentMutation,
|
||||
getFeedQueryKey,
|
||||
likeCommentMutation,
|
||||
listCommentsInfiniteOptions,
|
||||
listCommentsInfiniteQueryKey,
|
||||
unlikeCommentMutation,
|
||||
} from '@nalssigak/shared/api/client/@tanstack/react-query.gen';
|
||||
import type { CommentData, ResCommentList } from '@nalssigak/shared/api/client/types.gen';
|
||||
import { ago, loadOf } from '@nalssigak/shared/lib';
|
||||
import { useInfiniteQuery, useMutation, useQueryClient, type InfiniteData } from '@tanstack/react-query';
|
||||
import { useRef, useState } from 'react';
|
||||
import { Pressable, Text, TextInput, View } from 'react-native';
|
||||
import { useCSSVariable } from 'uniwind';
|
||||
|
||||
import { Avatar, Skel, StateBox } from '@/components/ui';
|
||||
import { useAuth } from '@/lib/auth';
|
||||
import { useRegion } from '@/lib/region';
|
||||
import { useSheet } from '@/lib/sheet';
|
||||
|
||||
import { Body, ErrorLine, Field, HeartIcon } from './parts';
|
||||
|
||||
const NOT_FOUND = 3001; // 서버 ErrorType.NOT_FOUND
|
||||
const EMOJIS = ['☀️', '🌂', '🍻', '🌇', '😍', '👍'];
|
||||
const N3 = [0, 1, 2];
|
||||
|
||||
function CommentsSkel() {
|
||||
return (
|
||||
<View>
|
||||
{N3.map((i) => (
|
||||
<View key={i} className="flex-row items-start gap-2.5 py-2">
|
||||
<View className="size-[34px] rounded-full bg-skel" />
|
||||
<View className="flex-1 gap-1.5">
|
||||
<Skel className="w-3/4" />
|
||||
<Skel className="w-24" />
|
||||
</View>
|
||||
</View>
|
||||
))}
|
||||
</View>
|
||||
);
|
||||
}
|
||||
|
||||
function CommentRow({ c, postId }: { c: CommentData; postId: number }) {
|
||||
const { region } = useRegion();
|
||||
const { loggedIn } = useAuth();
|
||||
const { open } = useSheet();
|
||||
const queryClient = useQueryClient();
|
||||
const [heart, ink3] = useCSSVariable(['--color-heart', '--color-ink3']).map(String);
|
||||
const [liked, setLiked] = useState(c.liked);
|
||||
const [count, setCount] = useState(c.like_count);
|
||||
const like = useMutation(likeCommentMutation());
|
||||
const unlike = useMutation(unlikeCommentMutation());
|
||||
const remove = useMutation(deleteCommentMutation());
|
||||
const [removeFailed, setRemoveFailed] = useState(false);
|
||||
const [likeFailed, setLikeFailed] = useState(false);
|
||||
|
||||
const toggle = () => {
|
||||
if (!loggedIn) return open({ type: 'login' });
|
||||
if (like.isPending || unlike.isPending) return;
|
||||
const next = !liked;
|
||||
setLikeFailed(false);
|
||||
setLiked(next);
|
||||
setCount((n) => n + (next ? 1 : -1));
|
||||
const revert = () => {
|
||||
setLiked(!next);
|
||||
setCount(c.like_count);
|
||||
};
|
||||
(next ? like : unlike).mutate(
|
||||
{ path: { comment_id: c.comment_id } },
|
||||
{
|
||||
onSuccess: (res) => {
|
||||
if (res.result?.success) {
|
||||
setLiked(!!res.liked);
|
||||
if (res.like_count != null) setCount(res.like_count);
|
||||
return;
|
||||
}
|
||||
revert();
|
||||
// 그 사이 지워진 댓글이면 알림 없이 목록을 다시 받아 사라지게 한다
|
||||
if (res.result?.code === NOT_FOUND) queryClient.invalidateQueries({ queryKey: listCommentsInfiniteQueryKey({ path: { post_id: postId } }) });
|
||||
else setLikeFailed(true);
|
||||
},
|
||||
onError: () => {
|
||||
revert();
|
||||
setLikeFailed(true);
|
||||
},
|
||||
},
|
||||
);
|
||||
};
|
||||
|
||||
// 지우는 동안은 흐리게 두고, 성공하면 다시 받기 전에 목록 캐시에서 바로 뺀다
|
||||
const del = () => {
|
||||
setRemoveFailed(false);
|
||||
remove.mutate(
|
||||
{ path: { comment_id: c.comment_id } },
|
||||
{
|
||||
onSuccess: async (res) => {
|
||||
if (!res.result?.success) return setRemoveFailed(true);
|
||||
const key = listCommentsInfiniteQueryKey({ path: { post_id: postId } });
|
||||
await queryClient.cancelQueries({ queryKey: key });
|
||||
queryClient.setQueryData<InfiniteData<ResCommentList>>(key, (data) =>
|
||||
data ? { ...data, pages: data.pages.map((p) => ({ ...p, comments: p.comments?.filter((x) => x.comment_id !== c.comment_id) })) } : data,
|
||||
);
|
||||
queryClient.invalidateQueries({ queryKey: key });
|
||||
queryClient.invalidateQueries({ queryKey: getFeedQueryKey({ query: { region: region.code } }) });
|
||||
},
|
||||
onError: () => setRemoveFailed(true),
|
||||
},
|
||||
);
|
||||
};
|
||||
|
||||
return (
|
||||
<View className={`flex-row items-start gap-2.5 py-2 ${remove.isPending ? 'opacity-50' : ''}`}>
|
||||
<Avatar character={c.character} color={c.color} mood={c.mood} />
|
||||
<View className="flex-1">
|
||||
<Text className="text-sm text-ink">
|
||||
<Text className="font-extrabold">{c.nickname} </Text>
|
||||
{c.body}
|
||||
</Text>
|
||||
<Text className="mt-0.5 text-xs font-bold text-ink3">
|
||||
{ago(c.created_at)} · {likeFailed ? <Text className="text-heart">좋아요를 못 했어요. 잠시 뒤에 다시 눌러 주세요</Text> : `좋아요 ${count}개`}
|
||||
{c.mine && (
|
||||
<>
|
||||
{' · '}
|
||||
<Text onPress={remove.isPending ? undefined : del} className={removeFailed && !remove.isPending ? 'text-heart' : 'text-ink3'}>
|
||||
{remove.isPending ? '지우는 중…' : removeFailed ? '못 지웠어요 · 다시' : '지우기'}
|
||||
</Text>
|
||||
</>
|
||||
)}
|
||||
</Text>
|
||||
</View>
|
||||
<Pressable onPress={toggle} hitSlop={12} accessibilityRole="button" accessibilityState={{ selected: liked }} accessibilityLabel="댓글 좋아요" className="pt-1">
|
||||
<HeartIcon size={16} fill={liked ? heart : 'none'} stroke={liked ? heart : ink3} />
|
||||
</Pressable>
|
||||
</View>
|
||||
);
|
||||
}
|
||||
|
||||
export function CommentsSheet({ postId, draft }: { postId: number; draft?: string }) {
|
||||
const { region } = useRegion();
|
||||
const { open } = useSheet();
|
||||
const { loggedIn, me } = useAuth();
|
||||
const queryClient = useQueryClient();
|
||||
const list = useInfiniteQuery({
|
||||
...listCommentsInfiniteOptions({ path: { post_id: postId } }),
|
||||
initialPageParam: { path: { post_id: postId } },
|
||||
getNextPageParam: (last) => last.next_cursor ?? undefined,
|
||||
});
|
||||
const add = useMutation(addCommentMutation());
|
||||
const [addFailed, setAddFailed] = useState(false);
|
||||
const [text, setText] = useState(draft ?? '');
|
||||
const inputRef = useRef<TextInput>(null);
|
||||
const comments = list.data?.pages.flatMap((p) => p.comments ?? []) ?? [];
|
||||
|
||||
const submit = () => {
|
||||
if (!loggedIn) return open({ type: 'login', then: { type: 'comments', postId, draft: text } });
|
||||
const body = text.trim();
|
||||
if (!body || add.isPending) return;
|
||||
setAddFailed(false);
|
||||
add.mutate(
|
||||
{ path: { post_id: postId }, body: { body } },
|
||||
{
|
||||
onSuccess: (res) => {
|
||||
setAddFailed(!res.result?.success);
|
||||
if (!res.result?.success) return;
|
||||
setText('');
|
||||
queryClient.invalidateQueries({ queryKey: listCommentsInfiniteQueryKey({ path: { post_id: postId } }) });
|
||||
queryClient.invalidateQueries({ queryKey: getFeedQueryKey({ query: { region: region.code } }) });
|
||||
},
|
||||
onError: () => setAddFailed(true),
|
||||
},
|
||||
);
|
||||
};
|
||||
|
||||
return (
|
||||
<>
|
||||
<Body>
|
||||
{comments.length ? (
|
||||
<>
|
||||
{/* 다시 받은 좋아요 상태(로그인 바뀜 등)가 행에 반영되도록 key 에 넣는다 */}
|
||||
{comments.map((c) => (
|
||||
<CommentRow key={`${c.comment_id}:${c.liked}:${c.like_count}`} c={c} postId={postId} />
|
||||
))}
|
||||
{list.hasNextPage && (
|
||||
<Pressable onPress={() => list.fetchNextPage()} disabled={list.isFetchingNextPage} className="my-2 disabled:opacity-50">
|
||||
<Text className="text-center text-sm font-bold text-ink3">{list.isFetchingNextPage ? '불러오는 중…' : '댓글 더 보기'}</Text>
|
||||
</Pressable>
|
||||
)}
|
||||
</>
|
||||
) : loadOf(list) === 'loading' ? (
|
||||
<CommentsSkel />
|
||||
) : (
|
||||
<StateBox load={loadOf(list)} onRetry={() => list.refetch()} empty="아직 댓글이 없어요. 첫 댓글을 남겨 보세요." />
|
||||
)}
|
||||
</Body>
|
||||
<View className="flex-row justify-around pt-1" accessibilityLabel="빠른 이모지">
|
||||
{EMOJIS.map((e) => (
|
||||
<Pressable
|
||||
key={e}
|
||||
disabled={add.isPending}
|
||||
className="disabled:opacity-40"
|
||||
onPress={() => {
|
||||
setText((t) => (t + e).slice(0, 60));
|
||||
inputRef.current?.focus();
|
||||
}}
|
||||
>
|
||||
<Text className="text-[22px]">{e}</Text>
|
||||
</Pressable>
|
||||
))}
|
||||
</View>
|
||||
<ErrorLine className="h-4 text-xs leading-4" text={addFailed ? '댓글을 올리지 못했어요. 다시 해 주세요.' : undefined} />
|
||||
<View className="flex-row items-center gap-2.5 border-t border-line px-3 py-2.5">
|
||||
<View className="flex-1">
|
||||
<Field ref={inputRef} round value={text} onChangeText={setText} editable={!add.isPending} maxLength={60} placeholder={me ? `${me.nickname}(으)로 댓글 달기…` : '댓글 달기…'} accessibilityLabel="댓글" returnKeyType="send" onSubmitEditing={submit} />
|
||||
</View>
|
||||
<Pressable onPress={submit} disabled={add.isPending || (loggedIn && !text.trim())} className="disabled:opacity-40">
|
||||
<Text className="font-extrabold text-rain">{add.isPending ? '올리는 중…' : '게시'}</Text>
|
||||
</Pressable>
|
||||
</View>
|
||||
</>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user