Skip to content

Commit

Permalink
use react-query in room event hook
Browse files Browse the repository at this point in the history
  • Loading branch information
ajbura committed Dec 8, 2024
1 parent 26540fa commit 5b88924
Showing 1 changed file with 13 additions and 16 deletions.
29 changes: 13 additions & 16 deletions src/app/hooks/useRoomEvent.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { MatrixEvent, Room } from 'matrix-js-sdk';
import { useCallback, useEffect } from 'react';
import { useCallback, useMemo } from 'react';
import to from 'await-to-js';
import { CryptoBackend } from 'matrix-js-sdk/lib/common-crypto/CryptoBackend';
import { useQuery } from '@tanstack/react-query';
import { useMatrixClient } from './useMatrixClient';
import { AsyncStatus, useAsyncCallback } from './useAsyncCallback';

const useFetchEvent = (room: Room, eventId: string) => {
const mx = useMatrixClient();
Expand All @@ -19,7 +19,7 @@ const useFetchEvent = (room: Room, eventId: string) => {
return mEvent;
}, [mx, room.roomId, eventId]);

return useAsyncCallback(fetchEventCallback);
return fetchEventCallback;
};

/**
Expand All @@ -29,22 +29,19 @@ const useFetchEvent = (room: Room, eventId: string) => {
* @returns `MatrixEvent`, `undefined` means loading, `null` means failure
*/
export const useRoomEvent = (room: Room, eventId: string) => {
const event = room.findEventById(eventId);
const event = useMemo(() => room.findEventById(eventId), [room, eventId]);

const [fetchState, fetchEvent] = useFetchEvent(room, eventId);
const fetchEvent = useFetchEvent(room, eventId);

useEffect(() => {
if (!event) {
fetchEvent();
}
}, [event, fetchEvent]);
const { data, error } = useQuery({
enabled: event === undefined,
queryKey: [room.roomId, eventId],
queryFn: fetchEvent,
});

if (event) return event;
if (data) return data;
if (error) return null;

if (fetchState.status === AsyncStatus.Idle || fetchState.status === AsyncStatus.Loading)
return undefined;

if (fetchState.status === AsyncStatus.Success) return fetchState.data;

return null;
return undefined;
};

0 comments on commit 5b88924

Please sign in to comment.