Skip to content
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

733 default to past tab when all questions are closed #734

Merged
11 changes: 4 additions & 7 deletions packages/berlin/src/components/tabs/TabsHeader.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
import { Fragment, useState } from 'react';
import { Fragment } from 'react';
import { Body } from '../typography/Body.styled';
import { Tab } from './TabsHeader.styled';

type TabsHeaderProps = {
tabNames: string[];
initialTab?: string;
initialTab: string | null;
className?: string;
onTabChange?: (tab: string) => void;
};

export function TabsHeader({ tabNames, initialTab, className, onTabChange }: TabsHeaderProps) {
const [activeTab, setActiveTab] = useState<string>(initialTab || tabNames[0]);

const handleTabClick = (tab: string) => {
setActiveTab(tab);
if (onTabChange) {
if (onTabChange && initialTab !== tab) {
onTabChange(tab);
diegoalzate marked this conversation as resolved.
Show resolved Hide resolved
}
};
Expand All @@ -24,7 +21,7 @@ export function TabsHeader({ tabNames, initialTab, className, onTabChange }: Tab
{tabNames.map((tabName, index) => (
<Fragment key={tabName}>
<Tab
className={activeTab === tabName ? 'active' : ''}
className={initialTab === tabName ? 'active' : ''}
onClick={() => handleTabClick(tabName)}
>
{tabName}
Expand Down
23 changes: 19 additions & 4 deletions packages/berlin/src/pages/Event.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { useQuery } from '@tanstack/react-query';
import { useParams } from 'react-router-dom';

// API
import { fetchEvent, fetchEventCycles } from 'api';
import { fetchEvent, fetchEventCycles, GetCycleResponse } from 'api';
diegoalzate marked this conversation as resolved.
Show resolved Hide resolved

// Components
import { Body } from '../components/typography/Body.styled';
Expand All @@ -18,6 +18,10 @@ import Link from '../components/link';
import Markdown from 'react-markdown';
import Onboarding from '@/components/onboarding';

function getInitialTab(openCycles: GetCycleResponse[] | undefined) {
return openCycles && openCycles.length > 0 ? 'upcoming' : 'past';
}

function Event() {
const { eventId } = useParams();
const { data: event } = useQuery({
Expand Down Expand Up @@ -45,7 +49,12 @@ function Event() {
);

const tabNames = ['upcoming', 'past'];
const [activeTab, setActiveTab] = useState<string>('upcoming');
const [activeTab, setActiveTab] = useState<string | null>(null);

if (openCycles && activeTab === null) {
const initialTab = getInitialTab(openCycles);
setActiveTab(initialTab);
}

const tabs = {
upcoming: <Cycles cycles={openCycles} eventId={eventId} errorMessage="No upcoming events..." />,
Expand Down Expand Up @@ -85,10 +94,16 @@ function Event() {
</section>
<section className="flex w-full flex-col justify-between gap-2 md:flex-row md:items-center">
<Subtitle>Questions</Subtitle>
<Tabs.TabsHeader className="tabs" tabNames={tabNames} onTabChange={setActiveTab} />
<Tabs.TabsHeader
key={activeTab} // Ensure TabsHeader re-renders when activeTab changes
className="tabs"
tabNames={tabNames}
initialTab={activeTab}
onTabChange={setActiveTab}
/>
</section>
<FlexColumn className="cycles">
<Tabs.TabsManager tabs={tabs} tab={activeTab} fallback={'Tab not found'} />
<Tabs.TabsManager tabs={tabs} tab={activeTab || ''} fallback={'Tab not found'} />
</FlexColumn>
</FlexColumn>
</>
Expand Down
Loading