Skip to content

Commit

Permalink
Add printing capabilities for emails.
Browse files Browse the repository at this point in the history
  • Loading branch information
TimoVink committed Jan 7, 2025
1 parent 6233f4e commit ef2df11
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 17 deletions.
31 changes: 21 additions & 10 deletions web/app/(main)/emails/components-client.jsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
'use client';

import { useRef, useEffect } from 'react';
import Link from 'next/link';
import { useSearchParams } from 'next/navigation';
import { Letter } from 'react-letter';
import { extract } from 'letterparser';
import { format, formatDistanceToNow } from 'date-fns';
import { useReactToPrint } from 'react-to-print';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { faPrint } from '@fortawesome/free-solid-svg-icons';

import { ThreadEntriesSkeleton, Card, ThreadEntryScrollContainer } from './components-server';
import { useApiCall, useTextApiCall } from '@/lib/api';
Expand Down Expand Up @@ -92,7 +94,7 @@ export const ThreadEntry = ({ entry }) => {
}, [emailRef.current]);

return (
<Card id={`entry-${entry.entryId}`}>
<Card id={`entry-${entry.entryId}`} className="break-inside-avoid">
<div className="p-4 border-b">
<div className="flex items-center space-x-1">
<div className="flex flex-1 justify-between items-baseline space-x-1">
Expand Down Expand Up @@ -127,17 +129,26 @@ export const ThreadEntry = ({ entry }) => {


export const ThreadEntriesFetch = ({ threadId }) => {
const printRef = useRef(null);
const triggerPrint = useReactToPrint({ contentRef: printRef });
const { data } = useApiCall(`api/email/entries?threadId=${threadId}`)

return (
<ThreadEntryScrollContainer>
{data && data.length && <>
<div className="text-xl px-4 pt-2">{data[data.length -1].emailSubject}</div>
{data.map(e => (
<ThreadEntry key={e.entryId} entry={e} />
))}
</>}
</ThreadEntryScrollContainer>
<div ref={printRef} className="h-full">
<ThreadEntryScrollContainer>
{data && data.length && <>
<div className="text-xl px-4 pt-2 flex justify-between">
<div>{data[data.length -1].emailSubject}</div>
<button className="print:hidden text-muted-foreground" onClick={triggerPrint}>
<FontAwesomeIcon icon={faPrint} />
</button>
</div>
{data.map(e => (
<ThreadEntry key={e.entryId} entry={e} />
))}
</>}
</ThreadEntryScrollContainer>
</div>
)
}

Expand Down
6 changes: 6 additions & 0 deletions web/app/globals.css
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,9 @@
@apply bg-background text-foreground;
}
}

@media print{
@page {
margin: 2cm;
}
}
63 changes: 56 additions & 7 deletions web/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
"lint": "next lint"
},
"dependencies": {
"@fortawesome/fontawesome-svg-core": "^6.7.2",
"@fortawesome/free-solid-svg-icons": "^6.7.2",
"@fortawesome/react-fontawesome": "^0.2.2",
"@icons-pack/react-simple-icons": "^10.2.0",
"@radix-ui/react-avatar": "^1.1.2",
"@radix-ui/react-dropdown-menu": "^2.1.4",
Expand All @@ -29,6 +32,7 @@
"react-infinite-scroll-component": "^6.1.0",
"react-letter": "^0.4.0",
"react-markdown": "^9.0.3",
"react-to-print": "^3.0.4",
"tailwind-merge": "^2.6.0",
"tailwindcss-animate": "^1.0.7"
},
Expand Down
2 changes: 2 additions & 0 deletions web/tailwind.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ module.exports = {
padding: "2rem",
screens: {
"2xl": "1400px",
"print": {raw: 'print'},
"screen": {raw: 'screen'},
},
},
extend: {
Expand Down

0 comments on commit ef2df11

Please sign in to comment.