This commit is contained in:
Shusui MOYATANI
2023-06-03 20:44:22 +09:00
parent 26700c0cae
commit 5db35d7028
33 changed files with 1227 additions and 717 deletions

View File

@@ -1,3 +1,49 @@
import { useEvent } from '@/nostr/useBatchedEvents';
import { createMemo } from 'solid-js';
import { createQuery, useQueryClient, type CreateQueryResult } from '@tanstack/solid-query';
import { Event as NostrEvent } from 'nostr-tools';
import { exec } from '@/nostr/useBatchedEvents';
import timeout from '@/utils/timeout';
export type UseEventProps = {
eventId: string;
};
export type UseEvent = {
event: () => NostrEvent | null;
query: CreateQueryResult<NostrEvent | null>;
};
const useEvent = (propsProvider: () => UseEventProps | null): UseEvent => {
const props = createMemo(propsProvider);
const query = createQuery(
() => ['useEvent', props()] as const,
({ queryKey, signal }) => {
const [, currentProps] = queryKey;
if (currentProps == null) return null;
const { eventId } = currentProps;
const promise = exec({ type: 'Event', eventId }, signal).then((batchedEvents) => {
const event = batchedEvents().events[0];
if (event == null) throw new Error(`event not found: ${eventId}`);
return event;
});
return timeout(15000, `useEvent: ${eventId}`)(promise);
},
{
// Text notes never change, so they can be stored for a long time.
// However, events tend to be unreferenced as time passes.
staleTime: 4 * 60 * 60 * 1000, // 4 hour
cacheTime: 4 * 60 * 60 * 1000, // 4 hour
refetchOnWindowFocus: false,
refetchOnMount: false,
},
);
const event = () => query.data ?? null;
return { event, query };
};
export default useEvent;