251210_01_ENH : 프로젝트 수정, 에디터 구현중
This commit is contained in:
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;
|
||||
}
|
||||
@@ -3,7 +3,7 @@
|
||||
import { useCallback, useEffect, useState } from "react";
|
||||
import { cookieStore } from "@/store/cookie/cookie";
|
||||
import { CookieKeys } from "@/store/cookie/cookie-keys";
|
||||
import commonConst from "@/config/commonConst";
|
||||
import {FONT_SIZE} from "@/const/commonConst";
|
||||
|
||||
const MIN_FONT_SIZE = 12;
|
||||
const MAX_FONT_SIZE = 24;
|
||||
@@ -48,7 +48,7 @@ type UseFontSizeOptions = {
|
||||
}
|
||||
|
||||
export const useFontSize = (
|
||||
defaultSize: number = commonConst.fontSizeDefault,
|
||||
defaultSize: number = FONT_SIZE.DEFAULT,
|
||||
options: UseFontSizeOptions = {},
|
||||
) => {
|
||||
const { hydrateFromCookie = true } = options;
|
||||
@@ -100,7 +100,7 @@ export const useFontSize = (
|
||||
export const useFontSizeValue = (
|
||||
options: UseFontSizeOptions = {},
|
||||
) => {
|
||||
const defaultSize = commonConst.fontSizeDefault;
|
||||
const defaultSize = FONT_SIZE.DEFAULT;
|
||||
const [fontSize] = useFontSize(defaultSize, options);
|
||||
return fontSize;
|
||||
}
|
||||
109
src/hooks/useItrntUserList.ts
Normal file
109
src/hooks/useItrntUserList.ts
Normal file
@@ -0,0 +1,109 @@
|
||||
"use client";
|
||||
|
||||
import { useCallback, useEffect, useRef, useState } from "react";
|
||||
import { StorageKeys } from "@/store/storage/storage-keys";
|
||||
import { useStorage } from "@/hooks/useStorage";
|
||||
import { nextApi } from "@/lib/http-client";
|
||||
|
||||
export type ItrntUserInfo = {
|
||||
userId: string;
|
||||
userNm: string;
|
||||
userCmpn: string;
|
||||
userTeam: string;
|
||||
userRole: number;
|
||||
};
|
||||
|
||||
let inFlight: Promise<ItrntUserInfo[]> | null = null;
|
||||
|
||||
export const useItrntUserList = () => {
|
||||
const endpoint = "/intranet/user-list";
|
||||
const storage = useStorage(StorageKeys.ItrntUserList);
|
||||
const { get, set, remove } = storage;
|
||||
|
||||
// ✅ storage 함수 레퍼런스 고정(최신 함수는 effect로 갱신)
|
||||
const storageRef = useRef({ get, set, remove });
|
||||
useEffect(() => {
|
||||
storageRef.current = { get, set, remove };
|
||||
}, [get, set, remove]);
|
||||
|
||||
const [users, setUsers] = useState<ItrntUserInfo[]>([]);
|
||||
const [isLoading, setIsLoading] = useState<boolean>(true);
|
||||
const [error, setError] = useState<unknown>(null);
|
||||
|
||||
const mountedRef = useRef(true);
|
||||
useEffect(() => {
|
||||
mountedRef.current = true;
|
||||
return () => {
|
||||
mountedRef.current = false;
|
||||
};
|
||||
}, []);
|
||||
|
||||
const fetchUsers = useCallback(async (): Promise<ItrntUserInfo[]> => {
|
||||
return await nextApi.get<ItrntUserInfo[]>(endpoint);
|
||||
}, [endpoint]);
|
||||
|
||||
// ✅ ensure는 이제 storage 함수들에 "직접" 의존하지 않음(루프 차단)
|
||||
const ensure = useCallback(async (force = false): Promise<ItrntUserInfo[]> => {
|
||||
const { get: sGet, set: sSet } = storageRef.current;
|
||||
|
||||
if (!force) {
|
||||
const cached = sGet();
|
||||
if (Array.isArray(cached)) return cached as ItrntUserInfo[];
|
||||
}
|
||||
|
||||
if (!inFlight) {
|
||||
inFlight = fetchUsers().finally(() => {
|
||||
inFlight = null;
|
||||
});
|
||||
}
|
||||
|
||||
const list = await inFlight;
|
||||
sSet(list);
|
||||
return list;
|
||||
}, [fetchUsers]);
|
||||
|
||||
useEffect(() => {
|
||||
(async () => {
|
||||
setIsLoading(true);
|
||||
setError(null);
|
||||
|
||||
try {
|
||||
const list = await ensure(false);
|
||||
if (!mountedRef.current) return;
|
||||
setUsers(list);
|
||||
} catch (e) {
|
||||
if (!mountedRef.current) return;
|
||||
setUsers([]);
|
||||
setError(e);
|
||||
} finally {
|
||||
if (!mountedRef.current) return;
|
||||
setIsLoading(false);
|
||||
}
|
||||
})();
|
||||
}, [ensure]);
|
||||
|
||||
const refresh = useCallback(async () => {
|
||||
setIsLoading(true);
|
||||
setError(null);
|
||||
|
||||
try {
|
||||
const list = await ensure(true);
|
||||
if (!mountedRef.current) return;
|
||||
setUsers(list);
|
||||
} catch (e) {
|
||||
if (!mountedRef.current) return;
|
||||
setUsers([]);
|
||||
setError(e);
|
||||
} finally {
|
||||
if (!mountedRef.current) return;
|
||||
setIsLoading(false);
|
||||
}
|
||||
}, [ensure]);
|
||||
|
||||
const clearCache = useCallback(() => {
|
||||
storageRef.current.remove();
|
||||
setUsers([]);
|
||||
}, []);
|
||||
|
||||
return { users, isLoading, error, refresh, clearCache } 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;
|
||||
};
|
||||
Reference in New Issue
Block a user