Skip to content

Commit 043bc38

Browse files
authored
Add a configChanged signal to the model (#68)
1 parent 2f4a533 commit 043bc38

File tree

4 files changed

+36
-11
lines changed

4 files changed

+36
-11
lines changed

packages/jupyter-chat/src/components/chat-input.tsx

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,27 @@ import {
1616
} from '@mui/material';
1717
import { Send, Cancel } from '@mui/icons-material';
1818
import clsx from 'clsx';
19-
import { AutocompleteCommand, IAutocompletionCommandsProps } from '../types';
19+
import { IChatModel } from '../model';
2020
import { IAutocompletionRegistry } from '../registry';
21+
import { AutocompleteCommand, IAutocompletionCommandsProps } from '../types';
2122

2223
const INPUT_BOX_CLASS = 'jp-chat-input-container';
2324
const SEND_BUTTON_CLASS = 'jp-chat-send-button';
2425
const CANCEL_BUTTON_CLASS = 'jp-chat-cancel-button';
2526

2627
export function ChatInput(props: ChatInput.IProps): JSX.Element {
27-
const { autocompletionName, autocompletionRegistry, sendWithShiftEnter } =
28-
props;
28+
const { autocompletionName, autocompletionRegistry, model } = props;
2929
const autocompletion = useRef<IAutocompletionCommandsProps>();
3030
const [input, setInput] = useState<string>(props.value || '');
31+
const [sendWithShiftEnter, setSendWithShiftEnter] = useState<boolean>(
32+
model.config.sendWithShiftEnter ?? false
33+
);
34+
35+
useEffect(() => {
36+
model.configChanged.connect((_, config) => {
37+
setSendWithShiftEnter(config.sendWithShiftEnter ?? false);
38+
});
39+
}, [model]);
3140

3241
// The autocomplete commands options.
3342
const [commandOptions, setCommandOptions] = useState<AutocompleteCommand[]>(
@@ -124,7 +133,7 @@ export function ChatInput(props: ChatInput.IProps): JSX.Element {
124133
}
125134

126135
// Set the helper text based on whether Shift+Enter is used for sending.
127-
const helperText = props.sendWithShiftEnter ? (
136+
const helperText = sendWithShiftEnter ? (
128137
<span>
129138
Press <b>Shift</b>+<b>Enter</b> to send message
130139
</span>
@@ -248,6 +257,10 @@ export namespace ChatInput {
248257
* The properties of the react element.
249258
*/
250259
export interface IProps {
260+
/**
261+
* The chat model.
262+
*/
263+
model: IChatModel;
251264
/**
252265
* The initial value of the input (default to '')
253266
*/
@@ -260,10 +273,6 @@ export namespace ChatInput {
260273
* The function to be called to cancel editing.
261274
*/
262275
onCancel?: () => unknown;
263-
/**
264-
* Whether using shift+enter to send the message.
265-
*/
266-
sendWithShiftEnter: boolean;
267276
/**
268277
* Custom mui/material styles.
269278
*/

packages/jupyter-chat/src/components/chat-messages.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ export function ChatMessages(props: BaseMessageProps): JSX.Element {
7474
* Effect: listen to chat messages.
7575
*/
7676
useEffect(() => {
77-
function handleChatEvents(_: IChatModel) {
77+
function handleChatEvents() {
7878
setMessages([...model.messages]);
7979
}
8080

@@ -392,7 +392,7 @@ export function ChatMessage(props: ChatMessageProps): JSX.Element {
392392
value={message.body}
393393
onSend={(input: string) => updateMessage(message.id, input)}
394394
onCancel={() => cancelEdition()}
395-
sendWithShiftEnter={model.config.sendWithShiftEnter ?? false}
395+
model={model}
396396
/>
397397
) : (
398398
<RendermimeMarkdown

packages/jupyter-chat/src/components/chat.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export function ChatBody(props: Chat.IChatBodyProps): JSX.Element {
4242
paddingBottom: 0,
4343
borderTop: '1px solid var(--jp-border-color1)'
4444
}}
45-
sendWithShiftEnter={model.config.sendWithShiftEnter ?? false}
45+
model={model}
4646
autocompletionRegistry={autocompletionRegistry}
4747
/>
4848
</>

packages/jupyter-chat/src/model.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ export interface IChatModel extends IDisposable {
5454
*/
5555
readonly messagesUpdated: ISignal<IChatModel, void>;
5656

57+
/**
58+
* A signal emitting when the messages list is updated.
59+
*/
60+
get configChanged(): ISignal<IChatModel, IConfig>;
61+
5762
/**
5863
* A signal emitting when unread messages change.
5964
*/
@@ -215,6 +220,9 @@ export class ChatModel implements IChatModel {
215220

216221
this._config = { ...this._config, ...value };
217222

223+
this._configChanged.emit(this._config);
224+
225+
// Update the stacked status of the messages and the view.
218226
if (stackMessagesChanged) {
219227
if (this._config.stackMessages) {
220228
this._messages.slice(1).forEach((message, idx) => {
@@ -288,6 +296,13 @@ export class ChatModel implements IChatModel {
288296
return this._messagesUpdated;
289297
}
290298

299+
/**
300+
* A signal emitting when the messages list is updated.
301+
*/
302+
get configChanged(): ISignal<IChatModel, IConfig> {
303+
return this._configChanged;
304+
}
305+
291306
/**
292307
* A signal emitting when unread messages change.
293308
*/
@@ -468,6 +483,7 @@ export class ChatModel implements IChatModel {
468483
private _commands?: CommandRegistry;
469484
private _notificationId: string | null = null;
470485
private _messagesUpdated = new Signal<IChatModel, void>(this);
486+
private _configChanged = new Signal<IChatModel, IConfig>(this);
471487
private _unreadChanged = new Signal<IChatModel, number[]>(this);
472488
private _viewportChanged = new Signal<IChatModel, number[]>(this);
473489
}

0 commit comments

Comments
 (0)