Skip to content

Commit

Permalink
Fix build.
Browse files Browse the repository at this point in the history
  • Loading branch information
bLopata committed Dec 11, 2024
1 parent 42db168 commit 4e8c8ff
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 48 deletions.
25 changes: 3 additions & 22 deletions www/app/Chat.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use client';
import useSWR from 'swr';
import { createStore, del, get, set, clear } from 'idb-keyval';

import dynamic from 'next/dynamic';

Expand All @@ -19,6 +18,7 @@ import { getFreeMessageCount, useFreeTrial } from '@/utils/supabase/actions';
import { getConversations, createConversation } from './actions/conversations';
import { getMessages, addOrRemoveReaction } from './actions/messages';
import { type Message } from '@/utils/types';
import { cacheProvider } from '@/utils/swrCache';

import useAutoScroll from '@/hooks/autoscroll';

Expand All @@ -33,9 +33,6 @@ const Sidebar = dynamic(() => import('@/components/sidebar'), {
ssr: false,
});

const conversationsStore = createStore('bloom-db', 'conversations');
const messagesStore = createStore('bloom-db', 'messages');

async function fetchStream(
type: 'thought' | 'response' | 'honcho',
message: string,
Expand Down Expand Up @@ -93,22 +90,6 @@ interface ChatProps {
interface HonchoResponse {
content: string;
}
const cacheProvider = {
conversationsProvider: {
get: async (key: string) => get(key, conversationsStore),
set: async (key: string, value: any) => set(key, value, conversationsStore),
delete: async (key: string) => del(key, conversationsStore),
},
messagesProvider: {
get: async (key: string) => get(key, messagesStore),
set: async (key: string, value: any) => set(key, value, messagesStore),
delete: async (key: string) => del(key, messagesStore),
},
clearAll: async () => {
await clear(conversationsStore);
await clear(messagesStore);
}
};

const defaultMessage: Message = {
content: `I'm your Aristotelian learning companion — here to help you follow your curiosity in whatever direction you like. My engineering makes me extremely receptive to your needs and interests. You can reply normally, and I'll always respond!\n\nIf I'm off track, just say so!\n\nNeed to leave or just done chatting? Let me know! I'm conversational by design so I'll say goodbye 😊.`,
Expand Down Expand Up @@ -189,7 +170,7 @@ export default function Chat({
await mutateConversations();
}
},
storage: cacheProvider.conversationsProvider,
provider: cacheProvider,
revalidateOnFocus: false,
}
);
Expand All @@ -211,7 +192,7 @@ export default function Chat({
() => messagesFetcher(conversationId!),
{
fallbackData: initialMessages,
storage: cacheProvider.messagesProvider,
provider: cacheProvider,
revalidateOnFocus: false,
revalidateOnReconnect: false,
dedupingInterval: 60000,
Expand Down
10 changes: 6 additions & 4 deletions www/app/providers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@ import { PostHogProvider } from 'posthog-js/react';
import { usePathname, useSearchParams } from 'next/navigation';
import { useEffect } from 'react';
import { SWRConfig } from 'swr';
import { createStore } from 'idb-keyval';
import { cacheProvider } from '@/utils/swrCache';

const posthogKey: string = process.env.NEXT_PUBLIC_POSTHOG_KEY || '';
const posthogHost: string = process.env.NEXT_PUBLIC_POSTHOG_HOST || '';

const swrStore = createStore('bloom-db', 'swr-cache');

if (
typeof window !== 'undefined' &&
process.env.NEXT_PUBLIC_SITE_URL != 'http://localhost:3000'
Expand Down Expand Up @@ -46,7 +44,11 @@ export function PHProvider({ children }: { children: React.ReactNode }) {

export function SWRProvider({ children }: { children: React.ReactNode }) {
return (
<SWRConfig value={{ provider: () => new Map() }}>
<SWRConfig
value={{
provider: cacheProvider
}}
>
{children}
</SWRConfig>
);
Expand Down
60 changes: 38 additions & 22 deletions www/utils/swrCache.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,45 @@
import { Cache, State } from 'swr';
import { createStore, get, set, clear } from 'idb-keyval';
import { createStore, get, set, del, clear, } from 'idb-keyval';
import type { Cache } from 'swr';

const swrStore = createStore('bloom-db', 'swr-cache');
const store = typeof window !== 'undefined'
? createStore('bloom-db', 'messages')
: null;

export async function clearSWRCache() {
await clear(swrStore);
}
// In-memory cache for synchronous operations
const memoryCache = new Map<string, any>();

export function indexedDBProvider(): Cache {
const cache = new Map<string, State>();
export const clearSWRCache = async () => {
memoryCache.clear();
if (store && typeof window !== 'undefined') {
try {
await clear(store);
} catch (error) {
console.error('Error clearing cache:', error);
}
}
};

// When initializing, restore data from IndexedDB into the map
if (typeof window !== 'undefined') {
get('swr-cache', swrStore).then((data: Array<[string, State]> | undefined) => {
if (data) {
data.forEach(([key, value]) => cache.set(key, value));
export const cacheProvider = (cache: Readonly<Cache<any>>) => {
return {
get: (key: string) => {
return memoryCache.get(key);
},
set: (key: string, value: any) => {
memoryCache.set(key, value);
// Persist to IndexedDB in background
if (store) {
set(key, value, store).catch(console.error);
}
});

// Before unloading, save map data to IndexedDB
window.addEventListener('beforeunload', () => {
const entries = Array.from(cache.entries());
set('swr-cache', entries, swrStore);
});
},
delete: (key: string) => {
memoryCache.delete(key);
// Delete from IndexedDB in background
if (store) {
del(key, store).catch(console.error);
}
},
keys: () => {
return memoryCache.keys();
}
}

return cache;
}

0 comments on commit 4e8c8ff

Please sign in to comment.