onedrive/components/Navbar.tsx

161 lines
6.1 KiB
TypeScript
Raw Normal View History

2021-06-23 20:27:51 +00:00
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
2021-12-20 09:10:29 +00:00
import { IconName } from '@fortawesome/fontawesome-svg-core'
2021-08-30 13:34:37 +00:00
import { Dialog, Transition } from '@headlessui/react'
import toast, { Toaster } from 'react-hot-toast'
2021-08-29 21:31:42 +00:00
import Link from 'next/link'
2021-08-30 19:47:25 +00:00
import Image from 'next/image'
2021-08-30 13:34:37 +00:00
import { useRouter } from 'next/router'
2021-08-30 14:51:19 +00:00
import { Fragment, useEffect, useState } from 'react'
2021-06-23 20:27:51 +00:00
import siteConfig from '../config/site.json'
const Navbar = () => {
2021-08-30 13:34:37 +00:00
const router = useRouter()
2021-08-30 14:51:19 +00:00
const [tokenPresent, setTokenPresent] = useState(false)
2021-08-30 13:34:37 +00:00
const [isOpen, setIsOpen] = useState(false)
2021-08-30 14:51:19 +00:00
useEffect(() => {
const storedToken = () => {
for (const r of siteConfig.protectedRoutes) {
if (localStorage.hasOwnProperty(r)) {
return true
}
}
return false
}
setTokenPresent(storedToken())
}, [])
2021-08-30 13:34:37 +00:00
const clearTokens = () => {
setIsOpen(false)
siteConfig.protectedRoutes.forEach(r => {
localStorage.removeItem(r)
})
toast.success('Cleared all tokens')
setTimeout(() => {
router.reload()
}, 1000)
}
return (
2021-12-20 09:10:29 +00:00
<div className="bg-white dark:bg-gray-900 sticky top-0 bg-opacity-80 border-b border-gray-900/10 backdrop-blur-md z-[100]">
<Toaster />
2021-08-30 13:34:37 +00:00
2021-12-20 09:10:29 +00:00
<div className="flex items-center justify-between w-full max-w-5xl mx-auto pr-4 py-1">
2021-08-30 14:51:19 +00:00
<Link href="/">
<a className="dark:text-white hover:opacity-80 flex items-center p-2 space-x-2">
2021-12-29 07:26:04 +00:00
<Image src={siteConfig.icon} alt="icon" width="28" height="28" priority />
2021-12-20 09:10:29 +00:00
<span className="sm:block hidden text-lg font-bold">{siteConfig.title}</span>
2021-08-30 13:34:37 +00:00
</a>
2021-08-30 14:51:19 +00:00
</Link>
2021-12-20 09:10:29 +00:00
<div className="flex items-center space-x-4 text-gray-700">
{siteConfig.links.map(l => (
<a
key={l.name}
href={l.link}
target="_blank"
rel="noopener noreferrer"
className="flex items-center space-x-2 dark:text-white hover:opacity-80"
>
<FontAwesomeIcon icon={['fab', l.name.toLowerCase() as IconName]} />
<span className="text-sm font-medium hidden md:inline-block">{l.name}</span>
2021-12-20 09:10:29 +00:00
</a>
))}
{siteConfig.email && (
<a href={siteConfig.email} className="flex items-center space-x-2 dark:text-white hover:opacity-80">
<FontAwesomeIcon icon={['far', 'envelope']} />
<span className="text-sm font-medium hidden md:inline-block">Email</span>
</a>
)}
2021-08-30 14:51:19 +00:00
{tokenPresent && (
<button
2021-09-04 14:15:09 +00:00
className="hover:bg-gray-200 dark:text-white dark:hover:bg-gray-700 flex items-center p-2 space-x-2 rounded"
2021-08-30 14:51:19 +00:00
onClick={() => setIsOpen(true)}
>
<span className="text-sm font-medium">Logout</span>
2021-08-30 14:51:19 +00:00
<FontAwesomeIcon icon="sign-out-alt" />
</button>
)}
2021-08-30 13:34:37 +00:00
</div>
</div>
2021-08-30 13:34:37 +00:00
<Transition appear show={isOpen} as={Fragment}>
<Dialog as="div" className="fixed inset-0 z-10 overflow-y-auto" open={isOpen} onClose={() => setIsOpen(false)}>
<div className="min-h-screen px-4 text-center">
<Transition.Child
as={Fragment}
enter="ease-out duration-100"
enterFrom="opacity-0"
enterTo="opacity-100"
leave="ease-in duration-50"
leaveFrom="opacity-100"
leaveTo="opacity-0"
>
2021-09-04 14:15:09 +00:00
<Dialog.Overlay className="bg-gray-50 dark:bg-gray-800 fixed inset-0" />
2021-08-30 13:34:37 +00:00
</Transition.Child>
{/* This element is to trick the browser into centering the modal contents. */}
<span className="inline-block h-screen align-middle" aria-hidden="true">
&#8203;
</span>
<Transition.Child
as={Fragment}
enter="ease-out duration-100"
enterFrom="opacity-0 scale-95"
enterTo="opacity-100 scale-100"
leave="ease-in duration-50"
leaveFrom="opacity-100 scale-100"
leaveTo="opacity-0 scale-95"
>
2021-12-17 06:35:24 +00:00
<div className="dark:bg-gray-900 inline-block w-full max-w-md p-6 my-8 overflow-hidden text-left align-middle transition-all transform bg-white rounded-lg">
2021-09-04 14:15:09 +00:00
<Dialog.Title className="dark:text-gray-100 text-lg font-bold text-gray-900">
2021-08-30 13:34:37 +00:00
Clear all tokens?
</Dialog.Title>
<div className="mt-2">
<p className="text-sm text-gray-500">
These tokens are used to authenticate yourself into password protected folders, clearing them means
that you will need to re-enter the passwords again.
</p>
</div>
2021-09-04 14:15:09 +00:00
<div className="dark:text-gray-100 max-h-32 mt-4 overflow-y-scroll font-mono text-sm">
2021-08-30 13:34:37 +00:00
{siteConfig.protectedRoutes.map((r, i) => (
2021-09-04 14:15:09 +00:00
<div key={i} className="flex items-center space-x-1">
2021-08-30 13:34:37 +00:00
<FontAwesomeIcon icon="key" />
<span className="truncate">{r}</span>
</div>
))}
</div>
2021-09-04 14:15:09 +00:00
<div className="flex items-center justify-end mt-8">
2021-08-30 13:34:37 +00:00
<button
className="focus:outline-none focus:ring focus:ring-blue-300 hover:bg-blue-400 inline-flex items-center justify-center px-4 py-2 mr-3 space-x-2 text-white bg-blue-500 rounded"
2021-08-30 13:34:37 +00:00
onClick={() => setIsOpen(false)}
>
Cancel
</button>
<button
className="focus:outline-none focus:ring focus:ring-red-300 hover:bg-red-400 inline-flex items-center justify-center px-4 py-2 space-x-2 text-white bg-red-500 rounded"
2021-08-30 13:34:37 +00:00
onClick={() => clearTokens()}
>
<FontAwesomeIcon icon={['far', 'trash-alt']} />
<span>Clear all</span>
</button>
</div>
</div>
</Transition.Child>
</div>
</Dialog>
</Transition>
</div>
)
}
export default Navbar