Apply suggestions

This commit is contained in:
Jaiwanth 2021-06-29 10:57:02 +05:30
parent 025a7eb9a5
commit e404fa3cae
4 changed files with 161 additions and 165 deletions

View file

@ -212,151 +212,154 @@ const ExportDialog: React.FC<IProps> = ({ room, onFinished }) => {
const sizePostFix = <span>{ _t("MB") }</span>;
let componentToDisplay: JSX.Element;
if (exportCancelled) {
// Display successful cancellation message
return <BaseDialog
title={_t("Export Cancelled")}
className="mx_ExportDialog"
contentId="mx_Dialog_content"
onFinished={onFinished}
fixedWidth={true}
>
<p>{ _t("The export was cancelled successfully") }</p>
return (
<BaseDialog
title={_t("Export Cancelled")}
className="mx_ExportDialog"
contentId="mx_Dialog_content"
onFinished={onFinished}
fixedWidth={true}
>
<p>{ _t("The export was cancelled successfully") }</p>
<DialogButtons
primaryButton={_t("Okay")}
hasCancel={false}
onPrimaryButtonClick={onFinished}
/>
</BaseDialog>
<DialogButtons
primaryButton={ _t("Okay") }
hasCancel={false}
onPrimaryButtonClick={onFinished}
/>
</BaseDialog>
);
} else if (exportSuccessful) {
// Display successful export message
return <BaseDialog
title={_t("Export Successful")}
className="mx_ExportDialog"
contentId="mx_Dialog_content"
onFinished={onFinished}
fixedWidth={true}
>
<p>{ _t("Your messages were successfully exported") }</p>
return (
<BaseDialog
title={_t("Export Successful")}
className="mx_ExportDialog"
contentId="mx_Dialog_content"
onFinished={onFinished}
fixedWidth={true}
>
<p>{ _t("Your messages were successfully exported") }</p>
<DialogButtons
primaryButton={_t("Okay")}
hasCancel={false}
onPrimaryButtonClick={onFinished}
/>
</BaseDialog>
<DialogButtons
primaryButton={_t("Okay")}
hasCancel={false}
onPrimaryButtonClick={onFinished}
/>
</BaseDialog>
);
} else if (!isExporting) {
// Display export settings
<BaseDialog
title={_t("Export Chat")}
className="mx_ExportDialog"
contentId="mx_Dialog_content"
hasCancel={true}
onFinished={onFinished}
fixedWidth={true}
>
<p>
{_t(
"Select from the options below to export chats from your timeline",
)}
</p>
<span className="mx_ExportDialog_subheading">{ _t("Format") }</span>
<StyledRadioGroup
name="feedbackRating"
value={exportFormat}
onChange={(key) => setExportFormat(exportFormats[key])}
definitions={exportFormatOptions}
/>
<span className="mx_ExportDialog_subheading">{ _t("Messages") }</span>
<Field
element="select"
value={exportType}
onChange={(e) => {
setExportType(exportTypes[e.target.value]);
}}
return (
<BaseDialog
title={_t("Export Chat")}
className="mx_ExportDialog"
contentId="mx_Dialog_content"
hasCancel={true}
onFinished={onFinished}
fixedWidth={true}
>
{ exportTypeOptions }
</Field>
{ messageCount }
<p>
{ _t("Select from the options below to export chats from your timeline") }
</p>
<span className="mx_ExportDialog_subheading">
{ _t("Size Limit") }
</span>
<span className="mx_ExportDialog_subheading">{ _t("Format") }</span>
<Field
type="number"
autoComplete="off"
onValidate={onValidateSize}
element="input"
ref={sizeLimitRef}
value={sizeLimit}
postfixComponent={sizePostFix}
onChange={(e) => setSizeLimit(parseInt(e.target.value))}
/>
<StyledRadioGroup
name="feedbackRating"
value={exportFormat}
onChange={(key) => setExportFormat(exportFormats[key])}
definitions={exportFormatOptions}
/>
<StyledCheckbox
checked={includeAttachments}
onChange={(e) =>
setAttachments((e.target as HTMLInputElement).checked)
}
>
{ _t("Include Attachments") }
</StyledCheckbox>
<span className="mx_ExportDialog_subheading">{ _t("Messages") }</span>
<DialogButtons
primaryButton={_t("Export")}
onPrimaryButtonClick={onExportClick}
onCancel={() => onFinished(false)}
/>
</BaseDialog>
<Field
element="select"
value={exportType}
onChange={(e) => {
setExportType(exportTypes[e.target.value]);
}}
>
{ exportTypeOptions }
</Field>
{ messageCount }
<span className="mx_ExportDialog_subheading">{ _t("Size Limit") }</span>
<Field
type="number"
autoComplete="off"
onValidate={onValidateSize}
element="input"
ref={sizeLimitRef}
value={sizeLimit}
postfixComponent={sizePostFix}
onChange={(e) => setSizeLimit(parseInt(e.target.value))}
/>
<StyledCheckbox
checked={includeAttachments}
onChange={(e) =>
setAttachments((e.target as HTMLInputElement).checked)
}
>
{ _t("Include Attachments") }
</StyledCheckbox>
<DialogButtons
primaryButton={_t("Export")}
onPrimaryButtonClick={onExportClick}
onCancel={() => onFinished(false)}
/>
</BaseDialog>
);
} else if (displayCancel) {
// Display cancel warning
<BaseDialog
title={_t("Warning")}
className="mx_ExportDialog"
contentId="mx_Dialog_content"
onFinished={onFinished}
fixedWidth={true}
>
<p>
{_t(
"Are you sure you want to stop exporting your data? If you do, you'll need to start over.",
)}
</p>
<DialogButtons
primaryButton={_t("Abort export process")}
primaryButtonClass="danger"
hasCancel={true}
cancelButton={_t("Continue")}
onCancel={() => setCancelWarning(false)}
onPrimaryButtonClick={confirmCanel}
/>
</BaseDialog>
return (
<BaseDialog
title={_t("Warning")}
className="mx_ExportDialog"
contentId="mx_Dialog_content"
onFinished={onFinished}
fixedWidth={true}
>
<p>
{_t(
"Are you sure you want to stop exporting your data? If you do, you'll need to start over.",
)}
</p>
<DialogButtons
primaryButton={_t("Abort export process")}
primaryButtonClass="danger"
hasCancel={true}
cancelButton={_t("Continue")}
onCancel={() => setCancelWarning(false)}
onPrimaryButtonClick={confirmCanel}
/>
</BaseDialog>
);
} else {
// Display progress dialog
return <BaseDialog
title={_t("Exporting your data...")}
className="mx_ExportDialog"
contentId="mx_Dialog_content"
onFinished={onFinished}
fixedWidth={true}
>
<DialogButtons
primaryButton={_t("Cancel")}
primaryButtonClass="danger"
hasCancel={false}
onPrimaryButtonClick={onCancel}
/>
</BaseDialog>
return (
<BaseDialog
title={_t("Exporting your data...")}
className="mx_ExportDialog"
contentId="mx_Dialog_content"
onFinished={onFinished}
fixedWidth={true}
>
<DialogButtons
primaryButton={_t("Cancel")}
primaryButtonClass="danger"
hasCancel={false}
onPrimaryButtonClick={onCancel}
/>
</BaseDialog>
);
}
return componentToDisplay;
};
export default ExportDialog;

View file

@ -73,16 +73,16 @@ export default class ReplyThread extends React.Component {
this.unmounted = false;
if (this.props.forExport) return;
if (this.props.forExport) {
this.context.on("Event.replaced", this.onEventReplaced);
this.room = this.context.getRoom(this.props.parentEv.getRoomId());
this.room.on("Room.redaction", this.onRoomRedaction);
this.room.on("Room.redactionCancelled", this.onRoomRedaction);
this.context.on("Event.replaced", this.onEventReplaced);
this.room = this.context.getRoom(this.props.parentEv.getRoomId());
this.room.on("Room.redaction", this.onRoomRedaction);
this.room.on("Room.redactionCancelled", this.onRoomRedaction);
this.onQuoteClick = this.onQuoteClick.bind(this);
this.canCollapse = this.canCollapse.bind(this);
this.collapse = this.collapse.bind(this);
this.onQuoteClick = this.onQuoteClick.bind(this);
this.canCollapse = this.canCollapse.bind(this);
this.collapse = this.collapse.bind(this);
}
}
static getParentEventId(ev) {

View file

@ -2261,19 +2261,19 @@
"Enter a number between %(min)s and %(max)s": "Enter a number between %(min)s and %(max)s",
"Number of messages": "Number of messages",
"MB": "MB",
"Are you sure you want to stop exporting your data? If you do, you'll need to start over.": "Are you sure you want to stop exporting your data? If you do, you'll need to start over.",
"Abort export process": "Abort export process",
"Export Cancelled": "Export Cancelled",
"The export was cancelled successfully": "The export was cancelled successfully",
"Okay": "Okay",
"Export Successful": "Export Successful",
"Your messages were successfully exported": "Your messages were successfully exported",
"Export Chat": "Export Chat",
"Select from the options below to export chats from your timeline": "Select from the options below to export chats from your timeline",
"Format": "Format",
"Size Limit": "Size Limit",
"Include Attachments": "Include Attachments",
"Export": "Export",
"Export Successful": "Export Successful",
"Your messages were successfully exported": "Your messages were successfully exported",
"Okay": "Okay",
"Export Cancelled": "Export Cancelled",
"The export was cancelled successfully": "The export was cancelled successfully",
"Are you sure you want to stop exporting your data? If you do, you'll need to start over.": "Are you sure you want to stop exporting your data? If you do, you'll need to start over.",
"Abort export process": "Abort export process",
"Exporting your data...": "Exporting your data...",
"Feedback sent": "Feedback sent",
"Rate %(brand)s": "Rate %(brand)s",

View file

@ -10,35 +10,30 @@ import { EventType } from "matrix-js-sdk/src/@types/event";
export default class JSONExporter extends Exporter {
protected totalSize: number;
protected messages: any[];
constructor(room: Room, exportType: exportTypes, exportOptions: exportOptions) {
super(room, exportType, exportOptions);
this.totalSize = 0;
this.messages = [];
}
protected wrapJSON(json: string): string {
protected createJSONString(): string {
const exportDate = formatFullDateNoDayNoTime(new Date());
const creator = this.room.currentState.getStateEvents(EventType.RoomCreate, "")?.getSender();
const creatorName = this.room?.getMember(creator)?.rawDisplayName || creator;
const topic = this.room.currentState.getStateEvents(EventType.RoomTopic, "")?.getContent()?.topic || "";
const exporter = this.client.getUserId();
const exporterName = this.room?.getMember(exporter)?.rawDisplayName || exporter;
return `{
"room_name": "${this.room.name}",
"room_creator": "${creatorName}",
"topic": "${topic}",
"export_date": "${exportDate}",
"exported_by": "${exporterName}",
"messages": [
${json}
]
}`
}
protected indentEachLine(JSONString: string, spaces: number) {
const indent = ' ';
const regex = /^(?!\s*$)/gm;
return JSONString.replace(regex, indent.repeat(spaces));
const jsonObject = {
room_name: this.room.name,
room_creator: creatorName,
topic,
export_date: exportDate,
exported_by: exporterName,
messages: this.messages,
}
return JSON.stringify(jsonObject, null, 2);
}
protected async getJSONString(mxEv: MatrixEvent) {
@ -57,18 +52,16 @@ ${json}
}
const jsonEvent: any = mxEv.toJSON();
const clearEvent = mxEv.isEncrypted() ? jsonEvent.decrypted : jsonEvent;
const jsonString = JSON.stringify(clearEvent, null, 2);
return jsonString.length > 2 ? jsonString + ",\n" : "";
return clearEvent;
}
protected async createOutput(events: MatrixEvent[]) {
let content = "";
for (const event of events) {
if (this.cancelled) return this.cleanUp();
if (!haveTileForEvent(event)) continue;
content += await this.getJSONString(event);
this.messages.push(await this.getJSONString(event));
}
return this.wrapJSON(this.indentEachLine(content.slice(0, -2), 2));
return this.createJSONString();
}
public async export() {