mirror of
https://github.com/cinnyapp/cinny.git
synced 2025-01-31 09:49:10 +01:00
convert account data hook to typescript
This commit is contained in:
parent
233ec012a0
commit
643da9e191
2 changed files with 22 additions and 21 deletions
|
@ -1,21 +0,0 @@
|
|||
/* eslint-disable import/prefer-default-export */
|
||||
import { useState, useEffect } from 'react';
|
||||
import { useMatrixClient } from './useMatrixClient';
|
||||
|
||||
export function useAccountData(eventType) {
|
||||
const mx = useMatrixClient();
|
||||
const [event, setEvent] = useState(mx.getAccountData(eventType));
|
||||
|
||||
useEffect(() => {
|
||||
const handleChange = (mEvent) => {
|
||||
if (mEvent.getType() !== eventType) return;
|
||||
setEvent(mEvent);
|
||||
};
|
||||
mx.on('accountData', handleChange);
|
||||
return () => {
|
||||
mx.removeListener('accountData', handleChange);
|
||||
};
|
||||
}, [mx, eventType]);
|
||||
|
||||
return event;
|
||||
}
|
22
src/app/hooks/useAccountData.ts
Normal file
22
src/app/hooks/useAccountData.ts
Normal file
|
@ -0,0 +1,22 @@
|
|||
import { useState, useCallback } from 'react';
|
||||
import { useMatrixClient } from './useMatrixClient';
|
||||
import { useAccountDataCallback } from './useAccountDataCallback';
|
||||
|
||||
export function useAccountData(eventType: string) {
|
||||
const mx = useMatrixClient();
|
||||
const [event, setEvent] = useState(() => mx.getAccountData(eventType));
|
||||
|
||||
useAccountDataCallback(
|
||||
mx,
|
||||
useCallback(
|
||||
(evt) => {
|
||||
if (evt.getType() === eventType) {
|
||||
setEvent(evt);
|
||||
}
|
||||
},
|
||||
[eventType, setEvent]
|
||||
)
|
||||
);
|
||||
|
||||
return event;
|
||||
}
|
Loading…
Reference in a new issue