import { clearTokens } from '@nalssigak/shared/api/auth';
import { listBlockedOptions, listBlockedQueryKey, meQueryKey, unblockMutation, updateMeMutation, withdrawMutation } from '@nalssigak/shared/api/client/@tanstack/react-query.gen';
import { AVATAR_COLORS, CHARACTERS, MOODS } from '@nalssigak/shared/lib';
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
import { useState } from 'react';
import { Pressable, View } from 'react-native';
import { Text } from '@/components/text';
import { Avatar, Ic } from '@/components/ui';
import { useAuth } from '@/lib/auth';
import { isCommentList } from './comments';
import { Body, Field, useSoft } from './parts';
/** 탈퇴: 한 번 더 묻고 지운다. 계정이 없어지니 이 기기 토큰도 지운다 */
function Withdraw({ onDone }: { onDone: () => void }) {
const withdraw = useMutation(withdrawMutation());
const [asking, setAsking] = useState(false);
const [failed, setFailed] = useState(false);
if (!asking)
return (
setAsking(true)} hitSlop={8} className="self-center p-1">
탈퇴하기
);
const run = () => {
setFailed(false);
withdraw.mutate(
{},
{
onSuccess: (res) => {
if (!res.result?.success) return setFailed(true);
clearTokens();
onDone();
},
onError: () => setFailed(true),
},
);
};
return (
{'탈퇴하면 계정과 남긴 댓글·좋아요가\n모두 지워져요. 되돌릴 수 없어요.'}
setAsking(false)} disabled={withdraw.isPending} className="flex-1 rounded-[14px] bg-card p-3">
그만둘게요
탈퇴할게요
{failed && 탈퇴하지 못했어요. 잠시 뒤에 다시 해 주세요.}
);
}
/** 차단한 사람: 있을 때만 보이고, 풀면 그 사람 댓글이 다시 보인다 */
function Blocked() {
const s = useSoft();
const queryClient = useQueryClient();
const list = useQuery(listBlockedOptions());
const unblock = useMutation(unblockMutation());
const [failed, setFailed] = useState(false);
const users = list.data?.users ?? [];
if (!users.length) return null;
const release = (user_id: string) => {
setFailed(false);
unblock.mutate(
{ path: { user_id } },
{
onSuccess: (res) => {
if (!res.result?.success) return setFailed(true);
queryClient.invalidateQueries({ queryKey: listBlockedQueryKey() });
queryClient.invalidateQueries({ predicate: isCommentList });
},
onError: () => setFailed(true),
},
);
};
return (
차단한 사람
{users.map((u) => (
{u.nickname}
release(u.user_id)} disabled={unblock.isPending} className="rounded-[12px] px-3 py-2 disabled:opacity-50" style={{ backgroundColor: s.soft }}>
풀기
))}
{failed && 잠시 뒤에 다시 해 주세요.}
);
}
/** 내 정보: 캐릭터·배경색·기분·닉네임·로그아웃·차단한 사람·탈퇴 */
export function MeSheet({ onDone }: { onDone: () => void }) {
const { me, logout } = useAuth();
const s = useSoft();
const queryClient = useQueryClient();
const update = useMutation(updateMeMutation());
const [nickname, setNickname] = useState(me?.nickname ?? '');
const [error, setError] = useState('');
if (!me) return null;
const save = (body: { nickname?: string; character?: string; color?: string; mood?: string }) => {
setError('');
update.mutate(
{ body },
{
onSuccess: (res) => {
if (!res.result?.success) return setError(res.result?.desc === 'INVALID_INPUT' ? '닉네임은 2~12자로 써 주세요.' : '잠시 뒤에 다시 해 주세요.');
queryClient.setQueryData(meQueryKey(), res);
// 댓글에 보이는 내 캐릭터·닉네임도 바뀐다
queryClient.invalidateQueries({ predicate: (q) => (q.queryKey[0] as { _id?: string } | undefined)?._id === 'listComments' });
},
onError: () => setError('서버에 닿지 못했어요. 잠시 뒤에 다시 해 주세요.'),
},
);
};
// 저장하는 동안은 고른 값을 바로 선택으로 보여 준다. 실패하면 me 값으로 돌아간다
const saving = update.isPending ? update.variables?.body : undefined;
const character = saving?.character ?? me.character;
const color = saving?.color ?? me.color;
const mood = saving?.mood ?? me.mood;
const nick = nickname.trim();
return (
{me.login_id}(으)로 로그인했어요
내 캐릭터
{CHARACTERS.map(([key, label]) => (
save({ character: key })}
className={`items-center gap-1 rounded-[18px] border-2 py-2.5 ${character === key ? 'border-orange' : 'border-transparent'}`}
style={{ width: '31.5%', backgroundColor: s.field }}
>
{label}
))}
배경색
{Object.entries(AVATAR_COLORS).map(([key, hex]) => (
save({ color: key })}
className={`size-9 rounded-full border-[3px] ${color === key ? 'border-ink' : 'border-white'}`}
style={{ backgroundColor: hex }}
/>
))}
오늘 기분
{MOODS.map(([key, emoji, label]) => (
save({ mood: mood === key ? '' : key })}
className={`items-center gap-0.5 rounded-[14px] border-2 py-2 ${mood === key ? 'border-orange' : 'border-transparent'}`}
style={{ width: '23%', backgroundColor: s.field }}
>
{emoji}
{label}
))}
한 번 더 누르면 지워져요
nick.length >= 2 && save({ nickname: nick })} />
save({ nickname: nick })} disabled={update.isPending || nick === me.nickname || nick.length < 2} className="rounded-[14px] bg-ink-btn px-4 py-3 disabled:opacity-40">
{saving?.nickname != null ? '저장 중…' : '바꾸기'}
{saving ? (saving.nickname == null ? '저장 중…' : '') : error}
{
logout();
onDone();
}}
className="rounded-[14px] p-[13px]"
style={{ backgroundColor: s.soft }}
>
로그아웃
);
}