Skip to content

Commit 4ba2123

Browse files
committed
Lift the typing indicator out of the input component
1 parent 7a046de commit 4ba2123

File tree

5 files changed

+45
-33
lines changed

5 files changed

+45
-33
lines changed

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

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import ArrowBackIcon from '@mui/icons-material/ArrowBack';
99
import SettingsIcon from '@mui/icons-material/Settings';
1010
import { IconButton } from '@mui/material';
1111
import { Box } from '@mui/system';
12-
import React, { useState } from 'react';
12+
import React, { useEffect, useState } from 'react';
1313

1414
import {
1515
ChatInput,
@@ -18,6 +18,7 @@ import {
1818
} from './input';
1919
import { JlThemeProvider } from './jl-theme-provider';
2020
import { ChatMessages } from './messages';
21+
import { WritingIndicator } from './writing-indicator';
2122
import { AttachmentOpenerContext } from '../context';
2223
import { IChatModel } from '../model';
2324
import {
@@ -29,10 +30,36 @@ import { ChatArea } from '../types';
2930

3031
export function ChatBody(props: Chat.IChatBodyProps): JSX.Element {
3132
const { model } = props;
33+
const [writers, setWriters] = useState<IChatModel.IWriter[]>([]);
3234
let { inputToolbarRegistry } = props;
3335
if (!inputToolbarRegistry) {
3436
inputToolbarRegistry = InputToolbarRegistry.defaultToolbarRegistry();
3537
}
38+
39+
/**
40+
* Handle the changes in the writers list.
41+
*/
42+
useEffect(() => {
43+
if (!model) {
44+
return;
45+
}
46+
47+
const updateWriters = (_: IChatModel, writers: IChatModel.IWriter[]) => {
48+
// Show all writers for now - AI generating responses will have messageID
49+
setWriters(writers);
50+
};
51+
52+
// Set initial writers state
53+
const initialWriters = model.writers;
54+
setWriters(initialWriters);
55+
56+
model.writersChanged?.connect(updateWriters);
57+
58+
return () => {
59+
model?.writersChanged?.disconnect(updateWriters);
60+
};
61+
}, [model]);
62+
3663
// const horizontalPadding = props.area === 'main' ? 8 : 4;
3764
const horizontalPadding = 4;
3865

@@ -60,6 +87,13 @@ export function ChatBody(props: Chat.IChatBodyProps): JSX.Element {
6087
area={props.area}
6188
chatModel={model}
6289
/>
90+
<WritingIndicator
91+
sx={{
92+
paddingLeft: horizontalPadding,
93+
paddingRight: horizontalPadding
94+
}}
95+
writers={writers}
96+
/>
6397
</AttachmentOpenerContext.Provider>
6498
);
6599
}

packages/jupyter-chat/src/components/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ export * from './input';
1010
export * from './jl-theme-provider';
1111
export * from './messages';
1212
export * from './scroll-container';
13+
export * from './writing-indicator';

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

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import { IInputModel, InputModel } from '../../input-model';
2424
import { IChatCommandRegistry } from '../../registers';
2525
import { IAttachment, ChatArea } from '../../types';
2626
import { IChatModel } from '../../model';
27-
import { InputWritingIndicator } from './writing-indicator';
2827

2928
const INPUT_BOX_CLASS = 'jp-chat-input-container';
3029
const INPUT_TEXTFIELD_CLASS = 'jp-chat-input-textfield';
@@ -47,7 +46,6 @@ export function ChatInput(props: ChatInput.IProps): JSX.Element {
4746
InputToolbarRegistry.IToolbarItem[]
4847
>([]);
4948
const [isFocused, setIsFocused] = useState<boolean>(false);
50-
const [writers, setWriters] = useState<IChatModel.IWriter[]>([]);
5149

5250
/**
5351
* Auto-focus the input when the component is first mounted.
@@ -110,30 +108,6 @@ export function ChatInput(props: ChatInput.IProps): JSX.Element {
110108
};
111109
}, [toolbarRegistry]);
112110

113-
/**
114-
* Handle the changes in the writers list.
115-
*/
116-
useEffect(() => {
117-
if (!props.chatModel) {
118-
return;
119-
}
120-
121-
const updateWriters = (_: IChatModel, writers: IChatModel.IWriter[]) => {
122-
// Show all writers for now - AI generating responses will have messageID
123-
setWriters(writers);
124-
};
125-
126-
// Set initial writers state
127-
const initialWriters = props.chatModel.writers;
128-
setWriters(initialWriters);
129-
130-
props.chatModel.writersChanged?.connect(updateWriters);
131-
132-
return () => {
133-
props.chatModel?.writersChanged?.disconnect(updateWriters);
134-
};
135-
}, [props.chatModel]);
136-
137111
const inputExists = !!input.trim();
138112

139113
/**
@@ -340,7 +314,6 @@ export function ChatInput(props: ChatInput.IProps): JSX.Element {
340314
))}
341315
</Box>
342316
</Box>
343-
<InputWritingIndicator writers={writers} />
344317
</Box>
345318
);
346319
}

packages/jupyter-chat/src/components/input/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,3 @@ export * from './buttons';
77
export * from './chat-input';
88
export * from './toolbar-registry';
99
export * from './use-chat-commands';
10-
export * from './writing-indicator';

packages/jupyter-chat/src/components/input/writing-indicator.tsx renamed to packages/jupyter-chat/src/components/writing-indicator.tsx

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
* Distributed under the terms of the Modified BSD License.
44
*/
55

6-
import { Box, Typography } from '@mui/material';
6+
import { Box, SxProps, Theme, Typography } from '@mui/material';
77
import React from 'react';
88

9-
import { IChatModel } from '../../model';
9+
import { IChatModel } from '../model';
1010

1111
/**
1212
* Classname on the root element. Used in E2E tests.
@@ -21,6 +21,10 @@ export interface IInputWritingIndicatorProps {
2121
* The list of users currently writing.
2222
*/
2323
writers: IChatModel.IWriter[];
24+
/**
25+
* Custom mui/material styles.
26+
*/
27+
sx?: SxProps<Theme>;
2428
}
2529

2630
/**
@@ -48,9 +52,9 @@ function formatWritersText(writers: IChatModel.IWriter[]): string {
4852
}
4953

5054
/**
51-
* The input writing indicator component, displaying typing status in the chat input area.
55+
* The writing indicator component, displaying typing status.
5256
*/
53-
export function InputWritingIndicator(
57+
export function WritingIndicator(
5458
props: IInputWritingIndicatorProps
5559
): JSX.Element {
5660
const { writers } = props;
@@ -62,6 +66,7 @@ export function InputWritingIndicator(
6266
<Box
6367
className={WRITERS_ELEMENT_CLASSNAME}
6468
sx={{
69+
...props.sx,
6570
minHeight: '16px'
6671
}}
6772
>

0 commit comments

Comments
 (0)