-
Notifications
You must be signed in to change notification settings - Fork 345
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[OPIK-905] [FE] Split find project endpoint into two endpoints: find …
…and stats (#1175) * [OPIK-905] [FE] Split find project endpoint into two endpoints: find and stats * - updates related to API contract changes * - fix after comment
- Loading branch information
1 parent
52a9f91
commit e38030c
Showing
12 changed files
with
208 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
apps/opik-frontend/src/api/projects/useProjectStatisticList.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { QueryFunctionContext, useQuery } from "@tanstack/react-query"; | ||
import api, { | ||
PROJECT_STATISTICS_KEY, | ||
PROJECTS_REST_ENDPOINT, | ||
QueryConfig, | ||
} from "@/api/api"; | ||
import { ProjectStatistic } from "@/types/projects"; | ||
import { processSorting } from "@/lib/sorting"; | ||
import { Sorting } from "@/types/sorting"; | ||
|
||
type UseProjectStatisticsListParams = { | ||
workspaceName: string; | ||
search?: string; | ||
sorting?: Sorting; | ||
page: number; | ||
size: number; | ||
}; | ||
|
||
type UseProjectStatisticsListResponse = { | ||
content: ProjectStatistic[]; | ||
total: number; | ||
}; | ||
|
||
const getProjectStatisticsList = async ( | ||
{ signal }: QueryFunctionContext, | ||
{ | ||
workspaceName, | ||
search, | ||
sorting, | ||
size, | ||
page, | ||
}: UseProjectStatisticsListParams, | ||
) => { | ||
const { data } = await api.get(`${PROJECTS_REST_ENDPOINT}stats`, { | ||
signal, | ||
params: { | ||
workspace_name: workspaceName, | ||
...processSorting(sorting), | ||
...(search && { name: search }), | ||
size, | ||
page, | ||
}, | ||
}); | ||
|
||
return data; | ||
}; | ||
|
||
export default function useProjectStatisticsList( | ||
params: UseProjectStatisticsListParams, | ||
options?: QueryConfig<UseProjectStatisticsListResponse>, | ||
) { | ||
return useQuery({ | ||
queryKey: [PROJECT_STATISTICS_KEY, params], | ||
queryFn: (context) => getProjectStatisticsList(context, params), | ||
...options, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
apps/opik-frontend/src/hooks/useProjectWithStatisticsList.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { useMemo } from "react"; | ||
import { keepPreviousData, UseQueryOptions } from "@tanstack/react-query"; | ||
import { Sorting } from "@/types/sorting"; | ||
import { ProjectStatistic, ProjectWithStatistic } from "@/types/projects"; | ||
import useProjectsList from "@/api/projects/useProjectsList"; | ||
import useProjectStatisticsList from "@/api/projects/useProjectStatisticList"; | ||
|
||
type UseProjectWithStatisticsParams = { | ||
workspaceName: string; | ||
search?: string; | ||
sorting?: Sorting; | ||
page: number; | ||
size: number; | ||
}; | ||
|
||
type UseProjectWithStatisticsResponse = { | ||
data: { | ||
content: ProjectWithStatistic[]; | ||
total: number; | ||
}; | ||
isPending: boolean; | ||
}; | ||
|
||
export default function useProjectWithStatisticsList( | ||
params: UseProjectWithStatisticsParams, | ||
config: Omit<UseQueryOptions, "queryKey" | "queryFn">, | ||
) { | ||
const { data: projectsData, isPending } = useProjectsList(params, { | ||
...config, | ||
placeholderData: keepPreviousData, | ||
} as never); | ||
|
||
const { data: projectsStatisticData } = useProjectStatisticsList( | ||
{ | ||
...params, | ||
}, | ||
{ | ||
...config, | ||
placeholderData: keepPreviousData, | ||
} as never, | ||
); | ||
|
||
const data = useMemo(() => { | ||
if (projectsData) { | ||
let statisticMap: Record<string, ProjectStatistic> = {}; | ||
|
||
if (projectsStatisticData && projectsStatisticData.content?.length > 0) { | ||
statisticMap = projectsStatisticData.content.reduce< | ||
Record<string, ProjectStatistic> | ||
>((acc, statistic) => { | ||
acc[statistic.project_id!] = statistic; | ||
return acc; | ||
}, {}); | ||
} | ||
|
||
return { | ||
...projectsData, | ||
content: projectsData.content.map((project) => { | ||
return statisticMap | ||
? { | ||
...project, | ||
...statisticMap[project.id], | ||
} | ||
: project; | ||
}), | ||
}; | ||
} | ||
|
||
return { content: [], total: 0 }; | ||
}, [projectsData, projectsStatisticData]); | ||
|
||
return { | ||
data, | ||
isPending, | ||
} as UseProjectWithStatisticsResponse; | ||
} |
Oops, something went wrong.