-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: ✨ displayed image attachment instead of base64 string (#13)
- Loading branch information
Showing
5 changed files
with
403 additions
and
354 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
'use client'; | ||
|
||
import React, { useState } from 'react'; | ||
import { | ||
Dialog, | ||
DialogContent, | ||
DialogDescription, | ||
DialogHeader, | ||
DialogTitle, | ||
DialogTrigger, | ||
} from '@/components/ui/dialog'; | ||
import { Button } from '@/components/ui/button'; | ||
import { Link } from 'lucide-react'; | ||
|
||
interface AttachmentDialogProps { | ||
content: string; | ||
title: string; | ||
description?: string; | ||
} | ||
|
||
/* eslint-disable @typescript-eslint/no-unused-vars */ | ||
const isBase64Image = (str: string): boolean => { | ||
try { | ||
return btoa(atob(str)) === str; | ||
} catch (err) { | ||
return false; | ||
} | ||
}; | ||
|
||
export function AttachmentDialog({ | ||
content, | ||
title, | ||
description, | ||
}: AttachmentDialogProps): JSX.Element { | ||
const [isOpen, setIsOpen] = useState(false); | ||
const isImage = isBase64Image(content); | ||
|
||
return ( | ||
<Dialog open={isOpen} onOpenChange={setIsOpen}> | ||
<DialogTrigger asChild> | ||
<Button variant='outline'> | ||
<Link className='h-4 w-4' /> | ||
</Button> | ||
</DialogTrigger> | ||
<DialogContent className='flex flex-col sm:max-h-[90vh] sm:max-w-[90vw]'> | ||
<DialogHeader> | ||
<DialogTitle>{title}</DialogTitle> | ||
{description && <DialogDescription>{description}</DialogDescription>} | ||
</DialogHeader> | ||
<div className='mt-4 flex-grow overflow-auto'> | ||
{isImage ? ( | ||
<div className='relative h-full min-h-[300px] w-full border border-gray-500'> | ||
<img | ||
src={`data:image/png;base64,${content}`} | ||
alt='Attachment' | ||
className='h-full w-full object-contain' | ||
/> | ||
</div> | ||
) : ( | ||
<div className='max-h-[60vh] overflow-y-auto'> | ||
<p className='whitespace-pre-wrap text-lg text-gray-800'> | ||
{content} | ||
</p> | ||
</div> | ||
)} | ||
</div> | ||
</DialogContent> | ||
</Dialog> | ||
); | ||
} |
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
Oops, something went wrong.