Skip to content

Commit

Permalink
Links: land to former conversations #356
Browse files Browse the repository at this point in the history
  • Loading branch information
enricoros committed Jan 23, 2024
1 parent 695af02 commit 15cfef0
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 14 deletions.
2 changes: 1 addition & 1 deletion pages/link/chat/[chatLinkId].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ export default function ChatLinkPage() {
// external state
const { chatLinkId } = useRouterQuery<{ chatLinkId: string | undefined }>();

return withLayout({ type: 'optima', suspendAutoModelsSetup: true }, <AppChatLink linkId={chatLinkId || ''} />);
return withLayout({ type: 'optima', suspendAutoModelsSetup: true }, <AppChatLink chatLinkId={chatLinkId || null} />);
}
53 changes: 40 additions & 13 deletions src/apps/link/AppChatLink.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as React from 'react';
import Head from 'next/head';
import { useQuery } from '@tanstack/react-query';

import { Box, Typography } from '@mui/joy';
import { Box, Card, CardContent, Typography } from '@mui/joy';

import { createConversationFromJsonV1 } from '~/modules/trade/trade.client';

Expand All @@ -20,6 +20,9 @@ import { AppChatLinkMenuItems } from './AppChatLinkMenuItems';
import { ViewChatLink } from './ViewChatLink';


const SPECIAL_LIST_PAGE_ID = 'list';


const Centerer = (props: { backgroundColor: string, children?: React.ReactNode }) =>
<Box sx={{
backgroundColor: props.backgroundColor,
Expand All @@ -29,6 +32,21 @@ const Centerer = (props: { backgroundColor: string, children?: React.ReactNode }
{props.children}
</Box>;

const ListPlaceholder = () =>
<Box sx={{ p: { xs: 3, md: 6 } }}>
<Card>
<CardContent>
<Typography level='title-md'>
Shared Conversations
</Typography>
<Typography level='body-sm'>
Here you can see formely exported shared conversations. Please select a conversation from the drawer.
</Typography>
</CardContent>
</Card>
</Box>;


const ShowLoading = () =>
<Centerer backgroundColor={themeBgAppDarker}>
<LogoProgress showProgress={true} />
Expand All @@ -48,7 +66,10 @@ const ShowError = (props: { error: any }) =>
* Note: we don't have react-query for the Node functions, so we use the immediate API here,
* and wrap it in a react-query hook
*/
async function fetchStoredChatV1(objectId: string) {
async function fetchStoredChatV1(objectId: string | null) {
if (!objectId)
throw new Error('No Stored Chat');

// fetch
const result = await apiAsyncNode.trade.storageGet.query({ objectId });
if (result.type === 'error')
Expand All @@ -68,13 +89,17 @@ async function fetchStoredChatV1(objectId: string) {
}


export function AppChatLink(props: { linkId: string }) {
export function AppChatLink(props: { chatLinkId: string | null }) {

// derived state
const isListPage = props.chatLinkId === SPECIAL_LIST_PAGE_ID;
const linkId = isListPage ? null : props.chatLinkId;

// external state
const { data, isError, error, isLoading } = useQuery({
enabled: !!props.linkId,
queryKey: ['chat-link', props.linkId],
queryFn: () => fetchStoredChatV1(props.linkId),
enabled: !!linkId,
queryKey: ['chat-link', linkId],
queryFn: () => fetchStoredChatV1(linkId),
refetchOnWindowFocus: false,
staleTime: 1000 * 60 * 60 * 24, // 24 hours
});
Expand All @@ -96,13 +121,15 @@ export function AppChatLink(props: { linkId: string }) {
<title>{capitalizeFirstLetter(pageTitle)} · {Brand.Title.Base} 🚀</title>
</Head>

{isLoading
? <ShowLoading />
: isError
? <ShowError error={error} />
: !!data?.conversation
? <ViewChatLink conversation={data.conversation} storedAt={data.storedAt} expiresAt={data.expiresAt} />
: <Centerer backgroundColor={themeBgAppDarker} />}
{isListPage
? <ListPlaceholder />
: isLoading
? <ShowLoading />
: isError
? <ShowError error={error} />
: !!data?.conversation
? <ViewChatLink conversation={data.conversation} storedAt={data.storedAt} expiresAt={data.expiresAt} />
: <Centerer backgroundColor={themeBgAppDarker} />}

</>;
}

0 comments on commit 15cfef0

Please sign in to comment.