added useChannelMetadata and support kind 41
This commit is contained in:
52
src/utils/hooks/useChannelMetadata.tsx
Normal file
52
src/utils/hooks/useChannelMetadata.tsx
Normal file
@@ -0,0 +1,52 @@
|
||||
import { RelayContext } from '@components/relaysProvider';
|
||||
|
||||
import { updateChannelMetadata } from '@utils/storage';
|
||||
|
||||
import { useCallback, useContext, useEffect, useRef, useState } from 'react';
|
||||
|
||||
export const useChannelMetadata = (id: string, fallback: string) => {
|
||||
const [pool, relays]: any = useContext(RelayContext);
|
||||
const [metadata, setMetadata] = useState(null);
|
||||
const unsubscribe = useRef(null);
|
||||
|
||||
const fetchMetadata = useCallback(() => {
|
||||
unsubscribe.current = pool.subscribe(
|
||||
[
|
||||
{
|
||||
kinds: [41],
|
||||
'#e': [id],
|
||||
},
|
||||
],
|
||||
relays,
|
||||
(event: { content: string }) => {
|
||||
const json = JSON.parse(event.content);
|
||||
// update state
|
||||
setMetadata(json);
|
||||
// update metadata in database
|
||||
updateChannelMetadata(id, event.content);
|
||||
},
|
||||
undefined,
|
||||
undefined,
|
||||
{
|
||||
unsubscribeOnEose: true,
|
||||
logAllEvents: false,
|
||||
}
|
||||
);
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
const json = JSON.parse(fallback);
|
||||
setMetadata(json);
|
||||
|
||||
// fetch kind 41
|
||||
fetchMetadata();
|
||||
|
||||
return () => {
|
||||
if (unsubscribe.current) {
|
||||
unsubscribe.current();
|
||||
}
|
||||
};
|
||||
}, [fetchMetadata]);
|
||||
|
||||
return metadata;
|
||||
};
|
||||
@@ -127,6 +127,12 @@ export async function createChannel(event_id: string, metadata: string, created_
|
||||
]);
|
||||
}
|
||||
|
||||
// update channel metadata
|
||||
export async function updateChannelMetadata(event_id: string, value: string) {
|
||||
const db = await connect();
|
||||
return await db.execute(`UPDATE channels SET metadata = "${value}" WHERE event_id = "${event_id}";`);
|
||||
}
|
||||
|
||||
// get all chats
|
||||
export async function getChats(account_id: number) {
|
||||
const db = await connect();
|
||||
|
||||
Reference in New Issue
Block a user