mirror of
https://github.com/vercel/commerce.git
synced 2025-05-18 23:46:58 +00:00
Adding titleImage to our context and MyDocument
This commit is contained in:
parent
3c0fb48518
commit
db9d3784f8
@ -5,11 +5,11 @@ import StoryPDFViewer from 'components/pdf/StoryPDFViewer';
|
|||||||
export default function GenerateStoryComponent() {
|
export default function GenerateStoryComponent() {
|
||||||
return (
|
return (
|
||||||
<GenerateStoryContextProvider>
|
<GenerateStoryContextProvider>
|
||||||
{({ story, loading }: IGenerateStoryContext) => {
|
{({ story, titleImage, loading }: IGenerateStoryContext) => {
|
||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
{loading ? <div>Loading...</div> : null}
|
{loading ? <div>Loading...</div> : null}
|
||||||
{story && <StoryPDFViewer story={story} />}
|
{story && <StoryPDFViewer story={story} titleImage={titleImage} />}
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}}
|
}}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
'use client';
|
'use client';
|
||||||
|
|
||||||
import chatOperations, { IStory } from 'operations/chatOperations';
|
import chatOperations, { IStory } from 'operations/chatOperations';
|
||||||
|
import imageOperations from 'operations/imageOperations';
|
||||||
import {
|
import {
|
||||||
PropsWithChildren,
|
PropsWithChildren,
|
||||||
createContext,
|
createContext,
|
||||||
@ -12,29 +13,40 @@ import {
|
|||||||
|
|
||||||
export interface IGenerateStoryContext {
|
export interface IGenerateStoryContext {
|
||||||
story?: IStory;
|
story?: IStory;
|
||||||
|
titleImage?: string;
|
||||||
loading: boolean;
|
loading: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
const GenerateStoryContext = createContext<IGenerateStoryContext>({
|
const GenerateStoryContext = createContext<IGenerateStoryContext>({
|
||||||
story: undefined,
|
story: undefined,
|
||||||
|
titleImage: undefined,
|
||||||
loading: false
|
loading: false
|
||||||
});
|
});
|
||||||
|
|
||||||
function GenerateStoryContextProvider({ children }: { children: PropsWithChildren<any> }) {
|
function GenerateStoryContextProvider({ children }: { children: PropsWithChildren<any> }) {
|
||||||
const [loading, setLoading] = useState<boolean>(false);
|
const [loading, setLoading] = useState<boolean>(false);
|
||||||
const [story, setStory] = useState<IStory>();
|
const [story, setStory] = useState<IStory>();
|
||||||
|
const [titleImage, setTitleImage] = useState<string>();
|
||||||
|
|
||||||
const value = useMemo<IGenerateStoryContext>(() => ({ story, loading }), [story, loading]);
|
// TODO (Patricio->Benson): should we memoize the titleImage like this? It takes the image a second to come back from the API...
|
||||||
|
const value = useMemo<IGenerateStoryContext>(
|
||||||
|
() => ({ story, titleImage, loading }),
|
||||||
|
[story, titleImage, loading]
|
||||||
|
);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
TODO(Benson -> Patricio): Make network call to get images
|
|
||||||
TODO(Benson -> Patricio): Write a helper function in this directory /generate/utils.ts
|
TODO(Benson -> Patricio): Write a helper function in this directory /generate/utils.ts
|
||||||
called mergeImages(story: IStory, images: string[]): IStory;
|
called mergeImages(story: IStory, images: string[]): IStory;
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
TODO(Patricio -> Benson): This Dalle call takes a while to come back. We should probably show a loading indicator in the meantime? We'll figure it out
|
||||||
|
*/
|
||||||
const getStoryAsync = useCallback(async () => {
|
const getStoryAsync = useCallback(async () => {
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
const story = await chatOperations.createStoryAsync();
|
const story = await chatOperations.createStoryAsync();
|
||||||
// const images = await imageOperations.getStoryImagesAsync(story);
|
const titleImage = await imageOperations.createImageAsync(story.introduction);
|
||||||
|
setTitleImage(titleImage);
|
||||||
setStory(story);
|
setStory(story);
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
}, []);
|
}, []);
|
||||||
|
10
components/generate/utils.tsx
Normal file
10
components/generate/utils.tsx
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
import { IStory } from 'operations/chatOperations';
|
||||||
|
|
||||||
|
function mergeImages(story: IStory, titleImage: string): IStory {
|
||||||
|
return {
|
||||||
|
...story,
|
||||||
|
titleImage
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export default mergeImages;
|
@ -18,7 +18,7 @@ const PDFViewerNoSSR = dynamic(() => import('@react-pdf/renderer').then((mod) =>
|
|||||||
ssr: false
|
ssr: false
|
||||||
});
|
});
|
||||||
|
|
||||||
export default function StoryPDFViewer({ story }: { story: IStory }) {
|
export default function StoryPDFViewer({ story }: { story: IStory }, titleImage: string) {
|
||||||
return (
|
return (
|
||||||
<div
|
<div
|
||||||
style={{
|
style={{
|
||||||
@ -28,7 +28,7 @@ export default function StoryPDFViewer({ story }: { story: IStory }) {
|
|||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<PDFViewerNoSSR style={styles.pdfContainer}>
|
<PDFViewerNoSSR style={styles.pdfContainer}>
|
||||||
<Document pages={story.pages} />
|
<Document pages={story.pages} titleImage={titleImage} />
|
||||||
</PDFViewerNoSSR>
|
</PDFViewerNoSSR>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
@ -125,7 +125,7 @@ with a solidified way at dispersing the text throughout the page.
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
// TODO(Benson -> Patricio): replace hardcoded images.
|
// TODO(Benson -> Patricio): replace hardcoded images.
|
||||||
const Document = ({ pages }: { pages: { text: string }[] }) => {
|
const Document = ({ pages }: { pages: { text: string }[] }, titleImage: string) => {
|
||||||
return (
|
return (
|
||||||
<PDFDocument>
|
<PDFDocument>
|
||||||
{pages.map(({ text }, index) => {
|
{pages.map(({ text }, index) => {
|
||||||
@ -134,7 +134,7 @@ const Document = ({ pages }: { pages: { text: string }[] }) => {
|
|||||||
<View key={index} style={{ ...styles.section, bottom: '10%' }}>
|
<View key={index} style={{ ...styles.section, bottom: '10%' }}>
|
||||||
<Text>{text}</Text>
|
<Text>{text}</Text>
|
||||||
</View>
|
</View>
|
||||||
<Image src={imgURL} style={styles.image} />
|
<Image src={titleImage} style={styles.image} />
|
||||||
</Page>
|
</Page>
|
||||||
);
|
);
|
||||||
})}
|
})}
|
||||||
|
@ -24,7 +24,7 @@ function getFunctionCallArguments<T>(response: any) {
|
|||||||
*/
|
*/
|
||||||
export interface IStory {
|
export interface IStory {
|
||||||
title: string;
|
title: string;
|
||||||
// titleImage?: string;
|
titleImage?: string;
|
||||||
topic: string;
|
topic: string;
|
||||||
introduction: string;
|
introduction: string;
|
||||||
narrativeStructure: string;
|
narrativeStructure: string;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user