Skip to content

Commit

Permalink
Merge pull request #95 from shayypy/empty-embed-warning
Browse files Browse the repository at this point in the history
Empty embed warning
  • Loading branch information
maddy committed Jul 5, 2023
2 parents 07f33b5 + 23ccba2 commit e7403c3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
6 changes: 6 additions & 0 deletions common/utilities/joinText.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export const joinWithAnd = (parts: string[]): string => {
if (parts.length === 1) return parts[0]
const firsts = parts.slice(0, parts.length - 1)
const last = parts[parts.length - 1]
return `${firsts.join(", ")}${parts.length > 2 ? "," : ""} and ${last}`
}
18 changes: 18 additions & 0 deletions modules/editor/message/MessageEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { ButtonList } from "../../../common/layout/ButtonList"
import { Stack } from "../../../common/layout/Stack"
import { ModalManagerContext } from "../../../common/modal/ModalManagerContext"
import { useRequiredContext } from "../../../common/state/useRequiredContext"
import { joinWithAnd } from "../../../common/utilities/joinText"
import { Markdown } from "../../markdown/Markdown"
import type { MessageItemFormState } from "../../message/state/editorForm"
import type { EmbedLike } from "../../message/state/models/EmbedModel"
Expand Down Expand Up @@ -64,6 +65,10 @@ export function MessageEditor(props: MessageEditorProps) {
),
})

const emptyEmbedNumbers = message.embeds
.map((embed, index) => (!embed.displayName ? index + 1 : -1))
.filter(index => index !== -1)

return useObserver(() => (
<Stack gap={16}>
<PrimaryContentEditor message={message} form={form} />
Expand All @@ -78,6 +83,19 @@ export function MessageEditor(props: MessageEditorProps) {
}
/>
</EmbedValidationErrorContainer>
<EmbedValidationErrorContainer>
<InputError
error={
emptyEmbedNumbers.length === 1
? `Embed ${emptyEmbedNumbers[0]} is empty`
: emptyEmbedNumbers.length > 1
? `Embeds ${joinWithAnd(
emptyEmbedNumbers.map(String),
)} are empty`
: undefined
}
/>
</EmbedValidationErrorContainer>
<Stack gap={16}>
{message.embeds.map((embed, index) => (
<EmbedEditor
Expand Down

0 comments on commit e7403c3

Please sign in to comment.