fix: ssr request to user; navbar positioning

This commit is contained in:
2026-01-06 20:36:22 -05:00
parent a912d0463b
commit bffc4e2bf4
9 changed files with 64 additions and 44 deletions
@@ -20,7 +20,7 @@
: '';
</script>
<div class="z-100 h-15 w-full bg-neutral-900">
<div class="fixed top-0 left-0 z-100 h-15 w-full bg-neutral-900">
<div class="mx-auto flex h-15 max-w-5xl items-center px-4 py-2">
<a href={resolve('/')} class="text-xl font-bold text-white hover:text-neutral-300">Sharpbin</a>
{#if $user}
@@ -46,4 +46,5 @@
</svelte:head>
<Navbar {data} />
<div class="h-15 w-full"></div>
<slot />
+2 -2
View File
@@ -100,7 +100,7 @@
</svelte:head>
<main
class="flex min-h-screen w-full flex-col items-center justify-center bg-neutral-950 text-white"
class="flex min-h-[calc(100vh-60px)] w-full flex-col items-center justify-center bg-neutral-950 text-white pt-5"
>
<div class="w-[95%] max-w-7xl rounded border-2 border-neutral-800 bg-neutral-900 p-6">
<h1 class="mb-6 text-center text-4xl font-bold">Create a Paste</h1>
@@ -158,7 +158,7 @@
{#if error}
<div
transition:fly={{ y: 40, duration: 300 }}
class="rounded border border-red-900 bg-red-950 p-2 text-sm text-red-200"
class="rounded border border-red-900 bg-red-950 p-2 text-sm text-red-200 mt-2"
>
{error}
</div>
@@ -308,7 +308,7 @@
</svelte:head>
<main
class="flex min-h-screen w-full flex-col items-center justify-center bg-neutral-950 text-white"
class="flex w-full min-h-[calc(100vh-60px)] flex-col items-center justify-center bg-neutral-950 text-white pt-5 pb-5"
>
<div
class="max-h-[80vh] w-[95%] max-w-7xl rounded border-2 border-neutral-800 bg-neutral-900 p-4"
@@ -460,7 +460,7 @@
<div class="overflow-hidden">
<div
class="flex items-center justify-between gap-2 rounded-t-md border-b border-neutral-700 bg-neutral-800 px-3 py-2"
class="flex flex-col md:flex-row items-center justify-between gap-2 rounded-t-md border-b border-neutral-700 bg-neutral-800 px-3 py-2"
>
<div class="flex items-center gap-2 text-sm text-neutral-300">
<span class="font-medium text-neutral-100"
@@ -621,7 +621,7 @@
</div>
{#if editing}
<textarea
class="mb-3 max-h-[60vh] min-h-10 w-full rounded-b bg-neutral-800 p-2 font-mono"
class="mb-3 max-h-[40vh] min-h-10 w-full rounded-b bg-neutral-800 p-2 font-mono"
rows="14"
placeholder="Paste content"
bind:value={editContent}
@@ -637,8 +637,7 @@
class:hidden={contentRendered && !editing}
></div>
<code
class="codeblock-with-lines hidden max-w-full overflow-x-auto overflow-y-auto"
style="max-width:100vw; min-width:0;"
class="codeblock-with-lines overflow-x-auto overflow-y-auto"
class:hidden={!contentRendered}
>
<!-- eslint-disable-next-line svelte/no-at-html-tags -->
@@ -687,7 +686,9 @@
display: block;
width: 100%;
max-width: 100%;
overflow: visible;
overflow: hidden;
flex: 1 1 auto;
max-height: 1%;
}
.codeblock-with-lines pre {
display: block;
@@ -701,7 +702,7 @@
box-sizing: border-box;
min-width: 0;
width: 100%;
max-height: 60vh;
max-height: 40vh;
}
.code-row {
display: flex;
@@ -68,7 +68,7 @@
</script>
<main
class="flex min-h-screen w-full flex-col items-center justify-center bg-neutral-950 text-white"
class="flex min-h-[calc(100vh-60px)] w-full flex-col items-center justify-center bg-neutral-950 text-white"
>
<div class="w-full max-w-md rounded border-2 border-neutral-800 bg-neutral-900 p-6">
<h1 class="mb-6 text-center text-3xl font-bold">Login</h1>
@@ -103,7 +103,7 @@
</script>
<main
class="flex min-h-screen w-full flex-col items-center justify-center bg-neutral-950 text-white"
class="flex min-h-[calc(100vh-60px)] w-full flex-col items-center justify-center bg-neutral-950 text-white"
>
<div class="w-full max-w-md rounded border-2 border-neutral-800 bg-neutral-900 p-6">
<h1 class="mb-6 text-center text-3xl font-bold">Register</h1>
@@ -2,15 +2,33 @@ import type { PageServerLoad } from './$types';
import { getServerToken } from '$lib/utils/auth';
import type { User } from '$lib/types/user';
const apiUrl = process.env.VITE_API_URL || 'http://localhost:5050';
export const load: PageServerLoad = async ({ params, fetch, cookies, url }) => {
const { username } = params;
const token = getServerToken(cookies);
const res = await fetch(`/api/user/${username}`, {
const res = await fetch(`${apiUrl}/api/user/${username}`, {
headers: {
Authorization: token ? `Bearer ${token}` : ''
}
});
const user = (await res.json()) as User;
if (res.status === 500) {
return {
status: 500,
error: { message: 'Internal server error' }
};
}
const json = await res.json();
if (res.status !== 200) {
return {
status: res.status,
error: { message: json.message || 'An error occurred' }
};
}
const user = json as User;
return { user, url: url.href };
};
@@ -22,14 +22,14 @@
export let data: PageData;
let currentPage = data.user.pagination?.page ?? 1;
let pagination: Pagination = data.user.pagination ?? {
let currentPage = data.user?.pagination?.page ?? 1;
let pagination: Pagination = data.user?.pagination ?? {
page: 1,
pageSize: 50,
totalCount: 0,
totalPages: 1
};
let pastes = data.user.pastes ?? [];
let pastes = data.user?.pastes ?? [];
let loading = false;
let isOwner = false;
let showEditModal = false;
@@ -37,7 +37,7 @@
let showRoleModal = false;
let modalError = '';
$: isOwner = data.user && $user ? data.user.uuid === $user.uuid : false;
$: isOwner = data.user && $user ? data.user?.uuid === $user.uuid : false;
const roleOptions = [
{ label: 'Member', value: 0 },
@@ -50,7 +50,7 @@
if (pageNum < 1 || pageNum > (pagination.totalPages || 1) || loading) return;
loading = true;
const token = getToken();
const res = await fetch(`/api/user/${data.user.username}?page=${pageNum}`, {
const res = await fetch(`/api/user/${data.user?.username}?page=${pageNum}`, {
headers: token ? { Authorization: `Bearer ${token}` } : {}
});
if (!res.ok) {
@@ -67,7 +67,7 @@
async function handleUserUpdate(displayName?: string, selectedRoles?: number[]) {
loading = true;
const token = getToken();
const res = await fetch(`/api/user/uuid/${data.user.uuid}`, {
const res = await fetch(`/api/user/uuid/${data.user?.uuid}`, {
method: 'PATCH',
headers: {
'Content-Type': 'application/json',
@@ -86,9 +86,9 @@
}
showEditModal = false;
showRoleModal = false;
data.user.displayName = displayName || data.user.displayName;
data.user!.displayName = displayName || data.user!.displayName;
if (selectedRoles !== undefined) {
data.user.roles = selectedRoles;
data.user!.roles = selectedRoles;
}
modalError = '';
}
@@ -96,7 +96,7 @@
async function handleDeleteAccount() {
loading = true;
const token = getToken();
const res = await fetch(`/api/user/uuid/${data.user.uuid}`, {
const res = await fetch(`/api/user/uuid/${data.user?.uuid}`, {
method: 'DELETE',
headers: {
Authorization: `Bearer ${token}`
@@ -114,22 +114,22 @@
</script>
<svelte:head>
<title>{data.user.displayName || data.user.username} - User Profile</title>
<title>{data.user?.displayName || data.user?.username} - User Profile</title>
<meta property="og:title" content="{data.user.displayName || data.user.username} - User Profile" />
<meta property="og:description" content="View the profile and {pagination.totalCount} paste{pagination.totalCount !== 1 ? 's' : ''} of {data.user.displayName || data.user.username} on Sharpbin." />
<meta property="og:title" content="{data.user?.displayName || data.user?.username} - User Profile" />
<meta property="og:description" content="View the profile and {pagination.totalCount} paste{pagination.totalCount !== 1 ? 's' : ''} of {data.user?.displayName || data.user?.username} on Sharpbin." />
<meta property="og:type" content="profile" />
<meta property="og:url" content="{data.url}" />
<meta property="og:site_name" content="Sharpbin" />
<meta property="profile:username" content="{data.user.username}" />
<meta property="profile:username" content="{data.user?.username}" />
</svelte:head>
<main
class="flex min-h-screen w-full flex-col items-center justify-center bg-neutral-950 text-white"
class="flex min-h-[calc(100vh-60px)] w-full flex-col items-center justify-center bg-neutral-950 text-white pt-5"
>
<div class="w-[95%] max-w-5xl rounded border-2 border-neutral-800 bg-neutral-900 p-6">
<h1 class="flex items-center justify-center gap-4 text-center text-4xl font-bold">
{#if data.user.roles.includes(403)}
{#if data.user?.roles.includes(403)}
<span
class="relative flex h-7 w-7 items-center justify-center overflow-hidden rounded-full"
use:tooltip={roles[403]}
@@ -137,8 +137,8 @@
<User class="h-8 w-8 text-neutral-500" />
<Ban class="absolute h-7 w-7 text-red-500" />
</span>
{:else if data.user.roles.includes(1) || data.user.roles.includes(255)}
<span use:tooltip={roles[Math.max(...data.user.roles) as keyof typeof roles]}>
{:else if data.user?.roles.includes(1) || data.user?.roles.includes(255)}
<span use:tooltip={roles[Math.max(...data.user?.roles) as keyof typeof roles]}>
<ShieldUser class="h-8 w-8 text-neutral-400" />
</span>
{:else}
@@ -146,12 +146,12 @@
<User class="h-8 w-8 text-neutral-400" />
</span>
{/if}
{data.user.displayName || data.user.username}
{data.user?.displayName || data.user?.username}
</h1>
{#if data.user.displayName && data.user.displayName !== data.user.username}
{#if data.user?.displayName && data.user?.displayName !== data.user?.username}
<div class="mb-2 flex items-center justify-center">
<AtSign class="mr-2 inline h-5 w-5 text-neutral-400" />
<span class="text-neutral-400">{data.user.username}</span>
<span class="text-neutral-400">{data.user?.username}</span>
</div>
{/if}
<div class="mb-2 flex flex-wrap items-center justify-center gap-4 text-sm">
@@ -159,9 +159,9 @@
<CalendarDays class="mr-2 h-6 w-6 text-neutral-400" />
<span
class="text-neutral-400"
use:tooltip={extractDateFromUUIDv7(data.user.uuid)?.toLocaleString() ?? 'Unknown'}
use:tooltip={extractDateFromUUIDv7(data.user?.uuid)?.toLocaleString() ?? 'Unknown'}
>
Joined {extractDateFromUUIDv7(data.user.uuid)?.toLocaleDateString() ?? 'Unknown'}
Joined {extractDateFromUUIDv7(data.user?.uuid)?.toLocaleDateString() ?? 'Unknown'}
</span>
</div>
<div class="flex shrink-0 items-center">
@@ -172,18 +172,18 @@
</div>
<div class="flex shrink-0 items-center">
<Hash class="mr-1 h-6 w-6 text-neutral-400" />
<span class="text-neutral-400" use:tooltip={`User #${data.user.uid}`}>
{data.user.uid}
<span class="text-neutral-400" use:tooltip={`User #${data.user?.uid}`}>
{data.user?.uid}
</span>
</div>
{#if isOwner && data.user.lastLogin}
{#if isOwner && data.user?.lastLogin}
<div class="flex shrink-0 items-center">
<Clock class="mr-2 h-6 w-6 text-neutral-400" />
<span
class="text-neutral-400"
use:tooltip={new Date(data.user.lastLogin).toLocaleString()}
use:tooltip={new Date(data.user?.lastLogin).toLocaleString()}
>
Last login {dateToRelativeString(new Date(data.user.lastLogin))}
Last login {dateToRelativeString(new Date(data.user?.lastLogin))}
</span>
</div>
{/if}
@@ -303,7 +303,7 @@
mode="multiselect"
title="Edit User Roles"
items={roleOptions}
initialValue={data.user.roles}
initialValue={data.user?.roles}
error={modalError}
onConfirm={(value) => handleUserUpdate(undefined, value as number[])}
onCancel={() => {