From de6a1a661cc9294d8226e0c435b47e9d9a2230b9 Mon Sep 17 00:00:00 2001 From: Kerry Date: Thu, 2 Mar 2023 22:58:05 +1300 Subject: [PATCH] apply strictnullchecks to src/components/views/beacon/* (#10272) --- src/components/views/beacon/BeaconListItem.tsx | 10 +++++----- .../views/beacon/BeaconStatusTooltip.tsx | 6 +++--- src/components/views/beacon/BeaconViewDialog.tsx | 6 +++--- .../views/beacon/DialogOwnBeaconStatus.tsx | 6 +++--- .../views/beacon/LeftPanelLiveShareWarning.tsx | 6 +++--- .../views/beacon/LiveTimeRemaining.tsx | 8 +++++++- src/components/views/beacon/OwnBeaconStatus.tsx | 2 +- .../views/beacon/ShareLatestLocation.tsx | 4 ++-- src/components/views/beacon/displayStatus.ts | 4 +--- src/utils/beacon/useOwnLiveBeacons.ts | 10 +++++----- .../views/beacon/OwnBeaconStatus-test.tsx | 16 +++++++++++++++- 11 files changed, 48 insertions(+), 30 deletions(-) diff --git a/src/components/views/beacon/BeaconListItem.tsx b/src/components/views/beacon/BeaconListItem.tsx index 24fdb05f32..caeff53bf5 100644 --- a/src/components/views/beacon/BeaconListItem.tsx +++ b/src/components/views/beacon/BeaconListItem.tsx @@ -42,14 +42,14 @@ const BeaconListItem: React.FC> = ({ beacon, .. const matrixClient = useContext(MatrixClientContext); const room = matrixClient.getRoom(beacon.roomId); - if (!latestLocationState || !beacon.isLive) { + if (!latestLocationState || !beacon.isLive || !room) { return null; } - const isSelfLocation = beacon.beaconInfo.assetType === LocationAssetType.Self; - const beaconMember = isSelfLocation ? room.getMember(beacon.beaconInfoOwner) : undefined; + const isSelfLocation = beacon.beaconInfo?.assetType === LocationAssetType.Self; + const beaconMember = isSelfLocation ? room.getMember(beacon.beaconInfoOwner) : null; - const humanizedUpdateTime = humanizeTime(latestLocationState.timestamp); + const humanizedUpdateTime = (latestLocationState.timestamp && humanizeTime(latestLocationState.timestamp)) || ""; return (
  • @@ -62,7 +62,7 @@ const BeaconListItem: React.FC> = ({ beacon, .. {/* eat events from interactive share buttons diff --git a/src/components/views/beacon/BeaconStatusTooltip.tsx b/src/components/views/beacon/BeaconStatusTooltip.tsx index a0f70504c1..d66f4a59d0 100644 --- a/src/components/views/beacon/BeaconStatusTooltip.tsx +++ b/src/components/views/beacon/BeaconStatusTooltip.tsx @@ -27,11 +27,11 @@ interface Props { beacon: Beacon; } -const useBeaconName = (beacon: Beacon): string => { +const useBeaconName = (beacon: Beacon): string | undefined => { const matrixClient = useContext(MatrixClientContext); - if (beacon.beaconInfo.assetType !== LocationAssetType.Self) { - return beacon.beaconInfo.description; + if (beacon.beaconInfo?.assetType !== LocationAssetType.Self) { + return beacon.beaconInfo?.description; } const room = matrixClient.getRoom(beacon.roomId); const member = room?.getMember(beacon.beaconInfoOwner); diff --git a/src/components/views/beacon/BeaconViewDialog.tsx b/src/components/views/beacon/BeaconViewDialog.tsx index b164168a2d..818ca82242 100644 --- a/src/components/views/beacon/BeaconViewDialog.tsx +++ b/src/components/views/beacon/BeaconViewDialog.tsx @@ -54,7 +54,7 @@ interface FocusedBeaconState { beacon?: Beacon; } -const getBoundsCenter = (bounds: Bounds): string | undefined => { +const getBoundsCenter = (bounds?: Bounds): string | undefined => { if (!bounds) { return; } @@ -70,10 +70,10 @@ const useMapPosition = ( { beacon, ts }: FocusedBeaconState, ): { bounds?: Bounds; - centerGeoUri: string; + centerGeoUri?: string; } => { const [bounds, setBounds] = useState(getBeaconBounds(liveBeacons)); - const [centerGeoUri, setCenterGeoUri] = useState( + const [centerGeoUri, setCenterGeoUri] = useState( beacon?.latestLocationState?.uri || getBoundsCenter(bounds), ); diff --git a/src/components/views/beacon/DialogOwnBeaconStatus.tsx b/src/components/views/beacon/DialogOwnBeaconStatus.tsx index 8c74586f35..3c456004f4 100644 --- a/src/components/views/beacon/DialogOwnBeaconStatus.tsx +++ b/src/components/views/beacon/DialogOwnBeaconStatus.tsx @@ -45,12 +45,12 @@ const DialogOwnBeaconStatus: React.FC = ({ roomId }) => { const matrixClient = useContext(MatrixClientContext); const room = matrixClient.getRoom(roomId); - if (!beacon?.isLive) { + if (!beacon?.isLive || !room) { return null; } - const isSelfLocation = beacon.beaconInfo.assetType === LocationAssetType.Self; - const beaconMember = isSelfLocation ? room.getMember(beacon.beaconInfoOwner) : undefined; + const isSelfLocation = beacon.beaconInfo?.assetType === LocationAssetType.Self; + const beaconMember = isSelfLocation ? room.getMember(beacon.beaconInfoOwner) : null; return (
    diff --git a/src/components/views/beacon/LeftPanelLiveShareWarning.tsx b/src/components/views/beacon/LeftPanelLiveShareWarning.tsx index d3577ba3ad..0104a79cae 100644 --- a/src/components/views/beacon/LeftPanelLiveShareWarning.tsx +++ b/src/components/views/beacon/LeftPanelLiveShareWarning.tsx @@ -25,7 +25,7 @@ import { Icon as LiveLocationIcon } from "../../../../res/img/location/live-loca import { ViewRoomPayload } from "../../../dispatcher/payloads/ViewRoomPayload"; import { Action } from "../../../dispatcher/actions"; import dispatcher from "../../../dispatcher/dispatcher"; -import AccessibleButton from "../elements/AccessibleButton"; +import AccessibleButton, { ButtonEvent } from "../elements/AccessibleButton"; interface Props { isMinimized?: boolean; @@ -121,7 +121,7 @@ const LeftPanelLiveShareWarning: React.FC = ({ isMinimized }) => { ); const onWarningClick = relevantBeacon - ? () => { + ? (_e: ButtonEvent) => { dispatcher.dispatch({ action: Action.ViewRoom, room_id: relevantBeacon.roomId, @@ -131,7 +131,7 @@ const LeftPanelLiveShareWarning: React.FC = ({ isMinimized }) => { highlighted: true, }); } - : undefined; + : null; const label = getLabel(hasStoppingErrors, hasLocationPublishErrors); diff --git a/src/components/views/beacon/LiveTimeRemaining.tsx b/src/components/views/beacon/LiveTimeRemaining.tsx index 3ffd248ca7..b6682d710b 100644 --- a/src/components/views/beacon/LiveTimeRemaining.tsx +++ b/src/components/views/beacon/LiveTimeRemaining.tsx @@ -40,13 +40,19 @@ const getUpdateInterval = (ms: number): number => { const useMsRemaining = (beacon: Beacon): number => { const beaconInfo = useEventEmitterState(beacon, BeaconEvent.Update, () => beacon.beaconInfo); - const [msRemaining, setMsRemaining] = useState(() => getBeaconMsUntilExpiry(beaconInfo)); + const [msRemaining, setMsRemaining] = useState(() => (beaconInfo ? getBeaconMsUntilExpiry(beaconInfo) : 0)); useEffect(() => { + if (!beaconInfo) { + return; + } setMsRemaining(getBeaconMsUntilExpiry(beaconInfo)); }, [beaconInfo]); const updateMsRemaining = useCallback(() => { + if (!beaconInfo) { + return; + } const ms = getBeaconMsUntilExpiry(beaconInfo); setMsRemaining(ms); }, [beaconInfo]); diff --git a/src/components/views/beacon/OwnBeaconStatus.tsx b/src/components/views/beacon/OwnBeaconStatus.tsx index 7baffe3e5f..92afa950da 100644 --- a/src/components/views/beacon/OwnBeaconStatus.tsx +++ b/src/components/views/beacon/OwnBeaconStatus.tsx @@ -42,7 +42,7 @@ const OwnBeaconStatus: React.FC> = ({ beacon, stoppingInProgress, onStopSharing, onResetLocationPublishError, - } = useOwnLiveBeacons([beacon?.identifier]); + } = useOwnLiveBeacons(beacon?.identifier ? [beacon?.identifier] : []); // combine display status with errors that only occur for user's own beacons const ownDisplayStatus = hasLocationPublishError || hasStopSharingError ? BeaconDisplayStatus.Error : displayStatus; diff --git a/src/components/views/beacon/ShareLatestLocation.tsx b/src/components/views/beacon/ShareLatestLocation.tsx index 175e1d46e2..ef525ad838 100644 --- a/src/components/views/beacon/ShareLatestLocation.tsx +++ b/src/components/views/beacon/ShareLatestLocation.tsx @@ -28,9 +28,9 @@ interface Props { } const ShareLatestLocation: React.FC = ({ latestLocationState }) => { - const [coords, setCoords] = useState(null); + const [coords, setCoords] = useState(); useEffect(() => { - if (!latestLocationState) { + if (!latestLocationState?.uri) { return; } const coords = parseGeoUri(latestLocationState.uri); diff --git a/src/components/views/beacon/displayStatus.ts b/src/components/views/beacon/displayStatus.ts index 51c63f95c6..48260cb672 100644 --- a/src/components/views/beacon/displayStatus.ts +++ b/src/components/views/beacon/displayStatus.ts @@ -40,7 +40,5 @@ export const getBeaconDisplayStatus = ( if (!latestLocationState) { return BeaconDisplayStatus.Loading; } - if (latestLocationState) { - return BeaconDisplayStatus.Active; - } + return BeaconDisplayStatus.Active; }; diff --git a/src/utils/beacon/useOwnLiveBeacons.ts b/src/utils/beacon/useOwnLiveBeacons.ts index 8d9c01a380..989d12c4fd 100644 --- a/src/utils/beacon/useOwnLiveBeacons.ts +++ b/src/utils/beacon/useOwnLiveBeacons.ts @@ -23,11 +23,11 @@ import { sortBeaconsByLatestExpiry } from "./duration"; type LiveBeaconsState = { beacon?: Beacon; - onStopSharing?: () => void; - onResetLocationPublishError?: () => void; - stoppingInProgress?: boolean; - hasStopSharingError?: boolean; - hasLocationPublishError?: boolean; + onStopSharing: () => void; + onResetLocationPublishError: () => void; + stoppingInProgress: boolean; + hasStopSharingError: boolean; + hasLocationPublishError: boolean; }; /** diff --git a/test/components/views/beacon/OwnBeaconStatus-test.tsx b/test/components/views/beacon/OwnBeaconStatus-test.tsx index 019af5d664..2abcbbc354 100644 --- a/test/components/views/beacon/OwnBeaconStatus-test.tsx +++ b/test/components/views/beacon/OwnBeaconStatus-test.tsx @@ -29,6 +29,14 @@ jest.mock("../../../../src/utils/beacon/useOwnLiveBeacons", () => ({ useOwnLiveBeacons: jest.fn(), })); +const defaultLiveBeaconsState = { + onStopSharing: jest.fn(), + onResetLocationPublishError: jest.fn(), + stoppingInProgress: false, + hasStopSharingError: false, + hasLocationPublishError: false, +}; + describe("", () => { const defaultProps = { displayStatus: BeaconDisplayStatus.Loading, @@ -43,7 +51,7 @@ describe("", () => { beforeEach(() => { jest.spyOn(global.Date, "now").mockReturnValue(123456789); - mocked(useOwnLiveBeacons).mockClear().mockReturnValue({}); + mocked(useOwnLiveBeacons).mockClear().mockReturnValue(defaultLiveBeaconsState); defaultBeacon = new Beacon(makeBeaconInfoEvent(userId, roomId)); }); @@ -57,6 +65,7 @@ describe("", () => { it("renders stop button", () => { const displayStatus = BeaconDisplayStatus.Active; mocked(useOwnLiveBeacons).mockReturnValue({ + ...defaultLiveBeaconsState, onStopSharing: jest.fn(), }); renderComponent({ displayStatus, beacon: defaultBeacon }); @@ -68,6 +77,7 @@ describe("", () => { const displayStatus = BeaconDisplayStatus.Active; const onStopSharing = jest.fn(); mocked(useOwnLiveBeacons).mockReturnValue({ + ...defaultLiveBeaconsState, onStopSharing, }); renderComponent({ displayStatus, beacon: defaultBeacon }); @@ -90,6 +100,7 @@ describe("", () => { it("renders in error mode", () => { const displayStatus = BeaconDisplayStatus.Active; mocked(useOwnLiveBeacons).mockReturnValue({ + ...defaultLiveBeaconsState, hasLocationPublishError: true, onResetLocationPublishError: jest.fn(), }); @@ -103,6 +114,7 @@ describe("", () => { const displayStatus = BeaconDisplayStatus.Active; const onResetLocationPublishError = jest.fn(); mocked(useOwnLiveBeacons).mockReturnValue({ + ...defaultLiveBeaconsState, hasLocationPublishError: true, onResetLocationPublishError, }); @@ -117,6 +129,7 @@ describe("", () => { it("renders in error mode", () => { const displayStatus = BeaconDisplayStatus.Active; mocked(useOwnLiveBeacons).mockReturnValue({ + ...defaultLiveBeaconsState, hasLocationPublishError: false, hasStopSharingError: true, onStopSharing: jest.fn(), @@ -131,6 +144,7 @@ describe("", () => { const displayStatus = BeaconDisplayStatus.Active; const onStopSharing = jest.fn(); mocked(useOwnLiveBeacons).mockReturnValue({ + ...defaultLiveBeaconsState, hasStopSharingError: true, onStopSharing, });