Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/plenty-crabs-sell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@team-plain/typescript-sdk': minor
---

Add `uiComponent` builder so you can more easily create ui components for use with the API.
61 changes: 61 additions & 0 deletions src/examples/createThread.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { PlainClient } from '../client';
import { ComponentTextColor, ComponentTextSize } from '../graphql/types';
import { uiComponent } from '../ui-components';

export async function createThreadA() {
const client = new PlainClient({ apiKey: 'XXX' });

const res = await client.createThread({
title: 'Contact form',
customerIdentifier: {
customerId: 'c_123',
},
components: [
{
componentText: {
text: 'hello world',
},
},
{
componentText: {
text: 'hello world',
textColor: ComponentTextColor.Error,
},
},
{
componentText: {
text: 'hello world',
textSize: ComponentTextSize.M,
},
},
],
});

if (res.error) {
console.error(res.error);
} else {
console.log(`Thread created with id=${res.data.thread.id}`);
}
}

export async function createThreadB() {
const client = new PlainClient({ apiKey: 'XXX' });

const res = await client.createThread({
title: 'Contact form',
customerIdentifier: {
customerId: 'c_123',
},
components: [
uiComponent.text({ text: 'hello world' }),
uiComponent.text({ text: 'hello world', color: 'ERROR' }),
uiComponent.text({ text: 'hello world', size: 'M' }),
],
});

if (res.error) {
console.error(res.error);
} else {
console.log(`Thread created with id=${res.data.thread.id}`);
}
}
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

export { PlainClient } from './client';

export { uiComponent } from './ui-components';

export {
// Enums
AttachmentType,
Expand Down
29 changes: 29 additions & 0 deletions src/ui-components/badgeComponent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { ComponentBadgeColor, type ComponentInput } from '../graphql/types';

type Color = `${ComponentBadgeColor}`;

function colorToEnum(color?: Color): ComponentBadgeColor | null {
if (!color) {
return null;
}
const map: Record<Color, ComponentBadgeColor> = {
BLUE: ComponentBadgeColor.Blue,
GREEN: ComponentBadgeColor.Green,
GREY: ComponentBadgeColor.Grey,
RED: ComponentBadgeColor.Red,
YELLOW: ComponentBadgeColor.Yellow,
};
return map[color];
}

/**
* Returns a ComponentInput which can be used with the API.
*/
export function badgeComponent(args: { label: string; color?: Color }): ComponentInput {
return {
componentBadge: {
badgeLabel: args.label,
badgeColor: colorToEnum(args.color),
},
};
}
12 changes: 12 additions & 0 deletions src/ui-components/containerComponent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { type ComponentInput } from '../graphql/types';

/**
* Returns a ComponentInput which can be used with the API.
*/
export function containerComponent(args: { content: ComponentInput[] }): ComponentInput {
return {
componentContainer: {
containerContent: args.content,
},
};
}
13 changes: 13 additions & 0 deletions src/ui-components/copyButtonComponent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { type ComponentInput } from '../graphql/types';

/**
* Returns a ComponentInput which can be used with the API.
*/
export function copyButtonComponent(args: { value: string; tooltip?: string }): ComponentInput {
return {
componentCopyButton: {
copyButtonValue: args.value,
copyButtonTooltipLabel: args.tooltip,
},
};
}
28 changes: 28 additions & 0 deletions src/ui-components/dividerComponent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { ComponentDividerSpacingSize, type ComponentInput } from '../graphql/types';

type Size = `${ComponentDividerSpacingSize}`;

function spacingSizeToEnum(size?: Size): ComponentDividerSpacingSize | null {
if (!size) {
return null;
}
const map: Record<Size, ComponentDividerSpacingSize> = {
XL: ComponentDividerSpacingSize.Xl,
L: ComponentDividerSpacingSize.L,
M: ComponentDividerSpacingSize.M,
S: ComponentDividerSpacingSize.S,
XS: ComponentDividerSpacingSize.Xs,
};
return map[size];
}

/**
* Returns a ComponentInput which can be used with the API.
*/
export function dividerComponent(args: { spacingSize?: Size }): ComponentInput {
return {
componentDivider: {
dividerSpacingSize: spacingSizeToEnum(args.spacingSize),
},
};
}
59 changes: 59 additions & 0 deletions src/ui-components/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { describe, expect, test } from 'vitest';

import { ComponentBadgeColor, ComponentTextColor, ComponentTextSize } from '../graphql/types';
import { uiComponent } from '.';

describe('ui components builder', () => {
test('basic example', () => {
expect([
uiComponent.text({ text: 'hello world' }),
uiComponent.text({ text: 'hello world', color: 'MUTED', size: 'S' }),
]).toEqual([
{
componentText: {
text: 'hello world',
textColor: null,
textSize: null,
},
},
{
componentText: {
text: 'hello world',
textColor: ComponentTextColor.Muted,
textSize: ComponentTextSize.S,
},
},
]);
});

test('container component', () => {
expect([
uiComponent.container({
content: [
uiComponent.text({ text: 'hello world' }),
uiComponent.badge({ label: 'success', color: 'GREEN' }),
],
}),
]).toEqual([
{
componentContainer: {
containerContent: [
{
componentText: {
text: 'hello world',
textColor: null,
textSize: null,
},
},
{
componentBadge: {
badgeLabel: 'success',
badgeColor: ComponentBadgeColor.Green,
},
},
],
},
},
]);
});
});
21 changes: 21 additions & 0 deletions src/ui-components/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { badgeComponent } from './badgeComponent';
import { containerComponent } from './containerComponent';
import { copyButtonComponent } from './copyButtonComponent';
import { dividerComponent } from './dividerComponent';
import { linkButtonComponent } from './linkButtonComponent';
import { plainTextComponent } from './plainTextComponent';
import { rowComponent } from './rowComponent';
import { spacerComponent } from './spacerComponent';
import { textComponent } from './textComponent';

export const uiComponent = {
badge: badgeComponent,
container: containerComponent,
copyButton: copyButtonComponent,
divider: dividerComponent,
linkButton: linkButtonComponent,
plainText: plainTextComponent,
row: rowComponent,
spacer: spacerComponent,
text: textComponent,
};
13 changes: 13 additions & 0 deletions src/ui-components/linkButtonComponent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { type ComponentInput } from '../graphql/types';

/**
* Returns a ComponentInput which can be used with the API.
*/
export function linkButtonComponent(args: { label: string; url: string }): ComponentInput {
return {
componentLinkButton: {
linkButtonLabel: args.label,
linkButtonUrl: args.url,
},
};
}
48 changes: 48 additions & 0 deletions src/ui-components/plainTextComponent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import {
type ComponentInput,
ComponentPlainTextColor,
ComponentPlainTextSize,
} from '../graphql/types';

type Size = `${ComponentPlainTextSize}`;

function textSizeToEnum(size?: Size): ComponentPlainTextSize | null {
if (!size) {
return null;
}
return ComponentPlainTextSize[size];
}

type Color = `${ComponentPlainTextColor}`;

function textColorToEnum(color?: Color): ComponentPlainTextColor | null {
if (!color) {
return null;
}

const map: Record<Color, ComponentPlainTextColor> = {
ERROR: ComponentPlainTextColor.Error,
MUTED: ComponentPlainTextColor.Muted,
NORMAL: ComponentPlainTextColor.Normal,
SUCCESS: ComponentPlainTextColor.Success,
WARNING: ComponentPlainTextColor.Warning,
};
return map[color];
}

/**
* Returns a ComponentInput which can be used with the API.
*/
export function plainTextComponent(args: {
text: string;
size?: Size;
color?: Color;
}): ComponentInput {
return {
componentPlainText: {
plainText: args.text,
plainTextColor: textColorToEnum(args.color),
plainTextSize: textSizeToEnum(args.size),
},
};
}
16 changes: 16 additions & 0 deletions src/ui-components/rowComponent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { type ComponentInput } from '../graphql/types';

/**
* Returns a ComponentInput which can be used with the API.
*/
export function rowComponent(args: {
mainContent: ComponentInput[];
asideContent: ComponentInput[];
}): ComponentInput {
return {
componentRow: {
rowMainContent: args.mainContent,
rowAsideContent: args.asideContent,
},
};
}
28 changes: 28 additions & 0 deletions src/ui-components/spacerComponent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { type ComponentInput, ComponentSpacerSize } from '../graphql/types';

type Size = `${ComponentSpacerSize}`;

function spacingSizeToEnum(size?: Size): ComponentSpacerSize | null {
if (!size) {
return null;
}
const map: Record<Size, ComponentSpacerSize> = {
XL: ComponentSpacerSize.Xl,
L: ComponentSpacerSize.L,
M: ComponentSpacerSize.M,
S: ComponentSpacerSize.S,
XS: ComponentSpacerSize.Xs,
};
return map[size];
}

/**
* Returns a ComponentInput which can be used with the API.
*/
export function spacerComponent(args: { spacingSize?: Size }): ComponentInput {
return {
componentSpacer: {
spacerSize: spacingSizeToEnum(args.spacingSize),
},
};
}
40 changes: 40 additions & 0 deletions src/ui-components/textComponent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { type ComponentInput, ComponentTextColor, ComponentTextSize } from '../graphql/types';

type Size = `${ComponentTextSize}`;

function textSizeToEnum(size?: Size): ComponentTextSize | null {
if (!size) {
return null;
}
return ComponentTextSize[size];
}

type Color = `${ComponentTextColor}`;

function textColorToEnum(color?: Color): ComponentTextColor | null {
if (!color) {
return null;
}

const map: Record<Color, ComponentTextColor> = {
ERROR: ComponentTextColor.Error,
MUTED: ComponentTextColor.Muted,
NORMAL: ComponentTextColor.Normal,
SUCCESS: ComponentTextColor.Success,
WARNING: ComponentTextColor.Warning,
};
return map[color];
}

/**
* Returns a ComponentInput which can be used with the API.
*/
export function textComponent(args: { text: string; size?: Size; color?: Color }): ComponentInput {
return {
componentText: {
text: args.text,
textColor: textColorToEnum(args.color),
textSize: textSizeToEnum(args.size),
},
};
}