import {Channel, ChannelContentItem} from '@common/channels/channel'; import { channelContentConfig, Sort, } from '@app/admin/channels/channel-content-config'; import {Button} from '@common/ui/buttons/button'; import {SortIcon} from '@common/icons/material/Sort'; import {Trans} from '@common/i18n/trans'; import React from 'react'; import {useSearchParams} from 'react-router-dom'; import { Menu, MenuItem, MenuTrigger, } from '@common/ui/navigation/menu/menu-trigger'; import {message} from '@common/i18n/message'; import {IconButton} from '@common/ui/buttons/icon-button'; interface ChannelSortButtonProps { channel: Channel; } export function ChannelSortButton({ channel, }: ChannelSortButtonProps) { const config = channelContentConfig.models[channel.config.contentModel]; const sortMethods = config?.sortMethods.map(method => ({ key: method, label: channelContentConfig.sortingMethods[method].label, })) || []; if (channel.config.contentType === 'manual') { sortMethods.unshift({ key: Sort.curated, label: message('Default order'), }); } const [searchParams, setSearchParams] = useSearchParams(); const selectedValue = searchParams.get('order') || channel.config.contentOrder; if (sortMethods?.length < 2) { return null; } const label = sortMethods?.find( method => method.key === selectedValue )?.label; return ( { // order by date added to channel, if content is cured if ( newValue === Sort.recent && channel.config.contentType === 'manual' ) { newValue = 'channelables.created_at:desc'; } setSearchParams( prev => { prev.set('order', newValue as string); return prev; }, { replace: true, } ); }} > {sortMethods?.map(method => ( ))} ); }