Skip to content

Commit

Permalink
feat: render description on Discord Webhooks
Browse files Browse the repository at this point in the history
  • Loading branch information
casperiv0 committed Jun 11, 2023
1 parent 14ef8d8 commit 016471a
Show file tree
Hide file tree
Showing 13 changed files with 105 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import { handleEndCall } from "lib/dispatch/911-calls/handle-end-911-call";
import { AuditLogActionType, createAuditLogEntry } from "@snailycad/audit-logger/server";
import { isFeatureEnabled } from "lib/upsert-cad";
import { _leoProperties, unitProperties } from "utils/leo/includes";
import { slateDataToString, type Descendant } from "@snailycad/utils/editor";

export const callInclude = Prisma.validator<Prisma.Call911Select>()({
position: true,
Expand Down Expand Up @@ -637,10 +638,11 @@ export class Calls911Controller {
locale?: string | null,
): Promise<{ embeds: APIEmbed[] }> {
const t = await getTranslator({ type: "webhooks", locale, namespace: "Calls" });
const formattedDescription = slateDataToString(call.descriptionData as Descendant[] | null);

const caller = call.name || t("unknown");
const location = `${call.location} ${call.postal ? call.postal : ""}`;
const description = call.description || t("couldNotRenderDescription");
const description = call.description || formattedDescription || t("couldNotRenderDescription");

return {
embeds: [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { DEFAULT_EDITOR_DATA } from "components/editor/editor";
import { HoverCard } from "components/shared/HoverCard";
import { classNames } from "lib/classNames";
import { dataToString } from "lib/editor/dataToString";
import { useTranslations } from "next-intl";
import type { Descendant } from "slate";
import dynamic from "next/dynamic";
import { slateDataToString } from "@snailycad/utils/editor";

const Editor = dynamic(async () => (await import("components/editor/editor")).Editor, {
ssr: false,
Expand All @@ -19,7 +19,7 @@ export function CallDescription({ data, nonCard }: Props) {
const common = useTranslations("Common");

const stringDescription =
dataToString(data.descriptionData as Descendant[] | null) || data.description;
slateDataToString(data.descriptionData as Descendant[] | null) || data.description;

if (!stringDescription) {
return <>{common("none")}</>;
Expand Down
11 changes: 4 additions & 7 deletions apps/client/src/components/editor/editor.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { SlateEditor, SlateElements, Text, slateDataToString } from "@snailycad/utils/editor";
import * as React from "react";
import { BaseEditor, Editor as _Editor, Node as SlateNode, Descendant, createEditor } from "slate";
import { Editor as _Editor, Node as SlateNode, Descendant, createEditor } from "slate";
import {
Editable,
ReactEditor,
Expand All @@ -8,21 +9,17 @@ import {
Slate,
withReact,
} from "slate-react";
import { type HistoryEditor, withHistory } from "slate-history";
import { withHistory } from "slate-history";
import { Toolbar } from "./toolbar";
import { toggleMark } from "lib/editor/utils";
import isHotkey from "is-hotkey";
import { SHORTCUTS, withShortcuts } from "lib/editor/withShortcuts";
import { withChecklists } from "lib/editor/withChecklists";
import type { SlateElements, Text } from "./types";
import { classNames } from "lib/classNames";
import { dataToString } from "lib/editor/dataToString";
import { useTranslations } from "use-intl";
import { EditorElement } from "./elements/element";
import { EditorLeaf } from "./elements/leaf";

export type SlateEditor = BaseEditor & ReactEditor & HistoryEditor;

declare module "slate" {
interface CustomTypes {
Editor: SlateEditor;
Expand Down Expand Up @@ -67,7 +64,7 @@ export function Editor(props: EditorProps) {
[],
);
const isEmpty = React.useMemo(() => {
return dataToString(props.value)?.trim() === "";
return slateDataToString(props.value)?.trim() === "";
}, [props.value]);

function handleChange(value: Descendant[]) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { classNames } from "lib/classNames";
import { Transforms } from "slate";
import { ReactEditor, useReadOnly, useSlateStatic, type RenderElementProps } from "slate-react";
import type { CheckListItemElement as ICheckListItemElement } from "../types";
import { CheckListItemElement as ICheckListItemElement } from "@snailycad/utils/editor";

type Props = RenderElementProps & { element: ICheckListItemElement };

Expand Down
2 changes: 1 addition & 1 deletion apps/client/src/components/editor/toolbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import { useSlate } from "slate-react";
import { Button } from "@snailycad/ui";
import { classNames } from "lib/classNames";
import { isBlockActive, toggleMark, toggleBlock, isMarkActive } from "lib/editor/utils";
import type { SlateElements, Text } from "./types";
import { SelectColorPopover } from "./toolbar/select-color-popover";
import { SlateElements, Text } from "@snailycad/utils/editor";

/**
* mostly example code from: https://github.com/ianstormtaylor/slate/blob/main/site/examples/richtext.tsx
Expand Down
3 changes: 1 addition & 2 deletions apps/client/src/lib/editor/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { SlateEditor } from "components/editor/editor";
import type { Text } from "components/editor/types";
import { SlateEditor, Text } from "@snailycad/utils/editor";
import { Editor, Transforms, Element as SlateElement } from "slate";

const LIST_TYPES = ["numbered-list", "bulleted-list"];
Expand Down
2 changes: 1 addition & 1 deletion apps/client/src/lib/editor/withChecklists.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { SlateEditor } from "@snailycad/utils/editor";
import { Editor, Transforms, Range, Point, Element as SlateElement } from "slate";
import type { SlateEditor } from "components/editor/editor";

export function withChecklists(editor: SlateEditor) {
const { deleteBackward } = editor;
Expand Down
2 changes: 1 addition & 1 deletion apps/client/src/lib/editor/withShortcuts.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { SlateEditor } from "@snailycad/utils/editor";
import { Editor, Transforms, Range, Point, Element as SlateElement } from "slate";
import type { SlateEditor } from "components/editor/editor";

type SHORTCUTS = (typeof SHORTCUTS)[keyof typeof SHORTCUTS];
export const SHORTCUTS = {
Expand Down
11 changes: 9 additions & 2 deletions packages/utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
"require": "./dist/case-number.js",
"import": "./dist/case-number.mjs"
},
"./editor": {
"require": "./dist/editor/index.js",
"import": "./dist/editor/index.mjs"
},
"./package.json": "./package.json"
},
"files": [
Expand All @@ -50,6 +54,9 @@
],
"case-number": [
"dist/case-number.d.ts"
],
"editor": [
"dist/editor/index.d.ts"
]
}
},
Expand All @@ -71,7 +78,7 @@
},
"tsup": {
"entry": [
"src/**/*.ts"
"src/**/**/*.ts"
],
"dts": true,
"bundle": false,
Expand All @@ -85,4 +92,4 @@
]
},
"sideEffects": false
}
}
17 changes: 17 additions & 0 deletions packages/utils/src/editor/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import type { SlateElements, Text } from "./types";
import type { BaseEditor } from "slate";
import type { ReactEditor } from "slate-react";
import type { HistoryEditor } from "slate-history";

export type SlateEditor = BaseEditor & ReactEditor & HistoryEditor;

declare module "slate" {
interface CustomTypes {
Element: SlateElements;
Text: Text;
}
}

export * from "./types";
export { slateDataToString } from "./slate-data-to-string";
export { Descendant } from "slate";
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Editor, Element as SlateElement, Descendant } from "slate";

export function dataToString(data: Descendant[] | null) {
export function slateDataToString(data: Descendant[] | null) {
const string: string[] = [];
if (!data) return null;

Expand Down
File renamed without changes.
64 changes: 64 additions & 0 deletions packages/utils/tests/editor.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/* eslint-disable quotes */
import { describe, expect, test } from "vitest";
import { slateDataToString } from "../src/editor/slate-data-to-string";
import { Descendant } from "../src/editor";

const TEST_VALUE = [
{
type: "paragraph",
children: [
{
text: "Hello ",
},
{
bold: true,
text: "world - this is a test",
},
],
},
{
type: "bulleted-list",
children: [
{
type: "list-item",
children: [
{
text: "hello world",
},
],
},
{
type: "list-item",
children: [
{
text: "testing",
},
],
},
{
type: "list-item",
children: [
{
text: "",
},
],
},
{
type: "list-item",
children: [
{
text: "line breaks",
},
],
},
],
},
] satisfies Descendant[];

describe("slateDataToString", () => {
test("should return a single line string", () => {
expect(slateDataToString(TEST_VALUE)).toMatchInlineSnapshot(
'"Hello world - this is a test hello world testing line breaks"',
);
});
});

0 comments on commit 016471a

Please sign in to comment.