mirror of
https://github.com/whoswhip/Sharpbin.git
synced 2026-12-06 02:48:43 +00:00
176 lines
6.1 KiB
Svelte
176 lines
6.1 KiB
Svelte
<script lang="ts">
|
|
import { user } from '#lib/stores/user';
|
|
import { resolve } from '$app/paths';
|
|
import { page } from '$app/state';
|
|
import { clearTokens, hasRole } from '#lib/utils/auth';
|
|
import logo from '#lib/assets/favicon.svg';
|
|
import type { PageData } from '../../routes/$types';
|
|
import { slide } from 'svelte/transition';
|
|
import { Menu, X } from '@lucide/svelte';
|
|
import { onMount } from 'svelte';
|
|
|
|
function logout() {
|
|
clearTokens();
|
|
user.set(null);
|
|
window.location.href = '/';
|
|
}
|
|
|
|
interface Props {
|
|
data: PageData;
|
|
}
|
|
|
|
let { data }: Props = $props();
|
|
|
|
let menuOpen = $state(false);
|
|
|
|
let currentPath = page.url.pathname;
|
|
let returnParam =
|
|
currentPath !== '/' && !currentPath.startsWith('/login') && !currentPath.startsWith('/register')
|
|
? `?return=${encodeURIComponent(page.url.pathname + page.url.search)}`
|
|
: '';
|
|
|
|
function handleClickOutside(event: MouseEvent) {
|
|
const navbar = document.getElementById('navbar');
|
|
const mobileMenu = document.getElementById('mobile-menu');
|
|
if (menuOpen && mobileMenu && navbar && !navbar.contains(event.target as Node)) {
|
|
menuOpen = false;
|
|
}
|
|
}
|
|
|
|
onMount(() => {
|
|
document.addEventListener('click', handleClickOutside);
|
|
return () => {
|
|
document.removeEventListener('click', handleClickOutside);
|
|
};
|
|
});
|
|
</script>
|
|
|
|
<div
|
|
class="sticky top-0 left-0 z-100 h-15 w-full border-b border-neutral-800 bg-neutral-900 shadow-md"
|
|
id="navbar"
|
|
>
|
|
<div class="mx-auto hidden h-15 w-[95%] max-w-7xl items-center py-2 md:flex">
|
|
<div class="flex items-center gap-6">
|
|
<a href={resolve('/')} class="group flex items-center gap-2">
|
|
<img src={logo} alt="Sharpbin Logo" class="h-7 w-7 group-hover:opacity-80" />
|
|
<span class="text-xl font-bold text-white group-hover:text-neutral-300">Sharpbin</span>
|
|
</a>
|
|
<div class="h-5 w-px bg-neutral-700"></div>
|
|
<a
|
|
href={resolve('/recent')}
|
|
class="relative text-neutral-300 after:absolute after:-bottom-0.5 after:left-0 after:h-px after:w-0 after:bg-white after:transition-all hover:text-white hover:after:w-full"
|
|
>Recent Pastes</a
|
|
>
|
|
</div>
|
|
{#if $user}
|
|
<div class="ml-auto flex items-center space-x-4">
|
|
{#if hasRole($user.roles, 2) || hasRole($user.roles, 4)}
|
|
<a
|
|
href={resolve('/reports')}
|
|
class="rounded bg-neutral-700 px-3 py-1 text-white transition-colors hover:bg-neutral-600"
|
|
>Reports</a
|
|
>
|
|
{/if}
|
|
<a
|
|
href={resolve(`/user/${$user.username}`)}
|
|
class="rounded bg-neutral-700 px-3 py-1 text-white transition-colors hover:bg-neutral-600"
|
|
>Profile</a
|
|
>
|
|
<button
|
|
onclick={logout}
|
|
class="cursor-pointer rounded bg-neutral-700 px-3 py-1 text-white transition-colors hover:bg-neutral-600"
|
|
>Logout</button
|
|
>
|
|
</div>
|
|
{:else}
|
|
<div class="ml-auto flex items-center space-x-4">
|
|
<a
|
|
href={resolve(`/login${returnParam}`)}
|
|
class="rounded bg-neutral-700 px-3 py-1 text-white transition-colors hover:bg-neutral-600"
|
|
>Login</a
|
|
>
|
|
<!-- eslint-disable svelte/no-navigation-without-resolve -->
|
|
<a
|
|
href={data.options?.registration_enabled ? resolve(`/register${returnParam}`) : '#'}
|
|
class="rounded bg-neutral-700 px-3 py-1 text-white transition-colors hover:bg-neutral-600 {data
|
|
.options?.registration_enabled
|
|
? ''
|
|
: 'cursor-not-allowed bg-neutral-800 text-neutral-500!'}">Register</a
|
|
>
|
|
<!-- eslint-enable svelte/no-navigation-without-resolve -->
|
|
</div>
|
|
{/if}
|
|
</div>
|
|
|
|
<div class="mx-auto flex h-15 w-[90%] max-w-7xl items-center py-2 md:hidden">
|
|
<a href={resolve('/')} class="group">
|
|
<img src={logo} alt="Sharpbin Logo" class="h-8 w-8 group-hover:opacity-80" />
|
|
</a>
|
|
<button
|
|
class="ml-auto flex h-10 w-10 items-center justify-center rounded border border-neutral-700 bg-neutral-800 p-2 hover:bg-neutral-700 focus:outline-none"
|
|
onclick={() => (menuOpen = !menuOpen)}
|
|
aria-label="Toggle Menu"
|
|
aria-expanded={menuOpen}
|
|
>
|
|
{#if menuOpen}
|
|
<X class="pointer-events-none h-6 w-6 text-white" />
|
|
{:else}
|
|
<Menu class="pointer-events-none h-6 w-6 text-white" />
|
|
{/if}
|
|
</button>
|
|
</div>
|
|
{#if menuOpen}
|
|
<div
|
|
class="mx-auto mb-2 w-full rounded-b border-b border-neutral-800 bg-neutral-900 px-2 py-3 md:hidden"
|
|
id="mobile-menu"
|
|
in:slide={{ duration: 180 }}
|
|
out:slide={{ duration: 180 }}
|
|
style="z-index:1000;position:relative;"
|
|
>
|
|
<a
|
|
href={resolve('/recent')}
|
|
class="block w-full rounded bg-neutral-800 px-3 py-2 text-center text-neutral-100 transition-colors hover:bg-neutral-700"
|
|
onclick={() => (menuOpen = false)}>Recent Pastes</a
|
|
>
|
|
{#if $user}
|
|
{#if hasRole($user.roles, 2) || hasRole($user.roles, 4)}
|
|
<a
|
|
href={resolve('/reports')}
|
|
class="mt-1 block w-full rounded bg-neutral-800 px-3 py-2 text-center text-neutral-100 transition-colors hover:bg-neutral-700"
|
|
onclick={() => (menuOpen = false)}>Reports</a
|
|
>
|
|
{/if}
|
|
<a
|
|
href={resolve(`/user/${$user.username}`)}
|
|
class="mt-1 block w-full rounded bg-neutral-800 px-3 py-2 text-center text-neutral-100 transition-colors hover:bg-neutral-700"
|
|
onclick={() => (menuOpen = false)}>Profile</a
|
|
>
|
|
<button
|
|
onclick={() => {
|
|
logout();
|
|
menuOpen = false;
|
|
}}
|
|
class="mt-1 w-full rounded bg-neutral-800 px-3 py-2 text-center text-neutral-100 transition-colors hover:bg-neutral-700"
|
|
>Logout</button
|
|
>
|
|
{:else}
|
|
<a
|
|
href={resolve(`/login${returnParam}`)}
|
|
class="mt-1 block w-full rounded bg-neutral-800 px-3 py-2 text-center text-neutral-100 transition-colors hover:bg-neutral-700"
|
|
onclick={() => (menuOpen = false)}>Login</a
|
|
>
|
|
<!-- eslint-disable svelte/no-navigation-without-resolve -->
|
|
<a
|
|
href={data.options?.registration_enabled ? resolve(`/register${returnParam}`) : '#'}
|
|
class="mt-1 block w-full rounded bg-neutral-800 px-3 py-2 text-center text-neutral-100 transition-colors hover:bg-neutral-700 {data
|
|
.options?.registration_enabled
|
|
? ''
|
|
: 'cursor-not-allowed bg-neutral-900 text-neutral-500'}"
|
|
onclick={() => (menuOpen = false)}>Register</a
|
|
>
|
|
<!-- eslint-enable svelte/no-navigation-without-resolve -->
|
|
{/if}
|
|
</div>
|
|
{/if}
|
|
</div>
|