fix(web): prevent invalid owner leave action in group/channel info
Some checks are pending
CI / test (push) Has started running
Some checks are pending
CI / test (push) Has started running
This commit is contained in:
@@ -92,6 +92,16 @@ export function ChatInfoPanel({ chatId, open, onClose }: Props) {
|
||||
const canManageMembers = Boolean(isGroupLike && (myRoleNormalized === "owner" || myRoleNormalized === "admin"));
|
||||
const canEditTitle = Boolean(isGroupLike && (myRoleNormalized === "owner" || myRoleNormalized === "admin"));
|
||||
const canEditChatProfile = Boolean(isGroupLike && (myRoleNormalized === "owner" || myRoleNormalized === "admin"));
|
||||
const canLeaveGroupLikeChat = useMemo(() => {
|
||||
if (!chat || !isGroupLike) {
|
||||
return false;
|
||||
}
|
||||
if (myRoleNormalized !== "owner") {
|
||||
return true;
|
||||
}
|
||||
const count = chat.members_count ?? members.length;
|
||||
return count <= 1;
|
||||
}, [chat, isGroupLike, myRoleNormalized, members.length]);
|
||||
const photoAttachments = useMemo(() => attachments.filter((item) => item.file_type.startsWith("image/")).sort((a, b) => b.id - a.id), [attachments]);
|
||||
const videoAttachments = useMemo(() => attachments.filter((item) => item.file_type.startsWith("video/")).sort((a, b) => b.id - a.id), [attachments]);
|
||||
const voiceAttachments = useMemo(() => attachments.filter((item) => item.message_type === "voice").sort((a, b) => b.id - a.id), [attachments]);
|
||||
@@ -983,8 +993,12 @@ export function ChatInfoPanel({ chatId, open, onClose }: Props) {
|
||||
</button>
|
||||
) : null}
|
||||
<button
|
||||
className="w-full rounded bg-slate-700 px-3 py-2 text-sm"
|
||||
className="w-full rounded bg-slate-700 px-3 py-2 text-sm disabled:cursor-not-allowed disabled:opacity-60"
|
||||
disabled={!canLeaveGroupLikeChat}
|
||||
onClick={async () => {
|
||||
if (!canLeaveGroupLikeChat) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
await leaveChat(chatId);
|
||||
await loadChats();
|
||||
@@ -995,7 +1009,9 @@ export function ChatInfoPanel({ chatId, open, onClose }: Props) {
|
||||
}
|
||||
}}
|
||||
>
|
||||
{chat.type === "channel" ? "Leave channel" : "Leave chat"}
|
||||
{canLeaveGroupLikeChat
|
||||
? (chat.type === "channel" ? "Leave channel" : "Leave chat")
|
||||
: "Owner cannot leave while members remain"}
|
||||
</button>
|
||||
</>
|
||||
) : null}
|
||||
|
||||
Reference in New Issue
Block a user