260303_01_ENH: TanStack Query 적용
This commit is contained in:
@@ -1,109 +1,59 @@
|
||||
"use client";
|
||||
|
||||
import { useCallback, useEffect, useRef, useState } from "react";
|
||||
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;
|
||||
import {ItrntUserInfo} from "@/types/user";
|
||||
import {useIntranetUsersQuery} from "@/query/intranet";
|
||||
import {useQueryClient} from "@tanstack/react-query";
|
||||
import {qk} from "@/query/keys";
|
||||
|
||||
export const useItrntUserList = () => {
|
||||
const endpoint = "/intranet/user-list";
|
||||
const storage = useStorage(StorageKeys.ItrntUserList);
|
||||
const { get, set, remove } = storage;
|
||||
|
||||
// ✅ storage 함수 레퍼런스 고정(최신 함수는 effect로 갱신)
|
||||
// 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 [cachedUsers] = useState<ItrntUserInfo[] | undefined>(() => {
|
||||
const cached = storage.get();
|
||||
return Array.isArray(cached) ? (cached as ItrntUserInfo[]) : undefined;
|
||||
});
|
||||
|
||||
const mountedRef = useRef(true);
|
||||
const enabled = !cachedUsers;
|
||||
|
||||
const query = useIntranetUsersQuery({
|
||||
enabled,
|
||||
initialData: cachedUsers,
|
||||
});
|
||||
|
||||
// 서버에서 받아온 최신 데이터는 storage에 반영
|
||||
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 (query.data) {
|
||||
storageRef.current.set(query.data);
|
||||
}
|
||||
}, [query.data]);
|
||||
|
||||
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 qc = useQueryClient();
|
||||
|
||||
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 res = await query.refetch();
|
||||
if (res.data) storageRef.current.set(res.data);
|
||||
}, [query]);
|
||||
|
||||
const clearCache = useCallback(() => {
|
||||
storageRef.current.remove();
|
||||
setUsers([]);
|
||||
}, []);
|
||||
qc.removeQueries({ queryKey: qk.intranetUsers() });
|
||||
}, [qc]);
|
||||
|
||||
return { users, isLoading, error, refresh, clearCache } as const;
|
||||
return {
|
||||
users: query.data ?? [],
|
||||
isLoading: query.isFetching, // 캐시 있으면 false, refetch 중이면 true
|
||||
error: query.error,
|
||||
refresh,
|
||||
clearCache,
|
||||
} as const;
|
||||
};
|
||||
@@ -2,26 +2,14 @@
|
||||
|
||||
import { useAtom } from "jotai";
|
||||
import { useCallback } from "react";
|
||||
import { loadingAtom } from "@/store/jotai/loading";
|
||||
import { manualLoadingAtom } from "@/store/jotai/loading";
|
||||
|
||||
export const useLoading = () => {
|
||||
const [isLoading, setIsLoading] = useAtom(loadingAtom);
|
||||
const [isLoading, setIsLoading] = useAtom(manualLoadingAtom);
|
||||
|
||||
const showLoading = useCallback(() => {
|
||||
setIsLoading(true);
|
||||
}, [setIsLoading]);
|
||||
const showLoading = useCallback(() => setIsLoading(true), [setIsLoading]);
|
||||
const hideLoading = useCallback(() => setIsLoading(false), [setIsLoading]);
|
||||
|
||||
const hideLoading = useCallback(() => {
|
||||
setIsLoading(false);
|
||||
}, [setIsLoading]);
|
||||
|
||||
/**
|
||||
* 비동기 함수 호출 시 편하게 쓸 수 있는 래퍼
|
||||
*
|
||||
* 예)
|
||||
* const { withLoading } = useLoading();
|
||||
* await withLoading(() => apiCall());
|
||||
*/
|
||||
const withLoadingAsync = useCallback(
|
||||
async <T,>(fn: () => Promise<T>): Promise<T> => {
|
||||
showLoading();
|
||||
@@ -31,7 +19,7 @@ export const useLoading = () => {
|
||||
hideLoading();
|
||||
}
|
||||
},
|
||||
[showLoading, hideLoading],
|
||||
[showLoading, hideLoading]
|
||||
);
|
||||
|
||||
const withLoading = useCallback(
|
||||
@@ -43,14 +31,8 @@ export const useLoading = () => {
|
||||
hideLoading();
|
||||
}
|
||||
},
|
||||
[showLoading, hideLoading],
|
||||
[showLoading, hideLoading]
|
||||
);
|
||||
|
||||
return {
|
||||
isLoading,
|
||||
showLoading,
|
||||
hideLoading,
|
||||
withLoading,
|
||||
withLoadingAsync,
|
||||
} as const;
|
||||
}
|
||||
return { isLoading, showLoading, hideLoading, withLoading, withLoadingAsync } as const;
|
||||
};
|
||||
Reference in New Issue
Block a user