-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cdp): Add mailgun destination (#24137)
- Loading branch information
1 parent
5e1dd83
commit 0c3d83d
Showing
18 changed files
with
680 additions
and
19 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
frontend/src/scenes/pipeline/hogfunctions/email-templater/EmailTemplater.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import { LemonButton, LemonLabel, LemonModal } from '@posthog/lemon-ui' | ||
import { useActions, useValues } from 'kea' | ||
import { Form } from 'kea-forms' | ||
import { LemonField } from 'lib/lemon-ui/LemonField' | ||
import { CodeEditorInline } from 'lib/monaco/CodeEditorInline' | ||
import { capitalizeFirstLetter } from 'lib/utils' | ||
import EmailEditor from 'react-email-editor' | ||
|
||
import { emailTemplaterLogic, EmailTemplaterLogicProps } from './emailTemplaterLogic' | ||
|
||
function EmailTemplaterForm({ | ||
mode, | ||
...props | ||
}: EmailTemplaterLogicProps & { | ||
mode: 'full' | 'preview' | ||
}): JSX.Element { | ||
const { setEmailEditorRef, emailEditorReady, setIsModalOpen } = useActions(emailTemplaterLogic(props)) | ||
|
||
return ( | ||
<Form | ||
className="flex flex-col border rounded overflow-hidden flex-1" | ||
logic={props.formLogic} | ||
props={props.formLogicProps} | ||
formKey={props.formKey} | ||
> | ||
{['from', 'to', 'subject'].map((field) => ( | ||
<LemonField | ||
key={field} | ||
name={`${props.formFieldsPrefix ? props.formFieldsPrefix + '.' : ''}${field}`} | ||
className="border-b shrink-0 gap-1 pl-2" | ||
// We will handle the error display ourselves | ||
renderError={() => null} | ||
> | ||
{({ value, onChange, error }) => ( | ||
<div className="flex items-center"> | ||
<LemonLabel className={error ? 'text-danger' : ''}> | ||
{capitalizeFirstLetter(field)} | ||
</LemonLabel> | ||
<CodeEditorInline | ||
embedded | ||
className="flex-1" | ||
globals={props.globals} | ||
value={value} | ||
onChange={onChange} | ||
/> | ||
</div> | ||
)} | ||
</LemonField> | ||
))} | ||
|
||
{mode === 'full' ? ( | ||
<EmailEditor ref={(r) => setEmailEditorRef(r)} onReady={() => emailEditorReady()} /> | ||
) : ( | ||
<LemonField | ||
name={`${props.formFieldsPrefix ? props.formFieldsPrefix + '.' : ''}html`} | ||
className="relative flex flex-col" | ||
> | ||
{({ value }) => ( | ||
<> | ||
<div className="absolute inset-0 p-2 flex items-end justify-center transition-opacity opacity-0 hover:opacity-100"> | ||
<div className="opacity-50 bg-bg-light absolute inset-0" /> | ||
<LemonButton type="primary" size="small" onClick={() => setIsModalOpen(true)}> | ||
Click to modify content | ||
</LemonButton> | ||
</div> | ||
|
||
<iframe srcDoc={value} className="flex-1" /> | ||
</> | ||
)} | ||
</LemonField> | ||
)} | ||
</Form> | ||
) | ||
} | ||
|
||
export function EmailTemplaterModal({ ...props }: EmailTemplaterLogicProps): JSX.Element { | ||
const { isModalOpen } = useValues(emailTemplaterLogic(props)) | ||
const { setIsModalOpen, onSave } = useActions(emailTemplaterLogic(props)) | ||
|
||
return ( | ||
<LemonModal isOpen={isModalOpen} width="90vw" onClose={() => setIsModalOpen(false)}> | ||
<div className="h-[80vh] flex"> | ||
<div className="flex flex-col flex-1"> | ||
<div className="shrink-0"> | ||
<h2>Editing email template</h2> | ||
</div> | ||
<EmailTemplaterForm {...props} mode="full" /> | ||
<div className="flex items-center mt-2 gap-2"> | ||
<div className="flex-1" /> | ||
<LemonButton onClick={() => setIsModalOpen(false)}>Cancel</LemonButton> | ||
<LemonButton type="primary" onClick={() => onSave()}> | ||
Save | ||
</LemonButton> | ||
</div> | ||
</div> | ||
</div> | ||
</LemonModal> | ||
) | ||
} | ||
|
||
export function EmailTemplater(props: EmailTemplaterLogicProps): JSX.Element { | ||
return ( | ||
<div className="flex flex-col flex-1"> | ||
<EmailTemplaterForm {...props} mode="preview" /> | ||
<EmailTemplaterModal {...props} /> | ||
</div> | ||
) | ||
} |
120 changes: 120 additions & 0 deletions
120
frontend/src/scenes/pipeline/hogfunctions/email-templater/emailTemplaterLogic.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
import { actions, connect, kea, listeners, LogicWrapper, path, props, reducers } from 'kea' | ||
import { capitalizeFirstLetter } from 'lib/utils' | ||
import { EditorRef as _EditorRef } from 'react-email-editor' | ||
|
||
import type { emailTemplaterLogicType } from './emailTemplaterLogicType' | ||
|
||
// Helping kea-typegen navigate the exported type | ||
export interface EditorRef extends _EditorRef {} | ||
|
||
export type EmailTemplate = { | ||
design: any | ||
html: string | ||
subject: string | ||
text: string | ||
from: string | ||
to: string | ||
} | ||
|
||
export interface EmailTemplaterLogicProps { | ||
formLogic: LogicWrapper | ||
formLogicProps: any | ||
formKey: string | ||
formFieldsPrefix?: string | ||
globals?: Record<string, any> | ||
} | ||
|
||
export const emailTemplaterLogic = kea<emailTemplaterLogicType>([ | ||
props({} as EmailTemplaterLogicProps), | ||
connect({ | ||
// values: [teamLogic, ['currentTeam'], groupsModel, ['groupTypes'], userLogic, ['hasAvailableFeature']], | ||
}), | ||
path(() => ['scenes', 'pipeline', 'hogfunctions', 'emailTemplaterLogic']), | ||
actions({ | ||
onSave: true, | ||
setEmailEditorRef: (emailEditorRef: EditorRef | null) => ({ emailEditorRef }), | ||
emailEditorReady: true, | ||
setIsModalOpen: (isModalOpen: boolean) => ({ isModalOpen }), | ||
}), | ||
reducers({ | ||
emailEditorRef: [ | ||
null as EditorRef | null, | ||
{ | ||
setEmailEditorRef: (_, { emailEditorRef }) => emailEditorRef, | ||
}, | ||
], | ||
isModalOpen: [ | ||
false, | ||
{ | ||
setIsModalOpen: (_, { isModalOpen }) => isModalOpen, | ||
}, | ||
], | ||
}), | ||
|
||
listeners(({ props, values, actions }) => ({ | ||
onSave: async () => { | ||
const editor = values.emailEditorRef?.editor | ||
if (!editor) { | ||
return | ||
} | ||
const data = await new Promise<any>((res) => editor.exportHtml(res)) | ||
|
||
// TRICKY: We have to build the action we need in order to nicely callback to the form field | ||
const setFormValue = props.formLogic.findMounted(props.formLogicProps)?.actions?.[ | ||
`set${capitalizeFirstLetter(props.formKey)}Value` | ||
] | ||
|
||
const pathParts = props.formFieldsPrefix ? props.formFieldsPrefix.split('.') : [] | ||
|
||
setFormValue(pathParts.concat('design'), data.design) | ||
setFormValue(pathParts.concat('html'), escapeHTMLStringCurlies(data.html)) | ||
|
||
// Load the logic and set the property... | ||
actions.setIsModalOpen(false) | ||
}, | ||
|
||
emailEditorReady: () => { | ||
const pathParts = (props.formFieldsPrefix ? props.formFieldsPrefix.split('.') : []).concat('design') | ||
|
||
let value = props.formLogic.findMounted(props.formLogicProps)?.values?.[props.formKey] | ||
|
||
// Get the value from the form and set it in the editor | ||
while (pathParts.length && value) { | ||
value = value[pathParts.shift()!] | ||
} | ||
|
||
if (value) { | ||
values.emailEditorRef?.editor?.loadDesign(value) | ||
} | ||
}, | ||
})), | ||
]) | ||
|
||
function escapeHTMLStringCurlies(htmlString: string): string { | ||
const parser = new DOMParser() | ||
const doc = parser.parseFromString(htmlString, 'text/html') | ||
|
||
function escapeCurlyBraces(text: string): string { | ||
return text.replace(/{/g, '\\{') | ||
} | ||
|
||
function processNode(node: Node): void { | ||
if (node.nodeType === Node.ELEMENT_NODE) { | ||
const element = node as HTMLElement | ||
if (element.tagName === 'STYLE' || element.tagName === 'SCRIPT') { | ||
element.textContent = escapeCurlyBraces(element.textContent || '') | ||
} else { | ||
Array.from(node.childNodes).forEach(processNode) | ||
} | ||
} else if (node.nodeType === Node.COMMENT_NODE) { | ||
const commentContent = (node as Comment).nodeValue || '' | ||
;(node as Comment).nodeValue = escapeCurlyBraces(commentContent) | ||
} | ||
} | ||
|
||
processNode(doc.head) | ||
processNode(doc.body) | ||
|
||
const serializer = new XMLSerializer() | ||
return serializer.serializeToString(doc) | ||
} |
Oops, something went wrong.