-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathViewContainer.tsx
More file actions
131 lines (121 loc) · 3.49 KB
/
ViewContainer.tsx
File metadata and controls
131 lines (121 loc) · 3.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import React, { useEffect, useState } from "react";
import Age from "../../../ui/Age/Age";
import ViewLayout from "./ViewLayout";
import ViewContent from "./ViewContent";
import { useViewData } from "../hooks/useViewData";
import { ErrorViewer } from "@flanksource-ui/components/ErrorViewer";
import {
Dialog,
DialogContent,
DialogDescription,
DialogHeader,
DialogTitle
} from "@flanksource-ui/components/ui/dialog";
interface ViewContainerProps {
id: string;
}
const ViewContainer: React.FC<ViewContainerProps> = ({ id }) => {
const {
viewResult,
isLoading,
isFetching,
error,
aggregatedVariables,
currentVariables,
sectionData,
handleForceRefresh
} = useViewData({ viewId: id });
const [refreshErrorOpen, setRefreshErrorOpen] = useState(false);
const refreshError =
viewResult?.refreshStatus === "error" ? viewResult.refreshError : undefined;
const isCachedResponse = viewResult?.responseSource === "cache";
useEffect(() => {
if (refreshError && isCachedResponse) {
setRefreshErrorOpen(true);
}
}, [refreshError, isCachedResponse, viewResult?.requestFingerprint]);
if (error && !viewResult) {
return (
<ViewLayout
title="View Error"
icon="workflow"
onRefresh={handleForceRefresh}
centered
>
<ErrorViewer error={error} className="mx-auto max-w-3xl" />
</ViewLayout>
);
}
if (isLoading && !viewResult) {
return (
<ViewLayout
title="View"
icon="workflow"
onRefresh={handleForceRefresh}
centered
>
<div className="text-center">
<div className="mx-auto mb-4 h-12 w-12 animate-spin rounded-full border-b-2 border-blue-600"></div>
<p className="text-gray-600">Loading view results...</p>
</div>
</ViewLayout>
);
}
if (!viewResult) {
return (
<ViewLayout
title="View Not Found"
icon="workflow"
onRefresh={handleForceRefresh}
centered
>
<ErrorViewer
error="The requested view could not be found."
className="mx-auto max-w-3xl"
/>
</ViewLayout>
);
}
const { icon, title, name } = viewResult;
return (
<>
<Dialog open={refreshErrorOpen} onOpenChange={setRefreshErrorOpen}>
<DialogContent className="max-w-xl">
<DialogHeader>
<DialogTitle>View refresh failed</DialogTitle>
<DialogDescription>
The view failed to refresh. You are seeing cached data from the
last successful refresh.
</DialogDescription>
</DialogHeader>
{refreshError ? (
<ErrorViewer error={refreshError} className="mt-4" />
) : null}
</DialogContent>
</Dialog>
<ViewLayout
title={title || name}
icon={icon || "workflow"}
onRefresh={handleForceRefresh}
loading={isFetching}
extra={
viewResult.lastRefreshedAt && (
<p className="text-sm text-gray-500">
Last refreshed:{" "}
<Age from={viewResult.lastRefreshedAt} format="full" />
</p>
)
}
>
<ViewContent
className="flex h-full w-full flex-1 flex-col overflow-y-auto px-6"
viewResult={viewResult}
sectionData={sectionData}
aggregatedVariables={aggregatedVariables}
currentVariables={currentVariables}
/>
</ViewLayout>
</>
);
};
export default ViewContainer;