Shared:Selectors:Files: refactoring

This commit is contained in:
Timofey Boyko 2024-01-31 21:17:37 +03:00
parent adbfd55004
commit 12784c6954
6 changed files with 42 additions and 17 deletions

View File

@ -107,7 +107,7 @@ export interface FilesSelectorProps {
rootThirdPartyId?: string;
roomsFolderId?: number;
currentFolderId: number | string;
parentId: number | string;
parentId?: number | string;
rootFolderType: FolderType;
onClose: () => void;

View File

@ -46,6 +46,8 @@ const useFilesHelper = ({
roomsFolderId,
getFilesArchiveError,
}: UseFilesHelpersProps) => {
const requestRunning = React.useRef(false);
const getFileList = React.useCallback(
async (
startIndex: number,
@ -53,6 +55,9 @@ const useFilesHelper = ({
itemId: number | string | undefined,
isInit?: boolean,
) => {
if (requestRunning.current) return;
requestRunning.current = true;
setIsNextPageLoading(true);
const currentSearch =
@ -116,6 +121,8 @@ const useFilesHelper = ({
await getRoomList(0, null, true, true);
const error = getFilesArchiveError(folder.title);
toastr.error(error);
requestRunning.current = false;
return;
}
await getRootData();
@ -125,7 +132,7 @@ const useFilesHelper = ({
const error = getFilesArchiveError(folder.title);
toastr.error(error);
}
requestRunning.current = false;
return;
}
}
@ -177,7 +184,7 @@ const useFilesHelper = ({
foundParentId = true;
setIsSelectedParentFolder(true);
}
requestRunning.current = false;
return {
label: title,
id: breadCrumbId,
@ -217,6 +224,7 @@ const useFilesHelper = ({
await setSettings(rootThirdPartyId, true);
toastr.error(e as TData);
requestRunning.current = false;
return;
}
@ -224,9 +232,11 @@ const useFilesHelper = ({
await getRoomList(0, null, true, true);
toastr.error(e as TData);
requestRunning.current = false;
return;
}
requestRunning.current = false;
getRootData?.();
if (onSetBaseFolderPath) {

View File

@ -10,16 +10,23 @@ const useFilesSettings = (
) => {
const [settings, setSettings] = React.useState({} as TFilesSettings);
const requestRunning = React.useRef(false);
const initSettings = React.useCallback(async () => {
if (requestRunning.current) return;
requestRunning.current = true;
if (getIconProp) return;
const res = await getSettingsFiles();
setSettings(res);
requestRunning.current = false;
}, [getIconProp]);
React.useEffect(() => {
if (settings.extsArchive) return;
initSettings();
}, [initSettings]);
}, [initSettings, settings.extsArchive]);
const isArchive = React.useCallback(
(extension: string) => presentInArray(settings.extsArchive, extension),

View File

@ -22,6 +22,7 @@ const useRoomsHelper = ({
isRoomsOnly,
onSetBaseFolderPath,
}: UseRoomsHelperProps) => {
const requestRunning = React.useRef(false);
const getRoomList = React.useCallback(
async (
startIndex: number,
@ -29,6 +30,9 @@ const useRoomsHelper = ({
isInit?: boolean,
isErrorPath?: boolean,
) => {
if (requestRunning.current) return;
requestRunning.current = true;
setIsNextPageLoading(true);
const filterValue = search || (search === null ? "" : searchValue || "");
@ -74,6 +78,7 @@ const useRoomsHelper = ({
});
}
requestRunning.current = false;
setIsNextPageLoading(false);
setIsRoot(false);
},

View File

@ -20,8 +20,12 @@ const useRootHelper = ({
isUserOnly,
}: UseRootHelperProps) => {
const [isRoot, setIsRoot] = React.useState<boolean>(false);
const requestRunning = React.useRef(false);
const getRootData = React.useCallback(async () => {
if (requestRunning.current) return;
requestRunning.current = true;
setBreadCrumbs([DEFAULT_BREAD_CRUMB]);
setIsRoot(true);
setIsBreadCrumbsLoading(false);
@ -64,6 +68,7 @@ const useRootHelper = ({
setTotal(newItems.length);
setHasNextPage(false);
setIsNextPageLoading(false);
requestRunning.current = false;
}, [
isUserOnly,
setBreadCrumbs,

View File

@ -368,16 +368,6 @@ const FilesSelector = ({
);
};
const isDisabled = getIsDisabled(
isFirstLoad,
isSelectedParentFolder,
selectedItemId,
selectedItemType,
isRoot,
selectedItemSecurity,
selectedFileInfo,
);
const SelectorBody = (
<Selector
withHeader={withHeader}
@ -395,12 +385,12 @@ const FilesSelector = ({
cancelButtonLabel={cancelButtonLabel}
onCancel={onClose}
emptyScreenImage={
theme.isBase ? EmptyScreenAltSvgUrl : EmptyScreenAltSvgDarkUrl
theme?.isBase ? EmptyScreenAltSvgUrl : EmptyScreenAltSvgDarkUrl
}
emptyScreenHeader={emptyScreenHeader}
emptyScreenDescription={emptyScreenDescription}
searchEmptyScreenImage={
theme.isBase
theme?.isBase
? EmptyScreenFilterAltSvgUrl
: EmptyScreenFilterAltDarkSvgUrl
}
@ -428,7 +418,15 @@ const FilesSelector = ({
loadNextPage={
isRoot ? null : selectedItemType === "rooms" ? getRoomList : getFileList
}
disableAcceptButton={isDisabled}
disableAcceptButton={getIsDisabled(
isFirstLoad,
isSelectedParentFolder,
selectedItemId,
selectedItemType,
isRoot,
selectedItemSecurity,
selectedFileInfo,
)}
withFooterInput={withFooterInput}
withFooterCheckbox={withFooterCheckbox}
footerInputHeader={footerInputHeader}