better structure

This commit is contained in:
Ren Amamiya
2023-05-10 16:17:38 +07:00
parent 1a30c10806
commit e7bcf6c3f8
94 changed files with 533 additions and 403 deletions

View File

@@ -1,4 +1,4 @@
import { getActiveAccount } from '@lume/utils/storage';
import { getActiveAccount } from '@utils/storage';
import useSWR from 'swr';

View File

@@ -1,56 +0,0 @@
import { RelayContext } from '@lume/shared/relayProvider';
import { READONLY_RELAYS } from '@lume/stores/constants';
import { getChannel } from '@lume/utils/storage';
import { useContext } from 'react';
import useSWR from 'swr';
import useSWRSubscription from 'swr/subscription';
const fetcher = async ([, id]) => {
const result = await getChannel(id);
if (result) {
return JSON.parse(result.metadata);
} else {
return null;
}
};
export function useChannelProfile(id: string, channelPubkey: string) {
const pool: any = useContext(RelayContext);
const { data: cache, isLoading } = useSWR(['channel-cache-profile', id], fetcher);
const { data, error } = useSWRSubscription(
!isLoading && cache ? ['channel-profile', id] : null,
([, key], { next }) => {
// subscribe to channel
const unsubscribe = pool.subscribe(
[
{
'#e': [key],
authors: [channelPubkey],
kinds: [41],
},
],
READONLY_RELAYS,
(event: { content: string }) => {
next(null, JSON.parse(event.content));
},
undefined,
undefined,
{
unsubscribeOnEose: true,
}
);
return () => {
unsubscribe();
};
}
);
if (!data || error) {
return cache;
} else {
return data;
}
}

View File

@@ -1,28 +0,0 @@
import { nip04 } from 'nostr-tools';
import { useCallback, useEffect, useState } from 'react';
export function useDecryptMessage(userKey: string, userPriv: string, data: any) {
const [content, setContent] = useState(null);
const extractSenderKey = useCallback(() => {
const keyInTags = data.tags.find(([k, v]) => k === 'p' && v && v !== '')[1];
if (keyInTags === userKey) {
return data.pubkey;
} else {
return keyInTags;
}
}, [data.pubkey, data.tags, userKey]);
const decrypt = useCallback(async () => {
const senderKey = extractSenderKey();
const result = await nip04.decrypt(userPriv, senderKey, data.content);
// update state with decrypt content
setContent(result);
}, [extractSenderKey, userPriv, data.content]);
useEffect(() => {
decrypt().catch(console.error);
}, [decrypt]);
return content ? content : null;
}

View File

@@ -1,5 +1,6 @@
import { METADATA_SERVICE } from '@lume/stores/constants';
import { createPleb, getPleb } from '@lume/utils/storage';
import { METADATA_SERVICE } from '@stores/constants';
import { createPleb, getPleb } from '@utils/storage';
import useSWR from 'swr';