BlueWallet/components/SaveFileButton.tsx

79 lines
2.1 KiB
TypeScript
Raw Normal View History

2024-03-22 01:54:40 +01:00
import React, { ReactNode } from 'react';
import { StyleProp, ViewStyle } from 'react-native';
2024-03-22 01:54:40 +01:00
import ToolTipMenu from './TooltipMenu';
import loc from '../loc';
2024-03-24 23:27:58 +01:00
import { ActionIcons } from '../typings/ActionIcons';
2024-04-09 22:36:59 +02:00
import * as fs from '../blue_modules/fs';
2024-03-22 01:54:40 +01:00
interface SaveFileButtonProps {
fileName: string;
2024-03-29 20:07:14 +01:00
fileContent: string;
2024-03-30 02:22:05 +01:00
children?: ReactNode;
2024-03-22 01:54:40 +01:00
style?: StyleProp<ViewStyle>;
2024-03-24 23:27:58 +01:00
afterOnPress?: () => void;
beforeOnPress?: () => Promise<void>; // Changed this line
onMenuWillHide?: () => void;
onMenuWillShow?: () => void;
2024-03-22 01:54:40 +01:00
}
const SaveFileButton: React.FC<SaveFileButtonProps> = ({
fileName,
fileContent,
children,
style,
beforeOnPress,
afterOnPress,
onMenuWillHide,
onMenuWillShow,
}) => {
2024-03-22 01:54:40 +01:00
const actions = [
2024-03-24 23:27:58 +01:00
{ id: 'save', text: loc._.save, icon: actionIcons.Save },
{ id: 'share', text: loc.receive.details_share, icon: actionIcons.Share },
2024-03-22 01:54:40 +01:00
];
2024-03-24 23:27:58 +01:00
const handlePressMenuItem = async (actionId: string) => {
if (beforeOnPress) {
await beforeOnPress(); // Now properly awaiting a function that returns a promise
}
2024-03-22 01:54:40 +01:00
const action = actions.find(a => a.id === actionId);
2024-03-24 23:27:58 +01:00
2024-03-22 01:54:40 +01:00
if (action?.id === 'save') {
await fs.writeFileAndExport(fileName, fileContent, false).finally(() => {
2024-03-24 23:27:58 +01:00
afterOnPress?.(); // Safely call afterOnPress if it exists
});
2024-03-22 01:54:40 +01:00
} else if (action?.id === 'share') {
2024-03-29 20:07:14 +01:00
await fs.writeFileAndExport(fileName, fileContent, true).finally(() => {
2024-03-24 23:27:58 +01:00
afterOnPress?.(); // Safely call afterOnPress if it exists
});
2024-03-22 01:54:40 +01:00
}
};
2024-03-24 23:27:58 +01:00
return (
2024-03-30 02:22:05 +01:00
// @ts-ignore: Tooltip must be refactored to use TSX}
<ToolTipMenu
onMenuWillHide={onMenuWillHide}
onMenuWillShow={onMenuWillShow}
isButton
isMenuPrimaryAction
actions={actions}
onPressMenuItem={handlePressMenuItem}
buttonStyle={style}
>
2024-04-02 14:20:56 +02:00
{children}
</ToolTipMenu>
2024-03-22 01:54:40 +01:00
);
};
export default SaveFileButton;
2024-03-24 23:27:58 +01:00
const actionIcons: { [key: string]: ActionIcons } = {
Share: {
iconType: 'SYSTEM',
iconValue: 'square.and.arrow.up',
},
Save: {
iconType: 'SYSTEM',
iconValue: 'square.and.arrow.down',
},
};