This commit is contained in:
john 2025-05-03 20:18:08 +02:00
parent 2586dc87c8
commit e84cf232a5
13 changed files with 605 additions and 139 deletions

View file

@ -1,42 +0,0 @@
#root {
max-width: 1280px;
margin: 0 auto;
padding: 2rem;
text-align: center;
}
.logo {
height: 6em;
padding: 1.5em;
will-change: filter;
transition: filter 300ms;
}
.logo:hover {
filter: drop-shadow(0 0 2em #646cffaa);
}
.logo.react:hover {
filter: drop-shadow(0 0 2em #61dafbaa);
}
@keyframes logo-spin {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}
@media (prefers-reduced-motion: no-preference) {
a:nth-of-type(2) .logo {
animation: logo-spin infinite 20s linear;
}
}
.card {
padding: 2em;
}
.read-the-docs {
color: #888;
}

View file

@ -1,34 +1,13 @@
import { useState } from 'react'
import reactLogo from './assets/react.svg'
import viteLogo from '/vite.svg'
import './App.css'
import { AuthorPage } from './pages/AuthorPage.tsx'
import { BrowserRouter, Route, Routes } from 'react-router'
function App() {
const [count, setCount] = useState(0)
return (
<>
<div>
<a href="https://vite.dev" target="_blank">
<img src={viteLogo} className="logo" alt="Vite logo" />
</a>
<a href="https://react.dev" target="_blank">
<img src={reactLogo} className="logo react" alt="React logo" />
</a>
</div>
<h1>Vite + React</h1>
<div className="card">
<button onClick={() => setCount((count) => count + 1)}>
count is {count}
</button>
<p>
Edit <code>src/App.tsx</code> and save to test HMR
</p>
</div>
<p className="read-the-docs">
Click on the Vite and React logos to learn more
</p>
</>
<BrowserRouter>
<Routes>
<Route path="/u/:username" element={<AuthorPage />} />
</Routes>
</BrowserRouter>
)
}

26
src/api/api.ts Normal file
View file

@ -0,0 +1,26 @@
import { components } from './schema.ts'
// TODO for now we just assume that the API and client are running on the same host
// i think this might change but dependes on what we do with deployment
const ApiHost = `http://${location.hostname}:5181`
console.debug('API HOST IS', ApiHost)
export async function loadPostsForAuthor(
authorId: string,
count?: number,
cursor?: string,
): Promise<components['schemas']['GetAuthorPostsResponse']> {
const url = new URL(`authors/${authorId}/posts`, ApiHost)
if (count != null) url.searchParams.set('count', count.toString())
if (cursor) url.searchParams.set('cursor', cursor)
const request = new Request(url)
const response = await fetch(request)
if (!response.ok) throw new Error(await response.text())
return await response.json()
}

View file

@ -0,0 +1,38 @@
import { Post } from '../model/posts/posts.ts'
import { Temporal } from '@js-temporal/polyfill'
interface PostsFeedProps {
posts: Post[]
}
export function PostsFeed({ posts }: PostsFeedProps) {
const formatDate = (date: Temporal.PlainDateTime) => {
return date.toLocaleString('en-US', {
year: 'numeric',
month: 'short',
day: 'numeric',
hour: '2-digit',
minute: '2-digit',
})
}
return (
<div className="flex flex-col gap-6 w-full">
{posts.map((post) => (
<article className="w-full p-4 " key={post.postId}>
<div className="text-sm text-gray-500 mb-3">{formatDate(post.createdAt)}</div>
<div className="text-gray-800 mb-4 whitespace-pre-wrap">{post.content}</div>
{post.media.length > 0 && (
<div className="grid gap-4 grid-cols-1">
{post.media.map((src) => (
<img key={src} src={src} alt="" className="w-full h-auto" loading="lazy" />
))}
</div>
)}
</article>
))}
</div>
)
}

13
src/hooks/useAsyncData.ts Normal file
View file

@ -0,0 +1,13 @@
import { useEffect, useState } from 'react'
export function useAsyncState<T>(loader: () => Promise<T>): T | undefined {
const [state, setState] = useState<T>()
useEffect(() => {
setTimeout(async () => {
setState(await loader())
})
}, [loader])
return state
}

View file

@ -1,68 +1,16 @@
@import "tailwindcss";
:root {
font-family: system-ui, Avenir, Helvetica, Arial, sans-serif;
line-height: 1.5;
font-weight: 400;
color-scheme: light dark;
color: rgba(255, 255, 255, 0.87);
background-color: #242424;
font-synthesis: none;
text-rendering: optimizeLegibility;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
font-family: system-ui, Avenir, Helvetica, Arial, sans-serif;
font-synthesis: none;
text-rendering: optimizeLegibility;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
a {
font-weight: 500;
color: #646cff;
text-decoration: inherit;
}
a:hover {
color: #535bf2;
}
body {
margin: 0;
display: flex;
place-items: center;
min-width: 320px;
min-height: 100vh;
}
h1 {
font-size: 3.2em;
line-height: 1.1;
}
button {
border-radius: 8px;
border: 1px solid transparent;
padding: 0.6em 1.2em;
font-size: 1em;
font-weight: 500;
font-family: inherit;
background-color: #1a1a1a;
cursor: pointer;
transition: border-color 0.25s;
}
button:hover {
border-color: #646cff;
}
button:focus,
button:focus-visible {
outline: 4px auto -webkit-focus-ring-color;
}
@media (prefers-color-scheme: light) {
:root {
color: #213547;
background-color: #ffffff;
}
a:hover {
color: #747bff;
}
button {
background-color: #f9f9f9;
}
}
margin: 0;
width: 100vw;
height: 100vh;
}

20
src/model/posts/posts.ts Normal file
View file

@ -0,0 +1,20 @@
import { Temporal } from '@js-temporal/polyfill'
import { components } from '../../api/schema.ts'
export class Post {
public readonly postId: string
public readonly content: string
public readonly media: string[]
public readonly createdAt: Temporal.PlainDateTime
constructor(postId: string, content: string, media: string[], createdAt: Temporal.PlainDateTime) {
this.postId = postId
this.content = content
this.media = media
this.createdAt = createdAt
}
public static fromDto(dto: components['schemas']['AuthorPostDto']): Post {
return new Post(dto.postId, dto.content, dto.media, Temporal.PlainDateTime.from(dto.createdAt))
}
}

23
src/pages/AuthorPage.tsx Normal file
View file

@ -0,0 +1,23 @@
import { PostsFeed } from '../components/PostsFeed.tsx'
import { useCallback } from 'react'
import { useParams } from 'react-router'
import { loadPostsForAuthor } from '../api/api.ts'
import { useAsyncState } from '../hooks/useAsyncData.ts'
import { Post } from '../model/posts/posts.ts'
export function AuthorPage() {
const { username } = useParams()
const fetchPosts = useCallback(async () => {
const result = await loadPostsForAuthor(username!)
return result.posts.map((post) => Post.fromDto(post))
}, [username])
const posts = useAsyncState(fetchPosts)
return (
<main className={`w-full max-w-full`}>
<PostsFeed posts={posts ?? []} />
</main>
)
}