/* eslint-disable react-hooks/rules-of-hooks */
import { Search } from "@styled-icons/boxicons-regular";
import {
UserPlus,
Cog,
PhoneCall,
PhoneOff,
Group,
} from "@styled-icons/boxicons-solid";
import { observer } from "mobx-react-lite";
import { useHistory } from "react-router-dom";
import { internalEmit } from "../../../lib/eventEmitter";
import { isTouchscreenDevice } from "../../../lib/isTouchscreenDevice";
import { voiceState, VoiceStatus } from "../../../lib/vortex/VoiceState";
import { useApplicationState } from "../../../mobx/State";
import { SIDEBAR_MEMBERS } from "../../../mobx/stores/Layout";
import { useIntermediate } from "../../../context/intermediate/Intermediate";
import UpdateIndicator from "../../../components/common/UpdateIndicator";
import IconButton from "../../../components/ui/IconButton";
import { ChannelHeaderProps } from "../ChannelHeader";
export default function HeaderActions({ channel }: ChannelHeaderProps) {
const layout = useApplicationState().layout;
const { openScreen } = useIntermediate();
const history = useHistory();
function openRightSidebar() {
const panels = document.querySelector("#app > div > div > div");
panels?.scrollTo({
behavior: "smooth",
left: panels.clientWidth * 3,
});
}
function openSidebar() {
if (isTouchscreenDevice) {
openRightSidebar();
} else {
layout.toggleSectionState(SIDEBAR_MEMBERS, true);
}
}
return (
<>
{channel.channel_type === "Group" && (
<>
openScreen({
id: "user_picker",
omit: channel.recipient_ids!,
callback: async (users) => {
for (const user of users) {
await channel.addMember(user);
}
},
})
}>
history.push(`/channel/${channel._id}/settings`)
}>
>
)}
{channel.channel_type !== "VoiceChannel" && (
{
internalEmit("RightSidebar", "open", "search");
openRightSidebar();
}}>
)}
{(channel.channel_type === "Group" ||
channel.channel_type === "TextChannel") && (
{
internalEmit("RightSidebar", "open", undefined);
openRightSidebar();
}}>
)}
>
);
}
const VoiceActions = observer(
({ channel }: Pick) => {
if (
channel.channel_type === "SavedMessages" ||
channel.channel_type === "TextChannel"
)
return null;
if (voiceState.status >= VoiceStatus.READY) {
if (voiceState.roomId === channel._id) {
return (
);
}
return (
{
await voiceState.loadVoice();
voiceState.disconnect();
voiceState.connect(channel);
}}>
);
}
return (
);
},
);