Skip to content

Commit

Permalink
Merge pull request #27 from ecency/feature/nextjs
Browse files Browse the repository at this point in the history
Next version
  • Loading branch information
feruzm authored Aug 7, 2024
2 parents ee8526a + 826c968 commit 429c6cf
Show file tree
Hide file tree
Showing 35 changed files with 491 additions and 469 deletions.
28 changes: 15 additions & 13 deletions lib/api/ecency-api/add-channel-to-ecency.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,26 @@ export function useAddChannelToEcency() {
const { privateApiHost, ecencyAccessToken } = useContext(ChatContext);
const queryClient = useQueryClient();

return useMutation(
["private-api", "add-community-channel"],
async (data: { username: string; channel_id: string; meta: any }) => {
return useMutation({
mutationKey: ["private-api", "add-community-channel"],
mutationFn: async (data: {
username: string;
channel_id: string;
meta: any;
}) => {
await axios.post<unknown>(`${privateApiHost}/private-api/channel-add`, {
...data,
code: ecencyAccessToken,
});
return data;
},
{
onSuccess: (data) => {
queryClient.invalidateQueries({
queryKey: ["private-api", "get-community-channel", data.username],
});
queryClient.invalidateQueries({
queryKey: [ChatQueries.COMMUNITY_CHANNEL, data.username],
});
},
onSuccess: (data) => {
queryClient.invalidateQueries({
queryKey: ["private-api", "get-community-channel", data.username],
});
queryClient.invalidateQueries({
queryKey: [ChatQueries.COMMUNITY_CHANNEL, data.username],
});
},
);
});
}
14 changes: 6 additions & 8 deletions lib/api/ecency-api/get-community-channel-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import axios from "axios";
export function useGetCommunityChannelQuery(communityName?: string) {
const { privateApiHost } = useContext(ChatContext);

return useQuery(
["private-api", "get-community-channel", communityName],
() =>
return useQuery({
queryKey: ["private-api", "get-community-channel", communityName],
queryFn: () =>
axios
.get<{
channel_id: string;
Expand All @@ -17,9 +17,7 @@ export function useGetCommunityChannelQuery(communityName?: string) {
meta: any;
}>(`${privateApiHost}/private-api/channel/${communityName}`)
.then((resp) => resp.data),
{
enabled: !!communityName,
refetchOnMount: false,
},
);
enabled: !!communityName,
refetchOnMount: false,
});
}
14 changes: 6 additions & 8 deletions lib/api/ecency-api/get-keys-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ export function useGetKeysQuery() {
const { privateApiHost, activeUsername, ecencyAccessToken } =
useContext(ChatContext);

return useQuery(
["private-api", "get-keys", activeUsername],
() =>
return useQuery({
queryKey: ["private-api", "get-keys", activeUsername],
queryFn: () =>
axios
.post<{ key: string; pubkey: string; iv: string }[]>(
`${privateApiHost}/private-api/chats`,
Expand All @@ -18,9 +18,7 @@ export function useGetKeysQuery() {
},
)
.then((resp) => resp.data[0]),
{
enabled: !!activeUsername,
refetchOnMount: false,
},
);
enabled: !!activeUsername,
refetchOnMount: false,
});
}
42 changes: 19 additions & 23 deletions lib/api/ecency-api/get-public-keys-query.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,30 @@
import { useQuery } from "@tanstack/react-query";
import {useQuery} from "@tanstack/react-query";
import axios from "axios";
import { useContext } from "react";
import { ChatContext } from "../../chat-context-provider";
import {useContext} from "react";
import {ChatContext} from "../../chat-context-provider";

export function useGetPublicKeysQuery(username?: string) {
const { privateApiHost } = useContext(ChatContext);

return useQuery(
["private-api", "get-pub-keys", username],
() =>
return useQuery({
queryKey: ["private-api", "get-pub-keys", username],
queryFn: () =>
axios
.get<{ pubkey: string }>(
`${privateApiHost}/private-api/chats-pub/${username}`,
)
.get<{
pubkey: string;
}>(`${privateApiHost}/private-api/chats-pub/${username}`)
.then((resp) => resp.data),
{
enabled: !!username,
refetchOnMount: false,
},
);
enabled: !!username,
refetchOnMount: false,
});
}

export function useGetSetOfPublicKeysQuery(usernames: string[] = []) {
const { privateApiHost } = useContext(ChatContext);

return useQuery(
["private-api", "get-pub-keys", usernames],
async () => {
return useQuery({
queryKey: ["private-api", "get-pub-keys", usernames],
queryFn: async () => {
const response = await axios.post<{ pubkey: string; username: string }[]>(
`${privateApiHost}/private-api/chats-get`,
{
Expand All @@ -35,10 +33,8 @@ export function useGetSetOfPublicKeysQuery(usernames: string[] = []) {
);
return response.data;
},
{
enabled: usernames.length > 0,
refetchOnMount: false,
initialData: [],
},
);
enabled: usernames.length > 0,
refetchOnMount: false,
initialData: [],
});
}
13 changes: 9 additions & 4 deletions lib/api/ecency-api/save-keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,14 @@ export function useSaveKeys() {
const { privateApiHost, activeUsername, ecencyAccessToken } =
useContext(ChatContext);

return useMutation(
["private-api", "save-keys"],
async (data: { key: string; pubkey: string; iv: string; meta: any }) =>
return useMutation({
mutationKey: ["private-api", "save-keys"],
mutationFn: async (data: {
key: string;
pubkey: string;
iv: string;
meta: any;
}) =>
axios
.post<{ chat_keys: { key: string; pubkey: string; iv: string }[] }>(
`${privateApiHost}/private-api/chats-add`,
Expand All @@ -19,5 +24,5 @@ export function useSaveKeys() {
},
)
.then((resp) => resp.data.chat_keys),
);
});
}
4 changes: 2 additions & 2 deletions lib/live-listeners/use-live-direct-messages-listener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,11 @@ export function useLiveDirectMessagesListener() {
),
),
);
await queryClient.invalidateQueries([
await queryClient.invalidateQueries({queryKey: [
NostrQueries.DIRECT_MESSAGES,
activeUsername,
contact.pubkey,
]);
]});
},
{ enabled: !!publicKey && !!privateKey },
);
Expand Down
4 changes: 2 additions & 2 deletions lib/live-listeners/use-live-public-messages-listener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ export function useLivePublicMessagesListener() {
),
),
);
await queryClient.invalidateQueries([
await queryClient.invalidateQueries({queryKey: [
NostrQueries.PUBLIC_MESSAGES,
activeUsername,
channel.id,
]);
]});
},
{ enabled: !!publicKey && !!privateKey },
);
Expand Down
108 changes: 57 additions & 51 deletions lib/mutations/add-community-channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,59 +24,65 @@ export function useAddCommunityChannel(channel?: Channel) {
() => {},
);

return useMutation(["chats/add-community-channel"], async () => {
console.debug("[ns-query] Attempting to add channel to list", channel);
const hasChannelAlready = channels?.some(({ id }) => id === channel?.id);
if (!hasChannelAlready && channel && activeUserNostrProfiles) {
const activeUserNostrProfile = activeUserNostrProfiles[0];
return useMutation({
mutationKey: ["chats/add-community-channel"],
mutationFn: async () => {
console.debug("[ns-query] Attempting to add channel to list", channel);
const hasChannelAlready = channels?.some(({ id }) => id === channel?.id);
if (!hasChannelAlready && channel && activeUserNostrProfiles) {
const activeUserNostrProfile = activeUserNostrProfiles[0];

const lastSeenRecords = activeUserNostrProfile.channelsLastSeenDate ?? {};
const lastSeenTags = Object.entries(lastSeenRecords).map(
([channelId, lastSeenTime]) => [
"lastSeenDate",
channelId,
lastSeenTime.getTime().toString(),
],
);

await updateProfile({
tags: [["p", publicKey!!], ...lastSeenTags],
eventMetadata: {
...activeUserNostrProfile,
joinedChannels: [
...(activeUserNostrProfile.joinedChannels ?? []),
channel.id,
const lastSeenRecords =
activeUserNostrProfile.channelsLastSeenDate ?? {};
const lastSeenTags = Object.entries(lastSeenRecords).map(
([channelId, lastSeenTime]) => [
"lastSeenDate",
channelId,
lastSeenTime.getTime().toString(),
],
},
});
console.debug("[ns-query] Joined channels list updated. Channel added.");
await queryClient.invalidateQueries([
["chats/nostr-get-user-profile", publicKey],
]);
queryClient.setQueryData(
[ChatQueries.ORIGINAL_JOINED_CHANNELS, activeUsername],
[
...(queryClient.getQueryData<Channel[]>([
ChatQueries.ORIGINAL_JOINED_CHANNELS,
activeUsername,
]) ?? []),
channel,
],
);
queryClient.setQueryData(
[ChatQueries.JOINED_CHANNELS, activeUsername],
[
...(
queryClient.getQueryData<Channel[]>([
ChatQueries.JOINED_CHANNELS,
);

await updateProfile({
tags: [["p", publicKey!!], ...lastSeenTags],
eventMetadata: {
...activeUserNostrProfile,
joinedChannels: [
...(activeUserNostrProfile.joinedChannels ?? []),
channel.id,
],
},
});
console.debug(
"[ns-query] Joined channels list updated. Channel added.",
);
await queryClient.invalidateQueries({
queryKey: ["chats/nostr-get-user-profile", publicKey],
});
queryClient.setQueryData(
[ChatQueries.ORIGINAL_JOINED_CHANNELS, activeUsername],
[
...(queryClient.getQueryData<Channel[]>([
ChatQueries.ORIGINAL_JOINED_CHANNELS,
activeUsername,
]) ?? []
)
// Since We ecency-vision able to add channels to query manually then We have to keep them unique
.filter((ch) => ch.id !== channel.id),
channel,
],
);
}
]) ?? []),
channel,
],
);
queryClient.setQueryData(
[ChatQueries.JOINED_CHANNELS, activeUsername],
[
...(
queryClient.getQueryData<Channel[]>([
ChatQueries.JOINED_CHANNELS,
activeUsername,
]) ?? []
)
// Since We ecency-vision able to add channels to query manually then We have to keep them unique
.filter((ch) => ch.id !== channel.id),
channel,
],
);
}
},
});
}
48 changes: 23 additions & 25 deletions lib/mutations/add-direct-contact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ export function useAddDirectContact() {
() => {},
);

return useMutation(
["chats/add-direct-contact"],
async (contact: DirectContact) => {
return useMutation({
mutationKey: ["chats/add-direct-contact"],
mutationFn: async (contact: DirectContact) => {
console.debug("[ns-query] Attempting adding direct contact", contact);
const directContacts =
queryClient.getQueryData<DirectContact[]>([
Expand Down Expand Up @@ -47,28 +47,26 @@ export function useAddDirectContact() {
console.debug("[ns-query] Added direct contact to list", contact);
return contact;
},
{
onSuccess: (contact) => {
if (contact) {
queryClient.setQueryData<DirectContact[]>(
[ChatQueries.DIRECT_CONTACTS, activeUsername],
(directContacts) => {
const notExists = directContacts?.every(
(dc) => dc.pubkey !== contact.pubkey,
);
if (notExists) {
return [...(directContacts ?? []), contact];
}
return directContacts;
},
);
onSuccess: (contact) => {
if (contact) {
queryClient.setQueryData<DirectContact[]>(
[ChatQueries.DIRECT_CONTACTS, activeUsername],
(directContacts) => {
const notExists = directContacts?.every(
(dc) => dc.pubkey !== contact.pubkey,
);
if (notExists) {
return [...(directContacts ?? []), contact];
}
return directContacts;
},
);

queryClient.setQueryData<DirectContact[]>(
[ChatQueries.ORIGINAL_DIRECT_CONTACTS, activeUsername],
(data) => [...(data ?? []), contact],
);
}
},
queryClient.setQueryData<DirectContact[]>(
[ChatQueries.ORIGINAL_DIRECT_CONTACTS, activeUsername],
(data) => [...(data ?? []), contact],
);
}
},
);
});
}
Loading

0 comments on commit 429c6cf

Please sign in to comment.