2017-10-07 02:23:39 +08:00
|
|
|
import fs from 'fs';
|
|
|
|
import temp from 'temp';
|
2017-10-09 02:18:29 +08:00
|
|
|
import { shell } from 'electron';
|
2019-03-05 03:03:12 +08:00
|
|
|
import React from 'react';
|
|
|
|
import { PropTypes, RegExpUtils } from 'mailspring-exports';
|
2016-05-28 05:05:27 +08:00
|
|
|
|
2019-03-05 03:03:12 +08:00
|
|
|
interface FormErrorProps {
|
|
|
|
message: string;
|
|
|
|
log?: string;
|
|
|
|
empty?: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
const FormErrorMessage = (props: FormErrorProps) => {
|
2017-10-07 02:23:39 +08:00
|
|
|
const { message, log, empty } = props;
|
2016-05-28 05:05:27 +08:00
|
|
|
if (!message) {
|
|
|
|
return <div className="message empty">{empty}</div>;
|
|
|
|
}
|
|
|
|
|
2019-03-05 03:03:12 +08:00
|
|
|
let rawLogLink: React.ReactChild = null;
|
2017-10-07 02:23:39 +08:00
|
|
|
if (log && log.length > 0) {
|
|
|
|
const onViewLog = () => {
|
|
|
|
const logPath = temp.path({ suffix: '.log' });
|
|
|
|
fs.writeFileSync(logPath, log);
|
|
|
|
shell.openItem(logPath);
|
|
|
|
};
|
|
|
|
rawLogLink = (
|
|
|
|
<a href="" onClick={onViewLog} style={{ paddingLeft: 5 }}>
|
|
|
|
View Log
|
|
|
|
</a>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-08-06 06:20:36 +08:00
|
|
|
if (typeof message === 'string') {
|
2019-03-05 03:03:12 +08:00
|
|
|
const linkMatch = RegExpUtils.urlRegex().exec(message);
|
2018-08-06 06:20:36 +08:00
|
|
|
if (linkMatch) {
|
|
|
|
const link = linkMatch[0];
|
|
|
|
return (
|
|
|
|
<div className="message error">
|
|
|
|
{message.substr(0, linkMatch.index)}
|
|
|
|
<a href={link}>{link}</a>
|
|
|
|
{message.substr(linkMatch.index + link.length)}
|
|
|
|
{rawLogLink}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
2016-05-28 05:05:27 +08:00
|
|
|
}
|
|
|
|
|
2017-10-07 02:23:39 +08:00
|
|
|
return (
|
|
|
|
<div className="message error">
|
|
|
|
{message}
|
|
|
|
{rawLogLink}
|
|
|
|
</div>
|
|
|
|
);
|
2017-09-27 02:33:08 +08:00
|
|
|
};
|
2016-05-28 05:05:27 +08:00
|
|
|
|
|
|
|
FormErrorMessage.propTypes = {
|
2018-08-06 06:20:36 +08:00
|
|
|
empty: PropTypes.oneOfType([PropTypes.string, PropTypes.element]),
|
|
|
|
message: PropTypes.oneOfType([PropTypes.string, PropTypes.element]),
|
2016-05-28 05:05:27 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
export default FormErrorMessage;
|