|
| 1 | +import React, { useEffect, useState } from 'react'; |
| 2 | +import { AISettingsModel } from '../models/settings-model'; |
| 3 | +import { ReactWidget } from '@jupyterlab/ui-components'; |
| 4 | +import { jupyternautIcon } from '../icons'; |
| 5 | + |
| 6 | +const STATUS_DISABLED_CLASS = 'jp-ai-status-completion-disabled'; |
| 7 | + |
| 8 | +/** |
| 9 | + * The completion status props. |
| 10 | + */ |
| 11 | +interface ICompletionStatusProps { |
| 12 | + /** |
| 13 | + * The settings model. |
| 14 | + */ |
| 15 | + settingsModel: AISettingsModel; |
| 16 | +} |
| 17 | + |
| 18 | +/** |
| 19 | + * The completion status component. |
| 20 | + */ |
| 21 | +function CompletionStatus(props: ICompletionStatusProps): JSX.Element { |
| 22 | + const [disabled, setDisabled] = useState<boolean>(true); |
| 23 | + const [title, setTitle] = useState<string>(''); |
| 24 | + |
| 25 | + /** |
| 26 | + * Handle changes in the settings. |
| 27 | + */ |
| 28 | + useEffect(() => { |
| 29 | + const stateChanged = (model: AISettingsModel) => { |
| 30 | + if (model.config.useSameProviderForChatAndCompleter) { |
| 31 | + setDisabled(false); |
| 32 | + setTitle(`Completion using ${model.getDefaultProvider()?.model}`); |
| 33 | + } else if (model.config.activeCompleterProvider) { |
| 34 | + setDisabled(false); |
| 35 | + setTitle( |
| 36 | + `Completion using ${model.getProvider(model.config.activeCompleterProvider)?.model}` |
| 37 | + ); |
| 38 | + } else { |
| 39 | + setDisabled(true); |
| 40 | + setTitle('No completion'); |
| 41 | + } |
| 42 | + }; |
| 43 | + |
| 44 | + props.settingsModel.stateChanged.connect(stateChanged); |
| 45 | + |
| 46 | + stateChanged(props.settingsModel); |
| 47 | + return () => { |
| 48 | + props.settingsModel.stateChanged.disconnect(stateChanged); |
| 49 | + }; |
| 50 | + }, [props.settingsModel]); |
| 51 | + |
| 52 | + return ( |
| 53 | + <jupyternautIcon.react |
| 54 | + className={disabled ? STATUS_DISABLED_CLASS : ''} |
| 55 | + top={'2px'} |
| 56 | + width={'16px'} |
| 57 | + stylesheet={'statusBar'} |
| 58 | + title={title} |
| 59 | + /> |
| 60 | + ); |
| 61 | +} |
| 62 | + |
| 63 | +/** |
| 64 | + * The completion status widget that will be added to the status bar. |
| 65 | + */ |
| 66 | +export class CompletionStatusWidget extends ReactWidget { |
| 67 | + constructor(options: ICompletionStatusProps) { |
| 68 | + super(); |
| 69 | + this._props = options; |
| 70 | + } |
| 71 | + |
| 72 | + render(): JSX.Element { |
| 73 | + return <CompletionStatus {...this._props} />; |
| 74 | + } |
| 75 | + |
| 76 | + private _props: ICompletionStatusProps; |
| 77 | +} |
0 commit comments