/
DeleteResourceModal.tsx
193 lines (176 loc) · 5.75 KB
/
DeleteResourceModal.tsx
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import { BasicTextField } from "@instill-ai/design-system";
import {
Dispatch,
FC,
memo,
SetStateAction,
useCallback,
useMemo,
useState,
} from "react";
import {
DestinationWithDefinition,
Model,
ModelInstance,
Pipeline,
SourceWithDefinition,
} from "@/lib/instill";
import ModalBase from "../ModalBase";
import { Nullable } from "@/types/general";
import OutlineButton from "@/components/ui/Buttons/OutlineButton";
export type DeleteResourceModalProps = {
resource: Nullable<
| SourceWithDefinition
| DestinationWithDefinition
| Pipeline
| Model
| ModelInstance
>;
modalIsOpen: boolean;
setModalIsOpen: Dispatch<SetStateAction<boolean>>;
handleDeleteResource: () => void;
};
const DeleteResourceModal: FC<DeleteResourceModalProps> = ({
modalIsOpen,
setModalIsOpen,
resource,
handleDeleteResource,
}) => {
// ###################################################################
// # #
// # Initialize the function of modal #
// # #
// ###################################################################
const modalDetails = useMemo<{ title: string; description: string }>(() => {
if (!resource) {
return {
title: "",
description: "",
};
}
const resourcePrefix = resource.name.split("/")[0];
let title: string;
let description: string;
switch (resourcePrefix) {
case "pipelines": {
title = "Delete This Pipeline";
description =
"This action cannot be undone. This will permanently delete the pipeline.";
break;
}
case "source-connectors": {
title = "Delete This Source";
description =
"This action cannot be undone. This will permanently delete the source.";
break;
}
case "destination-connectors": {
title = "Delete This Destination";
description =
"This action cannot be undone. This will permanently delete the destination.";
break;
}
case "models": {
if (resource.name.split("/")[2]) {
title = "Delete This Model Instance";
description =
"This action cannot be undone. This will permanently delete the model instance.";
break;
} else {
title = "Delete This Model";
description =
"This action cannot be undone. This will permanently delete the model.";
break;
}
}
default: {
title = "Delete resource";
description =
"Something went wrong when try to activate the flow of deleting resource, please contact our support.";
console.error(
"You have passed resource not included in Pipeline, Model and Connector"
);
}
}
return {
title,
description,
};
}, [resource]);
// ###################################################################
// # #
// # Check whether confirmation code is correct #
// # #
// ###################################################################
const [confirmationCode, setConfirmationCode] =
useState<Nullable<string>>(null);
const handleCodeChange = useCallback((_, input: string) => {
setConfirmationCode(input);
}, []);
const canDeleteResource = useMemo(() => {
if (!resource || confirmationCode !== resource.id) return false;
return true;
}, [confirmationCode, resource]);
return (
<ModalBase
modalBgColor="bg-white"
modalPadding="p-5"
modalIsOpen={modalIsOpen}
>
<div className="flex flex-col gap-y-5">
<h2 className="text-instill-h2">{modalDetails.title}</h2>
<p>{modalDetails.description}</p>
<BasicTextField
id="confirmationCode"
type="text"
label={`Please type "${resource ? resource.id : ""}" to confirm.`}
onChangeInput={handleCodeChange}
value={confirmationCode}
/>
<div className="grid grid-cols-2 gap-x-5">
<OutlineButton
type="button"
disabled={false}
disabledBgColor="bg-instillGrey15"
bgColor="bg-white"
hoveredBgColor="hover:bg-instillGrey50"
disabledTextColor="text-instillGrey50"
textColor="text-instillGrey50"
hoveredTextColor="hover:text-instillGrey05"
width="w-full"
borderSize="border"
borderColor="border-instillGrey50"
hoveredBorderColor={null}
disabledBorderColor={null}
onClickHandler={() => setModalIsOpen(false)}
position={null}
>
Cancel
</OutlineButton>
<OutlineButton
type="button"
disabled={canDeleteResource ? false : true}
disabledBgColor="bg-instillGrey15"
bgColor="bg-white"
hoveredBgColor="hover:bg-instillRed"
disabledTextColor="text-instillGrey30"
textColor={
canDeleteResource ? "text-instillRed" : "text-instillGrey50"
}
hoveredTextColor="hover:text-instillRed10"
width="w-full"
borderSize={canDeleteResource ? "border" : null}
borderColor="border-instillRed"
hoveredBorderColor={null}
disabledBorderColor={null}
onClickHandler={handleDeleteResource}
position={null}
>
Delete
</OutlineButton>
</div>
</div>
</ModalBase>
);
};
export default memo(DeleteResourceModal);