GoonCord/src/lib/ui/components/ContextMenu.tsx

53 lines
1.5 KiB
TypeScript
Raw Normal View History

import { instead } from "@lib/api/patcher";
2024-03-07 12:35:07 +08:00
import { OptionalKeys } from "@lib/utils/types";
2024-05-20 15:36:33 +08:00
import { findByProps } from "@metro/utils";
import { AccessibilityInfo, Platform, View } from "react-native";
2024-02-09 12:33:39 +08:00
const { ContextMenu: _ContextMenu } = findByProps("ContextMenu");
2024-02-09 12:33:39 +08:00
type ContextMenuItem = {
label: string,
iconSource: import("react-native").ImageSourcePropType,
action: () => unknown,
[key: string]: any;
2024-03-06 01:57:59 +08:00
};
type ContextMenuProps = {
2024-02-09 12:33:39 +08:00
triggerOnLongPress: boolean;
items: ContextMenuItem[] | ContextMenuItem[][];
2024-03-07 12:35:07 +08:00
align: OptionalKeys<["left", "right", "above", "below", "auto"]>;
2024-02-09 12:33:39 +08:00
title: string;
children: React.FC;
[key: string]: any;
2024-03-06 01:57:59 +08:00
};
export default function ContextMenu(props: ContextMenuProps) {
const ref = React.useRef(null);
React.useEffect(() => {
// hacky discord fix for android where pressing back with context menu open crashes the client
if (Platform.OS !== "android") return;
// @ts-ignore
const ctxMenuReactTag = ref.current?._children?.[0]?._nativeTag;
return () => {
if (!ctxMenuReactTag) return;
const unpatch = instead("setAccessibilityFocus", AccessibilityInfo, ([tag], orig) => {
if (tag !== ctxMenuReactTag) {
return orig.apply(AccessibilityInfo, [tag]);
} else {
unpatch(); // and skip this call
}
});
2024-03-06 01:57:59 +08:00
};
}, []);
return <View ref={ref}>
<_ContextMenu
{...props}
/>
</View>;
2024-03-06 01:57:59 +08:00
}