Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 27 additions & 13 deletions discord/src/pages/dashboard/panels/GuildListPanel.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ChannelStatusBox } from "@src/components/ChannelStatusBox";
import { GuildStatusBox } from "@src/components/GuildStatusBox";
import { useChatStore } from "@src/stores/chatStore";
import { useMemo, useState } from "react";
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
import { PanelWrapper } from "./PanelWrapper";
import ObserverWrapper from "@src/components/ObserverWrapper";

Expand All @@ -13,33 +13,47 @@ export const GuildListPanel = () => {
const clearSelectedGuild = useChatStore((state) => state.clearSelectedGuild);
const isLoading = useChatStore((state) => state.isLoading);
const [loadingPrompt, setLoadingPrompt] = useState<boolean>(false);
const [timeoutId, setTimeoutId] = useState<NodeJS.Timeout | null>(null);
const loadingPromptTimeout = useRef<ReturnType<typeof setTimeout> | null>(null);

const selectedGuildName = useMemo(() => {
const guild = guildList?.guilds.find((g) => g.id === selectedGuild);
return guild ? guild.name : "Select a Guild";
}, [guildList, selectedGuild]);

const startLoadingPromptTimeout = () => {
const resetLoadingPromptTimeout = useCallback(() => {
setLoadingPrompt(false);

if (timeoutId) {
clearTimeout(timeoutId);
if (loadingPromptTimeout.current) {
clearTimeout(loadingPromptTimeout.current);
}

const newTimeoutId = setTimeout(() => {
loadingPromptTimeout.current = setTimeout(() => {
setLoadingPrompt(true);
}, 5000); // Show loading prompt after 5 seconds of loading
setTimeoutId(newTimeoutId);
};
}, 5000);
}, []);

useEffect(() => {
if (isLoading) {
resetLoadingPromptTimeout();
} else {
setLoadingPrompt(false);
if (loadingPromptTimeout.current) {
clearTimeout(loadingPromptTimeout.current);
loadingPromptTimeout.current = null;
}
}

if (!timeoutId) {
startLoadingPromptTimeout();
}
return () => {
if (loadingPromptTimeout.current) {
clearTimeout(loadingPromptTimeout.current);
loadingPromptTimeout.current = null;
}
};
}, [isLoading, resetLoadingPromptTimeout]);

const handleFetchGuildList = async () => {
try {
startLoadingPromptTimeout();
resetLoadingPromptTimeout();
await fetchGuildList();
} catch (error) {
console.error("Failed to fetch guild list:", error);
Expand Down