-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #97 from shayypy/loud-send-errors
Loud send/edit errors
- Loading branch information
Showing
4 changed files
with
109 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import React from "react" | ||
import { SecondaryButton } from "../../../common/input/button/SecondaryButton" | ||
import { ModalAction } from "../../../common/modal/layout/ModalAction" | ||
import { ModalBody } from "../../../common/modal/layout/ModalBody" | ||
import { ModalContainer } from "../../../common/modal/layout/ModalContainer" | ||
import { ModalFooter } from "../../../common/modal/layout/ModalFooter" | ||
import { ModalHeader } from "../../../common/modal/layout/ModalHeader" | ||
import { ModalTitle } from "../../../common/modal/layout/ModalTitle" | ||
import { ModalContext } from "../../../common/modal/ModalContext" | ||
import { useRequiredContext } from "../../../common/state/useRequiredContext" | ||
import { remove } from "../../../icons/remove" | ||
import type { CodedError, DiscordError } from "../../../types/DiscordError" | ||
import { Markdown } from "../../markdown/Markdown" | ||
|
||
export type DiscordErrorsModalProps = { | ||
errors: DiscordError[] | ||
} | ||
|
||
export function DiscordErrorsModal(props: DiscordErrorsModalProps) { | ||
const { errors } = props | ||
|
||
const modal = useRequiredContext(ModalContext) | ||
|
||
const flattened = errors.map(discordError => { | ||
const flattenErrorObject = (d: DiscordError["errors"], key?: string) => { | ||
const items: string[][] = [] | ||
for (const [k, v] of Object.entries(d!)) { | ||
const newKey = key ? `${key}.${k}` : k | ||
|
||
if (typeof v === "object" && !Array.isArray(v) && v != null) { | ||
// eslint-disable-next-line no-underscore-dangle | ||
const codedErrors = (v as { _errors?: CodedError[] })._errors | ||
if (!codedErrors) { | ||
items.push( | ||
...flattenErrorObject(v as DiscordError["errors"], newKey), | ||
) | ||
} else { | ||
items.push([newKey, codedErrors.map(e => e.message).join(", ")]) | ||
} | ||
} else { | ||
items.push([newKey, String(v)]) | ||
} | ||
} | ||
|
||
return items | ||
} | ||
if (discordError.errors) { | ||
const items = flattenErrorObject(discordError.errors) | ||
return items.map(([k, v]) => `In \`${k}\`: ${v}`) | ||
} | ||
return [discordError.message] | ||
})[0] | ||
|
||
return ( | ||
<ModalContainer> | ||
<ModalHeader> | ||
<ModalTitle>Discord Error</ModalTitle> | ||
<ModalAction | ||
icon={remove} | ||
label="Close" | ||
onClick={() => modal.dismiss()} | ||
/> | ||
</ModalHeader> | ||
<ModalBody> | ||
<Markdown | ||
content={`Your message could not be submitted because it contains the following errors:\n\n${flattened.join( | ||
"\n", | ||
)}`} | ||
/> | ||
</ModalBody> | ||
<ModalFooter> | ||
<SecondaryButton onClick={() => modal.dismiss()}>Close</SecondaryButton> | ||
</ModalFooter> | ||
</ModalContainer> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// >= v8 | ||
export type DiscordError = { | ||
code: number | ||
message: string | ||
errors?: Record<string, unknown> | ||
} | ||
|
||
export type CodedError = { | ||
code: string | ||
message: string | ||
} |