-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Expand file tree
/
Copy pathHideAllChatsButton.tsx
More file actions
115 lines (103 loc) · 3.01 KB
/
HideAllChatsButton.tsx
File metadata and controls
115 lines (103 loc) · 3.01 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
import {
AlertDialog,
AlertDialogBody,
AlertDialogContent,
AlertDialogFooter,
AlertDialogHeader,
AlertDialogOverlay,
Button,
IconButton,
Tooltip,
useDisclosure,
useToast,
} from "@chakra-ui/react";
import { EyeOff } from "lucide-react";
import { useTranslation } from "next-i18next";
import { useCallback, useRef, useState } from "react";
import { API_ROUTES } from "src/lib/routes";
import useSWRMutation from "swr/mutation";
import { put } from "src/lib/api";
interface HideAllChatsButtonProps {
chatIds: string[];
onHideAll: () => void;
}
export const HideAllChatsButton = ({ chatIds, onHideAll }: HideAllChatsButtonProps) => {
const { t } = useTranslation(["chat", "common"]);
const { isOpen, onOpen, onClose } = useDisclosure();
const cancelRef = useRef<HTMLButtonElement>(null);
const toast = useToast();
const [isHiding, setIsHiding] = useState(false);
const { trigger: triggerHide } = useSWRMutation(API_ROUTES.UPDATE_CHAT(), put);
const handleHideAll = useCallback(async () => {
if (chatIds.length === 0) {
onClose();
return;
}
setIsHiding(true);
try {
// Hide all chats sequentially
for (const chatId of chatIds) {
await triggerHide({ chat_id: chatId, hidden: true });
}
toast({
title: t("chat:hide_all_success"),
status: "success",
duration: 3000,
isClosable: true,
});
onHideAll();
onClose();
} catch (error) {
toast({
title: t("chat:hide_all_error"),
status: "error",
duration: 3000,
isClosable: true,
});
} finally {
setIsHiding(false);
}
}, [chatIds, onHideAll, onClose, toast, t, triggerHide]);
if (chatIds.length === 0) {
return null;
}
return (
<>
<Tooltip label={t("chat:hide_all_chats")}>
<IconButton
icon={<EyeOff size="16px" />}
aria-label={t("chat:hide_all_chats")}
onClick={onOpen}
variant="ghost"
size="sm"
/>
</Tooltip>
<AlertDialog isOpen={isOpen} leastDestructiveRef={cancelRef} onClose={onClose}>
<AlertDialogOverlay>
<AlertDialogContent>
<AlertDialogHeader fontSize="lg" fontWeight="bold">
{t("chat:hide_all_chats")}
</AlertDialogHeader>
<AlertDialogBody>
{t("chat:hide_all_confirmation", { count: chatIds.length })}
</AlertDialogBody>
<AlertDialogFooter>
<Button ref={cancelRef} onClick={onClose} isDisabled={isHiding}>
{t("common:cancel")}
</Button>
<Button
colorScheme="blue"
onClick={handleHideAll}
ml={3}
isLoading={isHiding}
loadingText={t("chat:hiding")}
>
{t("common:confirm")}
</Button>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialogOverlay>
</AlertDialog>
</>
);
};