import { useState, useEffect, useMemo } from 'react' import Image from 'next/image' import useTranslation from 'next-translate/useTranslation' import { CogIcon, PlusIcon, MenuIcon, UsersIcon, XIcon } from '@heroicons/react/solid' import classNames from 'classnames' import { useMediaQuery } from 'react-responsive' import { useSwipeable } from 'react-swipeable' import { Sidebar, DirectionSidebar } from './Sidebar' import { IconButton } from 'components/design/IconButton' import { IconLink } from 'components/design/IconLink' import { Channels } from './Channels' import { Guilds } from './Guilds/Guilds' import { Divider } from '../design/Divider' import { Members } from './Members' import { useAuthentication } from 'utils/authentication' import { API_URL } from 'utils/api' export interface GuildsChannelsPath { guildId: number channelId: number } export interface ApplicationProps { path: | '/application' | '/application/guilds/join' | '/application/guilds/create' | '/application/users/[userId]' | GuildsChannelsPath } export const Application: React.FC = (props) => { const { children, path } = props const { t } = useTranslation() const { user } = useAuthentication() const [visibleSidebars, setVisibleSidebars] = useState({ left: true, right: false }) const [mounted, setMounted] = useState(false) const isMobile = useMediaQuery({ query: '(max-width: 900px)' }) const handleToggleSidebars = (direction: DirectionSidebar): void => { if (!isMobile) { if (direction === 'left') { return setVisibleSidebars({ ...visibleSidebars, left: !visibleSidebars.left }) } if (direction === 'right') { return setVisibleSidebars({ ...visibleSidebars, right: !visibleSidebars.right }) } } else { if (direction === 'right' && visibleSidebars.left) { return setVisibleSidebars({ left: false, right: true }) } if (direction === 'left' && visibleSidebars.right) { return setVisibleSidebars({ left: true, right: false }) } if (direction === 'left' && !visibleSidebars.right) { return setVisibleSidebars({ ...visibleSidebars, left: !visibleSidebars.left }) } if (direction === 'right' && !visibleSidebars.left) { return setVisibleSidebars({ ...visibleSidebars, right: !visibleSidebars.right }) } } handleCloseSidebars() } const handleCloseSidebars = (): void => { if (isMobile && (visibleSidebars.left || visibleSidebars.right)) { setVisibleSidebars({ left: false, right: false }) } } const swipeableHandlers = useSwipeable({ trackMouse: false, trackTouch: true, preventDefaultTouchmoveEvent: true, onSwipedRight: () => { if (visibleSidebars.right) { return setVisibleSidebars({ ...visibleSidebars, right: false }) } setVisibleSidebars({ ...visibleSidebars, left: true }) }, onSwipedLeft: () => { if (visibleSidebars.left) { return setVisibleSidebars({ ...visibleSidebars, left: false }) } setVisibleSidebars({ ...visibleSidebars, right: true }) } }) const title = useMemo(() => { if (typeof path !== 'string') { // TODO: Returns the real name of the channel when doing APIs calls return `# Channel ${path.channelId}` } if (path.startsWith('/application/users/')) { return 'Settings' } if (path === '/application/guilds/join') { return 'Join a Guild' } if (path === '/application/guilds/create') { return t('application:create-a-guild') } return 'Application' }, [path, t]) useEffect(() => { setMounted(true) }, []) if (!mounted) { return null } return ( <>
handleToggleSidebars('left')} > {!visibleSidebars.left ? : }
{title}
{title.startsWith('#') && ( handleToggleSidebars('right')} > {!visibleSidebars.right ? : } )}
logo
{typeof path !== 'string' && (

Guild Name

)}
{children}
) }