lbry-desktop/ui/modal/modalOpenExternalResource/view.jsx

83 lines
2.2 KiB
React
Raw Normal View History

2019-05-12 16:51:22 +02:00
// @flow
import React, { useCallback, useEffect } from 'react';
2019-05-12 16:51:22 +02:00
import { Modal } from 'modal/modal';
import { formatFileSystemPath } from 'util/url';
import { FormField } from 'component/common/form';
import usePersistedState from 'effects/use-persisted-state';
2019-05-12 16:51:22 +02:00
// @if TARGET='app'
import { shell } from 'electron';
// @endif
type Props = {
uri: string,
isTrusted: boolean,
2019-05-12 16:51:22 +02:00
path: string,
isMine: boolean,
2019-05-12 16:51:22 +02:00
closeModal: () => void,
};
function ModalOpenExternalResource(props: Props) {
const [stopWarning, setStopWarning] = usePersistedState('stop-warning', false);
const { uri, isTrusted, path, isMine, closeModal } = props;
const openResource = useCallback(() => {
2019-05-12 16:51:22 +02:00
// @if TARGET='app'
2020-10-16 16:49:01 +02:00
const { openExternal, openPath, showItemInFolder } = shell;
2019-05-12 16:51:22 +02:00
if (uri) {
openExternal(uri);
2019-05-12 19:59:57 +02:00
} else if (path) {
2020-10-16 16:49:01 +02:00
const success = openPath(path);
2019-05-12 16:51:22 +02:00
if (!success) {
showItemInFolder(path);
}
}
// @endif
// @if TARGET='web'
if (uri) {
window.open(uri);
} else if (path) {
window.open(formatFileSystemPath(path));
2019-05-12 16:51:22 +02:00
}
// @endif
closeModal();
}, [closeModal, path, uri]);
useEffect(() => {
if ((uri && isTrusted) || (path && isMine) || stopWarning) {
openResource();
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [uri, isTrusted, path, isMine, openResource]);
2019-05-12 16:51:22 +02:00
return (
<Modal
isOpen
title={__('Warning!')}
contentLabel={__('Confirm External Resource')}
type="confirm"
confirmButtonLabel={__('Continue')}
onConfirmed={() => openResource()}
onAborted={closeModal}
>
2019-07-21 23:31:22 +02:00
<p>
{(uri && __('This link leads to an external website.')) ||
(path && __('This file has been shared with you by other people.'))}
</p>
<blockquote>{uri || path}</blockquote>
<p>{__('LBRY Inc is not responsible for its content, click continue to proceed at your own risk.')}</p>
<div className="stop-warning">
<FormField
type="checkbox"
checked={stopWarning}
onChange={() => setStopWarning(!stopWarning)}
label={__("Don't Show This Message Again")}
name="stop_warning"
/>
</div>
</Modal>
);
2019-05-12 16:51:22 +02:00
}
export default ModalOpenExternalResource;