Press n or j to go to the next uncovered block, b, p or k for the previous block.
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 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 35x 35x 35x 35x 105x 35x 35x 35x 35x 35x 35x 35x | import { DialogProps, List, ListItem } from "@mui/material"; import Alert from "components/Alert"; import Avatar from "components/Avatar"; import Button from "components/Button"; import CenteredSpinner from "components/CenteredSpinner/CenteredSpinner"; import ConfirmationDialogWrapper from "components/ConfirmationDialogWrapper"; import { Dialog, DialogActions, DialogContent, DialogTitle, } from "components/Dialog"; import IconButton from "components/IconButton"; import { AddIcon, CloseIcon } from "components/Icons"; import TextBody from "components/TextBody"; import { useAuthContext } from "features/auth/AuthProvider"; import { useMembersDialogStyles } from "features/messages/groupchats/MembersDialog"; import { groupChatKey, groupChatMessagesKey, groupChatsListKey, } from "features/queryKeys"; import { useLiteUsers } from "features/userQueries/useLiteUsers"; import { Empty } from "google-protobuf/google/protobuf/empty_pb"; import { RpcError } from "grpc-web"; import { useTranslation } from "i18n"; import { GLOBAL, MESSAGES } from "i18n/namespaces"; import { LiteUser } from "proto/api_pb"; import { GroupChat } from "proto/conversations_pb"; import React, { useEffect, useState } from "react"; import { useMutation, useQueryClient } from "react-query"; import { service } from "service"; function AdminListItem({ groupChatId, member, memberIsAdmin, setError, }: { groupChatId: number; member: LiteUser.AsObject; memberIsAdmin: boolean; setError: (value: string) => void; }) { const { t } = useTranslation(MESSAGES); const classes = useMembersDialogStyles(); const isCurrentUser = useAuthContext().authState.userId === member.userId; const queryClient = useQueryClient(); const clearError = () => setError(""); const handleError = (error: RpcError) => setError(error.message); const invalidate = () => { queryClient.invalidateQueries(groupChatMessagesKey(groupChatId)); queryClient.invalidateQueries(groupChatsListKey); queryClient.invalidateQueries(groupChatKey(groupChatId)); }; const makeAdmin = useMutation<Empty, RpcError, void>( () => service.conversations.makeGroupChatAdmin(groupChatId, member), { onError: handleError, onMutate: clearError, onSuccess: () => { const previousGroupChat = queryClient.getQueryData<GroupChat.AsObject>([ "groupChat", groupChatId, ]); const newAdminUserIdsList = Array.from( previousGroupChat?.adminUserIdsList ?? [] ); newAdminUserIdsList.push(member.userId); queryClient.setQueryData(groupChatKey(groupChatId), { ...previousGroupChat, adminUserIdsList: newAdminUserIdsList, }); invalidate(); }, } ); const removeAdmin = useMutation<Empty, RpcError, void>( () => service.conversations.removeGroupChatAdmin(groupChatId, member), { onError: handleError, onMutate: clearError, onSuccess: () => { const previousGroupChat = queryClient.getQueryData<GroupChat.AsObject>( groupChatKey(groupChatId) ); const newAdminUserIdsList = Array.from( previousGroupChat?.adminUserIdsList ?? [] ); newAdminUserIdsList.splice( newAdminUserIdsList.indexOf(member.userId), 1 ); queryClient.setQueryData(groupChatKey(groupChatId), { ...previousGroupChat, adminUserIdsList: newAdminUserIdsList, }); invalidate(); }, } ); const handleMakeAdmin = () => makeAdmin.mutate(); const handleRemoveAdmin = () => removeAdmin.mutate(); return ( <ListItem dense className={classes.memberListItemContainer}> { //TODO: Colours memberIsAdmin ? ( isCurrentUser ? ( <ConfirmationDialogWrapper title={t("admins_dialog.step_down_confirmation_dialog.title")} message={t("admins_dialog.step_down_confirmation_dialog.message")} onConfirm={handleRemoveAdmin} > {(setIsOpen) => ( <IconButton aria-label={t("admins_dialog.remove_admin.action_a11y_label")} size="small" loading={removeAdmin.isLoading} onClick={() => setIsOpen(true)} > <CloseIcon /> </IconButton> )} </ConfirmationDialogWrapper> ) : ( <IconButton aria-label={t("admins_dialog.remove_admin.action_a11y_label")} size="small" loading={removeAdmin.isLoading} onClick={handleRemoveAdmin} > <CloseIcon /> </IconButton> ) ) : ( <IconButton aria-label={t("admins_dialog.add_admin.action_a11y_label")} size="small" loading={makeAdmin.isLoading} onClick={handleMakeAdmin} > <AddIcon /> </IconButton> ) } <Avatar user={member} className={classes.avatar} /> <TextBody noWrap>{member.name}</TextBody> </ListItem> ); } interface AdminsDialogProps extends DialogProps { groupChat?: GroupChat.AsObject; } export default function AdminsDialog({ groupChat, ...props }: AdminsDialogProps) { const { t } = useTranslation([GLOBAL, MESSAGES]); const [error, setError] = useState(""); const nonAdminIds = groupChat?.memberUserIdsList.filter( (id) => !groupChat?.adminUserIdsList.includes(id) ); const currentUserId = useAuthContext().authState.userId; const admins = useLiteUsers(groupChat?.adminUserIdsList ?? []); const nonAdmins = useLiteUsers(nonAdminIds ?? []); const onClose = props?.onClose; const isOpen = props.open; useEffect(() => { Iif (admins.data && onClose && isOpen) { Iif (!admins.data.has(currentUserId ?? 0)) { onClose({}, "escapeKeyDown"); } } }, [admins.data, currentUserId, onClose, isOpen]); return ( <Dialog {...props} aria-labelledby="admins-dialog-title"> {error && ( <DialogContent> <Alert severity="error">{error}</Alert> </DialogContent> )} <DialogTitle id="admins-dialog-title"> {t("messages:admins_dialog.remove_admin.title")} </DialogTitle> <DialogContent> <List> {admins.isLoading ? ( <CenteredSpinner /> ) : ( Array.from(admins.data?.values() ?? []) .sort((a, b) => b?.name.localeCompare(a?.name ?? "") ?? 0) .map((user) => user ? ( <AdminListItem key={`admin-dialog-${user.userId}`} member={user} memberIsAdmin={ groupChat?.adminUserIdsList.includes(user.userId) ?? false } groupChatId={groupChat?.groupChatId ?? 0} setError={setError} /> ) : null ) )} </List> </DialogContent> {nonAdminIds?.length !== 0 && ( <> <DialogTitle id="admins-dialog-title"> {t("messages:admins_dialog.add_admin.title")} </DialogTitle> <DialogContent> <List> {nonAdmins.isLoading ? ( <CenteredSpinner /> ) : ( Array.from(nonAdmins.data?.values() ?? []) .sort((a, b) => b?.name.localeCompare(a?.name ?? "") ?? 0) .map((user) => user ? ( <AdminListItem key={`admin-dialog-${user.userId}`} member={user} memberIsAdmin={ groupChat?.adminUserIdsList.includes(user.userId) ?? false } groupChatId={groupChat?.groupChatId ?? 0} setError={setError} /> ) : null ) )} </List> </DialogContent> </> )} <DialogActions> <Button onClick={() => (onClose ? onClose({}, "escapeKeyDown") : null)}> {t("global:ok")} </Button> </DialogActions> </Dialog> ); } |