From 307330c1d14cc243a00e45a7375e67f55dbead60 Mon Sep 17 00:00:00 2001 From: Sarah Wang Date: Wed, 31 Dec 2025 13:23:27 -0800 Subject: [PATCH] working + updated code --- src/pages/PoemViewer.tsx | 34 +++++++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/src/pages/PoemViewer.tsx b/src/pages/PoemViewer.tsx index f7750fb..df72bb4 100644 --- a/src/pages/PoemViewer.tsx +++ b/src/pages/PoemViewer.tsx @@ -1,17 +1,24 @@ -import React, { useState } from "react"; +import React, { useState, useMemo, useEffect } from "react"; import { Input, Button } from "@chakra-ui/react"; +import { Passages } from "../consts/passages"; const PoemViewer: React.FC = () => { - const [passageText] = useState( - "Twilight settled over Zuckerman’s barn, and a feeling of peace. Fern knew it was almost suppertime but she couldn’t bear to leave. Swallows passed on silent wings, in and out of the doorways, bringing food to their young ones. From across the road a bird sang “Whippoorwill, whippoorwill!” Lurvy sat down under an apple tree and lit his pipe; the animals sniffed the familiar smell of strong tobacco. Wilbur heard the trill of the tree toad and the occasional slamming of the kitchen door. All these sounds made him feel comfortable and happy, for he loved life and loved to be a part of the world on a summer evening. But as he lay there he remembered what the old sheep had told him. The thought of death came to him and he began to tremble with fear." - ); + const [selectedPassageId, setSelectedPassageId] = useState(Passages[0].id); + + const passageText = useMemo(() => { + const passage = Passages.find((p) => p.id === selectedPassageId); + return passage?.text ?? ""; + }, [selectedPassageId]); const words = passageText.split(" "); const [inputValue, setInputValue] = useState(""); - const [visibleIndexes, setVisibleIndexes] = useState( - Array.from({ length: words.length }, (_, i) => i) - ); + const [visibleIndexes, setVisibleIndexes] = useState([]); + + // Reset visible indexes when passage changes + useEffect(() => { + setVisibleIndexes(Array.from({ length: words.length }, (_, i) => i)); + }, [selectedPassageId, words.length]); const handleApply = () => { try { @@ -29,6 +36,19 @@ const PoemViewer: React.FC = () => { return (
+ {/* Passage Selector */} + + {/* Poem */}
{words.map((word, i) => {