Refactor Discord channel resource management and update imports

- Consolidated channel type definitions by moving them to discord-channel-types.ts for better organization.
- Removed redundant DASHBOARD_CHANNEL_TYPES and updated exports to streamline channel handling in the application.
- Enhanced type safety by utilizing the ChannelKind type in the discord-channel-select component.
This commit is contained in:
TheOnlyMace
2026-07-22 21:25:58 +02:00
parent ed2ea23e87
commit 042f68e777
3 changed files with 34 additions and 30 deletions

View File

@@ -16,11 +16,9 @@ import {
import { DiscordResourceMultiSelect } from '@/components/ui/discord-resource-multi-select'; import { DiscordResourceMultiSelect } from '@/components/ui/discord-resource-multi-select';
import { Popover, PopoverContent, PopoverTrigger } from '@/components/ui/popover'; import { Popover, PopoverContent, PopoverTrigger } from '@/components/ui/popover';
import { useGuildChannels } from '@/hooks/use-guild-channels'; import { useGuildChannels } from '@/hooks/use-guild-channels';
import { CHANNEL_TYPES, channelPrefix } from '@/lib/discord-guild-resources'; import { CHANNEL_TYPES, channelPrefix, type ChannelKind } from '@/lib/discord-channel-types';
import { cn } from '@/lib/utils'; import { cn } from '@/lib/utils';
type ChannelKind = keyof typeof CHANNEL_TYPES;
function filterChannels( function filterChannels(
channels: DiscordChannelOption[], channels: DiscordChannelOption[],
kinds: ChannelKind[] | undefined kinds: ChannelKind[] | undefined

View File

@@ -0,0 +1,31 @@
/** Discord channel type helpers shared by server fetchers and client pickers. */
export const CHANNEL_TYPES = {
text: [0, 5, 15, 16],
voice: [2, 13],
category: [4],
messageable: [0, 5, 15, 16]
} as const;
export type ChannelKind = keyof typeof CHANNEL_TYPES;
/** Channel types returned by the dashboard channels API. */
export const DASHBOARD_CHANNEL_TYPES = new Set([
0, // GUILD_TEXT
2, // GUILD_VOICE
4, // GUILD_CATEGORY
5, // GUILD_ANNOUNCEMENT
13, // GUILD_STAGE_VOICE
15, // GUILD_FORUM
16 // GUILD_MEDIA
]);
export function channelPrefix(type: number): string {
if (type === 2 || type === 13) {
return '🔊 ';
}
if (type === 4) {
return '📁 ';
}
return '#';
}

View File

@@ -1,28 +1,11 @@
import type { DiscordChannelOption, DiscordRoleOption } from '@nexumi/shared'; import type { DiscordChannelOption, DiscordRoleOption } from '@nexumi/shared';
import { DASHBOARD_CHANNEL_TYPES } from './discord-channel-types';
import { env } from './env'; import { env } from './env';
import { redis } from './redis'; import { redis } from './redis';
const DISCORD_API_BASE = 'https://discord.com/api/v10'; const DISCORD_API_BASE = 'https://discord.com/api/v10';
const CACHE_TTL_SECONDS = 60; const CACHE_TTL_SECONDS = 60;
/** Channel types useful in the dashboard (text, voice, category, forum, …). */
const DASHBOARD_CHANNEL_TYPES = new Set([
0, // GUILD_TEXT
2, // GUILD_VOICE
4, // GUILD_CATEGORY
5, // GUILD_ANNOUNCEMENT
13, // GUILD_STAGE_VOICE
15, // GUILD_FORUM
16 // GUILD_MEDIA
]);
export const CHANNEL_TYPES = {
text: [0, 5, 15, 16],
voice: [2, 13],
category: [4],
messageable: [0, 5, 15, 16]
} as const;
async function discordBotFetch<T>(path: string): Promise<T> { async function discordBotFetch<T>(path: string): Promise<T> {
const response = await fetch(`${DISCORD_API_BASE}${path}`, { const response = await fetch(`${DISCORD_API_BASE}${path}`, {
headers: { Authorization: `Bot ${env.BOT_TOKEN}` }, headers: { Authorization: `Bot ${env.BOT_TOKEN}` },
@@ -94,12 +77,4 @@ export async function listGuildRolesForDashboard(guildId: string): Promise<Disco
return options; return options;
} }
export function channelPrefix(type: number): string { export { CHANNEL_TYPES, channelPrefix, type ChannelKind } from './discord-channel-types';
if (type === 2 || type === 13) {
return '🔊 ';
}
if (type === 4) {
return '📁 ';
}
return '#';
}