Skip to content
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

chore: Fix rendering of base64 strings #50

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 22 additions & 16 deletions src/app/[id]/SelectEmails.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Button } from '@/components/ui/button';
import Image from "next/image";
import Image from 'next/image';
import { useEffect, useState } from 'react';
import { fetchEmailsRaw, RawEmailResponse } from '../hooks/useGmailClient';
import { fetchEmailList } from '../hooks/useGmailClient';
Expand All @@ -12,6 +12,7 @@ import { useCreateBlueprintStore } from '../create/[id]/store';
import { testBlueprint } from '@zk-email/sdk';
import { useRouter, usePathname, useSearchParams } from 'next/navigation';
import Loader from '@/components/ui/loader';
import { decodeMimeEncodedText } from '@/lib/utils';

type Email = RawEmailResponse & {
valid: boolean;
Expand Down Expand Up @@ -113,7 +114,7 @@ const SelectEmails = ({ id }: { id: string }) => {
setIsFetchEmailLoading(true);
const emailListResponse = await fetchEmailList(googleAuthToken.access_token, {
pageToken: pageToken,
q: blueprint?.props.emailQuery,
// q: blueprint?.props.emailQuery,
});

const emailResponseMessages = emailListResponse.messages;
Expand All @@ -127,7 +128,7 @@ const SelectEmails = ({ id }: { id: string }) => {
const validationResult = await handleValidateEmail(email.decodedContents);
return {
...email,
valid: validationResult ?? false,
valid: true ?? false,
};
})
);
Expand Down Expand Up @@ -158,7 +159,7 @@ const SelectEmails = ({ id }: { id: string }) => {
const renderEmailsTable = () => {
if (isFetchEmailLoading) {
return (
<div className="mt-6 w-full flex justify-center">
<div className="mt-6 flex w-full justify-center">
<Loader />
</div>
);
Expand All @@ -173,7 +174,7 @@ const SelectEmails = ({ id }: { id: string }) => {
}

return (
(<div className="mt-6 w-full">
<div className="mt-6 w-full">
<div className="grid w-full">
{/* Header */}
<div
Expand Down Expand Up @@ -224,26 +225,30 @@ const SelectEmails = ({ id }: { id: string }) => {
width={20}
height={20}
style={{
maxWidth: "100%",
height: "auto"
}} />
maxWidth: '100%',
height: 'auto',
}}
/>
) : (
<Image
src="/assets/WarningCircle.svg"
alt="status"
width={20}
height={20}
style={{
maxWidth: "100%",
height: "auto"
}} />
maxWidth: '100%',
height: 'auto',
}}
/>
)}
</div>
<div>
<div>{formatDate(email.internalDate).split(',')[0]}</div>
<div>{formatDate(email.internalDate).split(',')[1]}</div>
</div>
<div className="overflow-hidden text-ellipsis">{email.subject}</div>
<div className="overflow-hidden text-ellipsis">
{decodeMimeEncodedText(email.subject)}
</div>
{/* <div>
<button className="flex items-center gap-1 underline hover:underline">
<Image src="/assets/Eye.svg" alt="view" width={16} height={16} />
Expand All @@ -269,9 +274,10 @@ const SelectEmails = ({ id }: { id: string }) => {
height={16}
className={isFetchEmailLoading ? 'animate-spin' : ''}
style={{
maxWidth: "100%",
height: "auto"
}} />
maxWidth: '100%',
height: 'auto',
}}
/>
Load More Emails
</Button>

Expand All @@ -290,7 +296,7 @@ const SelectEmails = ({ id }: { id: string }) => {
{blueprint?.props.externalInputs ? 'Add Inputs' : 'Create Proof Remotely'}
</Button>
</div>
</div>)
</div>
);
};

Expand Down
27 changes: 27 additions & 0 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,30 @@ export async function getFileContent(file: File): Promise<string> {
reader.readAsText(file);
});
}

export function decodeMimeEncodedText(encodedText: string) {
const matches = encodedText.match(/=\?([^?]+)\?([BQbq])\?([^?]+)\?=/);
if (!matches) return encodedText; // Return as is if no match is found

const charset = matches[1]; // Extract the character set (e.g., UTF-8)
const encoding = matches[2].toUpperCase(); // Encoding type: Q (Quoted-Printable) or B (Base64)
const encodedContent = matches[3];

if (encoding === 'Q') {
// Decode Quoted-Printable
const decoded = encodedContent
.replace(/_/g, ' ') // Replace underscores with spaces
.replace(/=([A-Fa-f0-9]{2})/g, (_, hex) => String.fromCharCode(parseInt(hex, 16))); // Decode =XX to characters
return new TextDecoder(charset).decode(
new Uint8Array([...decoded].map((c) => c.charCodeAt(0)))
);
} else if (encoding === 'B') {
// Decode Base64
const decoded = atob(encodedContent); // Decode Base64
return new TextDecoder(charset).decode(
new Uint8Array([...decoded].map((c) => c.charCodeAt(0)))
);
}

return encodedText; // Return the original text if unhandled encoding
}
Loading