BlueWallet/components/SaveFileButton.tsx

80 lines
2 KiB
TypeScript
Raw Normal View History

2024-06-17 15:38:01 -04:00
import React, { ReactNode, useCallback } from 'react';
import { StyleProp, ViewStyle } from 'react-native';
2024-05-20 10:54:13 +01:00
import * as fs from '../blue_modules/fs';
2024-03-21 20:54:40 -04:00
import loc from '../loc';
2024-03-24 18:27:58 -04:00
import { ActionIcons } from '../typings/ActionIcons';
2024-05-18 12:48:03 -04:00
import ToolTipMenu from './TooltipMenu';
2024-05-20 10:54:13 +01:00
import { Action } from './types';
2024-03-21 20:54:40 -04:00
interface SaveFileButtonProps {
fileName: string;
2024-03-29 15:07:14 -04:00
fileContent: string;
2024-03-29 21:22:05 -04:00
children?: ReactNode;
2024-03-21 20:54:40 -04:00
style?: StyleProp<ViewStyle>;
2024-03-24 18:27:58 -04:00
afterOnPress?: () => void;
2024-05-18 12:48:03 -04:00
beforeOnPress?: () => Promise<void>;
onMenuWillHide?: () => void;
onMenuWillShow?: () => void;
2024-03-21 20:54:40 -04:00
}
const SaveFileButton: React.FC<SaveFileButtonProps> = ({
fileName,
fileContent,
children,
style,
beforeOnPress,
afterOnPress,
onMenuWillHide,
onMenuWillShow,
}) => {
2024-06-17 15:38:01 -04:00
const handlePressMenuItem = useCallback(
async (actionId: string) => {
if (beforeOnPress) {
await beforeOnPress();
}
const action = actions.find(a => a.id === actionId);
2024-03-24 18:27:58 -04:00
2024-06-17 15:38:01 -04:00
if (action?.id === 'save') {
await fs.writeFileAndExport(fileName, fileContent, false).finally(() => {
afterOnPress?.();
});
} else if (action?.id === 'share') {
await fs.writeFileAndExport(fileName, fileContent, true).finally(() => {
afterOnPress?.();
});
}
},
[afterOnPress, beforeOnPress, fileContent, fileName],
);
2024-03-21 20:54:40 -04:00
2024-03-24 18:27:58 -04:00
return (
<ToolTipMenu
onMenuWillHide={onMenuWillHide}
onMenuWillShow={onMenuWillShow}
isButton
isMenuPrimaryAction
actions={actions}
onPressMenuItem={handlePressMenuItem}
2024-05-18 12:48:03 -04:00
buttonStyle={style as ViewStyle} // Type assertion to match ViewStyle
>
2024-04-02 08:20:56 -04:00
{children}
</ToolTipMenu>
2024-03-21 20:54:40 -04:00
);
};
export default SaveFileButton;
2024-03-24 18:27:58 -04:00
const actionIcons: { [key: string]: ActionIcons } = {
Share: {
iconValue: 'square.and.arrow.up',
},
Save: {
iconValue: 'square.and.arrow.down',
},
};
2024-05-18 12:48:03 -04:00
const actions: Action[] = [
{ id: 'save', text: loc._.save, icon: actionIcons.Save },
{ id: 'share', text: loc.receive.details_share, icon: actionIcons.Share },
];