feat: 앱 시트·로그인 상태·지역 저장 틀, 공통 UI(Avatar·Chips·Rows·Credit)

This commit is contained in:
minaChoi committed 2026-10-14 14:29:20 +09:00
1 parent e52f93fdbb
commit 28ff032160
8 files changed
+202 -15

No files matched your search

+37
View File
@@ -0,0 +1,37 @@
import { clearTokens, getAccessToken, subscribeAuth } from '@nalssigak/shared/api/auth';
import { meOptions } from '@nalssigak/shared/api/client/@tanstack/react-query.gen';
import { logout as logoutOnServer } from '@nalssigak/shared/api/client/sdk.gen';
import type { Me } from '@nalssigak/shared/api/client/types.gen';
import { useQuery, useQueryClient } from '@tanstack/react-query';
import { createContext, use, useEffect, useMemo, useSyncExternalStore, type ReactNode } from 'react';
/** mePending: 토큰은 있고 내 정보를 받는 중 (아바타 자리를 비워 두지 않으려고) */
export type AuthApi = { loggedIn: boolean; me: Me | null; mePending: boolean; logout: () => void };
const AuthContext = createContext<AuthApi | null>(null);
export function useAuth() {
const ctx = use(AuthContext);
if (!ctx) throw new Error('useAuth must be used inside AuthProvider');
return ctx;
}
const subscribe = (cb: () => void) => subscribeAuth((reason) => reason === 'change' && cb());
/** 서버에서 이 계정의 토큰을 모두 무효로 만들고(실패해도) 기기 토큰을 지운다 */
const logout = () => {
void logoutOnServer({ throwOnError: false }).finally(clearTokens);
};
export function AuthProvider({ children }: { children: ReactNode }) {
const token = useSyncExternalStore(subscribe, getAccessToken);
const queryClient = useQueryClient();
const me = useQuery({ ...meOptions(), enabled: !!token });
// 로그인·로그아웃하면 좋아요 여부가 달라지니 받아 둔 데이터를 다시 받는다
useEffect(() => subscribeAuth((reason) => reason === 'change' && queryClient.invalidateQueries()), [queryClient]);
const mePending = !!token && me.isPending;
const api = useMemo(() => ({ loggedIn: !!token, me: (token && me.data?.me) || null, mePending, logout }), [token, me.data, mePending]);
return <AuthContext value={api}>{children}</AuthContext>;
}
+24 -3
View File
@@ -1,6 +1,9 @@
import type { RegionData } from '@nalssigak/shared/api/client/types.gen';
import { DEFAULT_REGION } from '@nalssigak/shared/lib';
import { createContext, use, useMemo, useState, type ReactNode } from 'react';
import * as SecureStore from 'expo-secure-store';
import { createContext, use, useCallback, useMemo, useState, type ReactNode } from 'react';
const KEY = 'nalssigak.region';
export type RegionApi = { region: RegionData; setRegion: (r: RegionData) => void };
@@ -12,8 +15,26 @@ export function useRegion() {
return ctx;
}
function readStored(): RegionData | null {
try {
const raw = SecureStore.getItem(KEY);
return raw ? (JSON.parse(raw) as RegionData) : null;
} catch (e) {
console.warn('저장한 지역을 읽지 못함', e);
return null;
}
}
export function RegionProvider({ children }: { children: ReactNode }) {
const [region, setRegion] = useState<RegionData>(DEFAULT_REGION);
const api = useMemo(() => ({ region, setRegion }), [region]);
const [region, setRegionState] = useState<RegionData>(() => readStored() ?? DEFAULT_REGION);
const setRegion = useCallback((r: RegionData) => {
setRegionState(r);
try {
SecureStore.setItem(KEY, JSON.stringify(r));
} catch (e) {
console.warn('지역을 저장하지 못함', e);
}
}, []);
const api = useMemo(() => ({ region, setRegion }), [region, setRegion]);
return <RegionContext value={api}>{children}</RegionContext>;
}
+23
View File
@@ -0,0 +1,23 @@
import { createContext, use } from 'react';
type CommentsState = { type: 'comments'; postId: number; draft?: string };
export type SheetState =
| { type: 'gak'; indexId: number }
| { type: 'article'; articleId: number }
| CommentsState
| { type: 'seasonWeek' }
| { type: 'login'; then?: CommentsState }
| { type: 'me' }
| { type: 'region' }
| null;
export type SheetApi = { open: (s: Exclude<SheetState, null>) => void; close: () => void };
export const SheetContext = createContext<SheetApi | null>(null);
export function useSheet() {
const ctx = use(SheetContext);
if (!ctx) throw new Error('useSheet must be used inside SheetProvider');
return ctx;
}