"use client"; import { useRouter } from "next/navigation"; import { useEffect, useState } from "react"; import axios from "axios"; interface ServerSetting { _id: string; guildId: string; emailDomains: string[]; countingChannelId: string; generalChannelId: string; logChannelId: string; verificationChannelId: string; verifiedRoleName: string; actionItemsChannelId: string; actionItemsTargetChannelId: string; } interface Channel { id: string; name: string; } interface GuildData { guildId: string; guildName: string; guildIcon: string | null; settings: ServerSetting | null; channels: Channel[]; } const getChannelNames = async ( guildId: string, channelIds: string[] ): Promise => { const channels: Channel[] = []; try { const response = await axios.get( `/api/discord/channels?guildId=${guildId}` ); const allChannels: Channel[] = response.data; channelIds.forEach((channelId) => { const channel = allChannels.find((c) => c.id === channelId); if (channel) { channels.push({ id: channel.id, name: channel.name }); } }); } catch (error) { console.error("Error fetching channels:", error); } return channels; }; export default function ManageServerPage({ params, }: { params: Promise<{ guildId: string }>; }) { const router = useRouter(); const [guildData, setGuildData] = useState(null); const [activeTab, setActiveTab] = useState("settings"); useEffect(() => { const fetchParams = async () => { if (params) { const resolvedParams = await params; const guildId = resolvedParams.guildId; const response = await fetch(`/api/discord/guilds`); const serverSettings = await response.json(); const guildInfo = serverSettings.find( (setting: ServerSetting) => setting.guildId === guildId ); if (guildInfo) { const channelIds = [ guildInfo.countingChannelId, guildInfo.generalChannelId, guildInfo.logChannelId, guildInfo.verificationChannelId, guildInfo.actionItemsChannelId, guildInfo.actionItemsTargetChannelId, ]; const channels = await getChannelNames(guildId, channelIds); setGuildData({ guildId, guildName: guildInfo.guildName, guildIcon: guildInfo.guildIcon, settings: guildInfo, channels, }); } } }; fetchParams(); }, [params]); const renderSettingsContent = () => { if (!guildData?.settings) return
No settings found.
; const { emailDomains, countingChannelId, generalChannelId, logChannelId, verificationChannelId, verifiedRoleName, actionItemsChannelId, actionItemsTargetChannelId, } = guildData.settings; const countingChannel = guildData.channels?.find((c) => c.id === countingChannelId)?.name || countingChannelId; const generalChannel = guildData.channels?.find((c) => c.id === generalChannelId)?.name || generalChannelId; const logChannel = guildData.channels?.find((c) => c.id === logChannelId)?.name || logChannelId; const verificationChannel = guildData.channels?.find((c) => c.id === verificationChannelId)?.name || verificationChannelId; const actionItemsChannel = guildData.channels?.find((c) => c.id === actionItemsChannelId)?.name || actionItemsChannelId; const actionItemsTargetChannel = guildData.channels?.find((c) => c.id === actionItemsTargetChannelId) ?.name || actionItemsTargetChannelId; return (

Current Server Settings

Guild ID: {guildData.guildId}
Email Domains: {emailDomains.join(", ") || "None"}
Counting Channel: {countingChannel}
General Channel: {generalChannel}
Log Channel: {logChannel}
Verification Channel: {verificationChannel}
Verified Role Name: {verifiedRoleName}
Action Items Channel: {actionItemsChannel}
Action Items Target Channel:{" "} {actionItemsTargetChannel}
); }; const renderContent = () => { switch (activeTab) { case "settings": return renderSettingsContent(); case "roles": return
Roles management for {guildData?.guildName}
; case "channels": return
Channels management for {guildData?.guildName}
; case "logs": return
Logs for {guildData?.guildName}
; default: return
Select a tab to manage.
; } }; return (

Manage Server: {guildData ? guildData.guildName : "Loading..."}

{guildData?.guildIcon && ( {`${guildData.guildName} )} {renderContent()}
); }