Skip to content

Commit

Permalink
Added re-fetching of manual queries when public key is changing
Browse files Browse the repository at this point in the history
  • Loading branch information
dkildar committed Jan 9, 2024
1 parent d9ead94 commit 62a8172
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 91 deletions.
28 changes: 25 additions & 3 deletions lib/chat-init.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,36 @@
import { useActiveUserSwitching } from "./hooks";
import React from "react";
import React, { useEffect } from "react";
import {
useLiveDirectMessagesListener,
useLivePublicMessagesListener,
} from "./live-listeners";
import {
useCreatedChannelsQuery,
useDirectContactsQuery,
useJoinedChannelsQuery,
} from "./queries";
import { useKeysQuery, useNostrGetUserProfileQuery } from "./nostr";

export function ChatInit() {
useActiveUserSwitching();
const { publicKey } = useKeysQuery();
const directContactsQuery = useDirectContactsQuery();
const currentUserProfileQuery = useNostrGetUserProfileQuery(publicKey);
const joinedChannelsQuery = useJoinedChannelsQuery();
const createdChannelsQuery = useCreatedChannelsQuery();

// Initial fetching of manual queries based on public key
useEffect(() => {
init();
}, [publicKey]);

useLiveDirectMessagesListener();
useLivePublicMessagesListener();

const init = async () => {
await directContactsQuery.refetch();
await createdChannelsQuery.refetch();
await currentUserProfileQuery.refetch();
await joinedChannelsQuery.refetch();
};

return <></>;
}
1 change: 0 additions & 1 deletion lib/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export * from "./use-active-user-switching";
export * from "./use-auto-scroll-in-chat-box";
38 changes: 0 additions & 38 deletions lib/hooks/use-active-user-switching.ts

This file was deleted.

48 changes: 5 additions & 43 deletions lib/queries/channels-query.ts
Original file line number Diff line number Diff line change
@@ -1,49 +1,11 @@
import { ChatQueries } from "./queries";
import {
Channel,
useKeysQuery,
useNostrFetchQuery,
useNostrGetUserProfileQuery,
} from "../nostr";
import { Kind } from "nostr-tools";
import { convertEvent } from "../nostr/utils/event-converter";
import { useContext, useMemo } from "react";
import { ChatContext } from "../chat-context-provider";
import { useMemo } from "react";
import { useJoinedChannelsQuery } from "./joined-channels-query";
import { useCreatedChannelsQuery } from "./created-channels-query";

export function useChannelsQuery() {
const { activeUsername } = useContext(ChatContext);
const { hasKeys, publicKey } = useKeysQuery();
const { data: createdChannels } = useCreatedChannelsQuery();
const { data: joinedChannels } = useJoinedChannelsQuery();

const { data: createdChannels } = useNostrFetchQuery<Channel[]>(
[ChatQueries.CREATED_CHANNELS, activeUsername],
[Kind.ChannelCreation],
(events) =>
events
.map((event) => convertEvent<Kind.ChannelCreation>(event))
.filter((channel) => !!channel) as Channel[],
{
initialData: [],
enabled: hasKeys,
refetchOnMount: false,
},
);
const { data: activeUserNostrProfiles } =
useNostrGetUserProfileQuery(publicKey);
const { data: joinedChannels } = useNostrFetchQuery(
[ChatQueries.JOINED_CHANNELS, activeUsername],
activeUserNostrProfiles?.[0]?.joinedChannels?.map((id) => ({
kinds: [Kind.ChannelCreation],
ids: [id],
})) ?? [],
(events) =>
events
.map((event) => convertEvent<Kind.ChannelCreation>(event))
.filter((channel) => !!channel) as Channel[],
{
enabled: (activeUserNostrProfiles?.[0]?.joinedChannels?.length ?? 0) > 0,
refetchOnMount: false,
},
);
return useMemo(
() => ({
data: [...(createdChannels ?? []), ...(joinedChannels ?? [])],
Expand Down
25 changes: 25 additions & 0 deletions lib/queries/created-channels-query.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Channel, useKeysQuery, useNostrFetchQuery } from "../nostr";
import { ChatQueries } from "./queries";
import { Kind } from "nostr-tools";
import { convertEvent } from "../nostr/utils/event-converter";
import { useContext } from "react";
import { ChatContext } from "../chat-context-provider";

export function useCreatedChannelsQuery() {
const { activeUsername } = useContext(ChatContext);
const { hasKeys } = useKeysQuery();

return useNostrFetchQuery<Channel[]>(
[ChatQueries.CREATED_CHANNELS, activeUsername],
[Kind.ChannelCreation],
(events) =>
events
.map((event) => convertEvent<Kind.ChannelCreation>(event))
.filter((channel) => !!channel) as Channel[],
{
initialData: [],
enabled: hasKeys,
refetchOnMount: false,
},
);
}
2 changes: 2 additions & 0 deletions lib/queries/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ export * from "./community-channel-query";
export * from "./nostr-joined-community-team-query";
export * from "./left-community-channels-query";
export * from "./last-message-query";
export * from "./created-channels-query";
export * from "./joined-channels-query";
35 changes: 35 additions & 0 deletions lib/queries/joined-channels-query.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import {
Channel,
useKeysQuery,
useNostrFetchQuery,
useNostrGetUserProfileQuery,
} from "../nostr";
import { ChatQueries } from "./queries";
import { Kind } from "nostr-tools";
import { convertEvent } from "../nostr/utils/event-converter";
import { useContext } from "react";
import { ChatContext } from "../chat-context-provider";

export function useJoinedChannelsQuery() {
const { activeUsername } = useContext(ChatContext);
const { publicKey } = useKeysQuery();

const { data: activeUserNostrProfiles } =
useNostrGetUserProfileQuery(publicKey);

return useNostrFetchQuery(
[ChatQueries.JOINED_CHANNELS, activeUsername],
activeUserNostrProfiles?.[0]?.joinedChannels?.map((id) => ({
kinds: [Kind.ChannelCreation],
ids: [id],
})) ?? [],
(events) =>
events
.map((event) => convertEvent<Kind.ChannelCreation>(event))
.filter((channel) => !!channel) as Channel[],
{
enabled: (activeUserNostrProfiles?.[0]?.joinedChannels?.length ?? 0) > 0,
refetchOnMount: false,
},
);
}
6 changes: 5 additions & 1 deletion lib/queries/left-community-channels-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@ import { findTagValue, isSha256 } from "../nostr/utils";
import { ChatQueries } from "./queries";
import { convertEvent } from "../nostr/utils/event-converter";
import { Kind } from "nostr-tools";
import { useContext } from "react";
import { ChatContext } from "../chat-context-provider";

/**
* Fetch custom kind of event which stores the left channels
*/
export function useLeftCommunityChannelsQuery() {
const { activeUsername } = useContext(ChatContext);

return useNostrFetchQuery<string[]>(
[ChatQueries.LEFT_CHANNELS],
[ChatQueries.LEFT_CHANNELS, activeUsername],
[30078 as Kind],
(events) => {
if (events.length === 0) {
Expand Down
5 changes: 0 additions & 5 deletions lib/queries/messages-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ import {
DirectContact,
Message,
NostrQueries,
useDirectMessagesQuery,
useKeysQuery,
usePublicMessagesQuery,
} from "../nostr";
import { useContext } from "react";
import { ChatContext } from "../chat-context-provider";
Expand All @@ -26,9 +24,6 @@ export function useMessagesQuery(

const { hasKeys } = useKeysQuery();

useDirectMessagesQuery(currentContact);
usePublicMessagesQuery(currentChannel);

return useQuery<Message[]>(
[
ChatQueries.MESSAGES,
Expand Down

0 comments on commit 62a8172

Please sign in to comment.