2026-05-23 19:44:22 -04:00
|
|
|
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;
|
|
|
|
|
};
|
|
|
|
|
|
2026-05-30 17:13:54 -04:00
|
|
|
export type UserDevicesState =
|
|
|
|
|
| { status: 'loading' }
|
|
|
|
|
| { status: 'error' }
|
|
|
|
|
| { status: 'success'; devices: UserDevice[] };
|
|
|
|
|
|
|
|
|
|
export function useOtherUserDevices(userId: string): UserDevicesState {
|
2026-05-23 19:44:22 -04:00
|
|
|
const mx = useMatrixClient();
|
|
|
|
|
const crossSigningActive = useCrossSigningActive();
|
2026-05-30 17:13:54 -04:00
|
|
|
const [state, setState] = useState<UserDevicesState>({ status: 'loading' });
|
2026-05-23 19:44:22 -04:00
|
|
|
|
|
|
|
|
const fetchDevices = useCallback(async () => {
|
|
|
|
|
const crypto = mx.getCrypto();
|
|
|
|
|
if (!crypto || !crossSigningActive) {
|
2026-05-30 17:13:54 -04:00
|
|
|
setState({ status: 'success', devices: [] });
|
2026-05-23 19:44:22 -04:00
|
|
|
return;
|
|
|
|
|
}
|
2026-05-30 17:13:54 -04:00
|
|
|
setState({ status: 'loading' });
|
2026-05-23 19:44:22 -04:00
|
|
|
try {
|
|
|
|
|
const deviceMap = await crypto.getUserDeviceInfo([userId], true);
|
|
|
|
|
const userDevices = deviceMap.get(userId);
|
2026-05-30 17:13:54 -04:00
|
|
|
setState({
|
|
|
|
|
status: 'success',
|
|
|
|
|
devices: userDevices
|
|
|
|
|
? Array.from(userDevices.values()).map((device) => ({
|
|
|
|
|
deviceId: device.deviceId,
|
|
|
|
|
displayName: device.displayName,
|
|
|
|
|
}))
|
|
|
|
|
: [],
|
|
|
|
|
});
|
2026-05-23 19:44:22 -04:00
|
|
|
} catch {
|
2026-05-30 17:13:54 -04:00
|
|
|
setState({ status: 'error' });
|
2026-05-23 19:44:22 -04:00
|
|
|
}
|
|
|
|
|
}, [mx, userId, crossSigningActive]);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
fetchDevices();
|
|
|
|
|
}, [fetchDevices]);
|
|
|
|
|
|
|
|
|
|
useDeviceListChange(
|
|
|
|
|
useCallback(
|
|
|
|
|
(userIds: string[]) => {
|
|
|
|
|
if (userIds.includes(userId)) fetchDevices();
|
|
|
|
|
},
|
|
|
|
|
[userId, fetchDevices],
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
|
2026-05-30 17:13:54 -04:00
|
|
|
return state;
|
2026-05-23 19:44:22 -04:00
|
|
|
}
|