|
1 | 1 | <script lang="ts"> |
2 | | - import type { HTMLAttributes } from 'svelte/elements'; |
3 | | - import { Home, CommentsTwo, Search, Camera } from '$lib/icons'; |
4 | 2 | import { goto } from '$app/navigation'; |
5 | | - import { isNavigatingThroughNav, ownerId } from '$lib/store/store.svelte'; |
6 | 3 | import { page } from '$app/state'; |
| 4 | + import { Camera, CommentsTwo, Home, Search } from '$lib/icons'; |
| 5 | + import { isNavigatingThroughNav, ownerId } from '$lib/store/store.svelte'; |
| 6 | + import { uploadedImages } from '$lib/store/store.svelte'; |
| 7 | + import { revokeImageUrls } from '$lib/utils'; |
| 8 | + import type { HTMLAttributes } from 'svelte/elements'; |
7 | 9 |
|
8 | 10 | interface IBottomNavProps extends HTMLAttributes<HTMLElement> { |
9 | 11 | activeTab?: string; |
|
19 | 21 | let _activeTab = $derived(page.url.pathname); |
20 | 22 | let fullPath = $derived(page.url.pathname); |
21 | 23 |
|
| 24 | + let imageInput: HTMLInputElement; |
| 25 | + let images: FileList | null = $state(null); |
| 26 | +
|
22 | 27 | const handleNavClick = (newTab: string) => { |
23 | 28 | // activeTab = newTab; |
24 | 29 | isNavigatingThroughNav.value = true; |
|
29 | 34 | previousTab = newTab; |
30 | 35 | if (newTab === 'profile') { |
31 | 36 | goto(`/profile/${ownerId}`); |
| 37 | + } else if (newTab === "post") { |
| 38 | + uploadedImages.value = null; |
| 39 | + imageInput.value = ""; |
| 40 | + imageInput.click(); |
32 | 41 | } else { |
33 | 42 | goto(`/${newTab}`); |
34 | 43 | } |
35 | 44 | }; |
36 | 45 |
|
37 | 46 | $effect(() => { |
38 | 47 | activeTab = _activeTab.split('/').pop() ?? ''; |
| 48 | + if (images && images.length > 0 && activeTab !== 'post' && previousTab === 'post' && !_activeTab.includes('post/audience')) { |
| 49 | + if (uploadedImages.value) |
| 50 | + revokeImageUrls(uploadedImages.value); |
| 51 | + uploadedImages.value = Array.from(images).map(file => ({ |
| 52 | + url: URL.createObjectURL(file), |
| 53 | + alt: file.name |
| 54 | + })); |
| 55 | + images = null; // To prevent re-triggering the effect and thus making an infinite loop with /post route's effect when the length of uploadedImages goes to 0 |
| 56 | + if (uploadedImages.value.length > 0) { |
| 57 | + goto("/post"); |
| 58 | + } |
| 59 | + } |
39 | 60 | }); |
40 | 61 | </script> |
41 | 62 |
|
| 63 | +<input type="file" accept="image/*" multiple bind:files={images} bind:this={imageInput} class="hidden" /> |
42 | 64 | <!-- svelte-ignore a11y_no_noninteractive_element_to_interactive_role --> |
43 | 65 | <nav |
44 | 66 | aria-label="Main navigation" |
|
0 commit comments