Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…4-outreachvids into task/OV-439-generate-preview-for-template
  • Loading branch information
sergiy4 committed Sep 26, 2024
2 parents 6274e46 + d20cdc8 commit f58e0d8
Show file tree
Hide file tree
Showing 15 changed files with 98 additions and 57 deletions.
1 change: 1 addition & 0 deletions frontend/src/bundles/chat/helpers/helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { sanitizeJsonString } from './sanitize-json-string.js';
6 changes: 6 additions & 0 deletions frontend/src/bundles/chat/helpers/sanitize-json-string.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const sanitizeJsonString = (input: string): string => {
const match = input.match(/\[.*]/s);
return match ? match[0].trim() : input.trim();
};

export { sanitizeJsonString };
44 changes: 42 additions & 2 deletions frontend/src/bundles/chat/store/slice.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,67 @@
import { createSlice } from '@reduxjs/toolkit';

import { MessageSender } from '~/bundles/chat/enums/enums.js';
import { sanitizeJsonString } from '~/bundles/chat/helpers/helpers.js';
import {
type GenerateTextRequestDto,
type Message,
} from '~/bundles/chat/types/types.js';
import {
EMPTY_VALUE,
LAST_ELEMENT_INDEX,
} from '~/bundles/common/constants/constants.js';
import { DataStatus } from '~/bundles/common/enums/enums.js';
import { type ValueOf } from '~/bundles/common/types/types.js';
import {
type ValueOf,
type VideoScript,
} from '~/bundles/common/types/types.js';

import { deleteChat, sendMessage } from './actions.js';

type State = {
messages: Message[];
videoScripts: VideoScript[];
videoScriptErrorMessage: string;
dataStatus: ValueOf<typeof DataStatus>;
};

const initialState: State = {
messages: [],
videoScripts: [],
videoScriptErrorMessage: '',
dataStatus: DataStatus.IDLE,
};

const { reducer, actions, name } = createSlice({
initialState,
name: 'chat',
reducers: {},
reducers: {
generateVideoScript(state) {
const messages = state.messages.filter(
(message) => message.sender === MessageSender.AI,
);

if (!messages || messages.length === EMPTY_VALUE) {
return;
}

const lastMessage = messages.at(LAST_ELEMENT_INDEX);
if (!lastMessage) {
return;
}

try {
const sanitizedJson = sanitizeJsonString(lastMessage.text);
const videoScripts: VideoScript[] = JSON.parse(sanitizedJson);
state.videoScripts = videoScripts;
state.videoScriptErrorMessage = '';
} catch {
state.videoScripts = [];
state.videoScriptErrorMessage =
'There was an error Generating the Script, please Re-Generate';
}
},
},
extraReducers(builder) {
builder.addCase(sendMessage.pending, (state) => {
state.dataStatus = DataStatus.PENDING;
Expand Down Expand Up @@ -60,6 +98,8 @@ const { reducer, actions, name } = createSlice({
});
builder.addCase(deleteChat.fulfilled, (state) => {
state.messages = [];
state.videoScripts = [];
state.videoScriptErrorMessage = '';
state.dataStatus = DataStatus.FULFILLED;
});
builder.addCase(deleteChat.rejected, (state) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,18 @@ import { EMPTY_VALUE } from '~/bundles/common/constants/constants.js';
import { DataStatus } from '~/bundles/common/enums/data-status.enum.js';
import { useAppSelector } from '~/bundles/common/hooks/hooks.js';
import { IconName } from '~/bundles/common/icons/icons.js';
import { type VideoScript } from '~/bundles/common/types/types.js';

import { GenerateScriptPlaceholderContent } from '../generate-script-placeholder-content/generate-script-placeholder-content.js';
import { GenerateScriptScene } from '../generate-script-scene/generate-script-scene.js';
import styles from './styles.module.css';

type Properties = {
videoScripts: VideoScript[];
};

const GenerateScriptPlaceholder: React.FC<Properties> = ({ videoScripts }) => {
const { dataStatus } = useAppSelector(({ chat }) => ({
dataStatus: chat.dataStatus,
}));
const GenerateScriptPlaceholder: React.FC = () => {
const { dataStatus, videoScripts, videoScriptErrorMessage } =
useAppSelector(({ chat }) => ({
dataStatus: chat.dataStatus,
videoScripts: chat.videoScripts,
videoScriptErrorMessage: chat.videoScriptErrorMessage,
}));

const renderLoadingState = (): React.ReactNode => (
<Box mt="100px">
Expand All @@ -31,6 +29,13 @@ const GenerateScriptPlaceholder: React.FC<Properties> = ({ videoScripts }) => {
/>
);

const renderErrorMessage = (): React.ReactNode => (
<GenerateScriptPlaceholderContent
message={videoScriptErrorMessage}
icon={IconName.WARNING}
/>
);

const renderScripts = (): React.ReactNode => (
<>
{videoScripts.map((videoScript, index) => (
Expand All @@ -43,6 +48,9 @@ const GenerateScriptPlaceholder: React.FC<Properties> = ({ videoScripts }) => {
if (dataStatus === DataStatus.PENDING) {
return renderLoadingState();
}
if (videoScriptErrorMessage) {
return renderErrorMessage();
}
if (videoScripts.length === EMPTY_VALUE) {
return renderEmptyState();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,12 @@ import {
TabPanels,
Tabs,
} from '~/bundles/common/components/components.js';
import {
getVideoScriptMessageFromPayload,
sanitizeJsonString,
} from '~/bundles/common/components/video-modal/components/video-modal-content/helpers/helpers.js';
import { EMPTY_VALUE } from '~/bundles/common/constants/constants.js';
import { getVideoScriptMessageFromPayload } from '~/bundles/common/components/video-modal/components/video-modal-content/helpers/helpers.js';
import {
useAppDispatch,
useAppSelector,
useCallback,
useMemo,
} from '~/bundles/common/hooks/hooks.js';
import { type VideoScript } from '~/bundles/common/types/video-script.type.js';
import { type GenerateVideoScriptRequestDto } from '~/bundles/video-scripts/video-scripts.js';

import { GenerateScriptForm } from '../generate-script-form/generate-script-form.js';
Expand All @@ -41,35 +35,15 @@ const GenerateScriptView: React.FC = () => {
const sendMessageRequest: GenerateTextRequestDto = {
message: getVideoScriptMessageFromPayload(payload, messages),
};
void dispatch(chatActions.sendMessage(sendMessageRequest));
void dispatch(chatActions.sendMessage(sendMessageRequest)).then(
() => {
dispatch(chatActions.generateVideoScript());
},
);
},
[messages, dispatch],
);

const lastGeneratedScript: VideoScript[] = useMemo(() => {
if (!messages || messages.length === EMPTY_VALUE) {
return [];
}

const lastMessage = messages.at(-1);
if (!lastMessage) {
return [];
}

try {
const sanitizedJson = sanitizeJsonString(lastMessage.text);
const videoScripts: VideoScript[] = JSON.parse(sanitizedJson);
return videoScripts;
} catch {
return [
{
title: 'Scene',
description: lastMessage.text,
},
];
}
}, [messages]);

return (
<>
<Heading className={styles['script-view-heading']} variant="H3">
Expand All @@ -94,9 +68,7 @@ const GenerateScriptView: React.FC = () => {
<GenerateScriptForm
onSubmit={handleGenerateVideoScriptSubmit}
/>
<GenerateScriptPlaceholder
videoScripts={lastGeneratedScript}
/>
<GenerateScriptPlaceholder />
</HStack>
</TabPanel>
</TabPanels>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export { getVideoScriptMessageFromPayload } from './generate-script-message-template/generate-script-message-template.js';
export { sanitizeJsonString } from './sanitize-json-string/sanitize-json-string.js';
export { getVideoScriptMessageFromPayload } from './generate-script-message-template.js';
export { sanitizeJsonString } from './sanitize-json-string.js';
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const sanitizeJsonString = (input: string): string => {
const match = input.match(/\[.*]/s);
return match ? match[0].trim() : input.trim();
};

export { sanitizeJsonString };

This file was deleted.

2 changes: 1 addition & 1 deletion frontend/src/bundles/common/constants/constants.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export { FPS } from './fps.constant.js';
export { SOCKET_TRANSPORT_WEBSOCKETS } from './socket-trasnport.constants.js';
export { EMPTY_VALUE } from 'shared';
export { EMPTY_VALUE, LAST_ELEMENT_INDEX } from 'shared';
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,9 @@
border-radius: 4px;
padding: 2px 8px;
}

.card-title {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,11 @@ const VideoCard: React.FC<Properties> = ({
</Box>

<Box padding="7px 10px 5px 5px">
<Text variant="button" color="typography.900">
<Text
variant="button"
color="typography.900"
className={styles['card-title']}
>
{name}
</Text>
<Flex justify="space-between">
Expand Down
1 change: 1 addition & 0 deletions shared/src/constants/constants.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { EMPTY_VALUE } from './empty-length.constant.js';
export { LAST_ELEMENT_INDEX } from './last-element-index.constant.js';
3 changes: 3 additions & 0 deletions shared/src/constants/last-element-index.constant.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const LAST_ELEMENT_INDEX = -1;

export { LAST_ELEMENT_INDEX };
2 changes: 1 addition & 1 deletion shared/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export {
VideosApiPath,
VideoValidationMessage,
} from './bundles/videos/videos.js';
export { EMPTY_VALUE } from './constants/constants.js';
export { EMPTY_VALUE, LAST_ELEMENT_INDEX } from './constants/constants.js';
export {
ApiPath,
AppEnvironment,
Expand Down

0 comments on commit f58e0d8

Please sign in to comment.