|
1 | | -import { DiscordActionRow, DiscordButton } from '@derockdev/discord-components-react'; |
2 | | -import { ButtonStyle, ComponentType, type MessageActionRowComponent, type ActionRow } from 'discord.js'; |
| 1 | +import { DiscordActionRow, DiscordAttachment, DiscordSpoiler } from '@derockdev/discord-components-react'; |
| 2 | +import { |
| 3 | + ComponentType, |
| 4 | + type ThumbnailComponent, |
| 5 | + type MessageActionRowComponent, |
| 6 | + type TopLevelComponent, |
| 7 | +} from 'discord.js'; |
3 | 8 | import React from 'react'; |
4 | 9 | import { parseDiscordEmoji } from '../../utils/utils'; |
| 10 | +import DiscordSelectMenu from './components/Select Menu'; |
| 11 | +import DiscordContainer from './components/Container'; |
| 12 | +import DiscordSection from './components/section/Section'; |
| 13 | +import DiscordMediaGallery from './components/Media Gallery'; |
| 14 | +import DiscordSeparator from './components/Spacing'; |
| 15 | +import DiscordButton from './components/Button'; |
| 16 | +import DiscordThumbnail from './components/Thumbnail'; |
| 17 | +import MessageContent from './content'; |
| 18 | +import { RenderType } from './content'; |
| 19 | +import type { RenderMessageContext } from '..'; |
| 20 | +import { ButtonStyleMapping } from './components/styles'; |
5 | 21 |
|
6 | | -export default function ComponentRow({ row, id }: { row: ActionRow<MessageActionRowComponent>; id: number }) { |
7 | | - return ( |
8 | | - <DiscordActionRow key={id}> |
9 | | - {row.components.map((component, id) => ( |
10 | | - <Component component={component} id={id} key={id} /> |
11 | | - ))} |
12 | | - </DiscordActionRow> |
13 | | - ); |
14 | | -} |
| 22 | +export default function ComponentRow({ |
| 23 | + component, |
| 24 | + id, |
| 25 | + context, |
| 26 | +}: { |
| 27 | + component: TopLevelComponent; |
| 28 | + id: number; |
| 29 | + context: RenderMessageContext; |
| 30 | +}) { |
| 31 | + switch (component.type) { |
| 32 | + case ComponentType.ActionRow: |
| 33 | + return ( |
| 34 | + <DiscordActionRow key={id}> |
| 35 | + <> |
| 36 | + {component.components.map((nestedComponent, id) => ( |
| 37 | + <Component component={nestedComponent} id={id} key={id} /> |
| 38 | + ))} |
| 39 | + </> |
| 40 | + </DiscordActionRow> |
| 41 | + ); |
| 42 | + |
| 43 | + case ComponentType.Container: |
| 44 | + return ( |
| 45 | + <DiscordContainer key={id}> |
| 46 | + <> |
| 47 | + {component.components.map((nestedComponent, id) => ( |
| 48 | + <ComponentRow component={nestedComponent} id={id} key={id} context={context} /> |
| 49 | + ))} |
| 50 | + </> |
| 51 | + </DiscordContainer> |
| 52 | + ); |
| 53 | + |
| 54 | + case ComponentType.File: |
| 55 | + return ( |
| 56 | + <> |
| 57 | + {component.spoiler ? ( |
| 58 | + <DiscordSpoiler key={component.id} slot="attachment"> |
| 59 | + <DiscordAttachment |
| 60 | + type="file" |
| 61 | + key={component.id} |
| 62 | + slot="attachment" |
| 63 | + url={component.file.url} |
| 64 | + alt="Discord Attachment" |
| 65 | + /> |
| 66 | + </DiscordSpoiler> |
| 67 | + ) : ( |
| 68 | + <DiscordAttachment |
| 69 | + type="file" |
| 70 | + key={component.id} |
| 71 | + slot="attachment" |
| 72 | + url={component.file.url} |
| 73 | + alt="Discord Attachment" |
| 74 | + /> |
| 75 | + )} |
| 76 | + </> |
| 77 | + ); |
| 78 | + |
| 79 | + case ComponentType.MediaGallery: |
| 80 | + return <DiscordMediaGallery component={component} key={id} />; |
| 81 | + |
| 82 | + case ComponentType.Section: |
| 83 | + return ( |
| 84 | + <DiscordSection key={id} accessory={component.accessory} id={id}> |
| 85 | + {component.components.map((nestedComponent, id) => ( |
| 86 | + <ComponentRow component={nestedComponent} id={id} key={id} context={context} /> |
| 87 | + ))} |
| 88 | + </DiscordSection> |
| 89 | + ); |
| 90 | + |
| 91 | + case ComponentType.Separator: |
| 92 | + return <DiscordSeparator key={id} spacing={component.spacing} divider={component.divider} />; |
15 | 93 |
|
16 | | -const ButtonStyleMapping = { |
17 | | - [ButtonStyle.Primary]: 'primary', |
18 | | - [ButtonStyle.Secondary]: 'secondary', |
19 | | - [ButtonStyle.Success]: 'success', |
20 | | - [ButtonStyle.Danger]: 'destructive', |
21 | | - [ButtonStyle.Link]: 'secondary', |
22 | | - [ButtonStyle.Premium]: 'primary', |
23 | | -} satisfies Record<ButtonStyle, Parameters<typeof DiscordButton>[0]['type']>; |
24 | | - |
25 | | -export function Component({ component, id }: { component: MessageActionRowComponent; id: number }) { |
26 | | - if (component.type === ComponentType.Button) { |
27 | | - return ( |
28 | | - <DiscordButton |
29 | | - key={id} |
30 | | - type={ButtonStyleMapping[component.style]} |
31 | | - url={component.url ?? undefined} |
32 | | - emoji={component.emoji ? parseDiscordEmoji(component.emoji) : undefined} |
33 | | - > |
34 | | - {component.label} |
35 | | - </DiscordButton> |
36 | | - ); |
| 94 | + case ComponentType.TextDisplay: |
| 95 | + return <MessageContent key={id} content={component.content} context={{ ...context, type: RenderType.NORMAL }} />; |
| 96 | + |
| 97 | + default: |
| 98 | + return null; |
37 | 99 | } |
| 100 | +} |
| 101 | + |
| 102 | +export function Component({ |
| 103 | + component, |
| 104 | + id, |
| 105 | +}: { |
| 106 | + component: MessageActionRowComponent | ThumbnailComponent; |
| 107 | + id: number; |
| 108 | +}) { |
| 109 | + switch (component.type) { |
| 110 | + case ComponentType.Button: |
| 111 | + return ( |
| 112 | + <DiscordButton |
| 113 | + key={id} |
| 114 | + type={ButtonStyleMapping[component.style as keyof typeof ButtonStyleMapping]} |
| 115 | + url={component.url ?? undefined} |
| 116 | + emoji={component.emoji ? parseDiscordEmoji(component.emoji) : undefined} |
| 117 | + > |
| 118 | + {component.label} |
| 119 | + </DiscordButton> |
| 120 | + ); |
38 | 121 |
|
39 | | - return undefined; |
| 122 | + case ComponentType.StringSelect: |
| 123 | + case ComponentType.UserSelect: |
| 124 | + case ComponentType.RoleSelect: |
| 125 | + case ComponentType.MentionableSelect: |
| 126 | + case ComponentType.ChannelSelect: |
| 127 | + return <DiscordSelectMenu key={id} component={component} />; |
| 128 | + |
| 129 | + case ComponentType.Thumbnail: |
| 130 | + return <DiscordThumbnail key={id} url={component.media.url} />; |
| 131 | + |
| 132 | + default: |
| 133 | + return undefined; |
| 134 | + } |
40 | 135 | } |
0 commit comments