Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Commit

Permalink
LLS: rename wireError (#8401)
Browse files Browse the repository at this point in the history
* rename wireError in ownbeaconstore to locationPublishError

Signed-off-by: Kerry Archibald <kerrya@element.io>

* rename getLiveBeaconIdsWithWireError -> getLiveBeaconIdsWithLocationPublishError

Signed-off-by: Kerry Archibald <kerrya@element.io>

* rename wire error variables in components

Signed-off-by: Kerry Archibald <kerrya@element.io>

* new snapshots for new test names

Signed-off-by: Kerry Archibald <kerrya@element.io>

* fix bad capitalisation on onResetLocationPublishError

Signed-off-by: Kerry Archibald <kerrya@element.io>

* missed variable

Signed-off-by: Kerry Archibald <kerrya@element.io>
  • Loading branch information
Kerry authored Apr 25, 2022
1 parent a3f9123 commit 8c6786b
Show file tree
Hide file tree
Showing 11 changed files with 133 additions and 120 deletions.
14 changes: 7 additions & 7 deletions src/components/views/beacon/LeftPanelLiveShareWarning.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ const LeftPanelLiveShareWarning: React.FC<Props> = ({ isMinimized }) => {
() => OwnBeaconStore.instance.isMonitoringLiveLocation,
);

const beaconIdsWithWireError = useEventEmitterState(
const beaconIdsWithLocationPublishError = useEventEmitterState(
OwnBeaconStore.instance,
OwnBeaconStoreEvent.WireError,
() => OwnBeaconStore.instance.getLiveBeaconIdsWithWireError(),
OwnBeaconStoreEvent.LocationPublishError,
() => OwnBeaconStore.instance.getLiveBeaconIdsWithLocationPublishError(),
);

const liveBeaconIds = useEventEmitterState(
Expand All @@ -65,13 +65,13 @@ const LeftPanelLiveShareWarning: React.FC<Props> = ({ isMinimized }) => {
() => OwnBeaconStore.instance.getLiveBeaconIds(),
);

const hasWireErrors = !!beaconIdsWithWireError.length;
const hasLocationPublishErrors = !!beaconIdsWithLocationPublishError.length;

if (!isMonitoringLiveLocation) {
return null;
}

const relevantBeaconRoomId = chooseBestBeaconRoomId(liveBeaconIds, beaconIdsWithWireError);
const relevantBeaconRoomId = chooseBestBeaconRoomId(liveBeaconIds, beaconIdsWithLocationPublishError);

const onWarningClick = relevantBeaconRoomId ? () => {
dispatcher.dispatch<ViewRoomPayload>({
Expand All @@ -81,14 +81,14 @@ const LeftPanelLiveShareWarning: React.FC<Props> = ({ isMinimized }) => {
});
} : undefined;

const label = hasWireErrors ?
const label = hasLocationPublishErrors ?
_t('An error occured whilst sharing your live location') :
_t('You are sharing your live location');

return <AccessibleButton
className={classNames('mx_LeftPanelLiveShareWarning', {
'mx_LeftPanelLiveShareWarning__minimized': isMinimized,
'mx_LeftPanelLiveShareWarning__error': hasWireErrors,
'mx_LeftPanelLiveShareWarning__error': hasLocationPublishErrors,
})}
title={isMinimized ? label : undefined}
onClick={onWarningClick}
Expand Down
10 changes: 5 additions & 5 deletions src/components/views/beacon/OwnBeaconStatus.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ const OwnBeaconStatus: React.FC<Props & HTMLProps<HTMLDivElement>> = ({
beacon, displayStatus, ...rest
}) => {
const {
hasWireError,
hasLocationPublishError,
hasStopSharingError,
stoppingInProgress,
onStopSharing,
onResetWireError,
onResetLocationPublishError,
} = useOwnLiveBeacons([beacon?.identifier]);

// combine display status with errors that only occur for user's own beacons
const ownDisplayStatus = hasWireError || hasStopSharingError ?
const ownDisplayStatus = hasLocationPublishError || hasStopSharingError ?
BeaconDisplayStatus.Error :
displayStatus;

Expand All @@ -67,10 +67,10 @@ const OwnBeaconStatus: React.FC<Props & HTMLProps<HTMLDivElement>> = ({
{ _t('Stop') }
</AccessibleButton>
}
{ hasWireError && <AccessibleButton
{ hasLocationPublishError && <AccessibleButton
data-test-id='beacon-status-reset-wire-error'
kind='link'
onClick={onResetWireError}
onClick={onResetLocationPublishError}
className='mx_OwnBeaconStatus_button mx_OwnBeaconStatus_destructiveButton'
>
{ _t('Retry') }
Expand Down
18 changes: 9 additions & 9 deletions src/components/views/beacon/RoomLiveShareWarning.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ import StyledLiveBeaconIcon from './StyledLiveBeaconIcon';
import { Icon as CloseIcon } from '../../../../res/img/image-view/close.svg';
import LiveTimeRemaining from './LiveTimeRemaining';

const getLabel = (hasWireError: boolean, hasStopSharingError: boolean): string => {
if (hasWireError) {
const getLabel = (hasLocationPublishError: boolean, hasStopSharingError: boolean): string => {
if (hasLocationPublishError) {
return _t('An error occured whilst sharing your live location, please try again');
}
if (hasStopSharingError) {
Expand All @@ -45,22 +45,22 @@ interface RoomLiveShareWarningInnerProps {
const RoomLiveShareWarningInner: React.FC<RoomLiveShareWarningInnerProps> = ({ liveBeaconIds, roomId }) => {
const {
onStopSharing,
onResetWireError,
onResetLocationPublishError,
beacon,
stoppingInProgress,
hasStopSharingError,
hasWireError,
hasLocationPublishError,
} = useOwnLiveBeacons(liveBeaconIds);

if (!beacon) {
return null;
}

const hasError = hasStopSharingError || hasWireError;
const hasError = hasStopSharingError || hasLocationPublishError;

const onButtonClick = () => {
if (hasWireError) {
onResetWireError();
if (hasLocationPublishError) {
onResetLocationPublishError();
} else {
onStopSharing();
}
Expand All @@ -72,7 +72,7 @@ const RoomLiveShareWarningInner: React.FC<RoomLiveShareWarningInnerProps> = ({ l
<StyledLiveBeaconIcon className="mx_RoomLiveShareWarning_icon" withError={hasError} />

<span className="mx_RoomLiveShareWarning_label">
{ getLabel(hasWireError, hasStopSharingError) }
{ getLabel(hasLocationPublishError, hasStopSharingError) }
</span>

{ stoppingInProgress &&
Expand All @@ -90,7 +90,7 @@ const RoomLiveShareWarningInner: React.FC<RoomLiveShareWarningInnerProps> = ({ l
>
{ hasError ? _t('Retry') : _t('Stop sharing') }
</AccessibleButton>
{ hasWireError && <AccessibleButton
{ hasLocationPublishError && <AccessibleButton
data-test-id='room-live-share-wire-error-close-button'
title={_t('Stop sharing and close')}
element='button'
Expand Down
44 changes: 22 additions & 22 deletions src/stores/OwnBeaconStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const isOwnBeacon = (beacon: Beacon, userId: string): boolean => beacon.beaconIn
export enum OwnBeaconStoreEvent {
LivenessChange = 'OwnBeaconStore.LivenessChange',
MonitoringLivePosition = 'OwnBeaconStore.MonitoringLivePosition',
WireError = 'WireError',
LocationPublishError = 'LocationPublishError',
}

const MOVING_UPDATE_INTERVAL = 2000;
Expand All @@ -60,7 +60,7 @@ const BAIL_AFTER_CONSECUTIVE_ERROR_COUNT = 2;

type OwnBeaconStoreState = {
beacons: Map<BeaconIdentifier, Beacon>;
beaconWireErrors: Map<string, Beacon>;
beaconLocationPublishErrorCounts: Map<BeaconIdentifier, number>;
beaconsByRoomId: Map<Room['roomId'], Set<BeaconIdentifier>>;
liveBeaconIds: BeaconIdentifier[];
};
Expand Down Expand Up @@ -98,7 +98,7 @@ export class OwnBeaconStore extends AsyncStoreWithClient<OwnBeaconStoreState> {
* Counts consecutive wire errors per beacon
* Reset on successful publish of location
*/
public readonly beaconWireErrorCounts = new Map<BeaconIdentifier, number>();
public readonly beaconLocationPublishErrorCounts = new Map<BeaconIdentifier, number>();
/**
* ids of live beacons
* ordered by creation time descending
Expand Down Expand Up @@ -143,7 +143,7 @@ export class OwnBeaconStore extends AsyncStoreWithClient<OwnBeaconStoreState> {
this.beacons.clear();
this.beaconsByRoomId.clear();
this.liveBeaconIds = [];
this.beaconWireErrorCounts.clear();
this.beaconLocationPublishErrorCounts.clear();
}

protected async onReady(): Promise<void> {
Expand All @@ -168,21 +168,21 @@ export class OwnBeaconStore extends AsyncStoreWithClient<OwnBeaconStoreState> {
* Some live beacon has a wire error
* Optionally filter by room
*/
public hasWireErrors = (roomId?: string): boolean => {
return this.getLiveBeaconIds(roomId).some(this.beaconHasWireError);
public hasLocationPublishErrors = (roomId?: string): boolean => {
return this.getLiveBeaconIds(roomId).some(this.beaconHasLocationPublishError);
};

/**
* If a beacon has failed to publish position
* past the allowed consecutive failure count (BAIL_AFTER_CONSECUTIVE_ERROR_COUNT)
* Then consider it to have an error
*/
public beaconHasWireError = (beaconId: string): boolean => {
return this.beaconWireErrorCounts.get(beaconId) >= BAIL_AFTER_CONSECUTIVE_ERROR_COUNT;
public beaconHasLocationPublishError = (beaconId: string): boolean => {
return this.beaconLocationPublishErrorCounts.get(beaconId) >= BAIL_AFTER_CONSECUTIVE_ERROR_COUNT;
};

public resetWireError = (beaconId: string): void => {
this.incrementBeaconWireErrorCount(beaconId, false);
public resetLocationPublishError = (beaconId: string): void => {
this.incrementBeaconLocationPublishErrorCount(beaconId, false);

// always publish to all live beacons together
// instead of just one that was changed
Expand All @@ -198,8 +198,8 @@ export class OwnBeaconStore extends AsyncStoreWithClient<OwnBeaconStoreState> {
return this.liveBeaconIds.filter(beaconId => this.beaconsByRoomId.get(roomId)?.has(beaconId));
};

public getLiveBeaconIdsWithWireError = (roomId?: string): string[] => {
return this.getLiveBeaconIds(roomId).filter(this.beaconHasWireError);
public getLiveBeaconIdsWithLocationPublishError = (roomId?: string): string[] => {
return this.getLiveBeaconIds(roomId).filter(this.beaconHasLocationPublishError);
};

public getBeaconById = (beaconId: string): Beacon | undefined => {
Expand Down Expand Up @@ -300,7 +300,7 @@ export class OwnBeaconStore extends AsyncStoreWithClient<OwnBeaconStoreState> {
* Live beacon ids that do not have wire errors
*/
private get healthyLiveBeaconIds() {
return this.liveBeaconIds.filter(beaconId => !this.beaconHasWireError(beaconId));
return this.liveBeaconIds.filter(beaconId => !this.beaconHasLocationPublishError(beaconId));
}

private initialiseBeaconState = () => {
Expand Down Expand Up @@ -546,10 +546,10 @@ export class OwnBeaconStore extends AsyncStoreWithClient<OwnBeaconStoreState> {
const content = makeBeaconContent(geoUri, timestamp, beacon.beaconInfoId);
try {
await this.matrixClient.sendEvent(beacon.roomId, M_BEACON.name, content);
this.incrementBeaconWireErrorCount(beacon.identifier, false);
this.incrementBeaconLocationPublishErrorCount(beacon.identifier, false);
} catch (error) {
logger.error(error);
this.incrementBeaconWireErrorCount(beacon.identifier, true);
this.incrementBeaconLocationPublishErrorCount(beacon.identifier, true);
}
};

Expand All @@ -559,22 +559,22 @@ export class OwnBeaconStore extends AsyncStoreWithClient<OwnBeaconStoreState> {
* - increment count for beacon when is error
* - emit if beacon error count crossed threshold
*/
private incrementBeaconWireErrorCount = (beaconId: string, isError: boolean): void => {
const hadError = this.beaconHasWireError(beaconId);
private incrementBeaconLocationPublishErrorCount = (beaconId: string, isError: boolean): void => {
const hadError = this.beaconHasLocationPublishError(beaconId);

if (isError) {
// increment error count
this.beaconWireErrorCounts.set(
this.beaconLocationPublishErrorCounts.set(
beaconId,
(this.beaconWireErrorCounts.get(beaconId) ?? 0) + 1,
(this.beaconLocationPublishErrorCounts.get(beaconId) ?? 0) + 1,
);
} else {
// clear any error count
this.beaconWireErrorCounts.delete(beaconId);
this.beaconLocationPublishErrorCounts.delete(beaconId);
}

if (this.beaconHasWireError(beaconId) !== hadError) {
this.emit(OwnBeaconStoreEvent.WireError, beaconId);
if (this.beaconHasLocationPublishError(beaconId) !== hadError) {
this.emit(OwnBeaconStoreEvent.LocationPublishError, beaconId);
}
};
}
18 changes: 9 additions & 9 deletions src/utils/beacon/useOwnLiveBeacons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ import { sortBeaconsByLatestExpiry } from "./duration";
type LiveBeaconsState = {
beacon?: Beacon;
onStopSharing?: () => void;
onResetWireError?: () => void;
onResetLocationPublishError?: () => void;
stoppingInProgress?: boolean;
hasStopSharingError?: boolean;
hasWireError?: boolean;
hasLocationPublishError?: boolean;
};

/**
Expand All @@ -41,11 +41,11 @@ export const useOwnLiveBeacons = (liveBeaconIds: BeaconIdentifier[]): LiveBeacon
const [stoppingInProgress, setStoppingInProgress] = useState(false);
const [error, setError] = useState<Error>();

const hasWireError = useEventEmitterState(
const hasLocationPublishError = useEventEmitterState(
OwnBeaconStore.instance,
OwnBeaconStoreEvent.WireError,
OwnBeaconStoreEvent.LocationPublishError,
() =>
liveBeaconIds.some(OwnBeaconStore.instance.beaconHasWireError),
liveBeaconIds.some(OwnBeaconStore.instance.beaconHasLocationPublishError),
);

// reset stopping in progress on change in live ids
Expand All @@ -72,16 +72,16 @@ export const useOwnLiveBeacons = (liveBeaconIds: BeaconIdentifier[]): LiveBeacon
}
};

const onResetWireError = () => {
liveBeaconIds.map(beaconId => OwnBeaconStore.instance.resetWireError(beaconId));
const onResetLocationPublishError = () => {
liveBeaconIds.map(beaconId => OwnBeaconStore.instance.resetLocationPublishError(beaconId));
};

return {
onStopSharing,
onResetWireError,
onResetLocationPublishError,
beacon,
stoppingInProgress,
hasWireError,
hasLocationPublishError,
hasStopSharingError: !!error,
};
};
24 changes: 15 additions & 9 deletions test/components/views/beacon/LeftPanelLiveShareWarning-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jest.mock('../../../../src/stores/OwnBeaconStore', () => {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const EventEmitter = require("events");
class MockOwnBeaconStore extends EventEmitter {
public getLiveBeaconIdsWithWireError = jest.fn().mockReturnValue([]);
public getLiveBeaconIdsWithLocationPublishError = jest.fn().mockReturnValue([]);
public getBeaconById = jest.fn();
public getLiveBeaconIds = jest.fn().mockReturnValue([]);
}
Expand Down Expand Up @@ -96,7 +96,7 @@ describe('<LeftPanelLiveShareWarning />', () => {

beforeEach(() => {
mocked(OwnBeaconStore.instance).isMonitoringLiveLocation = true;
mocked(OwnBeaconStore.instance).getLiveBeaconIdsWithWireError.mockReturnValue([]);
mocked(OwnBeaconStore.instance).getLiveBeaconIdsWithLocationPublishError.mockReturnValue([]);
mocked(OwnBeaconStore.instance).getLiveBeaconIds.mockReturnValue([beacon2.identifier, beacon1.identifier]);
});

Expand Down Expand Up @@ -126,14 +126,18 @@ describe('<LeftPanelLiveShareWarning />', () => {
expect(component).toMatchSnapshot();
});

it('renders wire error', () => {
mocked(OwnBeaconStore.instance).getLiveBeaconIdsWithWireError.mockReturnValue([beacon1.identifier]);
it('renders location publish error', () => {
mocked(OwnBeaconStore.instance).getLiveBeaconIdsWithLocationPublishError.mockReturnValue(
[beacon1.identifier],
);
const component = getComponent();
expect(component).toMatchSnapshot();
});

it('goes to room of latest beacon with wire error when clicked', () => {
mocked(OwnBeaconStore.instance).getLiveBeaconIdsWithWireError.mockReturnValue([beacon1.identifier]);
it('goes to room of latest beacon with location publish error when clicked', () => {
mocked(OwnBeaconStore.instance).getLiveBeaconIdsWithLocationPublishError.mockReturnValue(
[beacon1.identifier],
);
const component = getComponent();
const dispatchSpy = jest.spyOn(dispatcher, 'dispatch');

Expand All @@ -150,16 +154,18 @@ describe('<LeftPanelLiveShareWarning />', () => {
});

it('goes back to default style when wire errors are cleared', () => {
mocked(OwnBeaconStore.instance).getLiveBeaconIdsWithWireError.mockReturnValue([beacon1.identifier]);
mocked(OwnBeaconStore.instance).getLiveBeaconIdsWithLocationPublishError.mockReturnValue(
[beacon1.identifier],
);
const component = getComponent();
// error mode
expect(component.find('.mx_LeftPanelLiveShareWarning').at(0).text()).toEqual(
'An error occured whilst sharing your live location',
);

act(() => {
mocked(OwnBeaconStore.instance).getLiveBeaconIdsWithWireError.mockReturnValue([]);
OwnBeaconStore.instance.emit(OwnBeaconStoreEvent.WireError, 'abc');
mocked(OwnBeaconStore.instance).getLiveBeaconIdsWithLocationPublishError.mockReturnValue([]);
OwnBeaconStore.instance.emit(OwnBeaconStoreEvent.LocationPublishError, 'abc');
});

component.setProps({});
Expand Down
Loading

0 comments on commit 8c6786b

Please sign in to comment.