Files
o2o-weather/mobile/src/app/talk.tsx
T

72 lines
3.7 KiB
TypeScript

import type { TalkTopic } from '@nalssigak/shared/api/client/types.gen';
import { router, useLocalSearchParams } from 'expo-router';
import { FlatList, Pressable, RefreshControl, ScrollView, View } from 'react-native';
import { ScreenHeader } from '@/components/screen-header';
import { Composer, TalkCard } from '@/components/talk/talk';
import { Text } from '@/components/text';
import { Sec } from '@/components/ui';
import { useAuth } from '@/lib/auth';
import { useSheet } from '@/lib/sheet';
import { openTopics, TOPICS, useMyTalks, useTopicTalks } from '@/lib/talk';
type Tab = TalkTopic | 'all' | 'mine';
/** 주제별 사람 글: 위 주제 탭(한 줄 가로로 밀기, 끝에 '내 글'), 아래 글 목록. ?topic= · ?mine=1 로 연다 (웹 TalkPage) */
export default function TalkScreen() {
const params = useLocalSearchParams<{ topic?: string; mine?: string }>();
const { loggedIn } = useAuth();
const { open } = useSheet();
const raw = Number(params.topic);
const tab: Tab = params.mine ? 'mine' : TOPICS.some(([k]) => k === raw) ? (raw as TalkTopic) : 'all';
const topicList = useTopicTalks(tab === 'all' || tab === 'mine' ? undefined : tab);
const mineList = useMyTalks(tab === 'mine' && loggedIn);
const { talks, loaded, more, loadMore } = tab === 'mine' ? mineList : topicList;
const tabs: (readonly [Tab, string])[] = [['all', '전체'], ...openTopics(), ['mine', '내 글']];
const go = (k: Tab) => router.setParams(k === 'mine' ? { mine: '1', topic: undefined } : k === 'all' ? { mine: undefined, topic: undefined } : { mine: undefined, topic: String(k) });
const head = (
<View>
<Sec title="동네 사람들 글" sub="가까운 동네 글부터 보여요" />
<View accessibilityRole="tablist" accessibilityLabel="글 주제">
<ScrollView horizontal showsHorizontalScrollIndicator={false} contentContainerClassName="gap-1.5 px-4 pb-1">
{tabs.map(([k, label]) => (
<Pressable key={label} onPress={() => go(k)} accessibilityRole="tab" accessibilityState={{ selected: tab === k }} className={`rounded-full px-3 py-1.5 ${tab === k ? 'bg-ink-btn' : 'bg-card'}`}>
<Text className={`text-[13px] font-extrabold ${tab === k ? 'text-white' : 'text-ink2'}`}>{label}</Text>
</Pressable>
))}
</ScrollView>
</View>
<Composer />
{tab === 'mine' && !loggedIn ? (
<Text className="mx-4 mt-6 text-center text-[14px] font-semibold text-ink3">
로그인하면 내가 쓴 글을 모아 볼 수 있어요.{' '}
<Text onPress={() => open({ type: 'login' })} className="font-extrabold text-orange">
로그인
</Text>
</Text>
) : (
loaded && !talks.length && <Text className="mx-4 mt-6 text-center text-[14px] font-semibold text-ink3">{tab === 'mine' ? '아직 쓴 글이 없어요. 위 칸에서 첫 글을 써 주세요.' : '아직 이 주제 글이 없어요. 위 칸에서 첫 글을 써 주세요.'}</Text>
)}
</View>
);
return (
<View className="flex-1">
<ScreenHeader title="동네 이야기" />
<FlatList
data={talks}
keyExtractor={(t) => `${t.talk_id}:${t.liked}:${t.like_count}`}
renderItem={({ item }) => <TalkCard t={item} />}
ListHeaderComponent={head}
contentContainerClassName="pb-8"
keyboardShouldPersistTaps="handled"
// 목록 끝이 가까워지면 다음 쪽을 받는다 (글이 많아도 버튼 없이 이어서)
onEndReached={() => more && loadMore()}
onEndReachedThreshold={1}
refreshControl={tab !== 'mine' ? <RefreshControl refreshing={topicList.refetching} onRefresh={() => topicList.refetch()} /> : undefined}
/>
</View>
);
}