-
Notifications
You must be signed in to change notification settings - Fork 4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(web): reusable sms preview component #5173
Changes from 5 commits
fcd7932
d5f8f68
870a165
752f691
0ddf3fb
12453f9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,8 +1,9 @@ | ||||||
import { errorMessage } from '@novu/design-system'; | ||||||
import { IEmailBlock } from '@novu/shared'; | ||||||
import type { IResponseError, IEmailBlock } from '@novu/shared'; | ||||||
import { IS_DOCKER_HOSTED } from '@novu/shared-web'; | ||||||
import { useMutation } from '@tanstack/react-query'; | ||||||
import { useCallback } from 'react'; | ||||||
|
||||||
import { getLocalesFromContent } from '../translations'; | ||||||
|
||||||
export interface ILocale { | ||||||
|
@@ -26,14 +27,11 @@ export const useGetLocalesFromContent = () => { | |||||
mutateAsync: getLocalesFromContentMutation, | ||||||
isLoading, | ||||||
data, | ||||||
} = useMutation<ILocale[], { error: string; message: string; statusCode: number }, Payload>( | ||||||
({ content }) => getLocalesFromContent({ content }), | ||||||
{ | ||||||
onError: (e: any) => { | ||||||
errorMessage(e.message || 'Unexpected error'); | ||||||
}, | ||||||
} | ||||||
); | ||||||
} = useMutation<ILocale[], IResponseError, Payload>(({ content }) => getLocalesFromContent({ content }), { | ||||||
onError: (e: any) => { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🤓 nitpick:
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it should be defined tho |
||||||
errorMessage(e.message || 'Unexpected error'); | ||||||
}, | ||||||
}); | ||||||
|
||||||
const getLocalesFromContentCallback = useCallback( | ||||||
async ({ content }: Payload) => { | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { useMutation, UseMutationOptions } from '@tanstack/react-query'; | ||
import { useCallback } from 'react'; | ||
import { errorMessage } from '@novu/design-system'; | ||
import type { IEmailBlock, IResponseError } from '@novu/shared'; | ||
import { IS_DOCKER_HOSTED } from '@novu/shared-web'; | ||
|
||
import { previewSms } from '../content-templates'; | ||
|
||
type PayloadType = { | ||
content?: string | IEmailBlock[]; | ||
payload: string; | ||
locale?: string; | ||
}; | ||
|
||
type ResultType = { content: string }; | ||
|
||
export const usePreviewSms = (options: UseMutationOptions<ResultType, IResponseError, PayloadType> = {}) => { | ||
const { mutateAsync, isLoading } = useMutation<ResultType, IResponseError, PayloadType>( | ||
({ content, payload, locale }) => previewSms({ content, payload, locale }), | ||
{ | ||
onError: (e) => { | ||
errorMessage(e.message || 'Unexpected error'); | ||
}, | ||
onSuccess: (result, variables, context) => { | ||
options?.onSuccess?.(result, variables, context); | ||
}, | ||
} | ||
); | ||
|
||
const getSmsPreview = useCallback( | ||
({ content, payload, locale }: PayloadType) => { | ||
if (IS_DOCKER_HOSTED) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎯 suggestion: Could you please add a comment in-code explaining why we do this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is a general pattern we use when something should not be available for the self-hosted users There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While it reads logically, it might be good to extract this structure to a single helper function to ensure we apply it consistently and with documentation / comments There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. let's have a tech debt ticket for this as we have many places around the code ;) |
||
return; | ||
} | ||
|
||
return mutateAsync({ | ||
content, | ||
payload, | ||
locale, | ||
}); | ||
}, | ||
[mutateAsync] | ||
); | ||
|
||
return { | ||
getSmsPreview, | ||
isLoading, | ||
}; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
❔ question: Do we have a more restricted type for
Locale
that could be used here?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unfortunately, we don't :/ might be hard to introduce now without refactoring
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good, created a ticket for the future: https://linear.app/novu/issue/NV-3499/create-a-reusable-typescript-type-for-locale