Fix bug with statistics display on mobile

This commit is contained in:
Jack Kingsman
2026-02-16 23:01:04 -08:00
parent 3232075767
commit f490cc756f
2 changed files with 51 additions and 3 deletions

View File

@@ -102,6 +102,7 @@ export function SettingsModal(props: SettingsModalProps) {
};
const [isMobileLayout, setIsMobileLayout] = useState(getIsMobileLayout);
const externalDesktopSidebarMode = externalSidebarNav && !isMobileLayout;
const [expandedSections, setExpandedSections] = useState<Record<SettingsSection, boolean>>(() => {
const isMobile = getIsMobileLayout();
return {
@@ -245,8 +246,16 @@ export function SettingsModal(props: SettingsModalProps) {
setSectionError(null);
}, [externalSidebarNav, desktopSection]);
// On mobile with external sidebar nav, auto-expand the selected section
useEffect(() => {
if (!externalSidebarNav || !isMobileLayout || !desktopSection) return;
setExpandedSections((prev) =>
prev[desktopSection] ? prev : { ...prev, [desktopSection]: true }
);
}, [externalSidebarNav, isMobileLayout, desktopSection]);
// Fetch statistics when the section becomes visible
const statisticsVisible = externalSidebarNav
const statisticsVisible = externalDesktopSidebarMode
? desktopSection === 'statistics'
: expandedSections.statistics;
@@ -596,8 +605,6 @@ export function SettingsModal(props: SettingsModalProps) {
setSectionError(null);
};
const externalDesktopSidebarMode = externalSidebarNav && !isMobileLayout;
const isSectionVisible = (section: SettingsSection) =>
externalDesktopSidebarMode ? desktopSection === section : expandedSections[section];

View File

@@ -341,4 +341,45 @@ describe('SettingsModal', () => {
expect(screen.getByText('general')).toBeInTheDocument();
expect(screen.getByText('42 msgs')).toBeInTheDocument();
});
it('fetches statistics when expanded in mobile external-nav mode', async () => {
const mockStats: StatisticsResponse = {
busiest_channels_24h: [],
contact_count: 10,
repeater_count: 3,
channel_count: 5,
total_packets: 200,
decrypted_packets: 150,
undecrypted_packets: 50,
total_dms: 25,
total_channel_messages: 80,
total_outgoing: 30,
contacts_heard: { last_hour: 2, last_24_hours: 7, last_week: 10 },
repeaters_heard: { last_hour: 1, last_24_hours: 3, last_week: 3 },
};
const fetchSpy = vi.spyOn(globalThis, 'fetch').mockResolvedValue(
new Response(JSON.stringify(mockStats), {
status: 200,
headers: { 'Content-Type': 'application/json' },
})
);
renderModal({
mobile: true,
externalSidebarNav: true,
desktopSection: 'radio',
});
expect(fetchSpy).not.toHaveBeenCalled();
fireEvent.click(screen.getByRole('button', { name: /Statistics/i }));
await waitFor(() => {
expect(fetchSpy).toHaveBeenCalledWith('/api/statistics', expect.any(Object));
});
await waitFor(() => {
expect(screen.getByText('Network')).toBeInTheDocument();
});
});
});