mirror of
https://github.com/whoswhip/Sharpbin.git
synced 2026-12-06 02:48:43 +00:00
feat: add recent pastes page with data fetching and display logic
This commit is contained in:
@@ -22,7 +22,11 @@
|
||||
|
||||
<div class="top-0 left-0 z-100 h-15 w-full bg-neutral-900">
|
||||
<div class="mx-auto flex h-15 w-[95%] max-w-7xl items-center py-2">
|
||||
<a href={resolve('/')} class="text-xl font-bold text-white hover:text-neutral-300">Sharpbin</a>
|
||||
<div>
|
||||
<a href={resolve('/')} class="text-xl font-bold text-white hover:text-neutral-300">Sharpbin</a
|
||||
>
|
||||
<a href={resolve('/recent')} class="ml-6 text-white hover:text-neutral-300">Recent Pastes</a>
|
||||
</div>
|
||||
{#if $user}
|
||||
<div class="ml-auto flex items-center space-x-4">
|
||||
{#if $user.roles.includes(1) || $user.roles.includes(255)}
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
import type { PageServerLoad } from './$types';
|
||||
import type { Paste } from '$lib/types/paste';
|
||||
import { extractError } from '$lib/utils/misc';
|
||||
|
||||
export const load: PageServerLoad = async ({ fetch }) => {
|
||||
try {
|
||||
const res = await fetch('/api/paste/recent');
|
||||
if (!res.ok) {
|
||||
const errorMsg = extractError(await res.json()) || 'Failed to fetch recent pastes.';
|
||||
throw new Error(errorMsg);
|
||||
}
|
||||
const recentPastes: Paste[] = await res.json();
|
||||
return { recentPastes };
|
||||
}
|
||||
catch (error) {
|
||||
console.error(error);
|
||||
return { recentPastes: [] };
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,42 @@
|
||||
<script lang="ts">
|
||||
import type { PageData } from './$types';
|
||||
import { extractDateFromUUIDv7, dateToRelativeString } from '$lib/utils/misc';
|
||||
import { resolve } from '$app/paths';
|
||||
export let data: PageData;
|
||||
</script>
|
||||
|
||||
<main
|
||||
class="flex min-h-[calc(100vh-120px)] w-full flex-col items-center justify-center pt-5 pb-5 text-white"
|
||||
>
|
||||
<div class="w-[95%] max-w-7xl rounded border-2 border-neutral-800 bg-neutral-900 p-6">
|
||||
{#if data.recentPastes.length === 0}
|
||||
<p class="text-lg">No recent pastes available.</p>
|
||||
{:else}
|
||||
<h1 class="mb-4 text-3xl font-bold">Recent Pastes</h1>
|
||||
<div class="grid grid-cols-1 gap-4">
|
||||
{#each data.recentPastes as paste}
|
||||
{@const createdAt = extractDateFromUUIDv7(paste.uuid)}
|
||||
<a
|
||||
href={resolve(`/${paste.id}`)}
|
||||
class="flex flex-col gap-2 rounded border border-neutral-700 bg-neutral-800 px-5 py-4 transition-colors duration-200 hover:bg-neutral-700 focus:ring-2 focus:ring-neutral-600 focus:outline-none"
|
||||
>
|
||||
<span class="truncate text-lg font-semibold text-neutral-100 hover:text-white">
|
||||
{paste.title || 'Untitled Paste'}
|
||||
</span>
|
||||
<p class="text-sm text-neutral-400">
|
||||
{#if createdAt}
|
||||
{#if createdAt.getTime() > Date.now() - 86_400_000}
|
||||
Created {dateToRelativeString(createdAt)}
|
||||
{:else}
|
||||
Created on {createdAt.toLocaleDateString()}
|
||||
{/if}
|
||||
{:else}
|
||||
Creation date unknown
|
||||
{/if}
|
||||
</p>
|
||||
</a>
|
||||
{/each}
|
||||
</div>
|
||||
{/if}
|
||||
</div>
|
||||
</main>
|
||||
Reference in New Issue
Block a user