|
1 | 1 | import { Dialog, showDialog, UseSignal } from '@jupyterlab/apputils';
|
2 | 2 | import { PathExt } from '@jupyterlab/coreutils';
|
| 3 | +import { FileDialog, FileBrowserModel } from '@jupyterlab/filebrowser'; |
| 4 | +import { DefaultIconReact } from '@jupyterlab/ui-components'; |
3 | 5 | import * as React from 'react';
|
4 | 6 | import { classes } from 'typestyle';
|
5 | 7 | import {
|
6 | 8 | gitPullStyle,
|
7 | 9 | gitPushStyle,
|
| 10 | + pinIconStyle, |
8 | 11 | repoPathStyle,
|
9 | 12 | repoRefreshStyle,
|
10 |
| - repoStyle |
| 13 | + repoStyle, |
| 14 | + repoPinStyle, |
| 15 | + repoPinnedStyle |
11 | 16 | } from '../style/PathHeaderStyle';
|
12 | 17 | import { GitCredentialsForm } from '../widgets/CredentialsBox';
|
13 | 18 | import { GitPullPushDialog, Operation } from '../widgets/gitPushPull';
|
14 | 19 | import { IGitExtension } from '../tokens';
|
15 | 20 |
|
16 | 21 | export interface IPathHeaderProps {
|
17 | 22 | model: IGitExtension;
|
| 23 | + fileBrowserModel: FileBrowserModel; |
18 | 24 | refresh: () => Promise<void>;
|
19 | 25 | }
|
20 | 26 |
|
21 |
| -export class PathHeader extends React.Component<IPathHeaderProps> { |
22 |
| - constructor(props: IPathHeaderProps) { |
23 |
| - super(props); |
| 27 | +/** |
| 28 | + * Displays the error dialog when the Git Push/Pull operation fails. |
| 29 | + * @param title the title of the error dialog |
| 30 | + * @param body the message to be shown in the body of the modal. |
| 31 | + */ |
| 32 | +async function showGitPushPullDialog( |
| 33 | + model: IGitExtension, |
| 34 | + operation: Operation |
| 35 | +): Promise<void> { |
| 36 | + let result = await showDialog({ |
| 37 | + title: `Git ${operation}`, |
| 38 | + body: new GitPullPushDialog(model, operation), |
| 39 | + buttons: [Dialog.okButton({ label: 'DISMISS' })] |
| 40 | + }); |
| 41 | + let retry = false; |
| 42 | + while (!result.button.accept) { |
| 43 | + retry = true; |
| 44 | + |
| 45 | + let response = await showDialog({ |
| 46 | + title: 'Git credentials required', |
| 47 | + body: new GitCredentialsForm( |
| 48 | + 'Enter credentials for remote repository', |
| 49 | + retry ? 'Incorrect username or password.' : '' |
| 50 | + ), |
| 51 | + buttons: [Dialog.cancelButton(), Dialog.okButton({ label: 'OK' })] |
| 52 | + }); |
| 53 | + |
| 54 | + if (response.button.accept) { |
| 55 | + // user accepted attempt to login |
| 56 | + result = await showDialog({ |
| 57 | + title: `Git ${operation}`, |
| 58 | + body: new GitPullPushDialog(model, operation, response.value), |
| 59 | + buttons: [Dialog.okButton({ label: 'DISMISS' })] |
| 60 | + }); |
| 61 | + } else { |
| 62 | + break; |
| 63 | + } |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +/** |
| 68 | + * Select a Git repository folder |
| 69 | + * |
| 70 | + * @param model Git extension model |
| 71 | + * @param fileModel file browser model |
| 72 | + */ |
| 73 | +async function selectGitRepository( |
| 74 | + model: IGitExtension, |
| 75 | + fileModel: FileBrowserModel |
| 76 | +) { |
| 77 | + const result = await FileDialog.getExistingDirectory({ |
| 78 | + iconRegistry: fileModel.iconRegistry, |
| 79 | + manager: fileModel.manager, |
| 80 | + title: 'Select a Git repository folder' |
| 81 | + }); |
| 82 | + |
| 83 | + if (result.button.accept) { |
| 84 | + const folder = result.value[0]; |
| 85 | + if (model.repositoryPinned) { |
| 86 | + model.pathRepository = folder.path; |
| 87 | + } else if (fileModel.path !== folder.path) { |
| 88 | + // Change current filebrowser path |
| 89 | + fileModel.cd('/' + folder.path); |
| 90 | + } |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +/** |
| 95 | + * React function component to render the toolbar and path header component |
| 96 | + * |
| 97 | + * @param props Properties for the path header component |
| 98 | + */ |
| 99 | +export const PathHeader: React.FunctionComponent<IPathHeaderProps> = ( |
| 100 | + props: IPathHeaderProps |
| 101 | +) => { |
| 102 | + const [pin, setPin] = React.useState(props.model.repositoryPinned); |
| 103 | + |
| 104 | + React.useEffect(() => { |
| 105 | + props.model.restored.then(() => { |
| 106 | + setPin(props.model.repositoryPinned); |
| 107 | + }); |
| 108 | + }); |
| 109 | + |
| 110 | + const pinStyles = [repoPinStyle, 'jp-Icon-16']; |
| 111 | + if (pin) { |
| 112 | + pinStyles.push(repoPinnedStyle); |
24 | 113 | }
|
25 | 114 |
|
26 |
| - render() { |
27 |
| - return ( |
| 115 | + return ( |
| 116 | + <div> |
28 | 117 | <div className={repoStyle}>
|
29 |
| - <UseSignal |
30 |
| - signal={this.props.model.repositoryChanged} |
31 |
| - initialArgs={{ |
32 |
| - name: 'pathRepository', |
33 |
| - oldValue: null, |
34 |
| - newValue: this.props.model.pathRepository |
35 |
| - }} |
36 |
| - > |
37 |
| - {(_, change) => ( |
38 |
| - <span className={repoPathStyle} title={change.newValue}> |
39 |
| - {PathExt.basename(change.newValue || '')} |
40 |
| - </span> |
41 |
| - )} |
42 |
| - </UseSignal> |
43 | 118 | <button
|
44 | 119 | className={classes(gitPullStyle, 'jp-Icon-16')}
|
45 | 120 | title={'Pull latest changes'}
|
46 | 121 | onClick={() =>
|
47 |
| - this.showGitPushPullDialog(this.props.model, Operation.Pull).catch( |
48 |
| - reason => { |
49 |
| - console.error( |
50 |
| - `An error occurs when pulling the changes.\n${reason}` |
51 |
| - ); |
52 |
| - } |
53 |
| - ) |
| 122 | + showGitPushPullDialog(props.model, Operation.Pull).catch(reason => { |
| 123 | + console.error( |
| 124 | + `An error occurs when pulling the changes.\n${reason}` |
| 125 | + ); |
| 126 | + }) |
54 | 127 | }
|
55 | 128 | />
|
56 | 129 | <button
|
57 | 130 | className={classes(gitPushStyle, 'jp-Icon-16')}
|
58 | 131 | title={'Push committed changes'}
|
59 | 132 | onClick={() =>
|
60 |
| - this.showGitPushPullDialog(this.props.model, Operation.Push).catch( |
61 |
| - reason => { |
62 |
| - console.error( |
63 |
| - `An error occurs when pulling the changes.\n${reason}` |
64 |
| - ); |
65 |
| - } |
66 |
| - ) |
| 133 | + showGitPushPullDialog(props.model, Operation.Push).catch(reason => { |
| 134 | + console.error( |
| 135 | + `An error occurs when pulling the changes.\n${reason}` |
| 136 | + ); |
| 137 | + }) |
67 | 138 | }
|
68 | 139 | />
|
69 | 140 | <button
|
70 | 141 | className={classes(repoRefreshStyle, 'jp-Icon-16')}
|
71 | 142 | title={'Refresh the repository to detect local and remote changes'}
|
72 |
| - onClick={() => this.props.refresh()} |
| 143 | + onClick={() => props.refresh()} |
73 | 144 | />
|
74 | 145 | </div>
|
75 |
| - ); |
76 |
| - } |
77 |
| - |
78 |
| - /** |
79 |
| - * Displays the error dialog when the Git Push/Pull operation fails. |
80 |
| - * @param title the title of the error dialog |
81 |
| - * @param body the message to be shown in the body of the modal. |
82 |
| - */ |
83 |
| - private async showGitPushPullDialog( |
84 |
| - model: IGitExtension, |
85 |
| - operation: Operation |
86 |
| - ): Promise<void> { |
87 |
| - let result = await showDialog({ |
88 |
| - title: `Git ${operation}`, |
89 |
| - body: new GitPullPushDialog(model, operation), |
90 |
| - buttons: [Dialog.okButton({ label: 'DISMISS' })] |
91 |
| - }); |
92 |
| - let retry = false; |
93 |
| - while (!result.button.accept) { |
94 |
| - retry = true; |
95 |
| - |
96 |
| - let response = await showDialog({ |
97 |
| - title: 'Git credentials required', |
98 |
| - body: new GitCredentialsForm( |
99 |
| - 'Enter credentials for remote repository', |
100 |
| - retry ? 'Incorrect username or password.' : '' |
101 |
| - ), |
102 |
| - buttons: [Dialog.cancelButton(), Dialog.okButton({ label: 'OK' })] |
103 |
| - }); |
| 146 | + <div> |
| 147 | + <UseSignal |
| 148 | + signal={props.model.repositoryChanged} |
| 149 | + initialArgs={{ |
| 150 | + name: 'pathRepository', |
| 151 | + oldValue: null, |
| 152 | + newValue: props.model.pathRepository |
| 153 | + }} |
| 154 | + > |
| 155 | + {(_, change) => { |
| 156 | + const pathStyles = [repoPathStyle]; |
| 157 | + let pinTitle = 'the repository path'; |
| 158 | + if (props.model.repositoryPinned) { |
| 159 | + pinTitle = 'Unpin ' + pinTitle; |
| 160 | + } else { |
| 161 | + pinTitle = 'Pin ' + pinTitle; |
| 162 | + } |
104 | 163 |
|
105 |
| - if (response.button.accept) { |
106 |
| - // user accepted attempt to login |
107 |
| - result = await showDialog({ |
108 |
| - title: `Git ${operation}`, |
109 |
| - body: new GitPullPushDialog(model, operation, response.value), |
110 |
| - buttons: [Dialog.okButton({ label: 'DISMISS' })] |
111 |
| - }); |
112 |
| - } else { |
113 |
| - break; |
114 |
| - } |
115 |
| - } |
116 |
| - } |
117 |
| -} |
| 164 | + return ( |
| 165 | + <React.Fragment> |
| 166 | + <label className={repoPinStyle} title={pinTitle}> |
| 167 | + <input |
| 168 | + type="checkbox" |
| 169 | + checked={props.model.repositoryPinned} |
| 170 | + onChange={() => { |
| 171 | + props.model.repositoryPinned = !props.model |
| 172 | + .repositoryPinned; |
| 173 | + setPin(props.model.repositoryPinned); |
| 174 | + }} |
| 175 | + /> |
| 176 | + <DefaultIconReact |
| 177 | + className={pinIconStyle} |
| 178 | + tag="span" |
| 179 | + name="git-pin" |
| 180 | + /> |
| 181 | + </label> |
| 182 | + <span |
| 183 | + className={classes(...pathStyles)} |
| 184 | + title={change.newValue} |
| 185 | + onClick={() => { |
| 186 | + selectGitRepository(props.model, props.fileBrowserModel); |
| 187 | + }} |
| 188 | + > |
| 189 | + {PathExt.basename( |
| 190 | + change.newValue || 'Click to select a Git repository.' |
| 191 | + )} |
| 192 | + </span> |
| 193 | + </React.Fragment> |
| 194 | + ); |
| 195 | + }} |
| 196 | + </UseSignal> |
| 197 | + </div> |
| 198 | + </div> |
| 199 | + ); |
| 200 | +}; |
0 commit comments