General clean up the _random appends. About to work on a new Hosted File Download Svelte component.
This commit is contained in:
@@ -186,14 +186,12 @@
|
||||
}
|
||||
let hosted_file_obj = result[x];
|
||||
|
||||
let hosted_file_id = hosted_file_obj.hosted_file_id_random;
|
||||
let hosted_file_id = hosted_file_obj.hosted_file_id;
|
||||
|
||||
let event_file_data: key_val = {};
|
||||
event_file_data['hosted_file_id'] = hosted_file_id;
|
||||
// event_file_data['hosted_file_id_random'] = hosted_file_id;
|
||||
event_file_data['for_type'] = link_to_type;
|
||||
event_file_data['for_id'] = link_to_id;
|
||||
// event_file_data['for_id_random'] = link_to_id;
|
||||
event_file_data['filename'] = hosted_file_obj.filename;
|
||||
event_file_data['extension'] = hosted_file_obj.extension;
|
||||
event_file_data['enable'] = true;
|
||||
|
||||
@@ -170,7 +170,7 @@
|
||||
History:
|
||||
{#each Object.entries($ae_loc.files.uploaded_file_kv) as [hosted_file_id, hosted_file_obj]}
|
||||
<div>
|
||||
<span>{hosted_file_obj.hosted_file_id_random}</span>
|
||||
<span>{hosted_file_obj.hosted_file_id}</span>
|
||||
<span>{hosted_file_obj.filename}</span>
|
||||
<span>{hosted_file_obj.extension}</span>
|
||||
<button
|
||||
|
||||
@@ -4,11 +4,11 @@
|
||||
* Manages and displays file attachments for Journal Entries.
|
||||
* Ported/Refactored 2026-01-26 to include View mode and strictly snake_case.
|
||||
*/
|
||||
|
||||
|
||||
// *** Import Lucide Icons
|
||||
import {
|
||||
FileUp, Trash2, Download, Paperclip,
|
||||
ExternalLink, Loader2, RefreshCw
|
||||
import {
|
||||
FileUp, Trash2, Download, Paperclip,
|
||||
ExternalLink, Loader2, RefreshCw
|
||||
} from 'lucide-svelte';
|
||||
|
||||
// *** Import Aether specific variables and functions
|
||||
@@ -49,12 +49,12 @@
|
||||
// *** State
|
||||
let ae_promises: Record<string, any> = $state({});
|
||||
let upload_complete: boolean = $state(false);
|
||||
|
||||
|
||||
// Selection state for "Select Existing" mode
|
||||
let slct_hosted_file_kv: key_val = $state({});
|
||||
let slct_hosted_file_id: string | null = $state(null);
|
||||
let slct_hosted_file_obj: any = $state(null);
|
||||
|
||||
|
||||
// Selection state for "Upload" mode
|
||||
let slct_hosted_file_id_li: string[] = $state([]);
|
||||
let slct_hosted_file_obj_li: any[] = $state([]);
|
||||
@@ -63,34 +63,34 @@
|
||||
let unified_file_li = $derived.by(() => {
|
||||
const entry = $lq__journal_entry_obj;
|
||||
if (!entry) return [];
|
||||
|
||||
|
||||
let files: any[] = [];
|
||||
|
||||
|
||||
// 1. Check new linked_li_json first
|
||||
if (entry.linked_li_json && Array.isArray(entry.linked_li_json)) {
|
||||
files = [...entry.linked_li_json];
|
||||
}
|
||||
|
||||
|
||||
// 2. Merge with legacy hosted_file_kv if not already present
|
||||
if (entry.data_json?.hosted_file_kv) {
|
||||
Object.entries(entry.data_json.hosted_file_kv).forEach(([id, obj]: [string, any]) => {
|
||||
if (!files.find(f => (f.hosted_file_id_random || f.id || f.hosted_file_id) === id)) {
|
||||
if (!files.find(f => (f.hosted_file_id || f.id || f.hosted_file_id) === id)) {
|
||||
files.push({ ...obj, id: id });
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
return files;
|
||||
});
|
||||
|
||||
async function update_journal_entry(updated_files: any[]) {
|
||||
let data_kv: key_val = {
|
||||
let data_kv: key_val = {
|
||||
linked_li_json: JSON.stringify(updated_files),
|
||||
// Maintain data_json.hosted_file_kv for backward compatibility
|
||||
data_json: {
|
||||
...$lq__journal_entry_obj?.data_json,
|
||||
hosted_file_kv: Object.fromEntries(
|
||||
updated_files.map(f => [f.hosted_file_id_random || f.id || f.hosted_file_id, f])
|
||||
updated_files.map(f => [f.hosted_file_id || f.id || f.hosted_file_id, f])
|
||||
)
|
||||
}
|
||||
};
|
||||
@@ -108,16 +108,16 @@
|
||||
}
|
||||
|
||||
// *** Effects for File Management
|
||||
|
||||
|
||||
// Handle "Select Existing" completion
|
||||
$effect(() => {
|
||||
if ($lq__journal_entry_obj && slct_hosted_file_id && slct_hosted_file_obj) {
|
||||
const new_file = { ...slct_hosted_file_obj, id: slct_hosted_file_id };
|
||||
const updated_li = [...unified_file_li, new_file];
|
||||
|
||||
|
||||
slct_hosted_file_id = null;
|
||||
slct_hosted_file_obj = null;
|
||||
|
||||
|
||||
update_journal_entry(updated_li);
|
||||
}
|
||||
});
|
||||
@@ -130,19 +130,19 @@
|
||||
id: id
|
||||
}));
|
||||
const updated_li = [...unified_file_li, ...new_files];
|
||||
|
||||
|
||||
slct_hosted_file_id_li = [];
|
||||
upload_complete = false;
|
||||
|
||||
|
||||
update_journal_entry(updated_li);
|
||||
}
|
||||
});
|
||||
|
||||
async function handle_remove_file(file_id: string) {
|
||||
if (!confirm('Are you sure you want to remove this file attachment?')) return;
|
||||
|
||||
const updated_li = unified_file_li.filter(f => (f.hosted_file_id_random || f.id || f.hosted_file_id) !== file_id);
|
||||
|
||||
|
||||
const updated_li = unified_file_li.filter(f => (f.hosted_file_id || f.id || f.hosted_file_id) !== file_id);
|
||||
|
||||
// Also perform physical/orphan deletion if admin
|
||||
if ($ae_loc.administrator_access) {
|
||||
await core_func.delete_ae_obj_id__hosted_file({
|
||||
@@ -155,12 +155,12 @@
|
||||
log_lvl
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
await update_journal_entry(updated_li);
|
||||
}
|
||||
|
||||
async function download_file(file: any) {
|
||||
const file_id = file.hosted_file_id_random || file.id || file.hosted_file_id;
|
||||
const file_id = file.hosted_file_id || file.id || file.hosted_file_id;
|
||||
ae_promises[file_id] = api.download_hosted_file({
|
||||
api_cfg: $ae_api,
|
||||
hosted_file_id: file_id,
|
||||
@@ -201,7 +201,7 @@
|
||||
{#if unified_file_li.length}
|
||||
<div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-3">
|
||||
{#each unified_file_li as file}
|
||||
{@const file_id = file.hosted_file_id_random || file.id || file.hosted_file_id}
|
||||
{@const file_id = file.hosted_file_id || file.id || file.hosted_file_id}
|
||||
<div class="flex items-center justify-between p-3 rounded-xl bg-surface-50-950 border border-surface-500/10 group hover:border-primary-500 transition-all shadow-sm">
|
||||
<div class="flex items-center gap-3 overflow-hidden">
|
||||
<div class="text-primary-500 shrink-0">
|
||||
@@ -214,8 +214,8 @@
|
||||
</div>
|
||||
|
||||
<div class="flex items-center gap-1">
|
||||
<button
|
||||
class="btn btn-sm variant-soft-primary"
|
||||
<button
|
||||
class="btn btn-sm variant-soft-primary"
|
||||
onclick={() => download_file(file)}
|
||||
title="Download file"
|
||||
>
|
||||
@@ -225,10 +225,10 @@
|
||||
<Download size="1.1em" />
|
||||
{/if}
|
||||
</button>
|
||||
|
||||
|
||||
{#if $ae_loc.edit_mode}
|
||||
<button
|
||||
class="btn btn-sm variant-soft-error"
|
||||
<button
|
||||
class="btn btn-sm variant-soft-error"
|
||||
onclick={() => handle_remove_file(file_id)}
|
||||
title="Remove attachment"
|
||||
>
|
||||
|
||||
Reference in New Issue
Block a user