2020-10-16 17:26:19 +02:00
|
|
|
import { useCallback } from 'react';
|
|
|
|
|
2020-11-07 17:59:37 +02:00
|
|
|
const { _ } = require('@joplin/lib/locale.js');
|
2020-11-05 18:58:23 +02:00
|
|
|
const { dialogs } = require('../../../utils/dialogs.js');
|
2021-01-22 19:41:11 +02:00
|
|
|
import Resource from '@joplin/lib/models/Resource';
|
2021-04-07 19:41:54 +02:00
|
|
|
import { copyToCache } from '../../../utils/ShareUtils';
|
2023-10-02 16:15:51 +02:00
|
|
|
import isEditableResource from '../../NoteEditor/ImageEditor/isEditableResource';
|
2024-08-02 15:51:49 +02:00
|
|
|
import shim from '@joplin/lib/shim';
|
|
|
|
import shareFile from '../../../utils/shareFile';
|
|
|
|
import Logger from '@joplin/utils/Logger';
|
|
|
|
|
|
|
|
const logger = Logger.create('useOnResourceLongPress');
|
2020-10-16 17:26:19 +02:00
|
|
|
|
2023-10-02 16:15:51 +02:00
|
|
|
interface Callbacks {
|
|
|
|
onJoplinLinkClick: (link: string)=> void;
|
|
|
|
onRequestEditResource: (message: string)=> void;
|
|
|
|
}
|
|
|
|
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2023-10-02 16:15:51 +02:00
|
|
|
export default function useOnResourceLongPress(callbacks: Callbacks, dialogBoxRef: any) {
|
|
|
|
const { onJoplinLinkClick, onRequestEditResource } = callbacks;
|
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
return useCallback(async (msg: string) => {
|
2020-10-16 17:26:19 +02:00
|
|
|
try {
|
|
|
|
const resourceId = msg.split(':')[1];
|
|
|
|
const resource = await Resource.load(resourceId);
|
2023-10-02 16:15:51 +02:00
|
|
|
|
|
|
|
// Handle the case where it's a long press on a link with no resource
|
|
|
|
if (!resource) {
|
2024-08-02 15:51:49 +02:00
|
|
|
logger.warn(`Long-press: Resource with ID ${resourceId} does not exist (may be a note).`);
|
2023-10-02 16:15:51 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-04-27 11:21:00 +02:00
|
|
|
const name = resource.title ? resource.title : resource.filename;
|
2023-10-02 16:15:51 +02:00
|
|
|
const mime: string|undefined = resource.mime;
|
|
|
|
|
|
|
|
const actions = [];
|
|
|
|
|
|
|
|
actions.push({ text: _('Open'), id: 'open' });
|
|
|
|
if (mime && isEditableResource(mime)) {
|
|
|
|
actions.push({ text: _('Edit'), id: 'edit' });
|
|
|
|
}
|
|
|
|
actions.push({ text: _('Share'), id: 'share' });
|
2020-10-16 17:26:19 +02:00
|
|
|
|
2023-10-02 16:15:51 +02:00
|
|
|
const action = await dialogs.pop({ dialogbox: dialogBoxRef.current }, name, actions);
|
2020-10-16 17:26:19 +02:00
|
|
|
|
|
|
|
if (action === 'open') {
|
|
|
|
onJoplinLinkClick(`joplin://${resourceId}`);
|
|
|
|
} else if (action === 'share') {
|
2021-04-07 19:41:54 +02:00
|
|
|
const fileToShare = await copyToCache(resource);
|
2024-08-02 15:51:49 +02:00
|
|
|
await shareFile(fileToShare, resource.mime);
|
2023-10-02 16:15:51 +02:00
|
|
|
} else if (action === 'edit') {
|
|
|
|
onRequestEditResource(`edit:${resourceId}`);
|
2020-10-16 17:26:19 +02:00
|
|
|
}
|
|
|
|
} catch (e) {
|
2024-08-02 15:51:49 +02:00
|
|
|
logger.error('Could not handle link long press', e);
|
|
|
|
void shim.showMessageBox(`An error occurred, check log for details: ${e}`);
|
2020-10-16 17:26:19 +02:00
|
|
|
}
|
2023-10-02 16:15:51 +02:00
|
|
|
}, [onJoplinLinkClick, onRequestEditResource, dialogBoxRef]);
|
2020-10-16 17:26:19 +02:00
|
|
|
}
|