36 lines
1.3 KiB
TypeScript
36 lines
1.3 KiB
TypeScript
|
|
import type { Channel } from '@/api/types';
|
||
|
|
|
||
|
|
export interface ChannelGroup {
|
||
|
|
category: Channel | null;
|
||
|
|
channels: Channel[];
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Группирует комнаты по категориям, сохраняя порядок `position`.
|
||
|
|
* Невидимые комнаты (`can_view === false`) сервер обычно не присылает,
|
||
|
|
* но если прислал — прячем их на клиенте.
|
||
|
|
*/
|
||
|
|
export function groupChannels(channels: Channel[]): ChannelGroup[] {
|
||
|
|
const visible = channels
|
||
|
|
.filter((channel) => channel.can_view !== false && channel.type !== 'category')
|
||
|
|
.slice()
|
||
|
|
.sort((left, right) => left.position - right.position);
|
||
|
|
const categories = channels
|
||
|
|
.filter((channel) => channel.type === 'category')
|
||
|
|
.slice()
|
||
|
|
.sort((left, right) => left.position - right.position);
|
||
|
|
|
||
|
|
const groups: ChannelGroup[] = [];
|
||
|
|
const uncategorized = visible.filter((channel) => channel.parent_id === undefined);
|
||
|
|
if (uncategorized.length > 0) {
|
||
|
|
groups.push({ category: null, channels: uncategorized });
|
||
|
|
}
|
||
|
|
for (const category of categories) {
|
||
|
|
const children = visible.filter((channel) => channel.parent_id === category.id);
|
||
|
|
if (children.length > 0) {
|
||
|
|
groups.push({ category, channels: children });
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return groups;
|
||
|
|
}
|