|
1 | | -import { UsersItem } from '@jupyter/collaboration'; |
2 | | -import { IJGISExternalCommand, JupyterGISModel } from '@jupytergis/schema'; |
| 1 | +import { UsersItem, DefaultIconRenderer } from '@jupyter/collaboration'; |
| 2 | +import { |
| 3 | + IUserData, |
| 4 | + IJGISExternalCommand, |
| 5 | + JupyterGISModel, |
| 6 | +} from '@jupytergis/schema'; |
3 | 7 | import { CommandToolbarButton } from '@jupyterlab/apputils'; |
4 | 8 | import { |
5 | 9 | MenuSvg, |
@@ -32,10 +36,41 @@ export class Separator extends Widget { |
32 | 36 | } |
33 | 37 | } |
34 | 38 |
|
| 39 | +function createUserIconRenderer(model: JupyterGISModel) { |
| 40 | + let selectedUserId: number | undefined; |
| 41 | + |
| 42 | + return (props: { user: IUserData }): JSX.Element => { |
| 43 | + const { user } = props; |
| 44 | + const isSelected = user.userId === selectedUserId; |
| 45 | + const className = isSelected ? 'selected' : ''; |
| 46 | + |
| 47 | + const onClick = () => { |
| 48 | + if (user.userId === selectedUserId) { |
| 49 | + selectedUserId = undefined; |
| 50 | + model.setUserToFollow(undefined); |
| 51 | + } else { |
| 52 | + selectedUserId = user.userId; |
| 53 | + model.setUserToFollow(user.userId); |
| 54 | + } |
| 55 | + }; |
| 56 | + |
| 57 | + return ( |
| 58 | + <DefaultIconRenderer |
| 59 | + user={user} |
| 60 | + onClick={onClick} |
| 61 | + className={className} |
| 62 | + /> |
| 63 | + ); |
| 64 | + }; |
| 65 | +} |
| 66 | + |
35 | 67 | export class ToolbarWidget extends ReactiveToolbar { |
| 68 | + private _model: JupyterGISModel; |
| 69 | + |
36 | 70 | constructor(options: ToolbarWidget.IOptions) { |
37 | 71 | super(); |
38 | 72 |
|
| 73 | + this._model = options.model; |
39 | 74 | this.addClass('jGIS-toolbar-widget'); |
40 | 75 |
|
41 | 76 | if (options.commands) { |
@@ -138,9 +173,12 @@ export class ToolbarWidget extends ReactiveToolbar { |
138 | 173 | this.addItem('spacer', ReactiveToolbar.createSpacerItem()); |
139 | 174 |
|
140 | 175 | // Users |
| 176 | + const iconRenderer = createUserIconRenderer(this._model); |
141 | 177 | this.addItem( |
142 | 178 | 'users', |
143 | | - ReactWidget.create(<UsersItem model={options.model} />), |
| 179 | + ReactWidget.create( |
| 180 | + <UsersItem model={this._model} iconRenderer={iconRenderer} />, |
| 181 | + ), |
144 | 182 | ); |
145 | 183 | } |
146 | 184 | } |
|
0 commit comments