Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
161 changes: 121 additions & 40 deletions app/frontend/components/filtered-articles-sidebar.component.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import React from "react";
import React, { useMemo, useState } from "react";
import { List } from "react-window";
import { GoTriangleLeft, GoTriangleRight } from "react-icons/go";
import { FiExternalLink } from "react-icons/fi";
import { BsEye, BsEyeSlash } from "react-icons/bs";
import { getWikiUrl } from "../utils/search-utils";
import type { ArticleRow } from "./article-detail-panel.component";

Expand All @@ -14,25 +15,48 @@ interface FilteredArticlesSidebarProps {
isOpen: boolean;
onToggle: () => void;
onArticleClick?: (article: ArticleRow) => void;
excludedOutliers?: Set<string>;
onToggleOutlier?: (article: string) => void;
onClearOutliers?: () => void;
}

interface SidebarRowProps {
articles: ArticleRow[];
wiki?: { language: string; project: string };
onArticleClick?: (article: ArticleRow) => void;
excludedOutliers?: Set<string>;
onToggleOutlier?: (article: string) => void;
}

function SidebarRow(props: SidebarRowProps): React.ReactElement | null {
const { articles, wiki, onArticleClick } = props;
const { articles, wiki, onArticleClick, excludedOutliers, onToggleOutlier } =
props;
const { index, style } = props as unknown as {
index: number;
style: React.CSSProperties;
};
const articleRow = articles[index];
if (!articleRow) return null;

const isExcluded = excludedOutliers?.has(articleRow.article) ?? false;

return (
<li style={style} className="Item">
<li style={style} className={`Item ${isExcluded ? "is-excluded" : ""}`}>
{onToggleOutlier && (
<button
type="button"
className={`TrimBtn ${isExcluded ? "is-excluded" : ""}`}
onClick={() => onToggleOutlier(articleRow.article)}
title={isExcluded ? "Restore to chart" : "Trim from chart"}
aria-label={
isExcluded
? `Restore ${articleRow.article} to chart`
: `Trim ${articleRow.article} from chart`
}
>
{isExcluded ? <BsEyeSlash /> : <BsEye />}
</button>
)}
<button
type="button"
className="Link"
Expand All @@ -57,46 +81,103 @@ function SidebarRow(props: SidebarRowProps): React.ReactElement | null {
}

const FilteredArticlesSidebar: React.FC<FilteredArticlesSidebarProps> =
React.memo(({ articles, wiki, isOpen, onToggle, onArticleClick }) => {
return (
<div className={`FilteredArticlesSidebar ${isOpen ? "is-open" : ""}`}>
<button
type="button"
className="Toggle"
onClick={onToggle}
aria-label={isOpen ? "Close sidebar" : "Open sidebar"}
>
<span className="Icon">
{isOpen ? <GoTriangleRight /> : <GoTriangleLeft />}
</span>
<span className="Count">
{articles.length}
</span>
</button>
<div className="Content">
<div className="Header">
<span className="Title">
Filtered Articles
</span>
<span className="Count">
{articles.length} article{articles.length !== 1 ? "s" : ""}
React.memo(
({
articles,
wiki,
isOpen,
onToggle,
onArticleClick,
excludedOutliers,
onToggleOutlier,
onClearOutliers,
}) => {
const [excludedOnly, setExcludedOnly] = useState<boolean>(false);

const excludedCount = useMemo(() => {
if (!excludedOutliers?.size) return 0;
return articles.reduce(
(count, row) => count + (excludedOutliers.has(row.article) ? 1 : 0),
0,
);
}, [articles, excludedOutliers]);

const hasExcluded = excludedCount > 0;
const showExcludedOnly = hasExcluded && excludedOnly;

const displayedArticles = useMemo(() => {
if (!showExcludedOnly) return articles;
return articles.filter(
(row) => excludedOutliers?.has(row.article) ?? false,
);
}, [articles, showExcludedOnly, excludedOutliers]);

return (
<div className={`FilteredArticlesSidebar ${isOpen ? "is-open" : ""}`}>
<button
type="button"
className="Toggle"
onClick={onToggle}
aria-label={isOpen ? "Close sidebar" : "Open sidebar"}
>
<span className="Icon">
{isOpen ? <GoTriangleRight /> : <GoTriangleLeft />}
</span>
<span className="Count">{articles.length}</span>
</button>
<div className="Content">
<div className="Header">
<div className="HeaderTop">
<span className="Title">Filtered Articles</span>
<span className="Count">
{displayedArticles.length} article
{displayedArticles.length !== 1 ? "s" : ""}
</span>
</div>
{hasExcluded && (
<div className="TrimControls">
<label className="TrimFilter">
<input
type="checkbox"
checked={excludedOnly}
onChange={(e) => setExcludedOnly(e.target.checked)}
/>
<span>Excluded only ({excludedCount})</span>
</label>
{onClearOutliers && (
<button
type="button"
className="ClearBtn"
onClick={onClearOutliers}
>
Clear all
</button>
)}
</div>
)}
</div>
{isOpen && (
<List
className="List"
rowComponent={SidebarRow}
rowCount={displayedArticles.length}
rowHeight={ITEM_HEIGHT}
rowProps={{
articles: displayedArticles,
wiki,
onArticleClick,
excludedOutliers,
onToggleOutlier,
}}
overscanCount={10}
style={{ maxHeight: LIST_HEIGHT }}
/>
)}
</div>
{isOpen && (
<List
className="List"
rowComponent={SidebarRow}
rowCount={articles.length}
rowHeight={ITEM_HEIGHT}
rowProps={{ articles, wiki, onArticleClick }}
overscanCount={10}
style={{ maxHeight: LIST_HEIGHT }}
/>
)}
</div>
</div>
);
});
);
},
);

FilteredArticlesSidebar.displayName = "FilteredArticlesSidebar";

Expand Down
2 changes: 2 additions & 0 deletions app/frontend/components/topic-detail.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ function TopicDetail() {
actions
wiki={topic.wiki}
topicId={id}
topicName={topic.name}
topicStartDate={topic.start_date}
topicEndDate={topic.end_date}
/>
Expand All @@ -297,6 +298,7 @@ function TopicDetail() {
actions
wiki={topic.wiki}
topicId={id}
topicName={topic.name}
topicStartDate={topic.start_date}
topicEndDate={topic.end_date}
/>
Expand Down
Loading
Loading