55 lines
1.4 KiB
TypeScript
55 lines
1.4 KiB
TypeScript
|
|
import { useCallback, useEffect, useState } from 'react';
|
||
|
|
import { useMatrixClient } from './useMatrixClient';
|
||
|
|
import { useCrossSigningActive } from './useCrossSigning';
|
||
|
|
import { useDeviceListChange } from './useDeviceList';
|
||
|
|
|
||
|
|
export type UserDevice = {
|
||
|
|
deviceId: string;
|
||
|
|
displayName?: string;
|
||
|
|
};
|
||
|
|
|
||
|
|
export function useOtherUserDevices(userId: string): UserDevice[] | undefined {
|
||
|
|
const mx = useMatrixClient();
|
||
|
|
const crossSigningActive = useCrossSigningActive();
|
||
|
|
const [devices, setDevices] = useState<UserDevice[] | undefined>(undefined);
|
||
|
|
|
||
|
|
const fetchDevices = useCallback(async () => {
|
||
|
|
const crypto = mx.getCrypto();
|
||
|
|
if (!crypto || !crossSigningActive) {
|
||
|
|
setDevices(undefined);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
try {
|
||
|
|
const deviceMap = await crypto.getUserDeviceInfo([userId], true);
|
||
|
|
const userDevices = deviceMap.get(userId);
|
||
|
|
if (!userDevices) {
|
||
|
|
setDevices([]);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
setDevices(
|
||
|
|
Array.from(userDevices.values()).map((device) => ({
|
||
|
|
deviceId: device.deviceId,
|
||
|
|
displayName: device.displayName,
|
||
|
|
})),
|
||
|
|
);
|
||
|
|
} catch {
|
||
|
|
setDevices(undefined);
|
||
|
|
}
|
||
|
|
}, [mx, userId, crossSigningActive]);
|
||
|
|
|
||
|
|
useEffect(() => {
|
||
|
|
fetchDevices();
|
||
|
|
}, [fetchDevices]);
|
||
|
|
|
||
|
|
useDeviceListChange(
|
||
|
|
useCallback(
|
||
|
|
(userIds: string[]) => {
|
||
|
|
if (userIds.includes(userId)) fetchDevices();
|
||
|
|
},
|
||
|
|
[userId, fetchDevices],
|
||
|
|
),
|
||
|
|
);
|
||
|
|
|
||
|
|
return devices;
|
||
|
|
}
|