-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Deploy to production (Automated) #1615
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f623c91
new email renderer (#1584)
ahmetskilinc 9fdf26e
let it rip (#1616)
MrgSub 2fe89c8
Hotfix it (#1617)
MrgSub e048af0
Hotfix? (#1618)
MrgSub 3fd7b97
minor fixes (#1619)
MrgSub 007d845
Prefetch latest html content (#1620)
MrgSub 5e99498
Minor fixes (#1623)
MrgSub e868824
Update apps/server/src/lib/email-processor.ts
MrgSub File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,206 @@ | ||
| import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'; | ||
| import { useEffect, useMemo, useRef, useState, useCallback } from 'react'; | ||
| import { defaultUserSettings } from '@zero/server/schemas'; | ||
| import { fixNonReadableColors } from '@/lib/email-utils'; | ||
| import { useTRPC } from '@/providers/query-provider'; | ||
| import { getBrowserTimezone } from '@/lib/timezones'; | ||
| import { useSettings } from '@/hooks/use-settings'; | ||
| import { m } from '@/paraglide/messages'; | ||
| import { useTheme } from 'next-themes'; | ||
| import { cn } from '@/lib/utils'; | ||
| import { toast } from 'sonner'; | ||
|
|
||
| interface MailContentProps { | ||
| id: string; | ||
| html: string; | ||
| senderEmail: string; | ||
| } | ||
|
|
||
| export function MailContent({ id, html, senderEmail }: MailContentProps) { | ||
| const { data, refetch } = useSettings(); | ||
| const queryClient = useQueryClient(); | ||
| const isTrustedSender = useMemo( | ||
| () => data?.settings?.externalImages || data?.settings?.trustedSenders?.includes(senderEmail), | ||
| [data?.settings, senderEmail], | ||
| ); | ||
| const [cspViolation, setCspViolation] = useState(false); | ||
| const [temporaryImagesEnabled, setTemporaryImagesEnabled] = useState(false); | ||
| const hostRef = useRef<HTMLDivElement>(null); | ||
| const shadowRootRef = useRef<ShadowRoot | null>(null); | ||
| const { resolvedTheme } = useTheme(); | ||
| const trpc = useTRPC(); | ||
|
|
||
| const { mutateAsync: saveUserSettings } = useMutation({ | ||
| ...trpc.settings.save.mutationOptions(), | ||
| onSuccess: () => { | ||
| queryClient.invalidateQueries({ queryKey: ['settings'] }); | ||
| }, | ||
| }); | ||
|
|
||
| const { mutateAsync: trustSender } = useMutation({ | ||
| mutationFn: async () => { | ||
| const existingSettings = data?.settings ?? { | ||
| ...defaultUserSettings, | ||
| timezone: getBrowserTimezone(), | ||
| }; | ||
|
|
||
| const { success } = await saveUserSettings({ | ||
| ...existingSettings, | ||
| trustedSenders: data?.settings?.trustedSenders | ||
| ? data.settings.trustedSenders.concat(senderEmail) | ||
| : [senderEmail], | ||
| }); | ||
|
|
||
| if (!success) { | ||
| throw new Error('Failed to trust sender'); | ||
| } | ||
| }, | ||
| onSuccess: () => { | ||
| refetch(); | ||
| }, | ||
| onError: () => { | ||
| toast.error('Failed to trust sender'); | ||
| }, | ||
| }); | ||
|
|
||
| const { mutateAsync: processEmailContent } = useMutation( | ||
| trpc.mail.processEmailContent.mutationOptions(), | ||
| ); | ||
|
|
||
| const { data: processedData } = useQuery({ | ||
| queryKey: ['email-content', id, isTrustedSender || temporaryImagesEnabled, resolvedTheme], | ||
| queryFn: async () => { | ||
| const result = await processEmailContent({ | ||
| html, | ||
| shouldLoadImages: isTrustedSender || temporaryImagesEnabled, | ||
| theme: (resolvedTheme as 'light' | 'dark') || 'light', | ||
| }); | ||
|
|
||
| return { | ||
| html: result.processedHtml, | ||
| hasBlockedImages: result.hasBlockedImages, | ||
| }; | ||
| }, | ||
| staleTime: 30 * 60 * 1000, | ||
| gcTime: 60 * 60 * 1000, | ||
| refetchOnWindowFocus: false, | ||
| refetchOnMount: false, | ||
| }); | ||
|
|
||
| useEffect(() => { | ||
| if (processedData) { | ||
| if (processedData.hasBlockedImages) { | ||
| setCspViolation(true); | ||
| } | ||
| } | ||
| }, [processedData]); | ||
|
|
||
| useEffect(() => { | ||
| if (!hostRef.current || shadowRootRef.current) return; | ||
|
|
||
| shadowRootRef.current = hostRef.current.attachShadow({ mode: 'open' }); | ||
| }, []); | ||
|
|
||
| useEffect(() => { | ||
| if (!shadowRootRef.current || !processedData) return; | ||
|
|
||
| shadowRootRef.current.innerHTML = processedData.html; | ||
| }, [processedData]); | ||
|
|
||
| useEffect(() => { | ||
| if (!shadowRootRef.current) return; | ||
|
|
||
| const root = shadowRootRef.current; | ||
|
|
||
| const applyFix: () => void = () => { | ||
| const topLevelEls = Array.from(root.children) as HTMLElement[]; | ||
| topLevelEls.forEach((el) => { | ||
| try { | ||
| fixNonReadableColors(el, { | ||
| defaultBackground: resolvedTheme === 'dark' ? 'rgb(10,10,10)' : '#ffffff', | ||
| }); | ||
| } catch (err) { | ||
| console.error('Failed to fix colors in email content:', err); | ||
| } | ||
| }); | ||
| }; | ||
|
|
||
| requestAnimationFrame(applyFix); | ||
| }, [processedData, resolvedTheme]); | ||
|
|
||
| useEffect(() => { | ||
| if (isTrustedSender || temporaryImagesEnabled) { | ||
| setCspViolation(false); | ||
| } | ||
| }, [isTrustedSender, temporaryImagesEnabled]); | ||
|
|
||
| const handleImageError = useCallback( | ||
| (e: Event) => { | ||
| const target = e.target as HTMLImageElement; | ||
| if (target.tagName === 'IMG') { | ||
| if (!(isTrustedSender || temporaryImagesEnabled)) { | ||
| setCspViolation(true); | ||
| } | ||
| target.style.display = 'none'; | ||
| } | ||
| }, | ||
| [isTrustedSender, temporaryImagesEnabled], | ||
| ); | ||
|
|
||
| useEffect(() => { | ||
| if (!shadowRootRef.current) return; | ||
|
|
||
| shadowRootRef.current.addEventListener('error', handleImageError, true); | ||
|
|
||
| const handleClick = (e: Event) => { | ||
| const target = e.target as HTMLElement; | ||
| if (target.tagName === 'A') { | ||
| e.preventDefault(); | ||
| const href = target.getAttribute('href'); | ||
| if (href && (href.startsWith('http://') || href.startsWith('https://'))) { | ||
| window.open(href, '_blank', 'noopener,noreferrer'); | ||
| } else if (href && href.startsWith('mailto:')) { | ||
| window.location.href = href; | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| shadowRootRef.current.addEventListener('click', handleClick); | ||
|
|
||
| return () => { | ||
| shadowRootRef.current?.removeEventListener('error', handleImageError, true); | ||
| shadowRootRef.current?.removeEventListener('click', handleClick); | ||
| }; | ||
| }, [handleImageError, processedData]); | ||
|
|
||
| return ( | ||
| <> | ||
| {cspViolation && !isTrustedSender && !data?.settings?.externalImages && ( | ||
| <div className="flex items-center justify-start bg-amber-600/20 px-2 py-1 text-sm text-amber-600"> | ||
| <p>{m['common.actions.hiddenImagesWarning']()}</p> | ||
| <button | ||
| onClick={() => setTemporaryImagesEnabled(!temporaryImagesEnabled)} | ||
| className="ml-2 cursor-pointer underline" | ||
| > | ||
| {temporaryImagesEnabled | ||
| ? m['common.actions.disableImages']() | ||
| : m['common.actions.showImages']()} | ||
| </button> | ||
| <button | ||
| onClick={async () => { | ||
| try { | ||
| await trustSender(); | ||
| } catch (error) { | ||
| console.error('Error trusting sender:', error); | ||
| } | ||
| }} | ||
| className="ml-2 cursor-pointer underline" | ||
| > | ||
| {m['common.actions.trustSender']()} | ||
| </button> | ||
| </div> | ||
| )} | ||
| <div ref={hostRef} className={cn('mail-content w-full flex-1 overflow-hidden')} /> | ||
| </> | ||
| ); | ||
| } | ||
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
logic: Race condition possible between reading existing settings and saving. Consider using optimistic updates or locking mechanism