260123_ENH : Layout가지 완성된 최초 commit
This commit is contained in:
42
src/hooks/useAuth.ts
Normal file
42
src/hooks/useAuth.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
"use client";
|
||||
|
||||
import { useCallback } from "react";
|
||||
import { useAtom } from "jotai";
|
||||
import { authStateAtom, type AuthState } from "@/store/jotai/auth";
|
||||
import type { LoginUser } from "@/types/auth";
|
||||
|
||||
export const useAuth = () => {
|
||||
const [auth, setAuth] = useAtom(authStateAtom);
|
||||
|
||||
// 로그인 성공 후 LoginResponse 를 바로 넣어줄 수 있게
|
||||
const setFromLoginResponse = useCallback(
|
||||
(res: LoginUser, loginSeatNumber:string|null = null) => {
|
||||
setAuth({
|
||||
user: res,
|
||||
seatNumber: loginSeatNumber,
|
||||
});
|
||||
},
|
||||
[setAuth],
|
||||
);
|
||||
|
||||
const updateAuth = useCallback(
|
||||
(next: AuthState | ((prev: AuthState) => AuthState)) => {
|
||||
setAuth(next);
|
||||
},
|
||||
[setAuth],
|
||||
);
|
||||
|
||||
const logout = useCallback(() => {
|
||||
setAuth({
|
||||
user: null,
|
||||
seatNumber: null,
|
||||
});
|
||||
}, [setAuth]);
|
||||
|
||||
return {
|
||||
auth,
|
||||
setAuth: updateAuth,
|
||||
setFromLoginResponse,
|
||||
logout,
|
||||
};
|
||||
}
|
||||
12
src/hooks/useDebouncedValue.ts
Normal file
12
src/hooks/useDebouncedValue.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
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;
|
||||
}
|
||||
56
src/hooks/useLoading.ts
Normal file
56
src/hooks/useLoading.ts
Normal file
@@ -0,0 +1,56 @@
|
||||
"use client";
|
||||
|
||||
import { useAtom } from "jotai";
|
||||
import { useCallback } from "react";
|
||||
import { loadingAtom } from "@/store/jotai/loading";
|
||||
|
||||
export const useLoading = () => {
|
||||
const [isLoading, setIsLoading] = useAtom(loadingAtom);
|
||||
|
||||
const showLoading = useCallback(() => {
|
||||
setIsLoading(true);
|
||||
}, [setIsLoading]);
|
||||
|
||||
const hideLoading = useCallback(() => {
|
||||
setIsLoading(false);
|
||||
}, [setIsLoading]);
|
||||
|
||||
/**
|
||||
* 비동기 함수 호출 시 편하게 쓸 수 있는 래퍼
|
||||
*
|
||||
* 예)
|
||||
* const { withLoading } = useLoading();
|
||||
* await withLoading(() => apiCall());
|
||||
*/
|
||||
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;
|
||||
}
|
||||
7
src/hooks/useMounted.ts
Normal file
7
src/hooks/useMounted.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
import { useEffect, useState } from "react";
|
||||
|
||||
export const useMounted = () => {
|
||||
const [mounted, setMounted] = useState(false);
|
||||
useEffect(() => setMounted(true), []);
|
||||
return mounted;
|
||||
};
|
||||
49
src/hooks/useStorage.ts
Normal file
49
src/hooks/useStorage.ts
Normal file
@@ -0,0 +1,49 @@
|
||||
"use client";
|
||||
import {
|
||||
setLocalStorage,
|
||||
getLocalStorage,
|
||||
removeLocalStorage,
|
||||
setSessionStorage,
|
||||
getSessionStorage,
|
||||
removeSessionStorage,
|
||||
} from "@/store/storage/secure-storage";
|
||||
import { StorageKey, StorageType } from "@/store/storage/storage-keys";
|
||||
|
||||
|
||||
/**
|
||||
* 사용법:
|
||||
* const { set, get, remove } = useStorage(StorageKeys.SeatNumber);
|
||||
*/
|
||||
export const useStorage = (key: StorageKey) => {
|
||||
const storageType = StorageType[key]; // "local" | "session"
|
||||
|
||||
const set = (value: any): void => {
|
||||
if (storageType === "session") {
|
||||
setSessionStorage(key, value);
|
||||
} else {
|
||||
setLocalStorage(key, value);
|
||||
}
|
||||
};
|
||||
|
||||
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;
|
||||
};
|
||||
Reference in New Issue
Block a user