diff --git a/app/src/lib/components/custom-source/CustomSources.svelte b/app/src/lib/components/custom-source/CustomSources.svelte index ad908b4..5e85ab1 100644 --- a/app/src/lib/components/custom-source/CustomSources.svelte +++ b/app/src/lib/components/custom-source/CustomSources.svelte @@ -2,37 +2,70 @@ import { getCustomSources } from '@/stores/customSources.svelte'; import * as Accordion from '../ui/accordion'; import RenderWebContent from './RenderWebContent.svelte'; + import { env } from '@env'; + import type { Sources } from '@/stores/customSources.svelte'; + import { getSessionContext } from '@/stores/sessionContext.svelte'; + import { browser } from '$app/environment'; + const { sessionId$ } = getSessionContext(); const { sources$ } = getCustomSources(); - $: sources = $sources$; + $: sessionId = $sessionId$; + $: sources = $sources$?.sort((a, b) => { + if (a.created_at && b.created_at) { + return new Date(a.created_at).getTime() - new Date(b.created_at).getTime(); + } + return 0; + }); + + $: if (browser) getCustomSource(sessionId).then((d) => sources$.set(d)); + + async function getCustomSource(sessionId: string) { + return fetch(`${env.API_BASE_URL}/get-custom-sources`, { + method: 'POST', + body: JSON.stringify({ sessionId }), + headers: { 'Content-Type': 'application/json' } + }) + .then((res) => { + if (res.ok) return res.json(); + throw new Error('Error fetching custom sources'); + }) + .catch(() => [] as Sources[]); + } function truncate(str: string, n: number) { return str.length > n ? str.substring(0, n - 1) + '...' : str; } -{#each sources as source, idx (source.id)} - - -
- - Custom Source {idx + 1} - - {#if source.type === 'link'} - {':'} - - {truncate(source.url, 45)} +{#if sources == null} +
+
+
+
+{:else} + {#each sources as source, idx (source.id)} + + +
+ + Custom Source {idx + 1} - {/if} -
-
- -
- -
-
-
-{/each} + {#if source.type === 'link'} + {':'} + + {truncate(source.url, 45)} + + {/if} +
+ + +
+ +
+
+ + {/each} +{/if} diff --git a/app/src/lib/stores/customSources.svelte.ts b/app/src/lib/stores/customSources.svelte.ts index e5ad04e..d882aaa 100644 --- a/app/src/lib/stores/customSources.svelte.ts +++ b/app/src/lib/stores/customSources.svelte.ts @@ -14,17 +14,18 @@ export type Sources = (LinkSources | CopyPasteSources) & { id: string; content_type: 'text/plain' | 'text/html' | 'application/pdf'; content: string; + created_at: string; }; const CONTEXT_KEY = {}; export const setCustomSources = (_sessionId: string) => { - const sources$ = writable([]); + const sources$ = writable(null); return setContext(CONTEXT_KEY, { sources$, addSource: (source: Sources) => { - sources$.update((sources) => [...sources, source]); + sources$.update((sources) => (!sources ? sources : [...sources, source])); return sources$; } });