"use client"; import { useState, useEffect, FormEvent } from "react"; interface Thought { id: number; author_id: number; content: string; created_at: string; } export default function Home() { // State to store the list of thoughts for the feed const [thoughts, setThoughts] = useState([]); // State for the content of the new thought being typed const [newThoughtContent, setNewThoughtContent] = useState(""); // State to manage loading status const [isLoading, setIsLoading] = useState(true); // State to hold any potential errors during API calls const [error, setError] = useState(null); // Function to fetch the feed from the backend API const fetchFeed = async () => { try { setError(null); const response = await fetch("http://localhost:8000/feed"); if (!response.ok) { throw new Error(`HTTP error! status: ${response.status}`); } const data = await response.json(); // The API returns { thoughts: [...] }, so we access the nested array setThoughts(data.thoughts || []); } catch (e: unknown) { console.error("Failed to fetch feed:", e); setError( "Could not load the feed. The backend might be busy. Please try refreshing." ); } finally { setIsLoading(false); } }; // useEffect hook to fetch the feed when the component first loads useEffect(() => { fetchFeed(); }, []); // Handler for submitting the new thought form const handleSubmitThought = async (e: FormEvent) => { e.preventDefault(); if (!newThoughtContent.trim()) return; // Prevent empty posts try { const response = await fetch("http://localhost:8000/thoughts", { method: "POST", headers: { "Content-Type": "application/json", }, // We are hardcoding author_id: 1 as we don't have auth yet body: JSON.stringify({ content: newThoughtContent, author_id: 1 }), }); if (!response.ok) { throw new Error(`HTTP error! status: ${response.status}`); } // Clear the input box setNewThoughtContent(""); // Refresh the feed to show the new post fetchFeed(); } catch (e: unknown) { console.error("Failed to post thought:", e); setError("Failed to post your thought. Please try again."); } }; return (
{/* Header */}

Thoughts

Your space on the decentralized web.

{/* New Thought Form */}