Skip to content

Commit c50b8e6

Browse files
committed
refactor, make reusable for any table
1 parent f7c07d1 commit c50b8e6

File tree

2 files changed

+107
-48
lines changed

2 files changed

+107
-48
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/** @jsxImportSource preact */
2+
3+
/**
4+
* ExpandableTableWrapper - Internal component for making tables collapsible
5+
*
6+
* DEFAULT BEHAVIOR: Content is always visible with no header (allowExpansion=false)
7+
*
8+
* INTERNAL USAGE:
9+
* <ExpandableTableWrapper
10+
* allowExpansion={true}
11+
* defaultExpanded={false}
12+
* title="Table Title"
13+
* description="Expand to view contents"
14+
* >
15+
* {tableContent}
16+
* </ExpandableTableWrapper>
17+
*/
18+
import { useState } from "preact/hooks"
19+
import { clsx } from "~/lib/clsx/clsx.ts"
20+
import tableStyles from "./Tables.module.css"
21+
22+
interface ExpandableTableWrapperProps {
23+
title?: string
24+
description?: string
25+
allowExpansion?: boolean
26+
defaultExpanded?: boolean
27+
children: preact.ComponentChildren
28+
}
29+
30+
export const ExpandableTableWrapper = ({
31+
title,
32+
description,
33+
allowExpansion = false,
34+
defaultExpanded = true,
35+
children,
36+
}: ExpandableTableWrapperProps) => {
37+
const [isExpanded, setIsExpanded] = useState(defaultExpanded)
38+
39+
// If expansion is not allowed, always show content without header
40+
if (!allowExpansion) {
41+
return <>{children}</>
42+
}
43+
44+
return (
45+
<div>
46+
<div
47+
className={tableStyles.expandableHeader}
48+
onClick={() => setIsExpanded(!isExpanded)}
49+
role="button"
50+
tabIndex={0}
51+
onKeyDown={(e) => {
52+
if (e.key === "Enter" || e.key === " ") {
53+
e.preventDefault()
54+
setIsExpanded(!isExpanded)
55+
}
56+
if (e.key === "Escape" && isExpanded) {
57+
setIsExpanded(false)
58+
}
59+
}}
60+
>
61+
<div className={tableStyles.expandableHeaderContent}>
62+
{title && <h2>{title}</h2>}
63+
{!isExpanded && description && (
64+
<p className={tableStyles.expandableDescription}>
65+
<em>{description}</em>
66+
</p>
67+
)}
68+
</div>
69+
<div className={clsx(tableStyles.expandableArrow, isExpanded && tableStyles.expandableArrowExpanded)} />
70+
</div>
71+
72+
{isExpanded && <div className={tableStyles.tableContainer}>{children}</div>}
73+
</div>
74+
)
75+
}

src/features/feeds/components/Tables.tsx

Lines changed: 32 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { StreamsNetworksData, type NetworkData } from "../data/StreamsNetworksDa
1212
import { FEED_CATEGORY_CONFIG } from "../../../db/feedCategories.js"
1313
import { useBatchedFeedCategories, getFeedCategoryFromBatch, getNetworkIdentifier } from "./useBatchedFeedCategories.ts"
1414
import { isSharedSVR, isAaveSVR } from "~/features/feeds/utils/svrDetection.ts"
15+
import { ExpandableTableWrapper } from "./ExpandableTableWrapper.tsx"
1516

1617
const feedItems = monitoredFeeds.mainnet
1718

@@ -589,7 +590,6 @@ export const StreamsNetworkAddressesTable = ({
589590
defaultExpanded?: boolean
590591
} = {}) => {
591592
const [searchValue, setSearchValue] = useState("")
592-
const [isExpanded, setIsExpanded] = useState(defaultExpanded)
593593

594594
const normalizedSearch = searchValue.toLowerCase().replaceAll(" ", "")
595595

@@ -609,53 +609,26 @@ export const StreamsNetworkAddressesTable = ({
609609
return networkMatch || match(mainnetLabel) || match(testnetLabel) || match(mainnetAddr) || match(testnetAddr)
610610
})
611611

612-
return (
613-
<div className={tableStyles.compactNetworksTable}>
614-
{allowExpansion && (
615-
<div
616-
className={tableStyles.expandableHeader}
617-
onClick={() => setIsExpanded(!isExpanded)}
618-
role="button"
619-
tabIndex={0}
620-
onKeyDown={(e) => {
621-
if (e.key === "Enter" || e.key === " ") {
622-
e.preventDefault()
623-
setIsExpanded(!isExpanded)
624-
}
625-
}}
626-
>
627-
<div className={tableStyles.expandableHeaderContent}>
628-
<h2>Streams Verifier Network Addresses</h2>
629-
{!isExpanded && (
630-
<p className={tableStyles.expandableDescription}>
631-
<em>Expand to view verifier proxy addresses required for onchain report verification</em>
632-
</p>
633-
)}
634-
</div>
635-
<div className={clsx(tableStyles.expandableArrow, isExpanded && tableStyles.expandableArrowExpanded)} />
612+
const tableContent = (
613+
<>
614+
<div className={feedList.filterDropdown_search} style={{ padding: "0.5rem" }}>
615+
<div style={{ display: "flex", alignItems: "center", gap: "0.5rem", flex: 1 }}>
616+
<input
617+
type="text"
618+
placeholder="Search"
619+
className={feedList.filterDropdown_searchInput}
620+
value={searchValue}
621+
onInput={(e) => setSearchValue((e.target as HTMLInputElement).value)}
622+
/>
623+
{searchValue && (
624+
<button className={clsx(button.secondary, feedList.clearFilterBtn)} onClick={() => setSearchValue("")}>
625+
Clear filter
626+
</button>
627+
)}
636628
</div>
637-
)}
638-
639-
{(!allowExpansion || isExpanded) && (
640-
<div className={tableStyles.tableContainer}>
641-
<div className={feedList.filterDropdown_search} style={{ padding: "0.5rem" }}>
642-
<div style={{ display: "flex", alignItems: "center", gap: "0.5rem", flex: 1 }}>
643-
<input
644-
type="text"
645-
placeholder="Search"
646-
className={feedList.filterDropdown_searchInput}
647-
value={searchValue}
648-
onInput={(e) => setSearchValue((e.target as HTMLInputElement).value)}
649-
/>
650-
{searchValue && (
651-
<button className={clsx(button.secondary, feedList.clearFilterBtn)} onClick={() => setSearchValue("")}>
652-
Clear filter
653-
</button>
654-
)}
655-
</div>
656-
</div>
629+
</div>
657630

658-
<table className={tableStyles.networksTable}>
631+
<table className={tableStyles.networksTable}>
659632
<thead>
660633
<tr>
661634
<th className={tableStyles.networkColumn}>Network</th>
@@ -796,8 +769,19 @@ export const StreamsNetworkAddressesTable = ({
796769
})}
797770
</tbody>
798771
</table>
799-
</div>
800-
)}
772+
</>
773+
)
774+
775+
return (
776+
<div className={tableStyles.compactNetworksTable}>
777+
<ExpandableTableWrapper
778+
title="Streams Verifier Network Addresses"
779+
description="Expand to view verifier proxy addresses required for onchain report verification"
780+
allowExpansion={allowExpansion}
781+
defaultExpanded={defaultExpanded}
782+
>
783+
{tableContent}
784+
</ExpandableTableWrapper>
801785
</div>
802786
)
803787
}

0 commit comments

Comments
 (0)