sonda/web/src/components/layout/Sidebar.tsx
tgiachi 0b3bf8d49f feat: a Radio page, endless play, and the radio of an artist
The Radio page shows the stations Sonda makes by itself, in groups (made for you,
by genre, by decade). A click starts one and the player knows it is a radio: before
the queue runs out it asks the server for other songs of the same station and puts
them at the end (not the ones already queued; a station so small that all its songs
are queued starts them over), one question at a time, never twice within two seconds,
and again after ten when the server cannot answer. Every artist has a Start radio
button. The full-screen player says which station plays.

- Radio item in the menu (Library group), on the Library page and in the phone tab
- player store: radio and appendTracks; useRadioRefill
2026-11-16 12:18:15 +01:00

199 lines
7.5 KiB
TypeScript

import { useState } from 'react'
import { Blend, ChartNoAxesColumn, Compass, Disc3, Heart, History, House, Inbox, ListMusic, MicVocal, Music, PanelLeftClose, PanelLeftOpen, Radio, Search, Settings, ShieldCheck, Upload, Users, WandSparkles } from 'lucide-react'
import type { LucideIcon } from 'lucide-react'
import { NavLink } from 'react-router-dom'
import { useTranslation } from 'react-i18next'
import { useFriends } from '@/api/friends'
import { useSession } from '@/auth/session'
import Logo from '@/components/Logo'
import VersionLabel from '@/components/VersionLabel'
import { cn } from '@/lib/utils'
interface Item {
to: string
key: string
icon: LucideIcon
end?: boolean
adminOnly?: boolean
friends?: boolean
}
interface Group {
/** The key of the heading, when the group has one: only the groups that are not obvious from their words. */
heading?: string
items: Item[]
}
/**
* Where to find things, then what is in the library, then what you listened to, then the friends. The settings and
* the admin area are not a group: they stay at the foot.
*/
const GROUPS: Group[] = [
{
items: [
{ to: '/', key: 'nav.home', icon: House, end: true },
{ to: '/search', key: 'nav.search', icon: Search },
{ to: '/discover', key: 'nav.discover', icon: Compass },
{ to: '/requests', key: 'nav.requests', icon: Inbox, adminOnly: true },
],
},
{
heading: 'nav.library',
items: [
{ to: '/artists', key: 'nav.artists', icon: MicVocal },
{ to: '/albums', key: 'nav.albums', icon: Disc3 },
{ to: '/songs', key: 'nav.songs', icon: Music },
{ to: '/favorites', key: 'nav.favorites', icon: Heart },
{ to: '/playlists', key: 'nav.playlists', icon: ListMusic },
{ to: '/smart-playlists', key: 'nav.smartPlaylists', icon: WandSparkles },
{ to: '/mixes', key: 'nav.mixes', icon: Blend },
{ to: '/radio', key: 'nav.radio', icon: Radio },
],
},
{
heading: 'nav.listening',
items: [
{ to: '/history', key: 'nav.history', icon: History },
{ to: '/stats', key: 'nav.stats', icon: ChartNoAxesColumn },
{ to: '/import', key: 'nav.import', icon: Upload },
],
},
{ items: [{ to: '/friends', key: 'nav.friends', icon: Users, friends: true }] },
]
const FOOT: Item[] = [
{ to: '/settings', key: 'nav.settings', icon: Settings },
{ to: '/admin', key: 'nav.admin', icon: ShieldCheck, adminOnly: true },
]
const KEY = 'sonda.sidebar'
/** Whether this device folded the menu: a choice of the device, like the theme, kept in its storage. */
function useCollapsed(): [boolean, (collapsed: boolean) => void] {
const [collapsed, set] = useState(() => {
try {
return localStorage.getItem(KEY) === 'collapsed'
} catch {
return false
}
})
return [
collapsed,
(value) => {
set(value)
try {
localStorage.setItem(KEY, value ? 'collapsed' : 'open')
} catch {
// The choice still applies for this page view.
}
},
]
}
function Row({ item, collapsed }: { item: Item; collapsed: boolean }) {
const { t } = useTranslation()
const Icon = item.icon
return (
<li>
<NavLink
to={item.to}
end={item.end ?? false}
title={collapsed ? t(item.key) : undefined}
className={({ isActive }) =>
cn(
'flex min-h-10 items-center gap-3 rounded-lg px-3 font-medium focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-ring',
collapsed && 'justify-center px-0',
isActive ? 'bg-card font-bold text-foreground' : 'text-muted-foreground hover:text-foreground',
)
}
>
{({ isActive }) => (
<>
<Icon
data-icon
aria-hidden="true"
strokeWidth={1.75}
className={cn('size-5 shrink-0', item.friends ? 'text-friend' : isActive && 'text-primary')}
/>
<span className={cn(collapsed && 'sr-only')}>{t(item.key)}</span>
</>
)}
</NavLink>
</li>
)
}
/**
* The main menu: an icon and a word for each page, in groups. The icon of the page that is open is the colour of this
* server; the icon of Friends is the colour of what comes from elsewhere, always, so that the one page that shows other
* people's music is told apart before it is opened. Space tells the groups apart; the two that need a name have a quiet one. On a
* phone the links are left to the bar of tabs (PhoneTabs).
*/
export default function Sidebar() {
const { t } = useTranslation()
const { user } = useSession()
const [collapsed, setCollapsed] = useCollapsed()
// Friends is in the menu only when there is a friend to browse (federation on, and a friendship both admins agreed to).
const hasFriends = (useFriends().data?.length ?? 0) > 0
const shown = (item: Item) => (!item.adminOnly || user?.is_admin) && (!item.friends || hasFriends)
const groups = GROUPS.map((g) => ({ ...g, items: g.items.filter(shown) })).filter((g) => g.items.length > 0)
const foot = FOOT.filter(shown)
const Toggle = collapsed ? PanelLeftOpen : PanelLeftClose
return (
<nav
aria-label={t('nav.main')}
data-collapsed={collapsed ? '' : undefined}
className={cn(
'flex flex-col border-r border-border py-8 max-sm:min-w-0 max-sm:flex-[1_1_100%] max-sm:border-r-0 max-sm:border-b max-sm:p-3',
collapsed ? 'flex-[0_0_76px] px-3' : 'flex-[0_0_232px] px-5',
)}
>
<div className={cn('mb-7 flex items-center max-sm:mb-0', collapsed ? 'flex-col gap-5' : 'justify-between pl-3')}>
<Logo name={t('app.name')} mark={collapsed} className={cn('w-auto self-start max-sm:h-8', collapsed ? 'h-8 self-center' : 'h-8')} />
<button
type="button"
aria-expanded={!collapsed}
aria-label={t(collapsed ? 'nav.expand' : 'nav.collapse')}
title={t(collapsed ? 'nav.expand' : 'nav.collapse')}
onClick={() => setCollapsed(!collapsed)}
className="flex size-10 items-center justify-center rounded-lg text-muted-foreground hover:text-foreground focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-ring max-sm:hidden"
>
<Toggle aria-hidden="true" strokeWidth={1.75} className="size-5" />
</button>
</div>
{/* On a phone the links are the bar of tabs at the bottom; only the logo stays on top. */}
<div className="flex min-h-0 flex-1 flex-col gap-5 overflow-y-auto max-sm:hidden">
{groups.map((group) => (
<div key={group.items[0].to} className="flex flex-col">
{group.heading && (
<p
id={`nav-group-${group.heading}`}
className={cn(collapsed ? 'sr-only' : 'flex min-h-7 items-center pl-11 text-[13px] font-medium text-muted-foreground')}
>
{t(group.heading)}
</p>
)}
<ul aria-labelledby={group.heading ? `nav-group-${group.heading}` : undefined} className="flex flex-col gap-1">
{group.items.map((item) => (
<Row key={item.to} item={item} collapsed={collapsed} />
))}
</ul>
</div>
))}
<ul className="mt-auto flex flex-col gap-1">
{foot.map((item) => (
<Row key={item.to} item={item} collapsed={collapsed} />
))}
</ul>
</div>
{!collapsed && (
<div className="px-3 pt-7 text-[13px] leading-snug text-muted-foreground max-sm:hidden">
<p>{t('app.privacy')}</p>
<VersionLabel className="mt-3" />
</div>
)}
</nav>
)
}