Skip to content

Commit

Permalink
create sessionItems$ store value
Browse files Browse the repository at this point in the history
  • Loading branch information
nwaughachukwuma committed Nov 27, 2024
1 parent 69c7425 commit 494d1d2
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 17 deletions.
20 changes: 3 additions & 17 deletions app/src/lib/components/Sidebar.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@

<script lang="ts">
import { createEventDispatcher } from 'svelte';
import { browser } from '$app/environment';
import { type Session, SESSION_KEY, getSessionContext } from '../stores/sessionContext.svelte';
import { getSessionContext } from '../stores/sessionContext.svelte';
import SidebarItem from './SidebarItem.svelte';
import { getAppContext } from '../stores/appContext.svelte';
import HeadphoneOff from 'lucide-svelte/icons/headphone-off';
Expand All @@ -18,12 +17,9 @@
const dispatch = createEventDispatcher<{ clickItem: void }>();
const { openSettingsDrawer$ } = getAppContext();
const { sessionItems$ } = getSessionContext();
$: ({ session$ } = getSessionContext());
$: sessionItems = browser || $session$ ? getSessionItems() : [];
$: sidebarItems = sessionItems
$: sidebarItems = $sessionItems$
.filter(([_, item]) => item.chats.length)
.map(([sessionId, session]) => ({
sessionId,
Expand All @@ -38,16 +34,6 @@
$: inLast7Days = sidebarItems.filter((i) => i.nonce < last24Hrs && i.nonce > last7Days);
$: inLast30Days = sidebarItems.filter((i) => i.nonce < last24Hrs && i.nonce < last7Days);
function getSessionItems() {
return Object.entries(localStorage)
.filter(([key]) => key.startsWith(SESSION_KEY))
.map(
([key, value]) =>
[key.replace(`${SESSION_KEY}_`, ''), JSON.parse(value) as Session] as const
)
.filter(([_, v]) => Boolean(v));
}
function dispatchClickItem() {
dispatch('clickItem');
}
Expand Down
13 changes: 13 additions & 0 deletions app/src/lib/stores/sessionContext.svelte.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { browser } from '$app/environment';
import { getCustomSources$ } from '@/db/db.customSources';
import { getSession$ } from '@/db/db.session';
import type { ContentCategory } from '@/utils/types';
Expand Down Expand Up @@ -25,6 +26,15 @@ export type Session = {
summary?: string;
};

export function getSessionItems() {
return Object.entries(localStorage)
.filter(([key]) => key.startsWith(SESSION_KEY))
.map(
([key, value]) => [key.replace(`${SESSION_KEY}_`, ''), JSON.parse(value) as Session] as const
)
.filter(([_, v]) => Boolean(v));
}

export function setSessionContext(sessionId: string) {
const session$ = persisted<Session | null>(`${SESSION_KEY}_${sessionId}`, null);
const sessionId$ = writable(sessionId);
Expand All @@ -33,6 +43,8 @@ export function setSessionContext(sessionId: string) {
const fetchingSource$ = writable(false);
const audioSource$ = persisted<string>(`AUDIOCAST_SOURCE_${sessionId}`, '');

const sessionItems$ = derived(session$, (v) => (browser || v ? getSessionItems() : []));

return setContext(CONTEXT_KEY, {
session$,
sessionId$,
Expand All @@ -41,6 +53,7 @@ export function setSessionContext(sessionId: string) {
audioSource$,
customSources$: getCustomSources$(sessionId),
sessionModel$: getSession$(sessionId),
sessionItems$,
startSession: (category: ContentCategory) => {
session$.set({
id: sessionId,
Expand Down

0 comments on commit 494d1d2

Please sign in to comment.