11import * as vscode from "vscode" ;
2- import { API } from "../../@types/git" ;
2+ import { API , Commit } from "../../@types/git" ;
33import { CommitAPI } from "../../commitAPI" ;
44import { getWebviewContent } from "../../utils" ;
55
@@ -24,6 +24,11 @@ const shareProjectUpdate = (context: vscode.ExtensionContext) => {
2424 return ;
2525 }
2626
27+ if ( ! projects ) {
28+ vscode . window . showInformationMessage ( "No projects found" ) ;
29+ return ;
30+ }
31+
2732 // Get default project
2833 const defaultProject : Project | undefined =
2934 context . workspaceState . get ( "defaultProject" ) ;
@@ -38,11 +43,6 @@ const shareProjectUpdate = (context: vscode.ExtensionContext) => {
3843 }
3944 }
4045
41- if ( ! projects ) {
42- vscode . window . showInformationMessage ( "No projects found" ) ;
43- return ;
44- }
45-
4646 // Show list of projects and get the selection
4747 let selectedProjectTitle = await vscode . window . showQuickPick (
4848 projects . map ( ( project : Project ) => {
@@ -72,71 +72,43 @@ const shareProjectUpdate = (context: vscode.ExtensionContext) => {
7272 vscode . window . showErrorMessage ( "Project not found" ) ;
7373 return ;
7474 }
75-
76- // Prompt user if they want multiline or single line update
77- const isMultilineUpdate = await vscode . window . showQuickPick (
78- [ "Yes" , "No" ] ,
75+ // Create a WebView Panel
76+ const shareProjectUpdatePanel = vscode . window . createWebviewPanel (
77+ "commitExtension" ,
78+ "Share Project Update" ,
79+ vscode . ViewColumn . One ,
7980 {
80- placeHolder : "Do you want to add a multiline update?" ,
81+ enableScripts : true ,
8182 }
8283 ) ;
8384
84- if ( isMultilineUpdate === "No" || isMultilineUpdate === undefined ) {
85- // Open TextInput dialog
86- vscode . window
87- . showInputBox ( {
88- prompt : "Enter the project update" ,
89- placeHolder : "Implementing new feature ..." ,
90- value : await getCommitMessage ( context ) ,
91- } )
92- . then ( ( value ) => {
93- if ( value ) {
94- try {
95- commitAPI . addProjectUpdate ( selectedProject ! . id , value ) ;
96-
97- vscode . window . showInformationMessage (
98- "Project update successfully added"
99- ) ;
100- } catch ( e ) {
101- // Show the error message
102- vscode . window . showErrorMessage ( "Unable to add project update" ) ;
103- }
104- }
105- } ) ;
106- } else {
107- // Create a WebView Panel
108- const shareProjectUpdatePanel = vscode . window . createWebviewPanel (
109- "commitExtension" ,
110- "Share Project Update" ,
111- vscode . ViewColumn . One ,
112- {
113- enableScripts : true ,
114- }
115- ) ;
85+ // Get git commits
86+ const repositoryCommits : Commit [ ] | undefined = await getGitCommits (
87+ context
88+ ) ;
11689
117- // Send Project ID to the WebView
118- shareProjectUpdatePanel . webview . postMessage ( {
119- command : "setWebViewProject" ,
120- data : JSON . stringify ( {
121- projectId : selectedProject . id ,
122- lastCommitMessage : await getCommitMessage ( context ) ,
123- } ) ,
124- } ) ;
125-
126- shareProjectUpdatePanel . webview . onDidReceiveMessage (
127- async ( message : any ) => {
128- await processWebviewMessage ( message , context ) ;
129-
130- // Close the WebView Panel
131- shareProjectUpdatePanel . dispose ( ) ;
132- } ,
133- undefined ,
134- context . subscriptions
135- ) ;
90+ // Send Project ID to the WebView3
91+ shareProjectUpdatePanel . webview . postMessage ( {
92+ command : "setWebViewProject" ,
93+ data : JSON . stringify ( {
94+ projectId : selectedProject . id ,
95+ repositoryCommits : repositoryCommits || [ ] ,
96+ } ) ,
97+ } ) ;
13698
137- // Add HTML to the WebView
138- shareProjectUpdatePanel . webview . html = getWebviewContent ( context ) ;
139- }
99+ shareProjectUpdatePanel . webview . onDidReceiveMessage (
100+ async ( message : any ) => {
101+ await processWebviewMessage ( message , context ) ;
102+
103+ // Close the WebView Panel
104+ shareProjectUpdatePanel . dispose ( ) ;
105+ } ,
106+ undefined ,
107+ context . subscriptions
108+ ) ;
109+
110+ // Add HTML to the WebView
111+ shareProjectUpdatePanel . webview . html = getWebviewContent ( context ) ;
140112 } ,
141113 } ;
142114} ;
@@ -151,7 +123,6 @@ const processWebviewMessage = async (
151123 case "submitUpdate" :
152124 const projectId = data . projectId ;
153125 const udpateContent = `${ data . updateContent } ` ;
154- console . log ( projectId , udpateContent ) ;
155126 const commitAPI = context . workspaceState . get ( "commitAPI" ) as CommitAPI ;
156127 try {
157128 await commitAPI . addProjectUpdate ( projectId , udpateContent ) ;
@@ -167,33 +138,34 @@ const processWebviewMessage = async (
167138 }
168139} ;
169140
170- const getCommitMessage = async ( context : vscode . ExtensionContext ) => {
141+ const getGitCommits : (
142+ context : vscode . ExtensionContext
143+ ) => Promise < Commit [ ] | undefined > = async (
144+ context : vscode . ExtensionContext ,
145+ maxEntries : number = 10
146+ ) => {
171147 const gitAPI = context . workspaceState . get < API > ( "gitAPI" ) ;
148+
172149 if ( ! gitAPI ) {
173- return ;
150+ return [ ] ;
174151 }
175152
176- // Get workspace folder
153+ // Get Worspace folder
177154 const workspaceFolders = vscode . workspace . workspaceFolders ;
178- if ( ! workspaceFolders ) {
179- return ;
180- }
181155
182- // Get root path
183- const rootPath = workspaceFolders [ 0 ] . uri . fsPath ;
184-
185- // Get repository
186- const repository = gitAPI . repositories . find (
187- ( repo ) => repo . rootUri . fsPath === rootPath
188- ) ;
189-
190- if ( ! repository ) {
191- return ;
156+ if ( ! workspaceFolders || workspaceFolders . length === 0 ) {
157+ return [ ] ;
192158 }
193159
194- // Get commit message
195- const commit = await repository . getCommit ( "HEAD" ) ;
196- return commit ?. message || "" ;
160+ // Get the respository in the first workspace folder
161+ // TODO: At this point this will always pick the the first workspace folder
162+ // TODO: Add support for multiple workspace folders
163+ const workspaceFolder = workspaceFolders [ 0 ] ;
164+ const repository = gitAPI . getRepository ( workspaceFolder . uri ) ;
165+
166+ return await repository ?. log ( {
167+ maxEntries,
168+ } ) ;
197169} ;
198170
199171export default shareProjectUpdate ;
0 commit comments