-
Notifications
You must be signed in to change notification settings - Fork 7
Proposal page polish: tabs, description measure, collapsible propdates #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sktbrd
wants to merge
6
commits into
BuilderOSS:main
Choose a base branch
from
sktbrd:feat/proposal-page-tabs
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
15f8858
feat(proposals): pure tab model for the proposal detail page
sktbrd 4c1754e
feat(proposals): add ProposalTabs client shell
sktbrd b84ffcf
feat(proposals): tab the description, transactions, votes and propdates
sktbrd 758470c
feat(propdates): collapse older updates, expand the newest
sktbrd a9e74cf
fix(propdates): hoist reply-cancel side effect out of setExpanded upd…
sktbrd fc31c3a
fix(ProposalTabs): unique ids, atomic tab activation, doc cost note
sktbrd File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,167 @@ | ||
| 'use client' | ||
|
|
||
| import { | ||
| type ReactNode, | ||
| useCallback, | ||
| useEffect, | ||
| useId, | ||
| useMemo, | ||
| useRef, | ||
| useState, | ||
| } from 'react' | ||
|
|
||
| import { | ||
| availableProposalTabs, | ||
| parseProposalTab, | ||
| PROPOSAL_TAB_LABELS, | ||
| type ProposalTabKey, | ||
| } from '@/lib/proposal-tabs' | ||
| import { cn } from '@/lib/utils' | ||
|
|
||
| type Props = { | ||
| propdatesSupported: boolean | ||
| /** Badge counts known server-side. Tabs without an entry render no badge. */ | ||
| counts: Partial<Record<ProposalTabKey, number>> | ||
| panels: Partial<Record<ProposalTabKey, ReactNode>> | ||
| } | ||
|
|
||
| function readTabFromLocation(available: readonly ProposalTabKey[]): ProposalTabKey { | ||
| const raw = new URLSearchParams(window.location.search).get('tab') | ||
| return parseProposalTab(raw, available) | ||
| } | ||
|
|
||
| /** | ||
| * Tab navigation for the proposal detail page. | ||
| * | ||
| * Panels arrive already rendered from the server component, so this file owns | ||
| * only selection. The active tab is mirrored into `?tab=` via | ||
| * `window.history.replaceState` — Next's supported shallow update — which keeps | ||
| * the prerendered HTML complete (no Suspense fallback) while still yielding a | ||
| * shareable link. A panel is mounted the first time it is opened and stays | ||
| * mounted afterwards, so `PropdateThread`'s 15s SWR poll never runs for readers | ||
| * who don't open that tab, and reopening a tab doesn't refetch — though once the | ||
| * Propdates tab has been opened, that poll keeps running for the rest of the | ||
| * session even while the tab is hidden. | ||
| */ | ||
| export function ProposalTabs({ propdatesSupported, counts, panels }: Props) { | ||
| const available = useMemo( | ||
| () => availableProposalTabs({ propdatesSupported }), | ||
| [propdatesSupported] | ||
| ) | ||
| const [active, setActive] = useState<ProposalTabKey>(() => available[0]) | ||
| const [mounted, setMounted] = useState<ProposalTabKey[]>(() => [available[0]]) | ||
| const tabRefs = useRef<Partial<Record<ProposalTabKey, HTMLButtonElement | null>>>({}) | ||
| const uid = useId() | ||
|
|
||
| // Activating a tab must mark it mounted in the same update as making it | ||
| // active — otherwise there's a frame where the previous panel is already | ||
| // hidden and the new one isn't mounted yet, and the panel region collapses. | ||
| const activate = useCallback((key: ProposalTabKey) => { | ||
| setActive(key) | ||
| setMounted((prev) => (prev.includes(key) ? prev : [...prev, key])) | ||
| }, []) | ||
|
|
||
| // Adopt the deep-linked tab after hydration, and follow back/forward. | ||
| useEffect(() => { | ||
| // eslint-disable-next-line react-hooks/set-state-in-effect | ||
| activate(readTabFromLocation(available)) | ||
| const onPopState = () => activate(readTabFromLocation(available)) | ||
| window.addEventListener('popstate', onPopState) | ||
| return () => window.removeEventListener('popstate', onPopState) | ||
| }, [available, activate]) | ||
|
|
||
| const selectTab = useCallback( | ||
| (key: ProposalTabKey) => { | ||
| activate(key) | ||
| const params = new URLSearchParams(window.location.search) | ||
| if (key === available[0]) params.delete('tab') | ||
| else params.set('tab', key) | ||
| const query = params.toString() | ||
| window.history.replaceState( | ||
| null, | ||
| '', | ||
| query ? `${window.location.pathname}?${query}` : window.location.pathname | ||
| ) | ||
| }, | ||
| [available, activate] | ||
| ) | ||
|
|
||
| const onKeyDown = useCallback( | ||
| (event: React.KeyboardEvent<HTMLDivElement>) => { | ||
| const index = available.indexOf(active) | ||
| let nextIndex = -1 | ||
| if (event.key === 'ArrowRight') nextIndex = (index + 1) % available.length | ||
| else if (event.key === 'ArrowLeft') | ||
| nextIndex = (index - 1 + available.length) % available.length | ||
| else if (event.key === 'Home') nextIndex = 0 | ||
| else if (event.key === 'End') nextIndex = available.length - 1 | ||
| if (nextIndex < 0) return | ||
| event.preventDefault() | ||
| const nextKey = available[nextIndex] | ||
| selectTab(nextKey) | ||
| tabRefs.current[nextKey]?.focus() | ||
| }, | ||
| [active, available, selectTab] | ||
| ) | ||
|
|
||
| return ( | ||
| <div className="flex flex-col gap-4"> | ||
| <div | ||
| role="tablist" | ||
| aria-label="Proposal sections" | ||
| onKeyDown={onKeyDown} | ||
| className="-mx-1 flex items-center gap-1 overflow-x-auto border-b border-border px-1" | ||
| > | ||
| {available.map((key) => { | ||
| const isActive = key === active | ||
| const count = counts[key] | ||
| return ( | ||
| <button | ||
| key={key} | ||
| ref={(node) => { | ||
| tabRefs.current[key] = node | ||
| }} | ||
| type="button" | ||
| role="tab" | ||
| id={`${uid}-tab-${key}`} | ||
| aria-selected={isActive} | ||
| aria-controls={`${uid}-panel-${key}`} | ||
| tabIndex={isActive ? 0 : -1} | ||
| onClick={() => selectTab(key)} | ||
| className={cn( | ||
| 'relative shrink-0 px-3 py-2 text-sm font-medium whitespace-nowrap transition-colors', | ||
| isActive ? 'text-fg' : 'text-muted-fg hover:text-fg' | ||
| )} | ||
| > | ||
| {PROPOSAL_TAB_LABELS[key]} | ||
| {typeof count === 'number' && ( | ||
| <span className="ml-1.5 text-[12.5px] font-normal text-muted-fg"> | ||
| {count} | ||
| </span> | ||
| )} | ||
| {isActive && ( | ||
| <span className="absolute inset-x-2 -bottom-px h-0.5 rounded-sm bg-fg" /> | ||
| )} | ||
| </button> | ||
| ) | ||
| })} | ||
| </div> | ||
|
|
||
| {available.map((key) => | ||
| mounted.includes(key) ? ( | ||
| <div | ||
| key={key} | ||
| role="tabpanel" | ||
| id={`${uid}-panel-${key}`} | ||
| aria-labelledby={`${uid}-tab-${key}`} | ||
| hidden={key !== active} | ||
| tabIndex={0} | ||
| className="rounded-xl border border-border bg-surface px-4 py-5 sm:px-6 sm:py-[22px]" | ||
| > | ||
| {panels[key]} | ||
| </div> | ||
| ) : null | ||
| )} | ||
| </div> | ||
| ) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.