Skip to content

Commit

Permalink
reset panes if search result
Browse files Browse the repository at this point in the history
  • Loading branch information
steezeburger committed Aug 16, 2024
1 parent e820645 commit 6ab9df8
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions web/src/components/WikipediaBrowser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import "./WikipediaBrowser.css";
interface Pane {
title: string;
content: string;
isHomepage: boolean;
isSearchResult: boolean;
width: number;
}

const SearchBar = memo(({ onSearch }: { onSearch: (term: string, isHomepage: boolean) => void }) => {
const SearchBar = memo(({ onSearch }: { onSearch: (term: string, isSearchResult: boolean) => void }) => {
const [searchTerm, setSearchTerm] = useState("");

const handleSubmit = (e: React.FormEvent) => {
Expand Down Expand Up @@ -125,7 +125,7 @@ const WikipediaBrowser: React.FC = () => {
const [clickedLinks, setClickedLinks] = useState<Set<string>>(new Set());

useEffect(() => {
fetchWikipediaContent("Main Page", true);
fetchWikipediaContent("Main Page");

// use wikipedia's stylesheet
const link = document.createElement("link");
Expand All @@ -139,15 +139,14 @@ const WikipediaBrowser: React.FC = () => {
};
}, []);

const fetchWikipediaContent = useCallback(async (title: string, isHomepage: boolean = false) => {
const fetchWikipediaContent = useCallback(async (title: string, isSearchResult: boolean = false) => {
setIsLoading(true);
try {
const response = await fetch(`https://en.wikipedia.org/w/api.php?action=parse&format=json&page=${encodeURIComponent(title)}&prop=text&formatversion=2&origin=*`);
const data = await response.json();
if (data.parse) {
const newPane = { title: data.parse.title, content: data.parse.text, isHomepage, width: 720 };
if (panes.length === 0 || (activePane === 0 && panes[0].isHomepage) || isHomepage) {
// For the first pane or updating the homepage, just set or update it
const newPane = { title: data.parse.title, content: data.parse.text, isSearchResult, width: 720 };
if (panes.length === 0 || isSearchResult) {
setPanes([newPane]);
setActivePane(0);
} else {
Expand Down

0 comments on commit 6ab9df8

Please sign in to comment.