260702_01_ENH: 리스트 작업 도중 공용 ui를 적용하기 위한 중간 커밋
This commit is contained in:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user