3dom4wf7p: Publish public text posts from a feed composer
This commit is contained in:
@@ -4,6 +4,7 @@ import { useCallback, useEffect, useRef, useState } from "react";
|
||||
import { fetchFeedPage, FeedLoadError } from "@/lib/feed-client";
|
||||
import { applyLikeOutcome, LikeError, sendLike } from "@/lib/like-client";
|
||||
import { mergePosts, toCardPosts, type CardPost } from "@/lib/feed-model";
|
||||
import { PostError, publishAndConfirm } from "@/lib/post-client";
|
||||
import { FeedView, type FeedStatus } from "./FeedView";
|
||||
|
||||
const POLL_MS = 45_000;
|
||||
@@ -17,6 +18,12 @@ export function Feed({ onSignedOut }: { onSignedOut: () => void }) {
|
||||
const [pending, setPending] = useState<CardPost[]>([]);
|
||||
const [likeBusy, setLikeBusy] = useState<ReadonlySet<string>>(new Set());
|
||||
const [likeErrors, setLikeErrors] = useState<ReadonlySet<string>>(new Set());
|
||||
const [draft, setDraft] = useState("");
|
||||
const [posting, setPosting] = useState(false);
|
||||
const [postFailed, setPostFailed] = useState(false);
|
||||
// Id of a post already created but not yet seen on the first page. A retry
|
||||
// re-checks the feed instead of creating a duplicate.
|
||||
const unconfirmed = useRef<{ id: string | null }>({ id: null });
|
||||
const sentinel = useRef<HTMLDivElement | null>(null);
|
||||
const busy = useRef(false);
|
||||
const shown = useRef<Set<string>>(new Set());
|
||||
@@ -149,6 +156,31 @@ export function Feed({ onSignedOut }: { onSignedOut: () => void }) {
|
||||
[fail],
|
||||
);
|
||||
|
||||
const publish = useCallback(async () => {
|
||||
const text = draft.trim();
|
||||
if (!text || posting) return;
|
||||
setPosting(true);
|
||||
setPostFailed(false);
|
||||
try {
|
||||
const page = await publishAndConfirm(text, unconfirmed.current);
|
||||
setPosts(mergePosts(toCardPosts(page.items)));
|
||||
setNextCursor(page.nextCursor);
|
||||
setPending([]);
|
||||
setDraft("");
|
||||
} catch (e) {
|
||||
if (e instanceof PostError && e.signedOut) onSignedOut();
|
||||
setPostFailed(true);
|
||||
} finally {
|
||||
setPosting(false);
|
||||
}
|
||||
}, [draft, posting, onSignedOut]);
|
||||
|
||||
function changeDraft(v: string) {
|
||||
// New text is a new post; forget a stale unconfirmed id.
|
||||
if (v !== draft) unconfirmed.current = { id: null };
|
||||
setDraft(v);
|
||||
}
|
||||
|
||||
function showNew() {
|
||||
setPosts((cur) => mergePosts(cur, pending));
|
||||
setPending([]);
|
||||
@@ -167,6 +199,7 @@ export function Feed({ onSignedOut }: { onSignedOut: () => void }) {
|
||||
onToggleLike={(p) => void toggleLike(p)}
|
||||
likeBusy={likeBusy}
|
||||
likeErrors={likeErrors}
|
||||
composer={{ value: draft, busy: posting, error: postFailed, onChange: changeDraft, onSubmit: () => void publish() }}
|
||||
sentinelRef={sentinel}
|
||||
/>
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user