-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add logic to upload and preview file attachments
- Loading branch information
1 parent
26b8637
commit 5441345
Showing
3 changed files
with
111 additions
and
6 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,55 @@ | ||
<script context="module"> | ||
const MAX_FILES = 5; | ||
</script> | ||
|
||
<script lang="ts"> | ||
import { PaperclipIcon } from 'lucide-svelte'; | ||
import { createEventDispatcher } from 'svelte'; | ||
import { Button } from './ui/button'; | ||
import { toast } from 'svelte-sonner'; | ||
export let selectedFiles: File[] = []; | ||
const dispatch = createEventDispatcher<{ | ||
attach: { files: File[] }; | ||
}>(); | ||
let fileInput: HTMLInputElement; | ||
function handleFileSelect(fileList: FileList | null) { | ||
if (!fileList) { | ||
return toast.error('No files selected'); | ||
} | ||
const files = Array.from(fileList); | ||
if (selectedFiles.length + files.length > MAX_FILES) { | ||
alert(`You can only upload up to ${MAX_FILES} files`); | ||
return; | ||
} | ||
selectedFiles = [...selectedFiles, ...files]; | ||
dispatch('attach', { files: selectedFiles }); | ||
// Reset input | ||
fileInput.value = ''; | ||
} | ||
</script> | ||
|
||
<Button | ||
variant="ghost" | ||
size="icon" | ||
class="text-zinc-400 hover:text-white" | ||
disabled={selectedFiles.length >= MAX_FILES} | ||
on:click={() => fileInput.click()} | ||
> | ||
<PaperclipIcon class="h-5 w-5" /> | ||
</Button> | ||
|
||
<input | ||
type="file" | ||
accept=".pdf,.txt" | ||
multiple | ||
class="hidden" | ||
bind:this={fileInput} | ||
on:change={(e) => handleFileSelect(e.currentTarget.files)} | ||
/> |
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,43 @@ | ||
<script lang="ts"> | ||
import { XIcon } from 'lucide-svelte'; | ||
import { Button } from './ui/button'; | ||
import { createEventDispatcher } from 'svelte'; | ||
export let selectedFiles: File[] = []; | ||
const dispatch = createEventDispatcher<{ updateAttach: { files: File[] } }>(); | ||
function removeFile(index: number) { | ||
selectedFiles = selectedFiles.filter((_, i) => i !== index); | ||
dispatch('updateAttach', { files: selectedFiles }); | ||
} | ||
function formatFileSize(bytes: number): string { | ||
if (bytes === 0) return '0 Bytes'; | ||
const k = 1024; | ||
const sizes = ['Bytes', 'KB', 'MB', 'GB']; | ||
const i = Math.floor(Math.log(bytes) / Math.log(k)); | ||
return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i]; | ||
} | ||
</script> | ||
|
||
<div class="p-2 space-y-2 bg-zinc-800/30"> | ||
{#each selectedFiles as file, index} | ||
<div class="flex items-center justify-between bg-zinc-700/30 rounded p-2"> | ||
<div class="flex-1 min-w-0"> | ||
<p class="text-sm text-white truncate">{file.name}</p> | ||
<p class="text-xs text-zinc-400"> | ||
{formatFileSize(file.size)} • {file.type || 'text/plain'} | ||
</p> | ||
</div> | ||
<Button | ||
variant="ghost" | ||
size="icon" | ||
class="text-zinc-400 hover:text-white" | ||
on:click={() => removeFile(index)} | ||
> | ||
<XIcon class="h-4 w-4" /> | ||
</Button> | ||
</div> | ||
{/each} | ||
</div> |