Optimize chat loading

This commit is contained in:
2023-07-05 14:11:55 +01:00
parent 672f1dd077
commit 67f69299a5
2 changed files with 14 additions and 6 deletions

View File

@ -11,7 +11,7 @@ import { Profile } from "./profile";
import { Text } from "./text"; import { Text } from "./text";
import { SendZapsDialog } from "./send-zap"; import { SendZapsDialog } from "./send-zap";
import { findTag } from "../utils"; import { findTag } from "../utils";
import { useInView } from "react-intersection-observer";
interface Emoji { interface Emoji {
id: string; id: string;
@ -38,12 +38,16 @@ export function ChatMessage({
reactions: readonly NostrEvent[]; reactions: readonly NostrEvent[];
}) { }) {
const ref = useRef(null); const ref = useRef(null);
const { inView } = useInView({
root: ref.current,
triggerOnce: true
});
const emojiRef = useRef(null); const emojiRef = useRef(null);
const isTablet = useMediaQuery("(max-width: 1020px)"); const isTablet = useMediaQuery("(max-width: 1020px)");
const isHovering = useHover(ref); const isHovering = useHover(ref);
const [showZapDialog, setShowZapDialog] = useState(false); const [showZapDialog, setShowZapDialog] = useState(false);
const [showEmojiPicker, setShowEmojiPicker] = useState(false); const [showEmojiPicker, setShowEmojiPicker] = useState(false);
const profile = useUserProfile(System, ev.pubkey); const profile = useUserProfile(System, inView ? ev.pubkey : undefined);
const zapTarget = profile?.lud16 ?? profile?.lud06; const zapTarget = profile?.lud16 ?? profile?.lud06;
const zaps = useMemo(() => { const zaps = useMemo(() => {
return reactions.filter(a => a.kind === EventKind.ZapReceipt) return reactions.filter(a => a.kind === EventKind.ZapReceipt)
@ -102,7 +106,7 @@ export function ChatMessage({
ref={ref} ref={ref}
onClick={() => setShowZapDialog(true)} onClick={() => setShowZapDialog(true)}
> >
<Profile pubkey={ev.pubkey} /> <Profile pubkey={ev.pubkey} profile={profile} />
<Text content={ev.content} tags={ev.tags} /> <Text content={ev.content} tags={ev.tags} />
{(hasReactions || hasZaps) && ( {(hasReactions || hasZaps) && (
<div className="message-reactions"> <div className="message-reactions">

View File

@ -5,6 +5,7 @@ import { UserMetadata } from "@snort/system";
import { hexToBech32 } from "@snort/shared"; import { hexToBech32 } from "@snort/shared";
import { Icon } from "element/icon"; import { Icon } from "element/icon";
import { System } from "index"; import { System } from "index";
import { useInView } from "react-intersection-observer";
export interface ProfileOptions { export interface ProfileOptions {
showName?: boolean; showName?: boolean;
@ -29,12 +30,15 @@ export function Profile({
pubkey, pubkey,
avatarClassname, avatarClassname,
options, options,
profile,
}: { }: {
pubkey: string; pubkey: string;
avatarClassname?: string; avatarClassname?: string;
options?: ProfileOptions; options?: ProfileOptions;
profile?: UserMetadata
}) { }) {
const profile = useUserProfile(System, pubkey); const { inView, ref } = useInView();
profile ??= useUserProfile(System, inView ? pubkey : undefined);
const showAvatar = options?.showAvatar ?? true; const showAvatar = options?.showAvatar ?? true;
const showName = options?.showName ?? true; const showName = options?.showName ?? true;
@ -61,9 +65,9 @@ export function Profile({
); );
return pubkey === "anon" ? ( return pubkey === "anon" ? (
<div className="profile">{content}</div> <div className="profile" ref={ref}>{content}</div>
) : ( ) : (
<Link to={`/p/${hexToBech32("npub", pubkey)}`} className="profile"> <Link to={`/p/${hexToBech32("npub", pubkey)}`} className="profile" ref={ref}>
{content} {content}
</Link> </Link>
); );