Shallie/src/routes/[tag]/+page.svelte

74 lines
1.8 KiB
Svelte

<script lang="ts">
import { Hero, PostCard, Index, TagHeader, Post, Container } from '@damillora/plachta';
import { browsePostWithTag, readTag } from '$lib/content/contentApi';
import type { Load } from '@sveltejs/kit';
import { onMount } from 'svelte';
import { browser } from '$app/environment';
import dayjs from 'dayjs';
import TagSeo from '$lib/components/SEO/TagSEO.svelte';
export let data;
let { posts } = data;
let newPosts: any[] = [];
let page = 1;
$: posts = [...posts, ...newPosts];
async function loadPage() {
const posts = await browsePostWithTag(data.tag.slug, page);
newPosts = posts;
}
let footer: HTMLElement;
onMount(() => {
if (browser) {
const handleIntersect: IntersectionObserverCallback = (entries, observer) => {
const first = entries[0];
if (first.isIntersecting) {
page++;
loadPage();
}
};
const options = { threshold: 0.125, rootMargin: '-100% 0% 100%' };
const observer = new IntersectionObserver(handleIntersect, options);
observer.observe(footer);
}
});
</script>
<svelte:head>
<title>Tag: {data.tag.name} - Damillora's Virtual Memoir</title>
</svelte:head>
<TagSeo tag={data.tag} />
<Hero background={data.tag.feature_image ?? `/images/default-feature.jpg`} />
<Container>
<Post>
<TagHeader
accent_color={data.tag.accent_color}
name={data.tag.name}
description={data.tag.description}
/>
</Post>
<Index>
{#each posts as post}
<PostCard
title={post.title}
authors={post.authors}
primary_tag={post.primary_tag}
date={dayjs(post.published_at).format('DD MMM YYYY')}
reading_time={`${post.reading_time} min read`}
excerpt={post.excerpt}
feature_image={post.feature_image ?? '/images/default-feature.jpg'}
url={post.url}
/>
{/each}
<div bind:this={footer} />
</Index>
</Container>