-
Notifications
You must be signed in to change notification settings - Fork 529
/
ActionModal.tsx
183 lines (172 loc) · 7.52 KB
/
ActionModal.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
import {
Dialog,
DialogContent, DialogHeader,
DialogTitle
} from "@/components/ui/dialog";
import { Button } from "@/components/ui/button";
import { useActionModalStateValue } from "@/hooks/actionModal";
import { InfoIcon, ListIcon, Undo2Icon } from "lucide-react";
import { useEffect, useState } from "react";
import { ScrollArea } from "@/components/ui/scroll-area";
import GenericSpinner from "@/components/GenericSpinner";
import { cn } from "@/lib/utils";
import { useBackendApi } from "@/hooks/fetch";
import ModalCentralMessage from "@/components/ModalCentralMessage";
import { HistoryActionModalResp, HistoryActionModalSuccess } from "@shared/historyApiTypes";
import ActionIdsTab from "./ActionIdsTab";
import ActionInfoTab from "./ActionInfoTab";
import ActionModifyTab from "./ActionModifyTab";
const modalTabs = [
{
title: 'Info',
icon: <InfoIcon className="mr-2 h-5 w-5 hidden xs:block" />,
},
{
title: 'IDs',
icon: <ListIcon className="mr-2 h-5 w-5 hidden xs:block" />,
},
{
//In the future, when adding "edit" and "remove" to the modal, join with "revoke" in a tab bellow
// title: 'Modify',
// icon: <EraserIcon className="mr-2 h-5 w-5 hidden xs:block" />,
title: 'Revoke',
icon: <Undo2Icon className="mr-2 h-5 w-5 hidden xs:block" />,
className: 'hover:bg-destructive hover:text-destructive-foreground',
},
]
export default function ActionModal() {
const { isModalOpen, closeModal, actionRef } = useActionModalStateValue();
const [selectedTab, setSelectedTab] = useState(modalTabs[0].title);
const [currRefreshKey, setCurrRefreshKey] = useState(0);
const [modalData, setModalData] = useState<HistoryActionModalSuccess | undefined>(undefined);
const [modalError, setModalError] = useState('');
const [tsFetch, setTsFetch] = useState(0);
const historyGetActionApi = useBackendApi<HistoryActionModalResp>({
method: 'GET',
path: `/history/action`,
abortOnUnmount: true,
});
//Helper for tabs to be able to refresh the modal data
const refreshModalData = () => {
setCurrRefreshKey(currRefreshKey + 1);
};
//Querying Action data when reference is available
useEffect(() => {
if (!actionRef) return;
setModalData(undefined);
setModalError('');
historyGetActionApi({
queryParams: { id: actionRef },
success: (resp) => {
if ('error' in resp) {
setModalError(resp.error);
} else {
setModalData(resp);
setTsFetch(Math.round(Date.now() / 1000));
}
},
error: (error) => {
setModalError(error);
},
});
}, [actionRef, currRefreshKey]);
//Resetting selected tab when modal is closed
useEffect(() => {
if (!isModalOpen) {
setTimeout(() => {
setSelectedTab(modalTabs[0].title);
}, 200);
}
}, [isModalOpen]);
const handleOpenClose = (newOpenState: boolean) => {
if (isModalOpen && !newOpenState) {
closeModal();
}
};
let pageTitle: JSX.Element;
if (modalData) {
const displayName = modalData.action.playerName !== false
? <span>{modalData.action.playerName}</span>
: <span className="italic opacity-75">unknown player</span>;
if (modalData.action.type === 'ban') {
pageTitle = <>
<span className="text-destructive-inline font-mono mr-2">[{modalData.action.id}]</span>
Banned {displayName}
</>;
} else if (modalData.action.type === 'warn') {
pageTitle = <>
<span className="text-warning-inline font-mono mr-2">[{modalData.action.id}]</span>
Warned {displayName}
</>;
} else {
throw new Error(`Unknown action type: ${modalData.action.type}`);
}
} else if (modalError) {
pageTitle = <span className="text-destructive-inline">Error!</span>;
} else {
pageTitle = <span className="text-muted-foreground italic">Loading...</span>;
}
return (
<Dialog open={isModalOpen} onOpenChange={handleOpenClose}>
<DialogContent
className="max-w-2xl h-full sm:h-auto max-h-full p-0 gap-1 sm:gap-4 flex flex-col"
onOpenAutoFocus={(e) => e.preventDefault()}
>
<DialogHeader className="p-4 border-b">
<DialogTitle className="tracking-wide line-clamp-1 break-all mr-6">
{pageTitle}
</DialogTitle>
</DialogHeader>
<div className="flex flex-col md:flex-row md:px-4 h-full">
<div className="flex flex-row md:flex-col gap-1 bg-muted md:bg-transparent p-1 md:p-0 mx-2 md:mx-0 rounded-md">
{modalTabs.map((tab) => (
<Button
id={`action-modal-tab-${tab.title}`}
key={tab.title}
variant={selectedTab === tab.title ? "secondary" : "ghost"}
className={cn(
'w-full tracking-wider justify-center md:justify-start',
'h-7 rounded-sm px-2 text-sm',
'md:h-10 md:text-base',
// @ts-ignore annoying, remove this when adding some class to any of the tabs
tab.className,
)}
onClick={() => setSelectedTab(tab.title)}
onKeyDown={handleTabButtonKeyDown}
>
{tab.icon} {tab.title}
</Button>
))}
</div>
{/* NOTE: consistent height: sm:h-[16.5rem] */}
<ScrollArea className="w-full max-h-[calc(100vh-3.125rem-4rem-5rem)] min-h-[16.5rem] md:max-h-[50vh] px-4 py-2 md:py-0">
{!modalData ? (
<ModalCentralMessage>
{modalError ? (
<span className="text-destructive-inline">Error: {modalError}</span>
) : (
<GenericSpinner msg="Loading..." />
)}
</ModalCentralMessage>
) : (
<>
{selectedTab === 'Info' && <ActionInfoTab
action={modalData.action}
serverTime={modalData.serverTime}
tsFetch={tsFetch}
/>}
{selectedTab === 'IDs' && <ActionIdsTab
action={modalData.action}
/>}
{selectedTab === 'Revoke' && <ActionModifyTab
action={modalData.action}
refreshModalData={refreshModalData}
/>}
</>
)}
</ScrollArea>
</div>
</DialogContent>
</Dialog>
);
}