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
2 changes: 2 additions & 0 deletions TelegramDownloader/ClientApp/apps/web/src/i18n/es/shell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ const shell: Record<string, string> = {
'Not in a folder': 'Sin carpeta',
'Filter channels': 'Filtrar canales',
'Expand the channel list': 'Ampliar la lista de canales',
'Resize the channel list': 'Ajustar el tamaño de la lista de canales',
'Drag to resize the channel list; double click to reset': 'Arrastra para ajustar la lista de canales; doble clic para restablecer',
'Show the menu': 'Mostrar el menú',
'Show all channels': 'Mostrar todos los canales',
'Only favourites': 'Solo favoritos',
Expand Down
2 changes: 2 additions & 0 deletions TelegramDownloader/ClientApp/apps/web/src/i18n/gl/shell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ const shell: Record<string, string> = {
'Not in a folder': 'Sen cartafol',
'Filter channels': 'Filtrar canles',
'Expand the channel list': 'Ampliar a lista de canles',
'Resize the channel list': 'Axustar o tamaño da lista de canles',
'Drag to resize the channel list; double click to reset': 'Arrastra para axustar a lista de canles; dobre clic para restablecer',
'Show the menu': 'Amosar o menú',
'Show all channels': 'Amosar todas as canles',
'Only favourites': 'Só favoritas',
Expand Down
60 changes: 59 additions & 1 deletion TelegramDownloader/ClientApp/apps/web/src/shell/Sidebar.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ function renderSidebar(inDrawer: boolean) {

beforeEach(() => {
localStorage.removeItem('tfm.web.ui');
useUi.setState({ drawerCollapsedSections: [], drawerChannelsExpanded: false });
useUi.setState({ drawerCollapsedSections: [], drawerChannelsExpanded: false, sidebarNavHeight: null });
});

describe('<Sidebar> in the mobile drawer', () => {
Expand Down Expand Up @@ -76,3 +76,61 @@ describe('<Sidebar> on desktop', () => {
expect(screen.queryByRole('button', { name: 'Expand the channel list' })).toBeNull();
});
});

/** Gives the navigation a layout (jsdom has none): its content, top and the sidebar bottom. */
function layout(contentHeight: number, top = 56, sidebarBottom = 900) {
const nav = screen.getByRole('navigation', { name: 'Main navigation' });
Object.defineProperty(nav, 'scrollHeight', { configurable: true, value: contentHeight });
nav.getBoundingClientRect = () => {
const height = useUi.getState().sidebarNavHeight ?? contentHeight;
return { top, bottom: top + height, height, left: 0, right: 0, width: 0, x: 0, y: top, toJSON: () => ({}) };
};
const container = nav.parentElement!;
container.getBoundingClientRect = () =>
({ top: 0, bottom: sidebarBottom, height: sidebarBottom, left: 0, right: 0, width: 0, x: 0, y: 0, toJSON: () => ({}) });
return nav;
}

describe('<Sidebar> navigation divider', () => {
it('is only on the desktop sidebar', () => {
renderSidebar(true);
expect(screen.queryByRole('separator', { name: 'Resize the channel list' })).toBeNull();
});

it('shrinks the navigation with the keyboard and remembers it', () => {
renderSidebar(false);
const nav = layout(500);
const divider = screen.getByRole('separator', { name: 'Resize the channel list' });
fireEvent.keyDown(divider, { key: 'ArrowUp' });
expect(useUi.getState().sidebarNavHeight).toBe(476);
expect(nav).toHaveStyle({ height: '476px' });
expect(localStorage.getItem('tfm.web.ui')).toContain('"sidebarNavHeight":476');
});

it('follows the pointer and never hides the channels nor the whole navigation', () => {
renderSidebar(false);
layout(500, 56, 600);
const divider = screen.getByRole('separator', { name: 'Resize the channel list' });
fireEvent.pointerDown(divider, { button: 0, clientY: 556, pointerId: 1 });
fireEvent.pointerMove(divider, { clientY: 300, pointerId: 1 });
expect(useUi.getState().sidebarNavHeight).toBe(244);
fireEvent.pointerMove(divider, { clientY: -500, pointerId: 1 });
expect(useUi.getState().sidebarNavHeight).toBe(48);
// The channel list keeps 160 px: 600 - 56 - 160
fireEvent.pointerMove(divider, { clientY: 2000, pointerId: 1 });
expect(useUi.getState().sidebarNavHeight).toBe(384);
fireEvent.pointerUp(divider, { pointerId: 1 });
});

it('goes back to the full navigation on double click or End', () => {
useUi.setState({ sidebarNavHeight: 200 });
renderSidebar(false);
layout(500);
const divider = screen.getByRole('separator', { name: 'Resize the channel list' });
fireEvent.doubleClick(divider);
expect(useUi.getState().sidebarNavHeight).toBeNull();
useUi.setState({ sidebarNavHeight: 200 });
fireEvent.keyDown(divider, { key: 'End' });
expect(useUi.getState().sidebarNavHeight).toBeNull();
});
});
100 changes: 97 additions & 3 deletions TelegramDownloader/ClientApp/apps/web/src/shell/Sidebar.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { cn, IconButton, Tooltip } from '@tfm/ui';
import { ChevronDown, PanelLeftClose, PanelLeftOpen } from 'lucide-react';
import { useRef, type KeyboardEvent, type PointerEvent, type RefObject } from 'react';
import { NavLink } from 'react-router';
import { t } from '@/i18n';
import { useUi } from '@/stores/ui';
Expand Down Expand Up @@ -35,6 +36,86 @@ function NavEntry({ item, collapsed, onNavigate }: { item: NavItem; collapsed: b
);
}

/** Smallest height of the navigation and of the channel list when dragging the divider. */
const MIN_NAV_HEIGHT = 48;
const MIN_CHANNELS_HEIGHT = 160;
const KEY_STEP = 24;

/**
* Divider between the navigation and the channel list of the desktop sidebar:
* drag it (or use the arrow keys) to give the channels more room. The height
* is remembered; double click or End goes back to the full navigation.
*/
function NavResizer({ navRef, containerRef }: { navRef: RefObject<HTMLElement | null>; containerRef: RefObject<HTMLElement | null> }) {
const navHeight = useUi((s) => s.sidebarNavHeight);
const setNavHeight = useUi((s) => s.setSidebarNavHeight);
const drag = useRef<{ startY: number; startHeight: number } | null>(null);

// Tallest useful height: the whole navigation, leaving room for the channels
const maxHeight = () => {
const nav = navRef.current;
const container = containerRef.current;
if (!nav || !container) return Number.POSITIVE_INFINITY;
const room = container.getBoundingClientRect().bottom - nav.getBoundingClientRect().top - MIN_CHANNELS_HEIGHT;
return Math.max(MIN_NAV_HEIGHT, Math.min(nav.scrollHeight, room));
};
const apply = (height: number) => {
const nav = navRef.current;
const clamped = Math.min(Math.max(height, MIN_NAV_HEIGHT), maxHeight());
// Dragged back to the full navigation: follow its natural height again
setNavHeight(nav && clamped >= nav.scrollHeight ? null : clamped);
};
const current = () => navRef.current?.getBoundingClientRect().height ?? navHeight ?? 0;

const onPointerDown = (e: PointerEvent<HTMLDivElement>) => {
if (e.button !== 0) return;
e.preventDefault();
e.currentTarget.setPointerCapture?.(e.pointerId);
drag.current = { startY: e.clientY, startHeight: current() };
};
const onPointerMove = (e: PointerEvent<HTMLDivElement>) => {
if (!drag.current) return;
apply(drag.current.startHeight + e.clientY - drag.current.startY);
};
const onPointerUp = (e: PointerEvent<HTMLDivElement>) => {
drag.current = null;
if (e.currentTarget.hasPointerCapture?.(e.pointerId)) e.currentTarget.releasePointerCapture(e.pointerId);
};
const onKeyDown = (e: KeyboardEvent<HTMLDivElement>) => {
const moves: Record<string, () => void> = {
ArrowUp: () => apply(current() - KEY_STEP),
ArrowDown: () => apply(current() + KEY_STEP),
Home: () => apply(MIN_NAV_HEIGHT),
End: () => setNavHeight(null)
};
const move = moves[e.key];
if (!move) return;
e.preventDefault();
move();
};

return (
<div
role="separator"
aria-orientation="horizontal"
aria-label={t('Resize the channel list')}
aria-valuemin={MIN_NAV_HEIGHT}
aria-valuenow={navHeight ?? undefined}
tabIndex={0}
title={t('Drag to resize the channel list; double click to reset')}
onPointerDown={onPointerDown}
onPointerMove={onPointerMove}
onPointerUp={onPointerUp}
onPointerCancel={onPointerUp}
onDoubleClick={() => setNavHeight(null)}
onKeyDown={onKeyDown}
className="group mx-3 flex h-2.5 shrink-0 cursor-row-resize touch-none items-center outline-none"
>
<div className="h-px w-full bg-border transition-colors group-hover:h-0.5 group-hover:bg-accent-400 group-focus-visible:h-0.5 group-focus-visible:bg-accent-400" />
</div>
);
}

export interface SidebarProps {
/** Icon rail (desktop collapsed). */
collapsed?: boolean;
Expand All @@ -52,9 +133,14 @@ export function Sidebar({ collapsed = false, inDrawer = false, onNavigate }: Sid
const sections = navSections();
// In the drawer the channel list can take the whole height (the pages stay one tap away)
const channelsOnly = inDrawer && drawerChannelsExpanded;
// Desktop: the user can drag the divider to give the channel list more room
const resizable = !inDrawer && !collapsed;
const navHeight = useUi((s) => s.sidebarNavHeight);
const containerRef = useRef<HTMLDivElement>(null);
const navRef = useRef<HTMLElement>(null);

return (
<div className="flex h-full min-h-0 flex-col">
<div ref={containerRef} className="flex h-full min-h-0 flex-col">
<div className={cn('flex h-14 shrink-0 items-center gap-2 px-3', collapsed && 'justify-center px-0')}>
{!collapsed && (
<NavLink to="/channels" onClick={onNavigate} className="flex min-w-0 flex-1 items-center gap-2.5 rounded-lg px-1 py-1 outline-none focus-visible:ring-2 focus-visible:ring-ring">
Expand All @@ -78,7 +164,11 @@ export function Sidebar({ collapsed = false, inDrawer = false, onNavigate }: Sid

{!channelsOnly && (
<>
<nav aria-label={t('Main navigation')} className={cn('flex min-h-0 shrink flex-col overflow-y-auto px-2 pb-2', inDrawer ? 'gap-1' : 'gap-3')}>
<nav
ref={navRef}
aria-label={t('Main navigation')}
style={resizable && navHeight !== null ? { height: navHeight } : undefined}
className={cn('flex min-h-0 shrink flex-col overflow-y-auto px-2 pb-2', inDrawer ? 'gap-1' : 'gap-3')}>
{sections.map((section) => {
// Sections fold in the drawer only (remembered), so the channels come up on a phone
const folded = inDrawer && foldedSections.includes(section.id);
Expand Down Expand Up @@ -106,7 +196,11 @@ export function Sidebar({ collapsed = false, inDrawer = false, onNavigate }: Sid
})}
</nav>

<div className="mx-3 h-px shrink-0 bg-border" />
{resizable ? (
<NavResizer navRef={navRef} containerRef={containerRef} />
) : (
<div className="mx-3 h-px shrink-0 bg-border" />
)}
</>
)}
<ChannelList
Expand Down
13 changes: 10 additions & 3 deletions TelegramDownloader/ClientApp/apps/web/src/stores/ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ interface UiState {
drawerCollapsedSections: string[];
/** Mobile drawer: the channel list takes the whole drawer (navigation hidden). */
drawerChannelsExpanded: boolean;
/** Desktop sidebar: height (px) the user dragged the navigation to; null = its natural height. */
sidebarNavHeight: number | null;
toggleSidebar: () => void;
setMobileNavOpen: (open: boolean) => void;
setCommandOpen: (open: boolean) => void;
Expand All @@ -26,6 +28,7 @@ interface UiState {
setSingleClickFolders: (on: boolean) => void;
toggleDrawerSection: (id: string) => void;
setDrawerChannelsExpanded: (on: boolean) => void;
setSidebarNavHeight: (height: number | null) => void;
}

export const useUi = create<UiState>()(
Expand All @@ -39,6 +42,7 @@ export const useUi = create<UiState>()(
singleClickFolders: true,
drawerCollapsedSections: [],
drawerChannelsExpanded: false,
sidebarNavHeight: null,
toggleSidebar: () => set((s) => ({ sidebarCollapsed: !s.sidebarCollapsed })),
setMobileNavOpen: (mobileNavOpen) => set({ mobileNavOpen }),
setCommandOpen: (commandOpen) => set({ commandOpen }),
Expand All @@ -51,17 +55,20 @@ export const useUi = create<UiState>()(
? s.drawerCollapsedSections.filter((x) => x !== id)
: [...s.drawerCollapsedSections, id]
})),
setDrawerChannelsExpanded: (drawerChannelsExpanded) => set({ drawerChannelsExpanded })
setDrawerChannelsExpanded: (drawerChannelsExpanded) => set({ drawerChannelsExpanded }),
setSidebarNavHeight: (sidebarNavHeight) =>
set({ sidebarNavHeight: sidebarNavHeight === null ? null : Math.max(0, Math.round(sidebarNavHeight)) })
}),
{
name: 'tfm.web.ui',
partialize: ({ sidebarCollapsed, channelTab, favouritesOnly, singleClickFolders, drawerCollapsedSections, drawerChannelsExpanded }) => ({
partialize: ({ sidebarCollapsed, channelTab, favouritesOnly, singleClickFolders, drawerCollapsedSections, drawerChannelsExpanded, sidebarNavHeight }) => ({
sidebarCollapsed,
channelTab,
favouritesOnly,
singleClickFolders,
drawerCollapsedSections,
drawerChannelsExpanded
drawerChannelsExpanded,
sidebarNavHeight
})
}
)
Expand Down
Loading