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