onedrive/components/Navbar.tsx

157 lines
5.8 KiB
TypeScript
Raw Normal View History

2021-06-23 20:27:51 +00:00
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
2021-08-30 14:51:19 +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-08-29 21:31:42 +00:00
<div className="text-left p-1 bg-white dark:bg-gray-900 sticky top-0 bg-opacity-80 backdrop-blur-md shadow-sm z-[100]">
2021-09-04 14:15:09 +00:00
<div className="flex items-center justify-between w-full max-w-4xl mx-auto">
2021-08-30 13:34:37 +00:00
<Toaster />
2021-08-30 14:51:19 +00:00
<Link href="/">
2021-09-04 14:15:09 +00:00
<a className="dark:text-white hover:opacity-80 flex items-center p-2 space-x-2 text-xl font-bold">
2021-08-30 19:47:25 +00:00
<Image src={siteConfig.icon} alt="icon" width="32" height="32" />
2021-09-04 14:15:09 +00:00
<span className="sm:block hidden">{siteConfig.title}</span>
2021-08-30 13:34:37 +00:00
</a>
2021-08-30 14:51:19 +00:00
</Link>
<div className="flex items-center">
{siteConfig.contacts.map((c, i) => (
<a
key={i}
href={c.link}
target="_blank"
rel="noopener noreferrer"
2021-09-04 14:15:09 +00:00
className="hover:bg-gray-200 dark:text-white dark:hover:bg-gray-700 p-2 rounded"
2021-08-30 14:51:19 +00:00
>
{c.platform === 'email' ? (
<FontAwesomeIcon icon={['far', 'envelope']} size="lg" />
) : (
<FontAwesomeIcon icon={['fab', c.platform as IconName]} size="lg" />
)}
</a>
))}
{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>Logout</span>
<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-09-04 14:15:09 +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 shadow-lg">
<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
2021-09-04 14:15:09 +00:00
className="focus:outline-none focus:ring focus:ring-blue-300 hover:bg-blue-600 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
2021-09-04 14:15:09 +00:00
className="focus:outline-none focus:ring focus:ring-red-300 hover:bg-red-600 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