import { jsxAsElement as jsx } from 'web-shared/src/lib/jsx'; import { TypedAttributes } from '../htmlElementTyped'; import { PostJSON } from 'web-shared/src/types/postJson'; import { oneDay } from './updatedAt'; import { Post } from '../../../build/steps/posts/create-post-object'; type CategorisedPosts = Record; const basePostsToShow = 25; export class RecentPosts extends HTMLElement { postsToShow = basePostsToShow; constructor() { super(); } fetchPosts = async (): Promise => { const postsResponse = await fetch('posts.json'); const posts = ((await postsResponse.json()) as { posts: PostJSON[] }).posts; return { All: posts.filter((p) => p.publishedAt), 'Updated this Month': posts .filter((p) => { return ( Math.abs( (Date.now().valueOf() - (p.updatedAt || p.publishedAt || 0)) / oneDay, ) < 30 ); }) .sort( (a, b) => (b.updatedAt || b.publishedAt || 0) - (a.updatedAt || a.publishedAt || 0), ), 'Weekly updates': posts.filter((post) => post.emoji === '📅'), Programming: posts.filter((post) => post.emoji === '💻'), Photography: posts.filter((post) => ['✈️', '📷', '🎤', '🏚️'].includes(post.emoji as string), ), Games: posts.filter((post) => post.emoji === '🎮'), Thoughts: posts.filter((post) => post.emoji === '🧠'), Music: posts.filter((post) => post.emoji === '🎵'), }; }; renderPostList = ( selectedPostOption: keyof T, postOptions: Record, ) => { const posts = postOptions[selectedPostOption]; this.replaceChildren(

{selectedPostOption}

,
Category: {...Object.keys(postOptions).map((postOption) => ( ))}
, , posts.length > this.postsToShow ? (
showing {this.postsToShow} / {posts.length} posts - 
) : (
showing {Math.min(this.postsToShow, posts.length)} / {posts.length}{' '} posts
),
, ); }; render = async () => { this.innerHTML = ''; this.append( , ); const categorised = await this.fetchPosts(); console.log(categorised); this.renderPostList('All', categorised); }; connectedCallback() { void this.render(); } }