import React, { useCallback, useEffect, useState } from 'react'; import { useAtom } from 'jotai'; import { Box, Button, config, Menu, Spinner, Text } from 'folds'; import { AuthDict, IMyDevice, MatrixError } from 'matrix-js-sdk'; import { SequenceCard } from '../../../components/sequence-card'; import { SequenceCardStyle } from '../styles.css'; import { ActionUIA, ActionUIAFlowsLoader } from '../../../components/ActionUIA'; import { DeviceDeleteBtn, DeviceTile } from './DeviceTile'; import { AsyncState, AsyncStatus, useAsync } from '../../../hooks/useAsyncCallback'; import { useMatrixClient } from '../../../hooks/useMatrixClient'; import { useUIAMatrixError } from '../../../hooks/useUIAFlows'; import { DeviceVerificationStatus } from '../../../components/DeviceVerificationStatus'; import { VerifyOtherDeviceTile } from './Verification'; import { VerificationStatus } from '../../../hooks/useDeviceVerificationStatus'; import { useAuthMetadata } from '../../../hooks/useAuthMetadata'; import { withSearchParam } from '../../../pages/pathUtils'; import { useAccountManagementActions } from '../../../hooks/useAccountManagement'; import { SettingTile } from '../../../components/setting-tile'; import { preselectSignOutAtom } from '../../../state/staleSessions'; import { staleSessions } from '../../../utils/securityNudge'; type OtherDevicesProps = { devices: IMyDevice[]; refreshDeviceList: () => Promise; showVerification?: boolean; }; export function OtherDevices({ devices, refreshDeviceList, showVerification }: OtherDevicesProps) { const mx = useMatrixClient(); const crypto = mx.getCrypto(); const authMetadata = useAuthMetadata(); const accountManagementActions = useAccountManagementActions(); const [deleted, setDeleted] = useState>(new Set()); // [Gitea #113] The stale-sessions nudge's Review: tick those sessions for // sign-out (one confirm below). OIDC accounts sign out on the provider's // dashboard instead, so nothing is ticked there. const [preselect, setPreselect] = useAtom(preselectSignOutAtom); useEffect(() => { if (preselect.length === 0) return; if (!authMetadata) { const present = new Set(devices.map((d) => d.device_id)); setDeleted(new Set(preselect.filter((id) => present.has(id)))); } setPreselect([]); }, [preselect, setPreselect, devices, authMetadata]); const staleIds = new Set( staleSessions(devices, mx.getDeviceId() ?? undefined, Date.now()).map((d) => d.device_id), ); const handleDashboardOIDC = useCallback(() => { const authUrl = authMetadata?.account_management_uri ?? authMetadata?.issuer; if (!authUrl) return; window.open( withSearchParam(authUrl, { action: accountManagementActions.sessionsList, }), '_blank', 'noopener,noreferrer', ); }, [authMetadata, accountManagementActions]); const handleDeleteOIDC = useCallback( (deviceId: string) => { const authUrl = authMetadata?.account_management_uri ?? authMetadata?.issuer; if (!authUrl) return; window.open( withSearchParam(authUrl, { action: accountManagementActions.sessionEnd, device_id: deviceId, }), '_blank', 'noopener,noreferrer', ); }, [authMetadata, accountManagementActions], ); const handleToggleDelete = useCallback((deviceId: string) => { setDeleted((deviceIds) => { const newIds = new Set(deviceIds); if (newIds.has(deviceId)) { newIds.delete(deviceId); } else { newIds.add(deviceId); } return newIds; }); }, []); const [deleteState, setDeleteState] = useState>({ status: AsyncStatus.Idle, }); const deleteDevices = useAsync( useCallback( async (authDict?: AuthDict) => { await mx.deleteMultipleDevices(Array.from(deleted), authDict); }, [mx, deleted], ), useCallback( (state: typeof deleteState) => { if (state.status === AsyncStatus.Success) { setDeleted(new Set()); refreshDeviceList(); } setDeleteState(state); }, [refreshDeviceList], ), ); const [authData, deleteError] = useUIAMatrixError( deleteState.status === AsyncStatus.Error ? deleteState.error : undefined, ); const deleting = deleteState.status === AsyncStatus.Loading || authData !== undefined; const handleCancelDelete = () => setDeleted(new Set()); const handleCancelAuth = useCallback(() => { setDeleteState({ status: AsyncStatus.Idle }); }, []); return devices.length > 0 ? ( <> Others {authMetadata && ( Open } /> )} {staleIds.size > 0 && ( {staleIds.size === 1 ? "1 session hasn't been used in 3 months or more (marked Inactive)." : `${staleIds.size} sessions haven't been used in 3 months or more (marked Inactive).`}{' '} Sign out the ones you no longer use. )} {[...devices] // [Gitea #113] Inactive (90+ days) sessions first, then most recent. .sort((d1, d2) => { const s1 = staleIds.has(d1.device_id) ? 0 : 1; const s2 = staleIds.has(d2.device_id) ? 0 : 1; if (s1 !== s2) return s1 - s2; if (!d1.last_seen_ts || !d2.last_seen_ts) return 0; return d1.last_seen_ts < d2.last_seen_ts ? 1 : -1; }) .map((device) => ( {staleIds.has(device.device_id) && ( Inactive for 3+ months )} ) : ( ) } /> {showVerification && crypto && ( {(status) => status === VerificationStatus.Unverified && ( ) } )} ))} {deleted.size > 0 && ( {deleteError ? ( Failed to logout devices! Please try again. {deleteError.message} ) : ( Logout from selected devices. ({deleted.size} selected) )} {authData && ( ( Authentication steps to perform this action are not supported by client. )} > {(ongoingFlow) => ( )} )} )} ) : null; }