Skip to content

Commit 1486030

Browse files
authored
Replace redux in page deployment-traces (#5955)
- remove modules/deploymentTrace - add query for get deployment traces infinitite Signed-off-by: kypham <[email protected]>
1 parent e1d78ff commit 1486030

File tree

7 files changed

+178
-336
lines changed

7 files changed

+178
-336
lines changed

web/src/components/deployment-trace-page/index.tsx

Lines changed: 39 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
Toolbar,
77
Typography,
88
} from "@mui/material";
9-
import { FC, useCallback, useEffect, useRef, useState } from "react";
9+
import { FC, useCallback, useEffect, useMemo, useRef, useState } from "react";
1010
import CloseIcon from "@mui/icons-material/Close";
1111
import FilterIcon from "@mui/icons-material/FilterList";
1212
import RefreshIcon from "@mui/icons-material/Refresh";
@@ -25,48 +25,60 @@ import {
2525
stringifySearchParams,
2626
useSearchParams,
2727
} from "~/utils/search-params";
28-
import { useAppDispatch, useAppSelector } from "~/hooks/redux";
29-
import {
30-
fetchDeploymentTraces,
31-
fetchMoreDeploymentTraces,
32-
} from "~/modules/deploymentTrace";
3328
import useGroupedDeploymentTrace from "./useGroupedDeploymentTrace";
3429
import DeploymentTraceItem from "./deployment-trace-item";
3530
import { useInView } from "react-intersection-observer";
31+
import { useGetDeploymentTracesInfinite } from "~/queries/deployment-traces/use-deployment-traces-infinite";
3632

3733
const DeploymentTracePage: FC = () => {
3834
const [openFilter, setOpenFilter] = useState(true);
39-
const dispatch = useAppDispatch();
40-
const status = useAppSelector((state) => state.deploymentTrace.status);
41-
const hasMore = useAppSelector((state) => state.deploymentTrace.hasMore);
4235
const navigate = useNavigate();
4336
const filterValues = useSearchParams();
44-
const { dates, deploymentTracesMap } = useGroupedDeploymentTrace();
45-
const isLoading = status === "loading";
4637

4738
const listRef = useRef(null);
4839
const [ref, inView] = useInView({
4940
rootMargin: "400px",
5041
root: listRef.current,
5142
});
5243

53-
useEffect(() => {
54-
dispatch(fetchDeploymentTraces(filterValues));
55-
}, [dispatch, filterValues]);
44+
const {
45+
data: deploymentTracesData,
46+
isFetching,
47+
fetchNextPage: fetchMoreDeploymentTraces,
48+
refetch: refetchDeploymentTraces,
49+
isSuccess,
50+
} = useGetDeploymentTracesInfinite(filterValues);
51+
52+
const deploymentTracesList = useMemo(() => {
53+
return deploymentTracesData?.pages.flatMap((item) => item.tracesList) || [];
54+
}, [deploymentTracesData]);
55+
56+
const hasMore = useMemo(() => {
57+
if (!deploymentTracesData || deploymentTracesData.pages.length === 0) {
58+
return false;
59+
}
60+
const lastIndex = deploymentTracesData?.pages.length - 1;
61+
return deploymentTracesData?.pages?.[lastIndex]?.hasMore || false;
62+
}, [deploymentTracesData]);
63+
64+
const { dates, deploymentTracesMap } = useGroupedDeploymentTrace(
65+
deploymentTracesList || []
66+
);
5667

5768
useEffect(() => {
58-
if (inView && hasMore && isLoading === false) {
59-
dispatch(fetchMoreDeploymentTraces(filterValues || {}));
69+
if (inView && hasMore && isFetching === false) {
70+
fetchMoreDeploymentTraces;
6071
}
61-
}, [dispatch, inView, hasMore, isLoading, filterValues]);
72+
}, [fetchMoreDeploymentTraces, hasMore, inView, isFetching]);
6273

6374
const handleRefreshClick = (): void => {
64-
dispatch(fetchDeploymentTraces(filterValues));
75+
refetchDeploymentTraces();
6576
};
6677

6778
const handleMoreClick = useCallback(() => {
68-
dispatch(fetchMoreDeploymentTraces(filterValues || {}));
69-
}, [dispatch, filterValues]);
79+
// dispatch(fetchMoreDeploymentTraces(filterValues || {}));
80+
fetchMoreDeploymentTraces();
81+
}, [fetchMoreDeploymentTraces]);
7082

7183
const handleFilterChange = (options: { commitHash?: string }): void => {
7284
navigate(
@@ -101,10 +113,10 @@ const DeploymentTracePage: FC = () => {
101113
color="primary"
102114
startIcon={<RefreshIcon />}
103115
onClick={handleRefreshClick}
104-
disabled={isLoading}
116+
disabled={isFetching}
105117
>
106118
{UI_TEXT_REFRESH}
107-
{isLoading && <SpinnerIcon />}
119+
{isFetching && <SpinnerIcon />}
108120
</Button>
109121
<Button
110122
color="primary"
@@ -134,7 +146,7 @@ const DeploymentTracePage: FC = () => {
134146
}}
135147
ref={listRef}
136148
>
137-
{dates.length === 0 && isLoading && (
149+
{dates.length === 0 && isFetching && (
138150
<Box
139151
sx={{
140152
display: "flex",
@@ -145,7 +157,7 @@ const DeploymentTracePage: FC = () => {
145157
<CircularProgress />
146158
</Box>
147159
)}
148-
{dates.length === 0 && !isLoading && (
160+
{dates.length === 0 && !isFetching && (
149161
<Box
150162
sx={{
151163
display: "flex",
@@ -181,18 +193,18 @@ const DeploymentTracePage: FC = () => {
181193
</Box>
182194
</Box>
183195
))}
184-
{status === "succeeded" && <div ref={ref} />}
196+
{isSuccess && <div ref={ref} />}
185197
{!hasMore && (
186198
<Button
187199
color="primary"
188200
variant="outlined"
189201
size="large"
190202
fullWidth
191203
onClick={handleMoreClick}
192-
disabled={isLoading}
204+
disabled={isFetching}
193205
>
194206
{UI_TEXT_MORE}
195-
{isLoading && <SpinnerIcon />}
207+
{isFetching && <SpinnerIcon />}
196208
</Button>
197209
)}
198210
</Box>

web/src/components/deployment-trace-page/useGroupedDeploymentTrace.tsx

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,23 @@
11
import dayjs from "dayjs";
22
import { useMemo } from "react";
3-
import { useShallowEqualSelector } from "~/hooks/redux";
4-
import { selectIds, selectById } from "~/modules/deploymentTrace";
53
import { sortDateFunc } from "~/utils/common";
64
import { ListDeploymentTracesResponse } from "~~/api_client/service_pb";
75

6+
type DeploymentTracesMapByDate = Record<
7+
string,
8+
ListDeploymentTracesResponse.DeploymentTraceRes.AsObject[]
9+
>;
10+
811
type GroupedDeploymentTrace = {
912
dates: string[];
10-
deploymentTracesMap: Record<
11-
string,
12-
ListDeploymentTracesResponse.DeploymentTraceRes.AsObject[]
13-
>;
13+
deploymentTracesMap: DeploymentTracesMapByDate;
1414
};
1515

16-
const useGroupedDeploymentTrace = (): GroupedDeploymentTrace => {
17-
const traceList = useShallowEqualSelector((state) => {
18-
const list = selectIds(state.deploymentTrace)
19-
.map((id) => selectById(state.deploymentTrace, id))
20-
.filter((trace) => trace !== undefined);
21-
return list;
22-
}) as ListDeploymentTracesResponse.DeploymentTraceRes.AsObject[];
23-
16+
const useGroupedDeploymentTrace = (
17+
traceList: ListDeploymentTracesResponse.DeploymentTraceRes.AsObject[]
18+
): GroupedDeploymentTrace => {
2419
const deploymentTracesMap = useMemo(() => {
25-
const listMap: Record<
26-
string,
27-
ListDeploymentTracesResponse.DeploymentTraceRes.AsObject[]
28-
> = {};
20+
const listMap: DeploymentTracesMapByDate = {};
2921

3022
traceList.forEach((item) => {
3123
if (!item.trace?.commitTimestamp) return;

web/src/modules/deploymentTrace/index.test.ts

Lines changed: 0 additions & 137 deletions
This file was deleted.

0 commit comments

Comments
 (0)