Skip to content

Commit e72e1ce

Browse files
r-farkhutdinovRuslan Farkhutdinov
andauthored
Chat: Add FileAttachments story (#31577)
Co-authored-by: Ruslan Farkhutdinov <[email protected]>
1 parent 5b66b24 commit e72e1ce

File tree

1 file changed

+124
-0
lines changed

1 file changed

+124
-0
lines changed

apps/react-storybook/stories/chat/Chat.stories.tsx

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -790,3 +790,127 @@ export const Editing: Story = {
790790
);
791791
}
792792
};
793+
794+
export const FileAttachments: Story = {
795+
args: {
796+
user: firstAuthor,
797+
...commonArgs,
798+
},
799+
argTypes: {
800+
user: {
801+
control: 'select',
802+
options: [firstAuthor.name, secondAuthor.name],
803+
mapping: {
804+
[firstAuthor.name]: firstAuthor,
805+
[secondAuthor.name]: secondAuthor,
806+
},
807+
defaultValue: firstAuthor.name,
808+
},
809+
},
810+
render: ({
811+
width,
812+
height,
813+
disabled,
814+
rtlEnabled,
815+
user,
816+
visible,
817+
activeStateEnabled,
818+
hoverStateEnabled,
819+
focusStateEnabled,
820+
}) => {
821+
const [messages, setMessages] = useState<ChatTypes.Message[]>([
822+
{
823+
id: 1,
824+
author: firstAuthor,
825+
text: 'Files attached in a very long message: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed vel tempus orci, sit amet tempor tortor. Etiam ut aliquam nisi.',
826+
attachments: [
827+
{ name: 'report.pdf', size: 1024 * 512 },
828+
{ name: 'presentation.pptx', size: 1024 * 1024 },
829+
{ name: 'photo.jpg', size: 1024 * 300 },
830+
{ name: 'song.mp3', size: 1024 * 300 },
831+
],
832+
},
833+
{
834+
id: 2,
835+
author: secondAuthor,
836+
text: 'Files, short message',
837+
attachments: [
838+
{ name: 'report.pdf', size: 1024 * 512 },
839+
{ name: 'song.mp3', size: 1024 * 1024 },
840+
],
841+
},
842+
]);
843+
844+
const [toastConfig, setToastConfig] = useState({
845+
visible: false,
846+
message: '',
847+
});
848+
849+
const onAttachmentDownloadClick = useCallback((e: ChatTypes.AttachmentDownloadClickEvent) => {
850+
const { attachment } = e;
851+
852+
setToastConfig({
853+
visible: true,
854+
message: `Downloading ${attachment?.name}...`,
855+
});
856+
857+
setTimeout(() => {
858+
setToastConfig({
859+
visible: true,
860+
message: `${attachment?.name} downloaded successfully`,
861+
});
862+
}, 1000);
863+
}, []);
864+
865+
const onMessageEntered = useCallback(
866+
({ message }) => {
867+
setMessages((prev) => [...prev, message]);
868+
},
869+
[]
870+
);
871+
872+
const onToastHiding = useCallback(() => {
873+
setToastConfig({ visible: false, message: '' });
874+
}, []);
875+
876+
const fileUploaderOptions = useMemo(() => ({
877+
uploadFile: (file: File) => {
878+
setToastConfig({
879+
visible: true,
880+
message: `Uploading ${file.name}...`,
881+
});
882+
setTimeout(() => {
883+
setToastConfig({
884+
visible: true,
885+
message: `${file.name} uploaded successfully`,
886+
});
887+
}, 1000);
888+
}
889+
}), []);
890+
891+
return (
892+
<div style={{ display: 'flex', justifyContent: 'center', alignItems: 'center' }}>
893+
<Chat
894+
width={width}
895+
height={height}
896+
items={messages}
897+
disabled={disabled}
898+
rtlEnabled={rtlEnabled}
899+
user={user}
900+
visible={visible}
901+
activeStateEnabled={activeStateEnabled}
902+
focusStateEnabled={focusStateEnabled}
903+
hoverStateEnabled={hoverStateEnabled}
904+
onAttachmentDownloadClick={onAttachmentDownloadClick}
905+
onMessageEntered={onMessageEntered}
906+
fileUploaderOptions={fileUploaderOptions}
907+
/>
908+
<Toast
909+
{...toastConfig}
910+
onHiding={onToastHiding}
911+
displayTime={1000}
912+
/>
913+
</div>
914+
);
915+
},
916+
};

0 commit comments

Comments
 (0)