mirror of
https://github.com/SableClient/Sable.git
synced 2026-11-21 17:41:25 -08:00
26 lines
840 B
TypeScript
26 lines
840 B
TypeScript
import { useEffect, useState } from 'react';
|
|
import { RelationsEvent, type Relations } from '$types/matrix-sdk';
|
|
|
|
export const useRelations = <T>(
|
|
relations: Relations,
|
|
getRelations: (relations: Relations) => T
|
|
) => {
|
|
const [data, setData] = useState(() => getRelations(relations));
|
|
|
|
useEffect(() => {
|
|
const handleUpdate = () => {
|
|
setData(getRelations(relations));
|
|
};
|
|
relations.on(RelationsEvent.Add, handleUpdate);
|
|
relations.on(RelationsEvent.Redaction, handleUpdate);
|
|
relations.on(RelationsEvent.Remove, handleUpdate);
|
|
return () => {
|
|
relations.removeListener(RelationsEvent.Add, handleUpdate);
|
|
relations.removeListener(RelationsEvent.Redaction, handleUpdate);
|
|
relations.removeListener(RelationsEvent.Remove, handleUpdate);
|
|
};
|
|
}, [relations, getRelations]);
|
|
|
|
return data;
|
|
};
|