90 }, []);
91
92 const fetchAndSummarize = async () => {
93 if (!postId) {
94 setError("Please enter a valid Hacker News post ID");
101 setPostDetails(null);
102 setProgress(10);
103 console.log("Starting fetchAndSummarize");
104
105 try {
106 // Fetch post details
107 setProgress(30);
108 console.log("Fetching HN post details");
109 const postResponse = await fetch(`/post-details?id=${postId}`);
110 if (!postResponse.ok) {
111 throw new Error(`Failed to fetch post details: ${postResponse.statusText}`);
112 }
113 const postData = await postResponse.json();
114 setPostDetails(postData);
115 console.log("Post details fetched", postData);
116
117 // Fetch comments and generate summary
118 setProgress(60);
119 console.log("Sending summary request");
120 const summaryResponse = await fetch("/summarize", {
121 method: "POST",
122 headers: {
143 console.log("Summarization complete");
144 } catch (err) {
145 console.error("Fetch Error:", err);
146 setError(`Error: ${err.message}`);
147 setLoading(false);
152 const handleKeyDown = (event) => {
153 if (event.key === "Enter" && !loading) {
154 fetchAndSummarize();
155 }
156 };
179 />
180 <button
181 onClick={fetchAndSummarize}
182 disabled={loading}
183 className="bg-orange-500 text-white p-3 rounded-r-lg hover:bg-orange-600 disabled:opacity-50 transition-colors duration-200 focus:outline-none focus:ring-2 focus:ring-orange-500"
304 }
305
306 // Handle post details fetch
307 if (request.method === "GET" && new URL(request.url).pathname === "/post-details") {
308 const url = new URL(request.url);
316
317 try {
318 const response = await fetch(`https://hacker-news.firebaseio.com/v0/item/${postId}.json`);
319 if (!response.ok) {
320 throw new Error(`Failed to fetch post details: ${response.statusText}`);
321 }
322 const postData = await response.json();
325 });
326 } catch (error) {
327 console.error("Error fetching post details:", error);
328 return new Response(JSON.stringify({ error: "Failed to fetch post details" }), {
329 status: 500,
330 headers: { "Content-Type": "application/json" },
357 const { postId, postDetails } = requestBody;
358
359 // Fetch comments
360 const commentsResponse = await fetch(`https://hn.algolia.com/api/v1/items/${postId}`);
361 const commentsData = await commentsResponse.json();
362