Skip to content

Commit

Permalink
feat: refactor content category handling and implement chat session m…
Browse files Browse the repository at this point in the history
…anagement
  • Loading branch information
nwaughachukwuma committed Nov 7, 2024
1 parent 22966f1 commit 352ea4a
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 23 deletions.
10 changes: 1 addition & 9 deletions app/src/lib/components/RenderExamples.svelte
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
<script lang="ts" context="module">
export type ContentCategory =
| 'podcast'
| 'sermon'
| 'audiodrama'
| 'lecture'
| 'commentary'
| 'voicenote'
| 'interview'
| 'soundbite';
import type { ContentCategory } from '@/utils/types';
export const content_examples: Record<ContentCategory, string> = {
podcast:
Expand Down
22 changes: 22 additions & 0 deletions app/src/lib/stores/chatStore.svelte.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { setContext, getContext } from 'svelte';
import { persisted } from 'svelte-persisted-store';

const CONTEXT_KEY = {};
const CHAT_SESSION_KEY = 'CHAT_SESSION_KEY';

export type ChatItem = {
id: string;
content: string;
role: 'user' | 'assistant';
};

export function setChatSession(sessionId: string) {
const chatSession$ = persisted<ChatItem[]>(`${CHAT_SESSION_KEY}_${sessionId}`, []);

return setContext(CONTEXT_KEY, {
chatSession$
});
}

export type ChatSession = ReturnType<typeof setChatSession>;
export const getChatSession = (): ChatSession => getContext(CONTEXT_KEY);
9 changes: 9 additions & 0 deletions app/src/lib/utils/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export type ContentCategory =
| 'podcast'
| 'sermon'
| 'audiodrama'
| 'lecture'
| 'commentary'
| 'voicenote'
| 'interview'
| 'soundbite';
4 changes: 3 additions & 1 deletion app/src/routes/+layout.server.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import type { ContentCategory } from '@/utils/types';
import type { LayoutServerLoad } from './$types';

export const ssr = true;
export const prerender = false;
export const load: LayoutServerLoad = ({ locals }) => {
return {
sessionId: locals.sessionId
sessionId: locals.sessionId,
category: null as ContentCategory | null
};
};
20 changes: 10 additions & 10 deletions app/src/routes/+layout.svelte
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
<script lang="ts" context="module">
import '../app.css';
import { page } from '$app/stores';
// import { browser } from '$app/environment';
// import { goto } from '$app/navigation';
// import { sessionStore } from '$lib/stores/session';
// import ChatInterface from '$lib/components/ChatInterface.svelte';
// import AudioInterface from '$lib/components/AudioInterface.svelte';
</script>

// $: userSpecification = $sessionStore.userSpecification;
<script>
import { page } from '$app/stores';
import { setChatSession } from '$lib/stores/chatStore.svelte';
export let data;
$: setChatSession(data.sessionId);
</script>

<svelte:head>
Expand All @@ -21,9 +20,10 @@
</div>

<div class="pt-2">
{#if $page.data.contentCategory}
<h3 class="mt-4 text-lg font-semibold">Content Category:</h3>
<p class="capitalize">{$page.data.contentCategory}</p>
{#if $page.data.category}
<h3 class="mt-4 text-lg font-medium capitalize">
Content Category: {$page.data.category}
</h3>
{:else}
<p class="mt-4 text-muted-foreground">
Your preferences and audiocast metadata will appear here
Expand Down
6 changes: 6 additions & 0 deletions app/src/routes/[sessionId=sessionId]/+layout.server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import type { LayoutServerLoad } from './$types';
export const load: LayoutServerLoad = ({ url }) => {
return {
category: url.searchParams.get('category')
};
};
4 changes: 1 addition & 3 deletions app/src/routes/[sessionId=sessionId]/+page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ import { error, redirect } from '@sveltejs/kit';
import type { PageLoad } from './$types';

export const load: PageLoad = async ({ url, parent }) => {
const category = url.searchParams.get('category');
const { sessionId, category } = await parent();
if (!category) error(400, 'Audio category was not found');

const { sessionId } = await parent();
if (url.pathname !== `/${sessionId}`) redirect(307, '/');

return {
Expand Down

0 comments on commit 352ea4a

Please sign in to comment.