Skip to content

Commit 9ec8607

Browse files
committed
fix: update based on suggestions
1 parent a280a04 commit 9ec8607

File tree

5 files changed

+35
-17
lines changed

5 files changed

+35
-17
lines changed

docs/chatControlIcons.stories.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { Source, Meta } from '@storybook/addon-docs'
66

77
The chat control options are a set of interactive buttons that allow users to perform various actions on messages within a chat interface. These options provide users with functionalities to engage with messages in different ways, such as editing, copying, and deleting.
88

9-
The examples of chat control options are shown in the `MessageCanvas` and `MessageSpace`'s stories. Users can import the necessary components and integrate them via the `getActionsComponent` props. Below are the available control options along with their accepted fields:
9+
The examples of chat control options are shown in the `MessageCanvas` and `MessageSpace`'s stories. Users can import the necessary components and integrate them via the `getActionsComponent` props. Keep in mind that the `getActionsComponent` props must always be a single React element and can be composed of several control options supported for the message. Below are the available control options along with their accepted fields:
1010

1111
## Copy
1212

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React from 'react'
1+
import React, { useState } from 'react'
22

33
import Icon from '../../../icon'
44
import type { ThreadableMessage } from '../../../types'
@@ -9,16 +9,31 @@ export interface CopyProps {
99
}
1010

1111
export default function Copy(props: CopyProps) {
12+
const [tooltipContent, setTooltipContent] = useState('Copy')
13+
const twoSeconds = 2000
14+
15+
function handleOnClick(message: ThreadableMessage) {
16+
if (message.data.text) {
17+
navigator.clipboard
18+
.writeText(message.data.text)
19+
.then(() => {
20+
setTooltipContent('Copied')
21+
setTimeout(() => {
22+
setTooltipContent('Copy')
23+
}, twoSeconds)
24+
})
25+
.catch(() => {
26+
setTooltipContent('Failed to copy')
27+
})
28+
}
29+
}
30+
1231
return (
1332
<Action
1433
message={props.message}
1534
icon={<Icon name="file_copy" />}
16-
onClick={(message: ThreadableMessage) => {
17-
if (message.data.text) {
18-
navigator.clipboard.writeText(message.data.text)
19-
}
20-
}}
21-
label="Copy"
35+
onClick={handleOnClick}
36+
label={tooltipContent}
2237
/>
2338
)
2439
}

src/components/messageCanvas/messageCanvas.stories.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import Card from '@mui/material/Card'
21
import React from 'react'
32

43
import { ElementRenderer, type ThreadableMessage } from '..'
@@ -165,7 +164,7 @@ export const WithCopyIcon = {
165164
getActionsComponent: (message: ThreadableMessage) => {
166165
const copyButton = message.format === 'text' && <Copy message={message} />
167166
if (copyButton) {
168-
return <Card variant="outlined">{copyButton}</Card>
167+
return <>{copyButton}</>
169168
}
170169
},
171170
},
@@ -180,7 +179,7 @@ export const WithCopyIcon = {
180179
getActionsComponent: (message: ThreadableMessage) => {
181180
const copyButton = message.format === 'text' && <Copy message={message} />
182181
if (copyButton) {
183-
return <Card variant="outlined">{copyButton}</Card>
182+
return <>{copyButton}</>
184183
}
185184
},
186185
message={{

src/components/messageCanvas/messageCanvas.tsx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ export interface MessageCanvasProps {
1616
message: ThreadableMessage
1717
/** React component to be displayed in the message canvas. */
1818
children: ReactNode
19-
/** Message actions. For example, this could be a list of buttons for different actions (e.g. copy, delete, save, etc.) */
20-
getActionsComponent?: (message: ThreadableMessage) => ReactNode
19+
/** A function that returns a single React element which contains message actions. For example, this could be a list of buttons for different actions (e.g. copy, delete, save, etc.). The function may also return no element if no actions are applicable or available for a particular message. */
20+
getActionsComponent?: (message: ThreadableMessage) => ReactNode | undefined
2121
}
2222

2323
export default function MessageCanvas(props: MessageCanvasProps) {
@@ -37,7 +37,12 @@ export default function MessageCanvas(props: MessageCanvasProps) {
3737
{props.children}
3838
</Card>
3939
<Box className="rustic-message-footer">
40-
{props.getActionsComponent && props.getActionsComponent(props.message)}
40+
{props.getActionsComponent &&
41+
props.getActionsComponent(props.message) && (
42+
<Card variant="outlined">
43+
{props.getActionsComponent(props.message)}
44+
</Card>
45+
)}
4146
<Box className="rustic-timestamp">
4247
{props.message.lastThreadMessage && (
4348
<Typography variant="caption">last updated: </Typography>

src/components/messageSpace/messageSpace.stories.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import Card from '@mui/material/Card'
21
import React from 'react'
32
import { v4 as getUUID } from 'uuid'
43

@@ -38,7 +37,7 @@ export default {
3837
},
3938
getActionsComponent: {
4039
description:
41-
'Message actions. For example, this could be a list of buttons for different actions (e.g. copy, delete, save, etc.)',
40+
'A function that returns a single React element which contains message actions. For example, this could be a list of buttons for different actions (e.g. copy, delete, save, etc.). However, the function may also return no element if no actions are applicable or available for a particular message.',
4241
},
4342
getProfileComponent: {
4443
description: "Profile icon to be shown before the sender's name.",
@@ -401,7 +400,7 @@ export const Default = {
401400
getActionsComponent: (message: ThreadableMessage) => {
402401
const copyButton = message.format === 'text' && <Copy message={message} />
403402
if (copyButton) {
404-
return <Card variant="outlined">{copyButton}</Card>
403+
return <>{copyButton}</>
405404
}
406405
},
407406
},

0 commit comments

Comments
 (0)