Skip to content

Commit c396843

Browse files
blueprint and project 404 redirection & UI message (#8556)
1 parent c33efe8 commit c396843

File tree

3 files changed

+43
-12
lines changed

3 files changed

+43
-12
lines changed

config-ui/src/hooks/use-refresh-data.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export const useRefreshData = <T>(request: (signal: AbortSignal) => Promise<T>,
2626
state: 'ready' | 'pending' | 'error';
2727
deps?: React.DependencyList;
2828
data?: T;
29+
error?: unknown;
2930
abortController?: AbortController;
3031
timer?: number;
3132
}>({
@@ -37,15 +38,14 @@ export const useRefreshData = <T>(request: (signal: AbortSignal) => Promise<T>,
3738
data: ref.current.data,
3839
ready: ref.current.state === 'ready',
3940
pending: ref.current.state === 'pending',
40-
error: ref.current.state === 'error',
41+
error: ref.current.error,
4142
};
4243
}
4344

44-
// When the last state transition has not waited until the new request is completed
45-
// Reset status to pending
4645
ref.current.state = 'pending';
4746
ref.current.deps = deps;
4847
ref.current.data = undefined;
48+
ref.current.error = undefined;
4949
clearTimeout(ref.current.timer);
5050
ref.current.abortController?.abort();
5151
ref.current.timer = window.setTimeout(() => {
@@ -54,13 +54,15 @@ export const useRefreshData = <T>(request: (signal: AbortSignal) => Promise<T>,
5454
.then((data: T) => {
5555
ref.current.data = data;
5656
ref.current.state = 'ready';
57+
ref.current.error = undefined;
5758
setVersion((v) => v + 1);
5859
})
5960
.catch((err: unknown) => {
6061
if (axios.isCancel(err)) {
6162
return;
6263
}
6364
ref.current.state = 'error';
65+
ref.current.error = err;
6466
console.error(err);
6567
setVersion((v) => v + 1);
6668
});
@@ -69,5 +71,6 @@ export const useRefreshData = <T>(request: (signal: AbortSignal) => Promise<T>,
6971
return {
7072
ready: false,
7173
pending: true,
74+
error: undefined,
7275
};
7376
};

config-ui/src/routes/blueprint/detail/blueprint-detail.tsx

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,13 @@
1717
*/
1818

1919
import { useEffect, useState } from 'react';
20-
import { useLocation } from 'react-router-dom';
21-
import { Tabs } from 'antd';
22-
20+
import { useLocation, useNavigate } from 'react-router-dom';
21+
import { Tabs, message } from 'antd';
22+
import axios from 'axios';
2323
import API from '@/api';
2424
import { PageLoading } from '@/components';
2525
import { useRefreshData } from '@/hooks';
26+
import { PATHS } from '@/config';
2627

2728
import { FromEnum } from '../types';
2829

@@ -40,16 +41,27 @@ export const BlueprintDetail = ({ id, from }: Props) => {
4041
const [activeKey, setActiveKey] = useState('status');
4142

4243
const { state } = useLocation();
44+
const navigate = useNavigate();
4345

4446
useEffect(() => {
4547
setActiveKey(state?.activeKey ?? 'status');
4648
}, [state]);
4749

48-
const { ready, data } = useRefreshData(async () => {
49-
const [bpRes, pipelineRes] = await Promise.all([API.blueprint.get(id), API.blueprint.pipelines(id)]);
50+
const { ready, data, error } = useRefreshData(async () => {
51+
const [bpRes, pipelineRes] = await Promise.all([
52+
API.blueprint.get(id),
53+
API.blueprint.pipelines(id),
54+
]);
5055
return [bpRes, pipelineRes.pipelines[0]];
5156
}, [version]);
5257

58+
useEffect(() => {
59+
if (axios.isAxiosError(error) && error.response?.status === 404) {
60+
message.error(`Blueprint not found with id: ${id}`);
61+
navigate(PATHS.BLUEPRINTS(), { replace: true });
62+
}
63+
}, [error, navigate, id]);
64+
5365
const handlRefresh = () => {
5466
setVersion((v) => v + 1);
5567
};

config-ui/src/routes/project/detail/index.tsx

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@
1717
*/
1818

1919
import { useEffect, useState } from 'react';
20-
import { useParams, useLocation } from 'react-router-dom';
20+
import { useParams, useLocation, useNavigate } from 'react-router-dom';
21+
import axios from 'axios';
22+
2123
import { Helmet } from 'react-helmet';
22-
import { Tabs } from 'antd';
24+
import { Tabs, message } from 'antd';
2325

2426
import API from '@/api';
2527
import { PageHeader, PageLoading } from '@/components';
@@ -39,12 +41,22 @@ export const ProjectDetailPage = () => {
3941

4042
const { pname } = useParams() as { pname: string };
4143
const { state } = useLocation();
44+
const navigate = useNavigate();
4245

4346
useEffect(() => {
4447
setTabId(state?.tabId ?? 'blueprint');
4548
}, [state]);
4649

47-
const { ready, data } = useRefreshData(() => API.project.get(pname), [pname, version]);
50+
const { ready, data, error } = useRefreshData(() => API.project.get(pname), [pname, version]);
51+
52+
useEffect(() => {
53+
if (axios.isAxiosError(error) && error.response?.status === 404) {
54+
message.error(`Project not found with project name: ${pname}`);
55+
setTimeout(() => {
56+
navigate(PATHS.PROJECTS(), { replace: true });
57+
}, 100);
58+
}
59+
}, [error, navigate, pname]);
4860

4961
const handleChangeTabId = (tabId: string) => {
5062
setTabId(tabId);
@@ -54,10 +66,14 @@ export const ProjectDetailPage = () => {
5466
setVersion((v) => v + 1);
5567
};
5668

57-
if (!ready || !data) {
69+
if (!ready && !error) {
5870
return <PageLoading />;
5971
}
6072

73+
if (!data) {
74+
return null;
75+
}
76+
6177
return (
6278
<PageHeader
6379
breadcrumbs={[

0 commit comments

Comments
 (0)