Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion frontend/src/App/Login/LoginByGithubCallback/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export const LoginByGithubCallback: React.FC = () => {
.then(async ({ creds: { token } }) => {
dispatch(setAuthData({ token }));
if (process.env.UI_VERSION === 'sky') {
const result = await getProjects().unwrap();
const result = await getProjects({}).unwrap();
if (result?.length === 0) {
navigate(ROUTES.PROJECT.ADD);
return;
Expand Down
13 changes: 8 additions & 5 deletions frontend/src/hooks/useCheckingForFleetsInProjectsOfMember.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ import { useGetOnlyNoFleetsProjectsQuery, useGetProjectsQuery } from 'services/p
type Args = { projectNames?: IProject['project_name'][] };

export const useCheckingForFleetsInProjects = ({ projectNames }: Args) => {
const { data: projectsData } = useGetProjectsQuery(undefined, {
skip: !!projectNames?.length,
});
const { data: projectsData } = useGetProjectsQuery(
{},
{
skip: !!projectNames?.length,
},
);

const { data: noFleetsProjectsData } = useGetOnlyNoFleetsProjectsQuery();

Expand All @@ -16,8 +19,8 @@ export const useCheckingForFleetsInProjects = ({ projectNames }: Args) => {
return projectNames;
}

if (projectsData) {
return projectsData.map((project) => project.project_name);
if (projectsData?.data) {
return projectsData.data.map((project) => project.project_name);
}

return [];
Expand Down
61 changes: 47 additions & 14 deletions frontend/src/hooks/useInfiniteScroll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,14 @@ const SCROLL_POSITION_GAP = 400;
type InfinityListArgs = Partial<Record<string, unknown>>;

type ListResponse<DataItem> = DataItem[];
type ResponseWithDataProp<DataItem> = { data: ListResponse<DataItem>; total_count: number };

type LazyQueryResponse<DataItem> = ResponseWithDataProp<DataItem> | ListResponse<DataItem>;

type UseInfinityParams<DataItem, Args extends InfinityListArgs> = {
useLazyQuery: UseLazyQuery<QueryDefinition<Args, any, any, ListResponse<DataItem>, any>>;
useLazyQuery: UseLazyQuery<QueryDefinition<Args, any, any, LazyQueryResponse<DataItem>, any>>;
args: { limit?: number } & Args;
getResponseItems?: (listItem: DataItem) => Partial<Args>;
getPaginationParams: (listItem: DataItem) => Partial<Args>;
skip?: boolean;
// options?: UseQueryStateOptions<QueryDefinition<Args, any, any, Data[], any>, Record<string, any>>;
Expand All @@ -26,32 +30,50 @@ export const useInfiniteScroll = <DataItem, Args extends InfinityListArgs>({
skip,
}: UseInfinityParams<DataItem, Args>) => {
const [data, setData] = useState<ListResponse<DataItem>>([]);
const [totalCount, setTotalCount] = useState<number | void>();
const scrollElement = useRef<HTMLElement>(document.documentElement);
const isLoadingRef = useRef<boolean>(false);
const isDisabledMoreRef = useRef<boolean>(false);
const lastRequestParams = useRef<Args | undefined>(undefined);
const [disabledMore, setDisabledMore] = useState(false);
const { limit, ...argsProp } = args;
const lastArgsProps = useRef<Partial<Args>>(null);

const [getItems, { isLoading, isFetching }] = useLazyQuery({ ...args } as Args);

const getDataRequest = (params: Args) => {
lastRequestParams.current = params;
if (isEqual(params, lastRequestParams.current)) {
return Promise.reject();
}

return getItems({
const request = getItems({
limit,
...params,
} as Args).unwrap();

request.then(() => {
lastRequestParams.current = { ...params };
});

return request;
};

const getEmptyList = () => {
isLoadingRef.current = true;

setData([]);

getDataRequest(argsProp as Args).then((result) => {
setDisabledMore(false);
setData(result as ListResponse<DataItem>);
getDataRequest(argsProp as Args).then((result: LazyQueryResponse<DataItem>) => {
// setDisabledMore(false);
isDisabledMoreRef.current = false;

if ('data' in result) {
setData(result.data as ListResponse<DataItem>);
setTotalCount(result.total_count);
} else {
setData(result as ListResponse<DataItem>);
setTotalCount();
}

isLoadingRef.current = false;
});
};
Expand All @@ -64,7 +86,7 @@ export const useInfiniteScroll = <DataItem, Args extends InfinityListArgs>({
}, [argsProp, lastArgsProps, skip]);

const getMore = async () => {
if (isLoadingRef.current || disabledMore || skip) {
if (isLoadingRef.current || isDisabledMoreRef.current || skip) {
return;
}

Expand All @@ -76,18 +98,28 @@ export const useInfiniteScroll = <DataItem, Args extends InfinityListArgs>({
...getPaginationParams(data[data.length - 1]),
} as Args);

if (result.length > 0) {
setData((prev) => [...prev, ...result]);
let listResponse: ListResponse<DataItem>;

if ('data' in result) {
listResponse = result.data;
setTotalCount(result.total_count);
} else {
listResponse = result;
setTotalCount();
}

if (listResponse.length > 0) {
setData((prev) => [...prev, ...listResponse]);
} else {
setDisabledMore(true);
isDisabledMoreRef.current = true;
}
} catch (e) {
console.log(e);
}

setTimeout(() => {
isLoadingRef.current = false;
}, 10);
}, 50);
};

useLayoutEffect(() => {
Expand All @@ -101,7 +133,7 @@ export const useInfiniteScroll = <DataItem, Args extends InfinityListArgs>({
}, [data]);

const onScroll = useCallback(() => {
if (disabledMore || isLoadingRef.current) {
if (isDisabledMoreRef.current || isLoadingRef.current) {
return;
}

Expand All @@ -112,7 +144,7 @@ export const useInfiniteScroll = <DataItem, Args extends InfinityListArgs>({
if (scrollPositionFromBottom < SCROLL_POSITION_GAP) {
getMore().catch(console.log);
}
}, [disabledMore, getMore]);
}, [getMore]);

useEffect(() => {
document.addEventListener('scroll', onScroll);
Expand All @@ -126,6 +158,7 @@ export const useInfiniteScroll = <DataItem, Args extends InfinityListArgs>({

return {
data,
totalCount,
isLoading: isLoading || (data.length === 0 && isFetching),
isLoadingMore,
refreshList: getEmptyList,
Expand Down
10 changes: 5 additions & 5 deletions frontend/src/hooks/useProjectFilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,20 @@ export const useProjectFilter = ({ localStorePrefix }: Args) => {
null,
);

const { data: projectsData } = useGetProjectsQuery();
const { data: projectsData } = useGetProjectsQuery({});

const projectOptions = useMemo<SelectCSDProps.Options>(() => {
if (!projectsData?.length) return [];
if (!projectsData?.data?.length) return [];

return projectsData.map((project) => ({ label: project.project_name, value: project.project_name }));
return projectsData.data.map((project) => ({ label: project.project_name, value: project.project_name }));
}, [projectsData]);

useEffect(() => {
if (!projectsData || !selectedProject) {
if (!projectsData?.data || !selectedProject) {
return;
}

const hasSelectedProject = projectsData.some(({ project_name }) => selectedProject?.value === project_name);
const hasSelectedProject = projectsData.data.some(({ project_name }) => selectedProject?.value === project_name);

if (!hasSelectedProject) {
setSelectedProject(null);
Expand Down
12 changes: 6 additions & 6 deletions frontend/src/layouts/AppLayout/TutorialPanel/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export const useTutorials = () => {
} = useAppSelector(selectTutorialPanel);

const { data: userBillingData } = useGetUserBillingInfoQuery({ username: useName ?? '' }, { skip: !useName });
const { data: projectData } = useGetProjectsQuery();
const { data: projectData } = useGetProjectsQuery({});
const { data: runsData } = useGetRunsQuery({
limit: 1,
});
Expand All @@ -54,14 +54,14 @@ export const useTutorials = () => {
useEffect(() => {
if (
userBillingData &&
projectData &&
projectData?.data &&
runsData &&
!completeIsChecked.current &&
location.pathname !== ROUTES.PROJECT.ADD
) {
const billingCompleted = userBillingData.balance > 0;
const configureCLICompleted = runsData.length > 0;
const createProjectCompleted = projectData.length > 0;
const createProjectCompleted = projectData.data.length > 0;

let tempHideStartUp = hideStartUp;

Expand All @@ -88,7 +88,7 @@ export const useTutorials = () => {
}, [userBillingData, runsData, projectData, location.pathname]);

useEffect(() => {
if (projectData && projectData.length > 0 && !createProjectCompleted) {
if (projectData?.data && projectData.data.length > 0 && !createProjectCompleted) {
dispatch(
updateTutorialPanelState({
createProjectCompleted: true,
Expand All @@ -114,8 +114,8 @@ export const useTutorials = () => {
}, []);

const startConfigCliTutorial = useCallback(() => {
if (projectData?.length) {
navigate(ROUTES.PROJECT.DETAILS.SETTINGS.FORMAT(projectData[0].project_name));
if (projectData?.data?.length) {
navigate(ROUTES.PROJECT.DETAILS.SETTINGS.FORMAT(projectData.data[0].project_name));
}
}, [projectData]);

Expand Down
18 changes: 10 additions & 8 deletions frontend/src/pages/Events/List/hooks/useFilters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ const targetTypes = [

export const useFilters = () => {
const [searchParams, setSearchParams] = useSearchParams();
const { data: projectsData } = useGetProjectsQuery();
const { data: usersData } = useGetUserListQuery();
const { data: projectsData } = useGetProjectsQuery({});
const { data: usersData } = useGetUserListQuery({});

const [propertyFilterQuery, setPropertyFilterQuery] = useState<PropertyFilterProps.Query>(() =>
requestParamsToTokens<RequestParamsKeys>({ searchParams, filterKeys }),
Expand All @@ -84,7 +84,7 @@ export const useFilters = () => {
const filteringOptions = useMemo(() => {
const options: PropertyFilterProps.FilteringOption[] = [];

projectsData?.forEach(({ project_name }) => {
projectsData?.data?.forEach(({ project_name }) => {
options.push({
propertyKey: filterKeys.TARGET_PROJECTS,
value: project_name,
Expand All @@ -96,7 +96,7 @@ export const useFilters = () => {
});
});

usersData?.forEach(({ username }) => {
usersData?.data?.forEach(({ username }) => {
options.push({
propertyKey: filterKeys.TARGET_USERS,
value: username,
Expand Down Expand Up @@ -229,30 +229,32 @@ export const useFilters = () => {
...(params[filterKeys.TARGET_PROJECTS] && Array.isArray(params[filterKeys.TARGET_PROJECTS])
? {
[filterKeys.TARGET_PROJECTS]: params[filterKeys.TARGET_PROJECTS]?.map(
(name: string) => projectsData?.find(({ project_name }) => project_name === name)?.['project_id'],
(name: string) =>
projectsData?.data?.find(({ project_name }) => project_name === name)?.['project_id'],
),
}
: {}),
...(params[filterKeys.WITHIN_PROJECTS] && Array.isArray(params[filterKeys.WITHIN_PROJECTS])
? {
[filterKeys.WITHIN_PROJECTS]: params[filterKeys.WITHIN_PROJECTS]?.map(
(name: string) => projectsData?.find(({ project_name }) => project_name === name)?.['project_id'],
(name: string) =>
projectsData?.data?.find(({ project_name }) => project_name === name)?.['project_id'],
),
}
: {}),

...(params[filterKeys.TARGET_USERS] && Array.isArray(params[filterKeys.TARGET_USERS])
? {
[filterKeys.TARGET_USERS]: params[filterKeys.TARGET_USERS]?.map(
(name: string) => usersData?.find(({ username }) => username === name)?.['id'],
(name: string) => usersData?.data?.find(({ username }) => username === name)?.['id'],
),
}
: {}),

...(params[filterKeys.ACTORS] && Array.isArray(params[filterKeys.ACTORS])
? {
[filterKeys.ACTORS]: params[filterKeys.ACTORS]?.map(
(name: string) => usersData?.find(({ username }) => username === name)?.['id'],
(name: string) => usersData?.data?.find(({ username }) => username === name)?.['id'],
),
}
: {}),
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/pages/Models/List/hooks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ const filterKeys: Record<string, RequestParamsKeys> = {
export const useFilters = (localStorePrefix = 'models-list-page') => {
const [searchParams, setSearchParams] = useSearchParams();
const { projectOptions } = useProjectFilter({ localStorePrefix });
const { data: usersData } = useGetUserListQuery();
const { data: usersData } = useGetUserListQuery({});

const [propertyFilterQuery, setPropertyFilterQuery] = useState<PropertyFilterProps.Query>(() =>
requestParamsToTokens<RequestParamsKeys>({ searchParams, filterKeys }),
Expand All @@ -151,7 +151,7 @@ export const useFilters = (localStorePrefix = 'models-list-page') => {
});
});

usersData?.forEach(({ username }) => {
usersData?.data?.forEach(({ username }) => {
options.push({
propertyKey: filterKeys.USER_NAME,
value: username,
Expand Down
Loading