|
1 | 1 | <script lang="ts"> |
2 | 2 | import { onMount } from 'svelte'; |
| 3 | + import { rename } from '$lib/utils/opfs'; |
3 | 4 | import Trash from '$lib/components/icons/trash.svelte'; |
4 | 5 |
|
5 | 6 | let recordings: string[] = []; |
|
22 | 23 | } |
23 | 24 |
|
24 | 25 | onMount(async () => { |
25 | | - recordings = await getRecordings(); |
| 26 | + const root = await navigator.storage.getDirectory(); |
| 27 | + const folders: string[] = await getRecordings(); |
| 28 | +
|
| 29 | + recordings = await Promise.all( |
| 30 | + folders.map(async (name) => { |
| 31 | + /** |
| 32 | + * Previously, recordings that were drag-n-drop'd in Videmo were saved with their original filename (can be alphanumeric). However, when users record directly from Chrome, |
| 33 | + * their filename is the recording date in UTC (is a number). This is a problem because OPFS sort files alphabetically, so: |
| 34 | + * 1. some recordings that were drag-n-drop'd are listed at top of recording list, even if the last user video was recorded directly from Chrome. |
| 35 | + * 2. some recordings that were drag-n-drop'd are listed at bottom of recording list, even if this was the latest video in Videmo. |
| 36 | + * With this conditional, I rename folders with original filename from drag-n-drop'd recordings and save it with the current date in UTC as filename |
| 37 | + * to keep compatibility with the old behavior. However, this create a new problem: users will search their recordings with original name because |
| 38 | + * those recordings were saved with this behavior. To solve this, in their associated object value in localStorage, I create a new key called `name` to save the original name. |
| 39 | + * This allows me to develop some new features: |
| 40 | + * 1. rename recordings |
| 41 | + * 2. sort recordings alphabetically and by date |
| 42 | + */ |
| 43 | + if (isNaN(Number(name))) { |
| 44 | + const folder = await root.getDirectoryHandle(name); |
| 45 | + const newFolderName = new Date().getTime().toString(); |
| 46 | + const info = JSON.parse(localStorage.getItem(name) ?? '{}'); |
| 47 | + const updated = { ...info, name }; |
| 48 | +
|
| 49 | + await rename(folder, newFolderName); |
| 50 | +
|
| 51 | + localStorage.removeItem(name); |
| 52 | + localStorage.setItem(newFolderName, JSON.stringify(updated)); |
| 53 | +
|
| 54 | + return newFolderName; |
| 55 | + } |
| 56 | +
|
| 57 | + return name; |
| 58 | + }) |
| 59 | + ); |
26 | 60 | }); |
27 | 61 | </script> |
28 | 62 |
|
29 | 63 | {#each recordings as record} |
30 | | - {@const folderName = isNaN(Number(record)) ? record : new Date(+record).toLocaleString()} |
| 64 | + {@const recordingInfo = JSON.parse(localStorage.getItem(record) ?? '{}')} |
| 65 | + {@const folderName = recordingInfo.name ?? new Date(+record).toLocaleString()} |
31 | 66 | <article class="flex flex-col gap-2"> |
32 | 67 | <a href="/{record}"> |
33 | 68 | <span class="w-full aspect-video bg-white/5 border border-white/5 rounded-md inline-block" /> |
|
0 commit comments