Fixed freezing and small optimisation

This commit is contained in:
2025-10-17 16:32:25 +02:00
parent 4eb490cd34
commit b21e1aa690
2 changed files with 24 additions and 33 deletions
+6 -20
View File
@@ -23,35 +23,21 @@ import { useIsMobile } from "@/hooks/use-mobile";
import { Textarea } from "./ui/textarea"; import { Textarea } from "./ui/textarea";
function MessageContainer({ message, app }: { message: Message; app: App }) { function MessageContainer({ message, app }: { message: Message; app: App }) {
useEffect(() => { const [profile, setProfile] = useState<UserProfile | null>();
axios
.get("/api/profile/", { params: { id: message.from } }) app.getUserById(message.from).then(setProfile);
.then((res) =>
app.setProfiles((prev) => ({
...prev,
[message.from]: res.data as UserProfile,
}))
)
.catch(() => {});
}, [message.from]);
return ( return (
<div className="flex flex-col gap-3 rounded-lg p-4 hover:bg-accent"> <div className="flex flex-col gap-3 rounded-lg p-4 hover:bg-accent">
<div className="flex gap-2"> <div className="flex gap-2">
{/* <div className="w-8 h-8 rounded-full bg-gray-400" /> */} {/* <div className="w-8 h-8 rounded-full bg-gray-400" /> */}
<ProfilePicture <ProfilePicture
url={app.profiles[message.from]?.avatar_url} url={profile?.avatar_url}
name={String( name={profile?.display_name || message.from}
app.profiles[message.from]
? app.profiles[message.from]?.display_name
: message.from
)}
/> />
<div className="flex flex-col flex-1"> <div className="flex flex-col flex-1">
<span className="font-bold flex gap-1 items-center"> <span className="font-bold flex gap-1 items-center">
{app.profiles[message.from] {profile?.display_name || message.from}
? app.profiles[message.from]?.display_name
: message.from}
<p className="text-neutral-500 text-xs"> <p className="text-neutral-500 text-xs">
{isToday(message.timestamp * 1000) {isToday(message.timestamp * 1000)
+12 -7
View File
@@ -8,7 +8,7 @@ import {
Volume2Icon, Volume2Icon,
} from "lucide-react"; } from "lucide-react";
import { Server, Channel, Message } from "@/types/types"; import { Server, Channel, Message } from "@/types/types";
import React, { useEffect, useRef, useState } from "react"; import React, { useEffect, useMemo, useState } from "react";
import Cookies from "js-cookie"; import Cookies from "js-cookie";
import { import {
Dialog, Dialog,
@@ -30,10 +30,8 @@ import { UserProfile } from "@/hooks/get-user";
import ProfilePicture from "./ProfilePicture"; import ProfilePicture from "./ProfilePicture";
import auth from "@/lib/auth"; import auth from "@/lib/auth";
import { toast } from "sonner"; import { toast } from "sonner";
import { StringMap } from "@/types/typeUtils";
import SettingsDialog from "./settings/SettingsDialog"; import SettingsDialog from "./settings/SettingsDialog";
import Link from "next/link"; import Link from "next/link";
import { get, Response } from "@/lib/request";
import { cn } from "@/lib/utils"; import { cn } from "@/lib/utils";
import App from "@/types/app"; import App from "@/types/app";
import { ProfileSettings } from "@/types/settings"; import { ProfileSettings } from "@/types/settings";
@@ -58,13 +56,15 @@ export default function AppSidebar({
const router = useRouter(); const router = useRouter();
const [filteredDms, setFilteredDms] = useState<UserProfile[]>([]); const [filteredDms, setFilteredDms] = useState<UserProfile[]>([]);
const dms = Array.from( const dms = useMemo(() => {
return Array.from(
new Set( new Set(
app.messages app.messages
?.flatMap((item) => [item.from, item.channel_id]) ?.flatMap((item) => [item.from, item.channel_id])
.filter((item) => item !== app.profile?.id) || [] .filter((item) => item !== app.profile?.id) || []
) )
); );
}, [app.messages, app.profile?.id]);
useEffect(() => { useEffect(() => {
const load = async () => { const load = async () => {
@@ -79,11 +79,16 @@ export default function AppSidebar({
); );
}; };
load(); load();
}, [dms]); }, [dms, query]);
useEffect(() => { useEffect(() => {
const s = Cookies.get("servers")?.split(","); const stored = Cookies.get("servers");
if (s) setServers(s.map((x) => x.trim().split("@") as [string, string])); if (!stored) return;
const parsed = stored
.split(",")
.map((x) => x.trim().split("@"))
.filter(([ip, name]) => ip && name) as [string, string][];
setServers(parsed);
}, []); }, []);
useEffect(() => { useEffect(() => {