2023-10-23 23:33:39 +02:00
|
|
|
import { useState, useEffect } from "react"
|
|
|
|
import Image from "next/image"
|
|
|
|
import { PlusIcon, MenuIcon, UsersIcon, XIcon } from "@heroicons/react/solid"
|
|
|
|
import classNames from "clsx"
|
|
|
|
import { useMediaQuery } from "react-responsive"
|
|
|
|
import { useSwipeable } from "react-swipeable"
|
|
|
|
|
|
|
|
import type { DirectionSidebar } from "./Sidebar"
|
|
|
|
import { Sidebar } from "./Sidebar"
|
|
|
|
import { IconButton } from "../design/IconButton"
|
|
|
|
import { IconLink } from "../design/IconLink"
|
|
|
|
import { Guilds } from "./Guilds/Guilds"
|
|
|
|
import { Divider } from "../design/Divider"
|
|
|
|
import { Members } from "./Members"
|
|
|
|
import { useAuthentication } from "../../tools/authentication"
|
2021-10-24 06:09:43 +02:00
|
|
|
|
2022-03-05 18:22:30 +01:00
|
|
|
export interface ChannelsPath {
|
2021-10-24 06:09:43 +02:00
|
|
|
channelId: number
|
|
|
|
}
|
2022-03-05 18:22:30 +01:00
|
|
|
export interface GuildsPath {
|
|
|
|
guildId: number
|
|
|
|
}
|
|
|
|
export interface GuildsChannelsPath extends GuildsPath, ChannelsPath {}
|
|
|
|
|
|
|
|
const isGuildsChannelsPath = (path: any): path is GuildsChannelsPath => {
|
|
|
|
return path.guildId !== undefined && path.channelId !== undefined
|
|
|
|
}
|
2021-10-24 06:09:43 +02:00
|
|
|
|
2022-01-01 20:42:25 +01:00
|
|
|
export type ApplicationPath =
|
2023-10-23 23:33:39 +02:00
|
|
|
| "/application"
|
|
|
|
| "/application/guilds/join"
|
|
|
|
| "/application/guilds/create"
|
2022-01-01 20:42:25 +01:00
|
|
|
| `/application/users/${number}`
|
2022-02-19 23:20:33 +01:00
|
|
|
| `/application/users/settings`
|
2022-01-01 20:42:25 +01:00
|
|
|
| GuildsChannelsPath
|
2022-03-05 18:22:30 +01:00
|
|
|
| GuildsPath
|
2022-01-01 20:42:25 +01:00
|
|
|
|
2021-10-24 06:09:43 +02:00
|
|
|
export interface ApplicationProps {
|
2022-01-01 20:42:25 +01:00
|
|
|
path: ApplicationPath
|
|
|
|
guildLeftSidebar?: React.ReactNode
|
|
|
|
title: string
|
2021-10-24 06:09:43 +02:00
|
|
|
}
|
|
|
|
|
2022-05-12 20:35:46 +02:00
|
|
|
export const Application: React.FC<
|
|
|
|
React.PropsWithChildren<ApplicationProps>
|
|
|
|
> = (props) => {
|
2022-01-01 20:42:25 +01:00
|
|
|
const { children, path, guildLeftSidebar, title } = props
|
2021-10-24 06:09:43 +02:00
|
|
|
|
|
|
|
const { user } = useAuthentication()
|
|
|
|
|
|
|
|
const [mounted, setMounted] = useState(false)
|
|
|
|
|
|
|
|
const isMobile = useMediaQuery({
|
2023-10-23 23:33:39 +02:00
|
|
|
query: "(max-width: 900px)",
|
2021-10-24 06:09:43 +02:00
|
|
|
})
|
|
|
|
|
2022-03-21 16:12:22 +01:00
|
|
|
const [visibleSidebars, setVisibleSidebars] = useState({
|
|
|
|
left: !isMobile,
|
2023-10-23 23:33:39 +02:00
|
|
|
right: false,
|
2022-03-21 16:12:22 +01:00
|
|
|
})
|
|
|
|
|
2021-10-24 06:09:43 +02:00
|
|
|
const handleToggleSidebars = (direction: DirectionSidebar): void => {
|
|
|
|
if (!isMobile) {
|
2023-10-23 23:33:39 +02:00
|
|
|
if (direction === "left") {
|
2021-10-24 06:09:43 +02:00
|
|
|
return setVisibleSidebars({
|
|
|
|
...visibleSidebars,
|
2023-10-23 23:33:39 +02:00
|
|
|
left: !visibleSidebars.left,
|
2021-10-24 06:09:43 +02:00
|
|
|
})
|
|
|
|
}
|
2023-10-23 23:33:39 +02:00
|
|
|
if (direction === "right") {
|
2021-10-24 06:09:43 +02:00
|
|
|
return setVisibleSidebars({
|
|
|
|
...visibleSidebars,
|
2023-10-23 23:33:39 +02:00
|
|
|
right: !visibleSidebars.right,
|
2021-10-24 06:09:43 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
} else {
|
2023-10-23 23:33:39 +02:00
|
|
|
if (direction === "right" && visibleSidebars.left) {
|
2021-10-24 06:09:43 +02:00
|
|
|
return setVisibleSidebars({
|
|
|
|
left: false,
|
2023-10-23 23:33:39 +02:00
|
|
|
right: true,
|
2021-10-24 06:09:43 +02:00
|
|
|
})
|
|
|
|
}
|
2023-10-23 23:33:39 +02:00
|
|
|
if (direction === "left" && visibleSidebars.right) {
|
2021-10-24 06:09:43 +02:00
|
|
|
return setVisibleSidebars({
|
|
|
|
left: true,
|
2023-10-23 23:33:39 +02:00
|
|
|
right: false,
|
2021-10-24 06:09:43 +02:00
|
|
|
})
|
|
|
|
}
|
2023-10-23 23:33:39 +02:00
|
|
|
if (direction === "left" && !visibleSidebars.right) {
|
2021-10-24 06:09:43 +02:00
|
|
|
return setVisibleSidebars({
|
|
|
|
...visibleSidebars,
|
2023-10-23 23:33:39 +02:00
|
|
|
left: !visibleSidebars.left,
|
2021-10-24 06:09:43 +02:00
|
|
|
})
|
|
|
|
}
|
2023-10-23 23:33:39 +02:00
|
|
|
if (direction === "right" && !visibleSidebars.left) {
|
2021-10-24 06:09:43 +02:00
|
|
|
return setVisibleSidebars({
|
|
|
|
...visibleSidebars,
|
2023-10-23 23:33:39 +02:00
|
|
|
right: !visibleSidebars.right,
|
2021-10-24 06:09:43 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-21 21:42:11 +01:00
|
|
|
const handleCloseSidebars = (): void => {
|
|
|
|
if (isMobile && (visibleSidebars.left || visibleSidebars.right)) {
|
|
|
|
return setVisibleSidebars({
|
|
|
|
left: false,
|
2023-10-23 23:33:39 +02:00
|
|
|
right: false,
|
2022-03-21 21:42:11 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-24 06:09:43 +02:00
|
|
|
const swipeableHandlers = useSwipeable({
|
|
|
|
trackMouse: false,
|
|
|
|
trackTouch: true,
|
2022-05-12 20:35:46 +02:00
|
|
|
preventScrollOnSwipe: true,
|
2021-10-24 06:09:43 +02:00
|
|
|
onSwipedRight: () => {
|
|
|
|
if (visibleSidebars.right) {
|
|
|
|
return setVisibleSidebars({ ...visibleSidebars, right: false })
|
|
|
|
}
|
|
|
|
setVisibleSidebars({
|
|
|
|
...visibleSidebars,
|
2023-10-23 23:33:39 +02:00
|
|
|
left: true,
|
2021-10-24 06:09:43 +02:00
|
|
|
})
|
|
|
|
},
|
|
|
|
onSwipedLeft: () => {
|
2022-04-09 11:10:34 +02:00
|
|
|
if (isGuildsChannelsPath(path)) {
|
|
|
|
if (visibleSidebars.left) {
|
|
|
|
return setVisibleSidebars({ ...visibleSidebars, left: false })
|
|
|
|
}
|
|
|
|
setVisibleSidebars({
|
|
|
|
...visibleSidebars,
|
2023-10-23 23:33:39 +02:00
|
|
|
right: true,
|
2022-04-09 11:10:34 +02:00
|
|
|
})
|
2021-10-24 06:09:43 +02:00
|
|
|
}
|
2023-10-23 23:33:39 +02:00
|
|
|
},
|
2021-10-24 06:09:43 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
setMounted(true)
|
|
|
|
}, [])
|
|
|
|
|
|
|
|
if (!mounted) {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
2023-10-23 23:33:39 +02:00
|
|
|
<header className="z-50 flex h-16 items-center justify-between bg-gray-200 px-2 py-3 shadow-lg dark:bg-gray-800">
|
2021-10-24 06:09:43 +02:00
|
|
|
<IconButton
|
2023-10-23 23:33:39 +02:00
|
|
|
className="h-10 w-10 p-2"
|
2022-08-31 21:44:33 +02:00
|
|
|
onClick={() => {
|
2023-10-23 23:33:39 +02:00
|
|
|
return handleToggleSidebars("left")
|
2022-08-31 21:44:33 +02:00
|
|
|
}}
|
2021-10-24 06:09:43 +02:00
|
|
|
>
|
|
|
|
{!visibleSidebars.left ? <MenuIcon /> : <XIcon />}
|
|
|
|
</IconButton>
|
2022-01-01 20:42:25 +01:00
|
|
|
<div
|
2023-10-23 23:33:39 +02:00
|
|
|
data-cy="application-title"
|
|
|
|
className="text-md font-semibold text-green-800 dark:text-green-400"
|
2022-01-01 20:42:25 +01:00
|
|
|
>
|
2021-10-24 06:09:43 +02:00
|
|
|
{title}
|
|
|
|
</div>
|
2023-10-23 23:33:39 +02:00
|
|
|
<div className="flex space-x-2">
|
|
|
|
{title.startsWith("#") && (
|
2021-10-24 06:09:43 +02:00
|
|
|
<IconButton
|
2023-10-23 23:33:39 +02:00
|
|
|
data-cy="icon-button-right-sidebar-members"
|
|
|
|
className="h-10 w-10 p-2"
|
2022-08-31 21:44:33 +02:00
|
|
|
onClick={() => {
|
2023-10-23 23:33:39 +02:00
|
|
|
return handleToggleSidebars("right")
|
2022-08-31 21:44:33 +02:00
|
|
|
}}
|
2021-10-24 06:09:43 +02:00
|
|
|
>
|
|
|
|
{!visibleSidebars.right ? <UsersIcon /> : <XIcon />}
|
|
|
|
</IconButton>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
</header>
|
|
|
|
|
|
|
|
<main
|
2023-10-23 23:33:39 +02:00
|
|
|
className="h-full-without-header relative flex overflow-hidden"
|
2021-10-24 06:09:43 +02:00
|
|
|
{...swipeableHandlers}
|
|
|
|
>
|
|
|
|
<Sidebar
|
2023-10-23 23:33:39 +02:00
|
|
|
direction="left"
|
2021-10-24 06:09:43 +02:00
|
|
|
visible={visibleSidebars.left}
|
|
|
|
isMobile={isMobile}
|
|
|
|
>
|
2023-10-23 23:33:39 +02:00
|
|
|
<div className="left-0 top-0 z-50 flex min-w-[92px] flex-col space-y-4 border-r-2 border-gray-500 bg-gray-200 py-2 dark:border-white/20 dark:bg-gray-800">
|
2021-10-24 06:09:43 +02:00
|
|
|
<IconLink
|
2022-02-19 23:20:33 +01:00
|
|
|
href={`/application/users/settings`}
|
|
|
|
selected={path === `/application/users/settings`}
|
2023-10-23 23:33:39 +02:00
|
|
|
title="Settings"
|
2021-10-24 06:09:43 +02:00
|
|
|
>
|
|
|
|
<Image
|
2022-04-07 16:54:05 +02:00
|
|
|
quality={100}
|
2023-10-23 23:33:39 +02:00
|
|
|
className="rounded-full"
|
2021-10-26 16:38:55 +02:00
|
|
|
src={
|
|
|
|
user.logo == null
|
2023-10-23 23:33:39 +02:00
|
|
|
? "/images/data/user-default.png"
|
2022-04-08 20:59:04 +02:00
|
|
|
: user.logo
|
2021-10-26 16:38:55 +02:00
|
|
|
}
|
2022-01-01 20:42:25 +01:00
|
|
|
alt={"Users's profil picture"}
|
|
|
|
draggable={false}
|
2021-10-24 06:09:43 +02:00
|
|
|
width={48}
|
|
|
|
height={48}
|
|
|
|
/>
|
|
|
|
</IconLink>
|
|
|
|
<IconLink
|
2023-10-23 23:33:39 +02:00
|
|
|
href="/application"
|
|
|
|
selected={path === "/application"}
|
|
|
|
title="Join or create a Guild"
|
2021-10-24 06:09:43 +02:00
|
|
|
>
|
2023-10-23 23:33:39 +02:00
|
|
|
<PlusIcon className="h-12 w-12 text-green-800 dark:text-green-400" />
|
2021-10-24 06:09:43 +02:00
|
|
|
</IconLink>
|
|
|
|
<Divider />
|
|
|
|
<Guilds path={path} />
|
|
|
|
</div>
|
2022-01-01 20:42:25 +01:00
|
|
|
{guildLeftSidebar}
|
2021-10-24 06:09:43 +02:00
|
|
|
</Sidebar>
|
|
|
|
|
|
|
|
<div
|
2023-10-23 23:33:39 +02:00
|
|
|
id="application-page-content"
|
2022-03-21 21:42:11 +01:00
|
|
|
onClick={handleCloseSidebars}
|
2021-10-24 06:09:43 +02:00
|
|
|
className={classNames(
|
2023-10-23 23:33:39 +02:00
|
|
|
"h-full-without-header relative top-0 z-0 flex w-full flex-1 flex-col overflow-y-auto transition",
|
2021-10-24 06:09:43 +02:00
|
|
|
{
|
2023-10-23 23:33:39 +02:00
|
|
|
"absolute opacity-20":
|
|
|
|
isMobile && (visibleSidebars.left || visibleSidebars.right),
|
|
|
|
},
|
2021-10-24 06:09:43 +02:00
|
|
|
)}
|
|
|
|
>
|
|
|
|
{children}
|
|
|
|
</div>
|
|
|
|
|
2022-03-05 18:22:30 +01:00
|
|
|
{isGuildsChannelsPath(path) && (
|
2022-01-01 20:42:25 +01:00
|
|
|
<Sidebar
|
2023-10-23 23:33:39 +02:00
|
|
|
direction="right"
|
2022-01-01 20:42:25 +01:00
|
|
|
visible={visibleSidebars.right}
|
|
|
|
isMobile={isMobile}
|
|
|
|
>
|
|
|
|
<Members />
|
|
|
|
</Sidebar>
|
|
|
|
)}
|
2021-10-24 06:09:43 +02:00
|
|
|
</main>
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|