Skip to content

Commit 14870b8

Browse files
committed
refactor: Removed default renderers from template context
1 parent b9f17da commit 14870b8

File tree

7 files changed

+54
-80
lines changed

7 files changed

+54
-80
lines changed

src/components/chat/chat-input.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -310,10 +310,7 @@ export default class IgcChatInputComponent extends LitElement {
310310
}
311311

312312
private _renderActionsArea() {
313-
const ctx: ChatRendererContext = {
314-
defaults: this._defaults,
315-
instance: this._state.host,
316-
};
313+
const ctx: ChatRendererContext = { instance: this._state.host };
317314

318315
return html`
319316
${this._getRenderer('fileUploadButton')(ctx)}
@@ -322,10 +319,7 @@ export default class IgcChatInputComponent extends LitElement {
322319
}
323320

324321
protected override render() {
325-
const ctx: ChatRendererContext = {
326-
defaults: this._defaults,
327-
instance: this._state.host,
328-
};
322+
const ctx: ChatRendererContext = { instance: this._state.host };
329323

330324
const inputCtx: InputRendererContext = {
331325
...ctx,

src/components/chat/chat-message.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,6 @@ export default class IgcChatMessageComponent extends LitElement {
208208
protected override render() {
209209
const ctx: MessageRendererContext = {
210210
message: this.message!,
211-
defaults: this._defaults,
212211
instance: this._state.host,
213212
};
214213

src/components/chat/chat.spec.ts

Lines changed: 46 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
fixture,
66
nextFrame,
77
} from '@open-wc/testing';
8-
import { html } from 'lit';
8+
import { html, nothing } from 'lit';
99
import { type SinonFakeTimers, spy, useFakeTimers } from 'sinon';
1010
import type IgcIconButtonComponent from '../button/icon-button.js';
1111
import {
@@ -26,6 +26,7 @@ import {
2626
import { simulateFileUpload } from '../file-input/file-input.spec.js';
2727
import IgcInputComponent from '../input/input.js';
2828
import IgcChatComponent from './chat.js';
29+
import type { IgcMessage, IgcMessageAttachment } from './types.js';
2930

3031
describe('Chat', () => {
3132
before(() => {
@@ -43,18 +44,22 @@ describe('Chat', () => {
4344
};
4445

4546
const messageTemplate = (msg: any) => {
46-
return html`<div>
47-
<h5>${msg.sender === 'user' ? 'You' : 'Bot'}:</h5>
48-
<p>${msg.text}</p>
49-
</div> `;
47+
return html`
48+
<div>
49+
<h5>${msg.sender === 'user' ? 'You' : 'Bot'}:</h5>
50+
<p>${msg.text}</p>
51+
</div>
52+
`;
5053
};
5154

52-
const messageActionsTemplate = (msg: any) => {
55+
const messageActionsTemplate = (msg: IgcMessage) => {
5356
return msg.sender !== 'user' && msg.text.trim()
54-
? html`<div style="float: right">
55-
<igc-button name="regenerate" variant="flat">...</igc-button>
56-
</div> `
57-
: html``;
57+
? html`
58+
<div style="float: right">
59+
<igc-button name="regenerate" variant="flat">...</igc-button>
60+
</div>
61+
`
62+
: nothing;
5863
};
5964

6065
const typingIndicatorTemplate = html`<span>loading...</span>`;
@@ -68,51 +73,51 @@ describe('Chat', () => {
6873
};
6974

7075
const attachmentContentTemplate = (attachment: any) => {
71-
return html`<p>
72-
This is a template rendered as content of ${attachment.name}
73-
</p>`;
76+
return html`
77+
<p>This is a template rendered as content of ${attachment.name}</p>
78+
`;
7479
};
7580

76-
const textInputTemplate = (text: string) =>
77-
html`<igc-input
78-
placeholder="Type text here..."
79-
.value=${text}
80-
></igc-input>`;
81+
const textInputTemplate = (text: string) => html`
82+
<igc-input placeholder="Type text here..." .value=${text}></igc-input>
83+
`;
8184

82-
const textAreaActionsTemplate = (ctx: any) =>
83-
html`<div>
84-
${ctx.defaults.fileUploadButton(ctx)}
85+
const textAreaActionsTemplate = () => html`
86+
<div>
8587
<igc-button>Upload</igc-button>
8688
<igc-button>Send</igc-button>
87-
</div>`;
88-
89-
const textAreaAttachmentsTemplate = (attachments: any[]) => {
90-
return html`<div>
91-
${attachments.map(
92-
(attachment) =>
93-
html`<a
94-
href=${attachment.file
95-
? URL.createObjectURL(attachment.file)
96-
: attachment.url}
97-
target="_blank"
98-
>${attachment.name}</a
99-
>`
100-
)}
101-
</div>`;
89+
</div>
90+
`;
91+
92+
const textAreaAttachmentsTemplate = (attachments: IgcMessageAttachment[]) => {
93+
return html`
94+
<div>
95+
${attachments.map(
96+
(attachment) => html`
97+
<a
98+
href=${attachment.file
99+
? URL.createObjectURL(attachment.file)
100+
: (attachment.url ?? '')}
101+
target="_blank"
102+
>
103+
${attachment.name}
104+
</a>
105+
`
106+
)}
107+
</div>
108+
`;
102109
};
103110

104-
const messages: any[] = [
111+
const messages: IgcMessage[] = [
105112
{
106113
id: '1',
107114
text: 'Hello! How can I help you today?',
108115
sender: 'bot',
109-
timestamp: new Date(Date.now() - 3600000),
110116
},
111117
{
112118
id: '2',
113119
text: 'Hello!',
114120
sender: 'user',
115-
timestamp: new Date(Date.now() - 3500000),
116121
attachments: [
117122
{
118123
id: 'img1',
@@ -126,7 +131,6 @@ describe('Chat', () => {
126131
id: '3',
127132
text: 'Thank you!',
128133
sender: 'bot',
129-
timestamp: new Date(Date.now() - 3400000),
130134
attachments: [
131135
{
132136
id: 'file1',
@@ -140,7 +144,6 @@ describe('Chat', () => {
140144
id: '4',
141145
text: 'Thank you too!',
142146
sender: 'user',
143-
timestamp: new Date(Date.now() - 3300000),
144147
},
145148
];
146149

@@ -333,7 +336,6 @@ describe('Chat', () => {
333336
id: '1',
334337
text: '<script>alert("XSS")</script> Hello!',
335338
sender: 'bot',
336-
timestamp: new Date(Date.now() - 3600000),
337339
},
338340
];
339341
chat = await fixture<IgcChatComponent>(
@@ -1176,7 +1178,7 @@ describe('Chat', () => {
11761178
chat.options = {
11771179
renderers: {
11781180
input: (ctx) => textInputTemplate(ctx.value),
1179-
inputActions: (ctx) => textAreaActionsTemplate(ctx),
1181+
inputActions: () => textAreaActionsTemplate(),
11801182
inputAttachments: (ctx) =>
11811183
textAreaAttachmentsTemplate(ctx.attachments),
11821184
},
@@ -1198,25 +1200,6 @@ describe('Chat', () => {
11981200
</div>
11991201
<div part="buttons-container">
12001202
<div>
1201-
<label
1202-
for="input_attachments"
1203-
part="upload-button"
1204-
>
1205-
<igc-icon-button
1206-
aria-label="Attach files"
1207-
name="attach_file"
1208-
type="button"
1209-
variant="flat"
1210-
>
1211-
</igc-icon-button>
1212-
<input
1213-
id="input_attachments"
1214-
aria-label="Upload button"
1215-
multiple=""
1216-
name="input_attachments"
1217-
type="file"
1218-
>
1219-
</label>
12201203
<igc-button type="button" variant="contained">Upload</igc-button>
12211204
<igc-button type="button" variant="contained">Send</igc-button>
12221205
</div>
@@ -1631,7 +1614,7 @@ describe('Chat', () => {
16311614

16321615
simulateClick(attachmentHeader);
16331616
expect(eventSpy).calledWith('igcAttachmentClick', {
1634-
detail: { ...messages[1].attachments[0] },
1617+
detail: { ...messages[1].attachments?.at(0) },
16351618
});
16361619
});
16371620

src/components/chat/chat.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -382,10 +382,7 @@ export default class IgcChatComponent extends EventEmitterMixin<
382382
private _renderSuggestions() {
383383
const hasContent = this._slots.hasAssignedElements('suggestions-header');
384384
const suggestions = this._state.options?.suggestions ?? [];
385-
const ctx = {
386-
defaults: this._defaults,
387-
instance: this,
388-
};
385+
const ctx = { instance: this };
389386

390387
return html`
391388
<div part="suggestions-container">

src/components/chat/extras/markdown-renderer.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,11 @@ export async function createMarkdownRenderer(
6969
markdown.use(
7070
markedShiki({
7171
highlight(code, lang, _) {
72-
return highlighter.codeToHtml(code, { lang, themes });
72+
try {
73+
return highlighter.codeToHtml(code, { lang, themes });
74+
} catch {
75+
return `<pre><code>${sanitizer(code)}</code></pre>`;
76+
}
7377
},
7478
})
7579
);

src/components/chat/message-attachments.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,6 @@ export default class IgcMessageAttachmentsComponent extends LitElement {
155155
const ctx: AttachmentRendererContext = {
156156
attachment,
157157
message: this.message!,
158-
defaults: this._defaults,
159158
instance: this._state.host,
160159
};
161160

@@ -195,7 +194,6 @@ export default class IgcMessageAttachmentsComponent extends LitElement {
195194
this._getRenderer('attachment')({
196195
attachment,
197196
message: this.message!,
198-
defaults: this._defaults,
199197
instance: this._state.host,
200198
})
201199
)}

src/components/chat/types.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,6 @@ export type ChatRenderers = {
150150
export type ChatTemplateRenderer<T> = (ctx: T) => unknown;
151151

152152
export interface ChatRendererContext {
153-
defaults: ChatRenderers;
154153
instance: IgcChatComponent;
155154
}
156155

0 commit comments

Comments
 (0)