Prettier for Journals
This commit is contained in:
@@ -1,142 +1,145 @@
|
||||
<script lang="ts">
|
||||
import { Modal } from 'flowbite-svelte';
|
||||
import { Check, CircleAlert, FileText, RefreshCw, Upload, X } from '@lucide/svelte';
|
||||
import {
|
||||
PARSERS,
|
||||
type AeJournalEntryInput
|
||||
} from '$lib/ae_journals/ae_journals_parsers';
|
||||
import { journals_func } from '$lib/ae_journals/ae_journals_functions';
|
||||
import { ae_api } from '$lib/stores/ae_stores';
|
||||
import { journals_slct } from '$lib/ae_journals/ae_journals_stores';
|
||||
import { Modal } from 'flowbite-svelte';
|
||||
import {
|
||||
Check,
|
||||
CircleAlert,
|
||||
FileText,
|
||||
RefreshCw,
|
||||
Upload,
|
||||
X
|
||||
} from '@lucide/svelte';
|
||||
import {
|
||||
PARSERS,
|
||||
type AeJournalEntryInput
|
||||
} from '$lib/ae_journals/ae_journals_parsers';
|
||||
import { journals_func } from '$lib/ae_journals/ae_journals_functions';
|
||||
import { ae_api } from '$lib/stores/ae_stores';
|
||||
import { journals_slct } from '$lib/ae_journals/ae_journals_stores';
|
||||
|
||||
interface Props {
|
||||
open: boolean;
|
||||
on_close: () => void;
|
||||
on_import_complete: () => void;
|
||||
interface Props {
|
||||
open: boolean;
|
||||
on_close: () => void;
|
||||
on_import_complete: () => void;
|
||||
}
|
||||
|
||||
let { open = $bindable(false), on_close, on_import_complete }: Props = $props();
|
||||
|
||||
let files: FileList | null = $state(null);
|
||||
let selected_parser: keyof typeof PARSERS = $state('standard');
|
||||
let parsed_entries: AeJournalEntryInput[] = $state([]);
|
||||
let is_parsing = $state(false);
|
||||
let is_importing = $state(false);
|
||||
let import_log: string[] = $state([]);
|
||||
let is_dragging = $state(false);
|
||||
|
||||
// Watch for file selection or parser change to trigger parsing
|
||||
$effect(() => {
|
||||
if (files && files.length > 0) {
|
||||
parse_files();
|
||||
}
|
||||
});
|
||||
|
||||
let {
|
||||
open = $bindable(false),
|
||||
on_close,
|
||||
on_import_complete
|
||||
}: Props = $props();
|
||||
function handle_drag_enter(e: DragEvent) {
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
is_dragging = true;
|
||||
}
|
||||
|
||||
let files: FileList | null = $state(null);
|
||||
let selected_parser: keyof typeof PARSERS = $state('standard');
|
||||
let parsed_entries: AeJournalEntryInput[] = $state([]);
|
||||
let is_parsing = $state(false);
|
||||
let is_importing = $state(false);
|
||||
let import_log: string[] = $state([]);
|
||||
let is_dragging = $state(false);
|
||||
function handle_drag_leave(e: DragEvent) {
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
is_dragging = false;
|
||||
}
|
||||
|
||||
// Watch for file selection or parser change to trigger parsing
|
||||
$effect(() => {
|
||||
if (files && files.length > 0) {
|
||||
parse_files();
|
||||
}
|
||||
});
|
||||
function handle_drag_over(e: DragEvent) {
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
is_dragging = true;
|
||||
}
|
||||
|
||||
function handle_drag_enter(e: DragEvent) {
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
is_dragging = true;
|
||||
function handle_drop(e: DragEvent) {
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
is_dragging = false;
|
||||
|
||||
if (e.dataTransfer?.files && e.dataTransfer.files.length > 0) {
|
||||
files = e.dataTransfer.files;
|
||||
}
|
||||
}
|
||||
|
||||
function handle_drag_leave(e: DragEvent) {
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
is_dragging = false;
|
||||
}
|
||||
async function parse_files() {
|
||||
if (!files) return;
|
||||
is_parsing = true;
|
||||
parsed_entries = [];
|
||||
|
||||
function handle_drag_over(e: DragEvent) {
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
is_dragging = true;
|
||||
}
|
||||
const parser = PARSERS[selected_parser];
|
||||
|
||||
function handle_drop(e: DragEvent) {
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
is_dragging = false;
|
||||
|
||||
if (e.dataTransfer?.files && e.dataTransfer.files.length > 0) {
|
||||
files = e.dataTransfer.files;
|
||||
for (let i = 0; i < files.length; i++) {
|
||||
const file = files[i];
|
||||
try {
|
||||
const text = await file.text();
|
||||
const entries = await parser(file, text);
|
||||
parsed_entries = [...parsed_entries, ...entries];
|
||||
} catch (err) {
|
||||
console.error(`Error parsing ${file.name}:`, err);
|
||||
}
|
||||
}
|
||||
is_parsing = false;
|
||||
}
|
||||
|
||||
async function parse_files() {
|
||||
if (!files) return;
|
||||
is_parsing = true;
|
||||
parsed_entries = [];
|
||||
async function handle_import() {
|
||||
if (parsed_entries.length === 0) return;
|
||||
is_importing = true;
|
||||
import_log = [];
|
||||
|
||||
const parser = PARSERS[selected_parser];
|
||||
|
||||
for (let i = 0; i < files.length; i++) {
|
||||
const file = files[i];
|
||||
try {
|
||||
const text = await file.text();
|
||||
const entries = await parser(file, text);
|
||||
parsed_entries = [...parsed_entries, ...entries];
|
||||
} catch (err) {
|
||||
console.error(`Error parsing ${file.name}:`, err);
|
||||
}
|
||||
}
|
||||
is_parsing = false;
|
||||
}
|
||||
|
||||
async function handle_import() {
|
||||
if (parsed_entries.length === 0) return;
|
||||
is_importing = true;
|
||||
import_log = [];
|
||||
|
||||
const journal_id = $journals_slct.journal_id;
|
||||
if (!journal_id) {
|
||||
alert(
|
||||
'No target journal selected. Please select a journal in the background first.'
|
||||
);
|
||||
is_importing = false;
|
||||
return;
|
||||
}
|
||||
|
||||
let success_count = 0;
|
||||
|
||||
for (const entry of parsed_entries) {
|
||||
try {
|
||||
// Construct payload
|
||||
const data_kv = {
|
||||
name: entry.name,
|
||||
content: entry.content,
|
||||
tags: entry.tags.join(', '),
|
||||
type_code: entry.type_code || 'note',
|
||||
created_on: entry.created_on,
|
||||
updated_on: entry.updated_on
|
||||
};
|
||||
|
||||
const res = await journals_func.create_ae_obj__journal_entry({
|
||||
api_cfg: $ae_api,
|
||||
journal_id: journal_id,
|
||||
data_kv: data_kv,
|
||||
log_lvl: 0
|
||||
});
|
||||
|
||||
if (res) {
|
||||
import_log.push(`✅ Imported: ${entry.name}`);
|
||||
success_count++;
|
||||
} else {
|
||||
import_log.push(`❌ Failed: ${entry.name}`);
|
||||
}
|
||||
} catch (err) {
|
||||
import_log.push(`❌ Error: ${entry.name} - ${err}`);
|
||||
}
|
||||
}
|
||||
|
||||
is_importing = false;
|
||||
const journal_id = $journals_slct.journal_id;
|
||||
if (!journal_id) {
|
||||
alert(
|
||||
`Import complete! ${success_count}/${parsed_entries.length} imported.`
|
||||
'No target journal selected. Please select a journal in the background first.'
|
||||
);
|
||||
on_import_complete();
|
||||
open = false;
|
||||
is_importing = false;
|
||||
return;
|
||||
}
|
||||
|
||||
let success_count = 0;
|
||||
|
||||
for (const entry of parsed_entries) {
|
||||
try {
|
||||
// Construct payload
|
||||
const data_kv = {
|
||||
name: entry.name,
|
||||
content: entry.content,
|
||||
tags: entry.tags.join(', '),
|
||||
type_code: entry.type_code || 'note',
|
||||
created_on: entry.created_on,
|
||||
updated_on: entry.updated_on
|
||||
};
|
||||
|
||||
const res = await journals_func.create_ae_obj__journal_entry({
|
||||
api_cfg: $ae_api,
|
||||
journal_id: journal_id,
|
||||
data_kv: data_kv,
|
||||
log_lvl: 0
|
||||
});
|
||||
|
||||
if (res) {
|
||||
import_log.push(`✅ Imported: ${entry.name}`);
|
||||
success_count++;
|
||||
} else {
|
||||
import_log.push(`❌ Failed: ${entry.name}`);
|
||||
}
|
||||
} catch (err) {
|
||||
import_log.push(`❌ Error: ${entry.name} - ${err}`);
|
||||
}
|
||||
}
|
||||
|
||||
is_importing = false;
|
||||
alert(
|
||||
`Import complete! ${success_count}/${parsed_entries.length} imported.`
|
||||
);
|
||||
on_import_complete();
|
||||
open = false;
|
||||
}
|
||||
</script>
|
||||
|
||||
<Modal
|
||||
@@ -144,21 +147,18 @@
|
||||
bind:open
|
||||
autoclose={false}
|
||||
size="xl"
|
||||
class="w-full"
|
||||
>
|
||||
class="w-full">
|
||||
<div class="space-y-4">
|
||||
<!-- Configuration -->
|
||||
<div class="grid grid-cols-1 md:grid-cols-2 gap-4">
|
||||
<div class="grid grid-cols-1 gap-4 md:grid-cols-2">
|
||||
<div>
|
||||
<label class="label">
|
||||
<span>Parser Strategy</span>
|
||||
<select class="select" bind:value={selected_parser}>
|
||||
<option value="standard"
|
||||
>Standard (1 File = 1 Entry)</option
|
||||
>
|
||||
>Standard (1 File = 1 Entry)</option>
|
||||
<option value="personal_log"
|
||||
>Personal Log (Split by Date)</option
|
||||
>
|
||||
>Personal Log (Split by Date)</option>
|
||||
<option value="amazon_vine">Amazon Vine Reviews</option>
|
||||
</select>
|
||||
</label>
|
||||
@@ -173,25 +173,23 @@
|
||||
<!-- svelte-ignore a11y_no_static_element_interactions -->
|
||||
<div
|
||||
class="
|
||||
border-2 border-dashed rounded-lg p-8 text-center cursor-pointer transition-all duration-200
|
||||
flex flex-col items-center justify-center gap-2
|
||||
flex cursor-pointer flex-col items-center justify-center gap-2 rounded-lg border-2
|
||||
border-dashed p-8 text-center transition-all duration-200
|
||||
{is_dragging
|
||||
? 'border-primary-500 bg-primary-50 dark:bg-primary-900/20'
|
||||
: 'border-gray-300 dark:border-gray-600 hover:border-gray-400 dark:hover:border-gray-500'}
|
||||
: 'border-gray-300 hover:border-gray-400 dark:border-gray-600 dark:hover:border-gray-500'}
|
||||
"
|
||||
ondragenter={handle_drag_enter}
|
||||
ondragleave={handle_drag_leave}
|
||||
ondragover={handle_drag_over}
|
||||
ondrop={handle_drop}
|
||||
onclick={() =>
|
||||
document.getElementById('file_import_input')?.click()}
|
||||
>
|
||||
document.getElementById('file_import_input')?.click()}>
|
||||
<Upload class="h-10 w-10 text-gray-400" />
|
||||
<p class="text-sm text-gray-500">
|
||||
<span
|
||||
class="font-semibold text-primary-600 hover:text-primary-500"
|
||||
>Click to upload</span
|
||||
>
|
||||
class="text-primary-600 hover:text-primary-500 font-semibold"
|
||||
>Click to upload</span>
|
||||
or drag and drop
|
||||
</p>
|
||||
<p class="text-xs text-gray-400">
|
||||
@@ -204,17 +202,15 @@
|
||||
class="hidden"
|
||||
multiple
|
||||
accept=".md,.txt"
|
||||
onchange={(e) => (files = e.currentTarget.files)}
|
||||
/>
|
||||
onchange={(e) => (files = e.currentTarget.files)} />
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Preview -->
|
||||
<div
|
||||
class="border rounded-lg p-2 bg-gray-50 dark:bg-gray-900 max-h-64 overflow-y-auto"
|
||||
>
|
||||
<h4 class="font-bold mb-2 flex justify-between">
|
||||
class="max-h-64 overflow-y-auto rounded-lg border bg-gray-50 p-2 dark:bg-gray-900">
|
||||
<h4 class="mb-2 flex justify-between font-bold">
|
||||
<span>Preview ({parsed_entries.length} entries)</span>
|
||||
{#if is_parsing}
|
||||
<RefreshCw class="animate-spin" />
|
||||
@@ -222,7 +218,7 @@
|
||||
</h4>
|
||||
|
||||
{#if parsed_entries.length > 0}
|
||||
<table class="table table-compact w-full text-xs">
|
||||
<table class="table-compact table w-full text-xs">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Name</th>
|
||||
@@ -234,9 +230,8 @@
|
||||
{#each parsed_entries as entry, i (i)}
|
||||
<tr>
|
||||
<td
|
||||
class="truncate max-w-[200px]"
|
||||
title={entry.name}>{entry.name}</td
|
||||
>
|
||||
class="max-w-[200px] truncate"
|
||||
title={entry.name}>{entry.name}</td>
|
||||
<td>{entry.created_on?.substring(0, 10)}</td>
|
||||
<td>{entry.tags.join(', ')}</td>
|
||||
</tr>
|
||||
@@ -244,11 +239,11 @@
|
||||
</tbody>
|
||||
</table>
|
||||
{:else if files && files.length > 0 && !is_parsing}
|
||||
<div class="text-center text-gray-500 py-4">
|
||||
<div class="py-4 text-center text-gray-500">
|
||||
No entries found in selected files.
|
||||
</div>
|
||||
{:else if !files}
|
||||
<div class="text-center text-gray-500 py-4">
|
||||
<div class="py-4 text-center text-gray-500">
|
||||
Select files to preview import.
|
||||
</div>
|
||||
{/if}
|
||||
@@ -257,8 +252,7 @@
|
||||
<!-- Import Log -->
|
||||
{#if import_log.length > 0}
|
||||
<div
|
||||
class="bg-black text-green-400 p-2 rounded text-xs font-mono max-h-32 overflow-y-auto"
|
||||
>
|
||||
class="max-h-32 overflow-y-auto rounded bg-black p-2 font-mono text-xs text-green-400">
|
||||
{#each import_log as log, i (i)}
|
||||
<div>{log}</div>
|
||||
{/each}
|
||||
@@ -269,14 +263,12 @@
|
||||
<button
|
||||
type="button"
|
||||
class="btn preset-tonal-secondary"
|
||||
onclick={on_close}>Cancel</button
|
||||
>
|
||||
onclick={on_close}>Cancel</button>
|
||||
<button
|
||||
type="button"
|
||||
class="btn preset-filled-primary"
|
||||
disabled={parsed_entries.length === 0 || is_importing}
|
||||
onclick={handle_import}
|
||||
>
|
||||
onclick={handle_import}>
|
||||
{#if is_importing}
|
||||
Importing...
|
||||
{:else}
|
||||
|
||||
Reference in New Issue
Block a user