Skip to content
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import useThemeStyles from '@hooks/useThemeStyles';

import Accessibility from '@libs/Accessibility';
import HapticFeedback from '@libs/HapticFeedback';
import type {Shortcut} from '@libs/KeyboardShortcut';
import mergeRefs from '@libs/mergeRefs';
import getRoutePath from '@libs/Navigation/helpers/getRoutePath';
import {isNewTabOrWindowMousePress} from '@libs/Navigation/helpers/openInternalRouteInNewTab';
Expand All @@ -19,7 +20,7 @@ import type WithSentryLabel from '@src/types/utils/SentryLabel';

import type {ComponentRef} from 'react';
import type {GestureResponderEvent, View} from 'react-native';
import type {DistributedOmit, ValueOf} from 'type-fest';
import type {DistributedOmit} from 'type-fest';

import React, {useEffect, useRef, useState} from 'react';
// eslint-disable-next-line no-restricted-imports
Expand All @@ -28,6 +29,9 @@ import {Pressable} from 'react-native';
/** The platform wrappers require the label: native passes it on to here, while web renders it as `data-sentry-label` and leaves it out. */
type BaseGenericPressableProps = DistributedOmit<PressableProps, 'sentryLabel'> & WithSentryLabel;

/** `useKeyboardShortcut` always needs a shortcut. A pressable without `keyboardShortcut` passes this empty one and keeps the hook inactive. */
const NO_KEYBOARD_SHORTCUT: Pick<Shortcut, 'shortcutKey' | 'descriptionKey' | 'modifiers'> = {shortcutKey: '', descriptionKey: '', modifiers: []};

function GenericPressable({
children,
onPress,
Expand Down Expand Up @@ -180,8 +184,7 @@ function GenericPressable({
return !isDisabled ? pressOnce : undefined;
};

const {shortcutKey, descriptionKey, modifiers} = keyboardShortcut ?? {};
useKeyboardShortcut({shortcutKey, descriptionKey, modifiers} as ValueOf<typeof CONST.KEYBOARD_SHORTCUTS>, onKeyboardShortcutPressHandler, {
useKeyboardShortcut(keyboardShortcut ?? NO_KEYBOARD_SHORTCUT, onKeyboardShortcutPressHandler, {
isActive: !!keyboardShortcut,
shouldBubble: false,
shouldPreventDefault: false,
Expand All @@ -204,7 +207,7 @@ function GenericPressable({

if (isRoleLink && !hasHref && event.nativeEvent.key === CONST.KEYBOARD_SHORTCUTS.ENTER.shortcutKey) {
event.preventDefault();
onPressHandler(event.nativeEvent as unknown as KeyboardEvent);
onPressHandler(event.nativeEvent);
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,35 @@ import mergeRefs from '@libs/mergeRefs';
import CONST from '@src/CONST';

import type {ComponentRef} from 'react';
import type {Role, View} from 'react-native';
import type {View} from 'react-native';

import React, {useLayoutEffect, useRef} from 'react';

import GenericPressable from './BaseGenericPressable';

function WebGenericPressable({focusable = true, ref, sentryLabel, 'aria-busy': explicitAriaBusy, ...props}: PressableProps) {
const accessible = (props.accessible ?? props.accessible === undefined) ? true : props.accessible;
const role = (props.accessibilityRole ?? props.role) as Role;
// Not every accessibilityRole is a valid `role`. React Native Web reads accessibilityRole itself, so only "link" is copied to
// `role`, because BaseGenericPressable checks `role` for its Enter key handler.
let role = props.role;
if (props.accessibilityRole) {
role = props.accessibilityRole === CONST.ROLE.LINK ? CONST.ROLE.LINK : undefined;
}
// React Native Web maps role="button" to a native <button>. Safari clears the native text range
// before the click handler can suppress row navigation, so render a div and restore the ARIA role.
const shouldRenderAsNonNativeButton = props.shouldAllowTextSelection && role === CONST.ROLE.BUTTON;
const effectiveRole = props.accessibilityRole ?? props.role;
const shouldRenderAsNonNativeButton = props.shouldAllowTextSelection && effectiveRole === CONST.ROLE.BUTTON;

// react-native-web's Pressable always sets aria-disabled from its own `disabled` prop,
// overriding any explicit aria-disabled we pass. We pass fullDisabled (not isDisabled) to
// preserve interaction/focus behavior, so we must set aria-disabled imperatively instead.
const internalRef = useRef<ComponentRef<typeof View>>(null);
// `||` is intentional so that falsy values like empty string or 0 are treated as not-disabled
const isAriaDisabled = props.fullDisabled || props.disabled || props.accessibilityState?.disabled;
// effectiveRole is a dependency even though the effect doesn't read it: React Native Web renders a different element when the role changes, and that element needs these attributes again.
useLayoutEffect(() => {
const el = internalRef.current as unknown as HTMLElement | null;
if (!el) {
const el = internalRef.current;
if (!(el instanceof HTMLElement)) {
return;
}
if (isAriaDisabled) {
Expand All @@ -35,9 +42,9 @@ function WebGenericPressable({focusable = true, ref, sentryLabel, 'aria-busy': e
el.removeAttribute('aria-disabled');
}
if (shouldRenderAsNonNativeButton) {
el.setAttribute('role', role);
el.setAttribute('role', CONST.ROLE.BUTTON);
}
}, [isAriaDisabled, role, shouldRenderAsNonNativeButton]);
}, [effectiveRole, isAriaDisabled, shouldRenderAsNonNativeButton]);

return (
<GenericPressable
Expand Down
3 changes: 2 additions & 1 deletion src/hooks/useKeyboardShortcut.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import KeyboardShortcut from '@libs/KeyboardShortcut';
import type {Shortcut as DocumentedShortcut} from '@libs/KeyboardShortcut';

import {useScreenFreezeContext} from '@navigation/PlatformStackNavigation/createPlatformStackNavigatorComponent/ScreenFreezeWrapper/ScreenFreezeContext';

Expand All @@ -9,7 +10,7 @@ import type {ValueOf} from 'type-fest';

import {useEffect} from 'react';

type Shortcut = ValueOf<typeof CONST.KEYBOARD_SHORTCUTS>;
type Shortcut = ValueOf<typeof CONST.KEYBOARD_SHORTCUTS> | Pick<DocumentedShortcut, 'shortcutKey' | 'descriptionKey' | 'modifiers'>;
type KeyboardShortcutConfig = {
/* Should we capture the event on inputs too? */
captureOnInputs?: boolean;
Expand Down
13 changes: 9 additions & 4 deletions src/libs/KeyboardShortcut/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ function getDisplayName(key: string, modifiers: string | string[]): string {
displayName = [...modifiers.sort(), ...displayName];
}

displayName = displayName.map((modifier) => CONST.KEYBOARD_SHORTCUT_KEY_DISPLAY_NAME[modifier.toUpperCase() as keyof typeof CONST.KEYBOARD_SHORTCUT_KEY_DISPLAY_NAME] ?? modifier);
// Any key can reach here, so read the display names as a string-keyed map; keys without one keep their own name.
const keyDisplayNames: Partial<Record<string, string>> = CONST.KEYBOARD_SHORTCUT_KEY_DISPLAY_NAME;
displayName = displayName.map((modifier) => keyDisplayNames[modifier.toUpperCase()] ?? modifier);

return displayName.join(' + ');
}
Expand All @@ -89,7 +91,9 @@ for (const shortcut of Object.values(CONST.KEYBOARD_SHORTCUTS)) {
continue;
}

const shortcutTrigger = (operatingSystem && shortcut.trigger[operatingSystem as keyof typeof shortcut.trigger]) ?? shortcut.trigger.DEFAULT;
// Most shortcuts only define a DEFAULT trigger, so read the triggers as a map where the current OS can be missing.
const triggers: Partial<Record<string, typeof shortcut.trigger.DEFAULT>> = shortcut.trigger;
const shortcutTrigger = (operatingSystem && triggers[operatingSystem]) ?? shortcut.trigger.DEFAULT;

KeyCommand.addListener(shortcutTrigger, (keyCommandEvent, event) => bindHandlerToKeydownEvent(getDisplayName, eventHandlers, keyCommandEvent, event));
}
Expand All @@ -113,8 +117,9 @@ function getPlatformEquivalentForKeys(keys: ShortcutModifiers): string[] {
return key;
}

const platformModifiers = CONST.PLATFORM_SPECIFIC_KEYS[key];
return platformModifiers?.[operatingSystem as keyof typeof platformModifiers] ?? platformModifiers.DEFAULT ?? key;
// Only some keys have a value for the current OS, so read them as a map where the OS can be missing.
const platformModifiers: Partial<Record<string, string>> = CONST.PLATFORM_SPECIFIC_KEYS[key];
return (operatingSystem && platformModifiers[operatingSystem]) ?? platformModifiers.DEFAULT ?? key;
});
}

Expand Down
31 changes: 3 additions & 28 deletions src/libs/actions/StoreReview/index.native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,42 +14,17 @@ import {hasAction as hasStoreReviewAction, isAvailableAsync, requestReview as re
async function requestReview(): Promise<void> {
try {
// Determine if any review action is possible on this build/device/config
const getHasAction = async (): Promise<boolean> => {
const candidate: unknown = hasStoreReviewAction;
if (typeof candidate === 'function') {
return (candidate as () => Promise<boolean>)();
}
return false;
};

const hasAction = await getHasAction();
const hasAction = await hasStoreReviewAction();
if (!hasAction) {
Log.info('[StoreReview] No review action found', false, {hasAction});
return;
}

// Check if available before requesting (returns false on web, TestFlight, Android <5.0)
const getAvailability = async (): Promise<boolean> => {
const candidate: unknown = isAvailableAsync;
if (typeof candidate === 'function') {
return (candidate as () => Promise<boolean>)();
}
return false;
};

const available = await getAvailability();
const available = await isAvailableAsync();
if (available) {
Log.info('[StoreReview] Requesting review', false, {available});
const performRequestReview = async (): Promise<void> => {
const candidate: unknown = requestNativeReview;
if (typeof candidate === 'function') {
return (candidate as () => Promise<void>)();
}

Log.hmmm('[StoreReview] No requestNativeReview function found');
};

await performRequestReview();
await requestNativeReview();
return;
}

Expand Down
8 changes: 0 additions & 8 deletions src/types/expo-store-review.d.ts

This file was deleted.

Loading