260702_01_ENH: 리스트 작업 도중 공용 ui를 적용하기 위한 중간 커밋
This commit is contained in:
@@ -1,42 +1,82 @@
|
||||
"use client";
|
||||
|
||||
import { useCallback } from "react";
|
||||
import { useAtom } from "jotai";
|
||||
import { authStateAtom, type AuthState } from "@/store/jotai/auth";
|
||||
import { createAuthHook, type AuthState as CommonAuthState } from "@kri/common/hooks";
|
||||
import { getLocalStorage, removeLocalStorage, setLocalStorage } from "@/store/storage/secure-storage";
|
||||
import { StorageKeys } from "@/store/storage/storage-keys";
|
||||
import type { LoginUser } from "@/types/auth";
|
||||
|
||||
export const useAuth = () => {
|
||||
const [auth, setAuth] = useAtom(authStateAtom);
|
||||
type ContactAuthExtra = {
|
||||
seatNumber: string | null;
|
||||
};
|
||||
|
||||
// 로그인 성공 후 LoginResponse 를 바로 넣어줄 수 있게
|
||||
const setFromLoginResponse = useCallback(
|
||||
(res: LoginUser, loginSeatNumber:string|null = null) => {
|
||||
setAuth({
|
||||
user: res,
|
||||
seatNumber: loginSeatNumber,
|
||||
});
|
||||
},
|
||||
[setAuth],
|
||||
export type AuthState = CommonAuthState<LoginUser, ContactAuthExtra>;
|
||||
|
||||
const initialAuthState: AuthState = {
|
||||
user: null,
|
||||
seatNumber: null,
|
||||
};
|
||||
|
||||
const loadInitialAuthState = (): AuthState => {
|
||||
const stored = getLocalStorage(StorageKeys.AuthState);
|
||||
|
||||
if (stored && typeof stored === "object" && "user" in stored) {
|
||||
return {
|
||||
...initialAuthState,
|
||||
...(stored as Partial<AuthState>),
|
||||
};
|
||||
}
|
||||
|
||||
return initialAuthState;
|
||||
};
|
||||
|
||||
const { authAtom, useAuth: useCommonAuth } = createAuthHook<LoginUser, ContactAuthExtra>({
|
||||
initialState: loadInitialAuthState(),
|
||||
});
|
||||
|
||||
export const authStateAtom = authAtom;
|
||||
|
||||
const persistAuth = (next: AuthState) => {
|
||||
if (next.user === null && next.seatNumber === null) {
|
||||
removeLocalStorage(StorageKeys.AuthState);
|
||||
return;
|
||||
}
|
||||
|
||||
setLocalStorage(StorageKeys.AuthState, next);
|
||||
};
|
||||
|
||||
export const useAuth = () => {
|
||||
const { auth, setAuth: setCommonAuth } = useCommonAuth();
|
||||
|
||||
const setAuth = useCallback(
|
||||
(next: AuthState | ((prev: AuthState) => AuthState)) => {
|
||||
setCommonAuth((prev) => {
|
||||
const resolved = typeof next === "function" ? next(prev) : next;
|
||||
persistAuth(resolved);
|
||||
return resolved;
|
||||
});
|
||||
},
|
||||
[setCommonAuth],
|
||||
);
|
||||
|
||||
const updateAuth = useCallback(
|
||||
(next: AuthState | ((prev: AuthState) => AuthState)) => {
|
||||
setAuth(next);
|
||||
},
|
||||
[setAuth],
|
||||
const setFromLoginResponse = useCallback(
|
||||
(res: LoginUser, loginSeatNumber: string | null = null) => {
|
||||
setAuth({
|
||||
user: res,
|
||||
seatNumber: loginSeatNumber,
|
||||
});
|
||||
},
|
||||
[setAuth],
|
||||
);
|
||||
|
||||
const logout = useCallback(() => {
|
||||
setAuth({
|
||||
user: null,
|
||||
seatNumber: null,
|
||||
});
|
||||
setAuth(initialAuthState);
|
||||
}, [setAuth]);
|
||||
|
||||
return {
|
||||
auth,
|
||||
setAuth: updateAuth,
|
||||
setAuth,
|
||||
setFromLoginResponse,
|
||||
logout,
|
||||
};
|
||||
}
|
||||
} as const;
|
||||
};
|
||||
|
||||
@@ -1,12 +0,0 @@
|
||||
import { useEffect, useState } from "react";
|
||||
|
||||
export function useDebouncedValue<T>(value: T, delayMs = 1200) {
|
||||
const [debounced, setDebounced] = useState(value);
|
||||
|
||||
useEffect(() => {
|
||||
const t = setTimeout(() => setDebounced(value), delayMs);
|
||||
return () => clearTimeout(t);
|
||||
}, [value, delayMs]);
|
||||
|
||||
return debounced;
|
||||
}
|
||||
@@ -1,48 +1,43 @@
|
||||
import { useAtom } from 'jotai'
|
||||
import {DialogId, openDialogIdsAtom} from "@/store/jotai/dialog";
|
||||
"use client";
|
||||
|
||||
import { useCallback } from "react";
|
||||
import { createDialogHook } from "@kri/common/hooks";
|
||||
import type { DialogId } from "@/store/jotai/dialog";
|
||||
|
||||
const { openDialogIdsAtom, useDialog: useCommonDialog } = createDialogHook<DialogId>();
|
||||
|
||||
export { openDialogIdsAtom };
|
||||
|
||||
export function useDialog() {
|
||||
const [openDialogIds, setOpenDialogIds] = useAtom(openDialogIdsAtom)
|
||||
const {
|
||||
openDialogIds,
|
||||
isDialogOpen,
|
||||
openDialog,
|
||||
closeDialog,
|
||||
closeAllDialogs,
|
||||
} = useCommonDialog();
|
||||
|
||||
/**
|
||||
* 다이얼로그 열기
|
||||
* @param dialogId 다이얼로그를 구분할 ID
|
||||
* @param onlyThis true 이면 기존 다이얼로그 전부 닫고 이 것만 연다.
|
||||
* false 이면 기존 배열에 추가한다.
|
||||
*/
|
||||
const showDialog = (dialogId: DialogId, onlyThis: boolean = true) => {
|
||||
setOpenDialogIds(prev => {
|
||||
const showDialog = useCallback(
|
||||
(dialogId: DialogId, onlyThis: boolean = true) => {
|
||||
if (onlyThis) {
|
||||
// 이 다이얼로그만 열기
|
||||
return [dialogId]
|
||||
closeAllDialogs();
|
||||
}
|
||||
|
||||
// 이미 열려 있으면 그대로
|
||||
if (prev.includes(dialogId)) {
|
||||
return prev
|
||||
}
|
||||
openDialog(dialogId);
|
||||
},
|
||||
[closeAllDialogs, openDialog],
|
||||
);
|
||||
|
||||
// 여러 개 동시에 열기 허용
|
||||
return [...prev, dialogId]
|
||||
})
|
||||
}
|
||||
const hideDialog = useCallback(
|
||||
(dialogId: DialogId) => {
|
||||
closeDialog(dialogId);
|
||||
},
|
||||
[closeDialog],
|
||||
);
|
||||
|
||||
/**
|
||||
* 다이얼로그 닫기
|
||||
*/
|
||||
const hideDialog = (dialogId: DialogId) => {
|
||||
setOpenDialogIds(prev => prev.filter(id => id !== dialogId))
|
||||
}
|
||||
|
||||
/**
|
||||
* 다이얼로그 열려있는지 여부
|
||||
*/
|
||||
const isDialogOpen = (dialogId: DialogId) => openDialogIds.includes(dialogId)
|
||||
|
||||
/**
|
||||
* 모든 다이얼로그 닫기 (있으면 편하니까 같이 제공)
|
||||
*/
|
||||
const hideAll = () => setOpenDialogIds([])
|
||||
const hideAll = useCallback(() => {
|
||||
closeAllDialogs();
|
||||
}, [closeAllDialogs]);
|
||||
|
||||
return {
|
||||
openDialogIds,
|
||||
@@ -50,5 +45,5 @@ export function useDialog() {
|
||||
showDialog,
|
||||
hideDialog,
|
||||
hideAll,
|
||||
}
|
||||
}
|
||||
} as const;
|
||||
}
|
||||
|
||||
@@ -1,106 +1,39 @@
|
||||
"use client";
|
||||
|
||||
import { useCallback, useEffect, useState } from "react";
|
||||
import { cookieStore } from "@/store/cookie/cookie";
|
||||
import { CookieKeys } from "@/store/cookie/cookie-keys";
|
||||
import {FONT_SIZE} from "@/const/commonConst";
|
||||
import {
|
||||
createFontSizeHook,
|
||||
} from "@kri/common/hooks";
|
||||
import {
|
||||
FONT_SIZE,
|
||||
} from "@/const/commonConst";
|
||||
import {
|
||||
cookieStore,
|
||||
} from "@/store/cookie/client-cookie";
|
||||
import {
|
||||
CookieKeys,
|
||||
} from "@/store/cookie/cookie-keys";
|
||||
|
||||
const MIN_FONT_SIZE = 12;
|
||||
const MAX_FONT_SIZE = 24;
|
||||
const DEFAULT_DAYS = 365;
|
||||
|
||||
const clampFontSize = (size: number) => {
|
||||
return Math.min(MAX_FONT_SIZE, Math.max(MIN_FONT_SIZE, size));
|
||||
}
|
||||
|
||||
/**
|
||||
* 쿠키에서 폰트 사이즈 읽기
|
||||
*/
|
||||
const readFontSizeFromCookie = (defaultSize: number): number => {
|
||||
const raw = cookieStore.get(CookieKeys.FontSize);
|
||||
if (!raw) return defaultSize;
|
||||
|
||||
const parsed = parseInt(raw, 10);
|
||||
if (Number.isNaN(parsed)) return defaultSize;
|
||||
|
||||
return clampFontSize(parsed);
|
||||
}
|
||||
|
||||
/**
|
||||
* 쿠키에 폰트 사이즈 저장
|
||||
*/
|
||||
const writeFontSizeToCookie = (size: number) => {
|
||||
const clamped = clampFontSize(size);
|
||||
cookieStore.set(CookieKeys.FontSize, String(clamped), {
|
||||
days: DEFAULT_DAYS,
|
||||
path: "/",
|
||||
sameSite: "Lax",
|
||||
});
|
||||
}
|
||||
|
||||
type UseFontSizeOptions = {
|
||||
/**
|
||||
* 클라이언트 마운트 시 쿠키에서 초기값을 다시 읽어올지 여부
|
||||
* - true: (기본값) 쿠키에서 읽어옴
|
||||
* - false: 전달받은 defaultSize를 그대로 사용 (SSR 값 유지, FOUC 방지)
|
||||
*/
|
||||
hydrateFromCookie?: boolean;
|
||||
}
|
||||
|
||||
export const useFontSize = (
|
||||
defaultSize: number = FONT_SIZE.DEFAULT,
|
||||
options: UseFontSizeOptions = {},
|
||||
) => {
|
||||
const { hydrateFromCookie = true } = options;
|
||||
|
||||
const [fontSize, setFontSize] = useState<number>(() => {
|
||||
// SSR로 전달된 초기값을 신뢰해야 하는 경우 쿠키 재조회 생략
|
||||
if (!hydrateFromCookie) return clampFontSize(defaultSize);
|
||||
return readFontSizeFromCookie(defaultSize);
|
||||
const fontSizeHook =
|
||||
createFontSizeHook({
|
||||
defaultSize: FONT_SIZE.DEFAULT,
|
||||
minSize: FONT_SIZE.MIN,
|
||||
maxSize: FONT_SIZE.MAX,
|
||||
cookieStore,
|
||||
cookieKey: CookieKeys.FontSize,
|
||||
cookieOptions: {
|
||||
days: 365,
|
||||
path: "/",
|
||||
sameSite: "Lax",
|
||||
},
|
||||
cssVariable: "--content-font-size",
|
||||
eventName: "fontSizeChange",
|
||||
});
|
||||
|
||||
// CSS 변수 동기화
|
||||
useEffect(() => {
|
||||
if (typeof document === "undefined") return;
|
||||
document.documentElement.style.setProperty(
|
||||
"--content-font-size",
|
||||
`${fontSize}px`,
|
||||
);
|
||||
}, [fontSize]);
|
||||
export const useFontSize =
|
||||
fontSizeHook.useFontSize;
|
||||
|
||||
// 다른 컴포넌트/페이지에서 변경한 폰트 크기와 상태 동기화
|
||||
// 요청사항 반영: 브로드캐스트 수신 시 event.detail 대신 "쿠키"를 다시 읽어 반영한다.
|
||||
useEffect(() => {
|
||||
if (typeof window === "undefined") return;
|
||||
export const useFontSizeValue =
|
||||
fontSizeHook.useFontSizeValue;
|
||||
|
||||
const handler = (_e: Event) => {
|
||||
// 쿠키에 저장된 최신 값을 읽어 상태를 동기화한다.
|
||||
// 기본값은 현재 상태값을 폴백으로 사용하여 불필요한 변경을 방지한다.
|
||||
const next = readFontSizeFromCookie(fontSize);
|
||||
setFontSize(next);
|
||||
};
|
||||
|
||||
window.addEventListener("fontSizeChange", handler as EventListener);
|
||||
return () => window.removeEventListener("fontSizeChange", handler as EventListener);
|
||||
}, []);
|
||||
|
||||
const updateFontSize = useCallback((next: number) => {
|
||||
const clamped = clampFontSize(next);
|
||||
setFontSize(clamped);
|
||||
writeFontSizeToCookie(clamped);
|
||||
// 전역 이벤트로 다른 훅 인스턴스들에 변경 사항 전파
|
||||
if (typeof window !== "undefined") {
|
||||
window.dispatchEvent(new CustomEvent("fontSizeChange", { detail: clamped }));
|
||||
}
|
||||
}, []);
|
||||
|
||||
return [fontSize, updateFontSize] as const;
|
||||
}
|
||||
|
||||
export const useFontSizeValue = (
|
||||
options: UseFontSizeOptions = {},
|
||||
) => {
|
||||
const defaultSize = FONT_SIZE.DEFAULT;
|
||||
const [fontSize] = useFontSize(defaultSize, options);
|
||||
return fontSize;
|
||||
}
|
||||
export const clampFontSize =
|
||||
fontSizeHook.clampFontSize;
|
||||
|
||||
@@ -1,38 +1,17 @@
|
||||
"use client";
|
||||
|
||||
import { useAtom } from "jotai";
|
||||
import { useCallback } from "react";
|
||||
import { manualLoadingAtom } from "@/store/jotai/loading";
|
||||
import { createLoadingHook } from "@kri/common/hooks";
|
||||
|
||||
export const useLoading = () => {
|
||||
const [isLoading, setIsLoading] = useAtom(manualLoadingAtom);
|
||||
const {
|
||||
loadingCountAtom,
|
||||
isLoadingAtom,
|
||||
useLoading,
|
||||
} = createLoadingHook();
|
||||
|
||||
const showLoading = useCallback(() => setIsLoading(true), [setIsLoading]);
|
||||
const hideLoading = useCallback(() => setIsLoading(false), [setIsLoading]);
|
||||
export {
|
||||
loadingCountAtom,
|
||||
isLoadingAtom,
|
||||
useLoading,
|
||||
};
|
||||
|
||||
const withLoadingAsync = useCallback(
|
||||
async <T,>(fn: () => Promise<T>): Promise<T> => {
|
||||
showLoading();
|
||||
try {
|
||||
return await fn();
|
||||
} finally {
|
||||
hideLoading();
|
||||
}
|
||||
},
|
||||
[showLoading, hideLoading]
|
||||
);
|
||||
|
||||
const withLoading = useCallback(
|
||||
<T,>(fn: () => T): T => {
|
||||
showLoading();
|
||||
try {
|
||||
return fn();
|
||||
} finally {
|
||||
hideLoading();
|
||||
}
|
||||
},
|
||||
[showLoading, hideLoading]
|
||||
);
|
||||
|
||||
return { isLoading, showLoading, hideLoading, withLoading, withLoadingAsync } as const;
|
||||
};
|
||||
export const manualLoadingAtom = isLoadingAtom;
|
||||
|
||||
@@ -1,7 +0,0 @@
|
||||
import { useEffect, useState } from "react";
|
||||
|
||||
export const useMounted = () => {
|
||||
const [mounted, setMounted] = useState(false);
|
||||
useEffect(() => setMounted(true), []);
|
||||
return mounted;
|
||||
};
|
||||
@@ -1,16 +1,18 @@
|
||||
"use client";
|
||||
|
||||
import { useAtom } from "jotai";
|
||||
import { selectedProjectAtom } from "@/store/jotai/project";
|
||||
import { selectedProjectAtom, selectedProjectHydratedAtom } from "@/store/jotai/project";
|
||||
|
||||
export const useSelectedProject = () => {
|
||||
const [selectedProject, setSelectedProject] = useAtom(selectedProjectAtom);
|
||||
const [isSelectedProjectHydrated] = useAtom(selectedProjectHydratedAtom);
|
||||
|
||||
const clearSelectedProject = () => setSelectedProject(null);
|
||||
|
||||
return {
|
||||
selectedProject,
|
||||
isSelectedProjectHydrated,
|
||||
setSelectedProject,
|
||||
clearSelectedProject,
|
||||
} as const;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,49 +1,43 @@
|
||||
"use client";
|
||||
|
||||
import { createStorageHook } from "@kri/common/hooks";
|
||||
import {
|
||||
setLocalStorage,
|
||||
getLocalStorage,
|
||||
removeLocalStorage,
|
||||
setSessionStorage,
|
||||
getSessionStorage,
|
||||
removeLocalStorage,
|
||||
removeSessionStorage,
|
||||
setLocalStorage,
|
||||
setSessionStorage,
|
||||
} from "@/store/storage/secure-storage";
|
||||
import { StorageKey, StorageType } from "@/store/storage/storage-keys";
|
||||
import { StorageKeys, StorageType } from "@/store/storage/storage-keys";
|
||||
import type { AuthState } from "@/hooks/useAuth";
|
||||
import type { SelectedProject } from "@/store/jotai/project";
|
||||
import type { ItrntUserInfo } from "@/types/user";
|
||||
|
||||
type ContactStorageSchema = {
|
||||
[StorageKeys.AuthState]: AuthState;
|
||||
[StorageKeys.SeatNumber]: string;
|
||||
[StorageKeys.LoginType]: string;
|
||||
[StorageKeys.SelectedProject]: SelectedProject;
|
||||
[StorageKeys.ItrntUserList]: ItrntUserInfo[];
|
||||
};
|
||||
|
||||
/**
|
||||
* 사용법:
|
||||
* const { set, get, remove } = useStorage(StorageKeys.SeatNumber);
|
||||
*/
|
||||
export const useStorage = (key: StorageKey) => {
|
||||
const storageType = StorageType[key]; // "local" | "session"
|
||||
type ContactStorageKey = Extract<keyof ContactStorageSchema, string>;
|
||||
|
||||
const set = (value: any): void => {
|
||||
if (storageType === "session") {
|
||||
setSessionStorage(key, value);
|
||||
} else {
|
||||
setLocalStorage(key, value);
|
||||
}
|
||||
};
|
||||
const useCommonStorage = createStorageHook<ContactStorageSchema>({
|
||||
resolveStorageScope: (key) => StorageType[key],
|
||||
localStorageAdapter: {
|
||||
get: getLocalStorage,
|
||||
set: setLocalStorage,
|
||||
remove: removeLocalStorage,
|
||||
},
|
||||
sessionStorageAdapter: {
|
||||
get: getSessionStorage,
|
||||
set: setSessionStorage,
|
||||
remove: removeSessionStorage,
|
||||
},
|
||||
});
|
||||
|
||||
const get = (): any => {
|
||||
if (storageType === "session") {
|
||||
return getSessionStorage(key);
|
||||
} else {
|
||||
return getLocalStorage(key);
|
||||
}
|
||||
};
|
||||
|
||||
const remove = (): void => {
|
||||
if (storageType === "session") {
|
||||
removeSessionStorage(key);
|
||||
} else {
|
||||
removeLocalStorage(key);
|
||||
}
|
||||
};
|
||||
|
||||
return {
|
||||
set,
|
||||
get,
|
||||
remove,
|
||||
} as const;
|
||||
};
|
||||
export const useStorage = <TKey extends ContactStorageKey>(key: TKey) => {
|
||||
return useCommonStorage(key);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user