some change

This commit is contained in:
john 2025-05-16 16:09:35 +02:00
parent d4a1492d56
commit 313f1def49
38 changed files with 475 additions and 401 deletions

View file

@ -1,70 +0,0 @@
import { useCallback, useState } from 'react'
import FeedView from './FeedView.tsx'
import { PostsService } from './models/posts/postsService.ts'
import { useUser } from '../store/userStore.ts'
import { MediaService } from '../model/media/mediaService.ts'
import NewPostWidget from '../components/NewPostWidget.tsx'
import { useFeedViewModel } from './FeedView.ts'
import { Post } from './models/posts/posts.ts'
import { Temporal } from '@js-temporal/polyfill'
import SingleColumnLayout from '../layouts/SingleColumnLayout.tsx'
import NavBar from '../components/NavBar.tsx'
interface HomePageProps {
postsService: PostsService
mediaService: MediaService
}
export default function HomePage({ postsService, mediaService }: HomePageProps) {
const [user] = useUser()
const [isSubmitting, setIsSubmitting] = useState(false)
const fetchPosts = useCallback(
async (cursor: string | null, amount: number | null) => {
return postsService.loadPublicFeed(cursor, amount)
},
[postsService],
)
const { pages, setPages, loadNextPage } = useFeedViewModel(fetchPosts)
const onCreatePost = useCallback(
async (content: string, files: { file: File; width: number; height: number }[]) => {
setIsSubmitting(true)
if (user == null) throw new Error('Not logged in')
try {
const media = await Promise.all(
files.map(async ({ file, width, height }) => {
console.debug('do mediaService.uploadFile', file, 'width', width, 'height', height)
const { mediaId, url } = await mediaService.uploadFile(file)
return {
mediaId,
url,
width,
height,
}
}),
)
const postId = await postsService.createNew(user.userId, content, media)
const post = new Post(postId, content, media, Temporal.Now.instant(), user.username)
setPages((pages) => [[post], ...pages])
} catch (error) {
console.error('Failed to create post:', error)
} finally {
setIsSubmitting(false)
}
},
[mediaService, postsService, setPages, user],
)
return (
<SingleColumnLayout navbar={<NavBar />}>
<main className={`w-full max-w-3xl mx-auto`}>
<NewPostWidget onSubmit={onCreatePost} isSubmitting={isSubmitting} />
<FeedView pages={pages} onLoadMore={loadNextPage} />
</main>
</SingleColumnLayout>
)
}