mirror of
https://github.com/aljazceru/rabbit.git
synced 2025-12-18 14:34:25 +01:00
update
This commit is contained in:
66
src/clients/useDeprecatedReposts.ts
Normal file
66
src/clients/useDeprecatedReposts.ts
Normal file
@@ -0,0 +1,66 @@
|
||||
import { createMemo, type Accessor } from 'solid-js';
|
||||
import { type Event as NostrEvent } from 'nostr-tools/event';
|
||||
import { createQuery, useQueryClient, type CreateQueryResult } from '@tanstack/solid-query';
|
||||
|
||||
import useBatchedEvents, { type BatchedEvents } from '@/clients/useBatchedEvents';
|
||||
import timeout from '@/utils/timeout';
|
||||
|
||||
export type UseDeprecatedRepostsProps = {
|
||||
relayUrls: string[];
|
||||
eventId: string;
|
||||
};
|
||||
|
||||
export type UseDeprecatedReposts = {
|
||||
reposts: Accessor<NostrEvent[]>;
|
||||
isRepostedBy: (pubkey: string) => boolean;
|
||||
invalidateDeprecatedReposts: () => Promise<void>;
|
||||
query: CreateQueryResult<Accessor<BatchedEvents>>;
|
||||
};
|
||||
|
||||
const { exec } = useBatchedEvents<UseDeprecatedRepostsProps>(() => ({
|
||||
generateKey: ({ eventId }) => eventId,
|
||||
mergeFilters: (args) => {
|
||||
const eventIds = args.map((arg) => arg.eventId);
|
||||
return [{ kinds: [6], '#e': eventIds }];
|
||||
},
|
||||
extractKey: (event: NostrEvent) => {
|
||||
return event.tags.find((e) => e[0] === 'e')?.[1];
|
||||
},
|
||||
}));
|
||||
|
||||
const useDeprecatedReposts = (
|
||||
propsProvider: () => UseDeprecatedRepostsProps,
|
||||
): UseDeprecatedReposts => {
|
||||
const props = createMemo(propsProvider);
|
||||
const queryKey = createMemo(() => ['useDeprecatedReposts', props()] as const);
|
||||
|
||||
const query = createQuery(
|
||||
() => queryKey(),
|
||||
({ queryKey: currentQueryKey, signal }) => {
|
||||
const [, currentProps] = currentQueryKey;
|
||||
return timeout(
|
||||
15000,
|
||||
`useDeprecatedReposts: ${currentProps.eventId}`,
|
||||
)(exec(currentProps, signal));
|
||||
},
|
||||
{
|
||||
// 1 minutes
|
||||
staleTime: 1 * 60 * 1000,
|
||||
cacheTime: 1 * 60 * 1000,
|
||||
},
|
||||
);
|
||||
|
||||
const reposts = () => query.data?.()?.events ?? [];
|
||||
|
||||
const isRepostedBy = (pubkey: string): boolean =>
|
||||
reposts().findIndex((event) => event.pubkey === pubkey) !== -1;
|
||||
|
||||
const invalidateDeprecatedReposts = (): Promise<void> => {
|
||||
const queryClient = useQueryClient();
|
||||
return queryClient.invalidateQueries(queryKey());
|
||||
};
|
||||
|
||||
return { reposts, isRepostedBy, invalidateDeprecatedReposts, query };
|
||||
};
|
||||
|
||||
export default useDeprecatedReposts;
|
||||
Reference in New Issue
Block a user