1
1
import { InjectionToken } from '@gitbutler/core/context' ;
2
2
import { chipToasts } from '@gitbutler/ui' ;
3
- import type { IBackend } from '$lib/backend' ;
4
3
import type { DiffSpec } from '$lib/hunks/hunk' ;
4
+ import type { BackendApi } from '$lib/state/clientState.svelte' ;
5
5
6
6
export type HookStatus =
7
7
| {
@@ -34,37 +34,29 @@ export type MessageHookStatus =
34
34
export const HOOKS_SERVICE = new InjectionToken < HooksService > ( 'HooksService' ) ;
35
35
36
36
export class HooksService {
37
- constructor ( private backend : IBackend ) { }
37
+ private api : ReturnType < typeof injectEndpoints > ;
38
38
39
- async preCommitDiffspecs ( projectId : string , changes : DiffSpec [ ] ) {
40
- return await this . backend . invoke < HookStatus > ( 'pre_commit_hook_diffspecs' , {
41
- projectId,
42
- changes
43
- } ) ;
39
+ constructor ( backendApi : BackendApi ) {
40
+ this . api = injectEndpoints ( backendApi ) ;
44
41
}
45
42
46
- async postCommit ( projectId : string ) {
47
- return await this . backend . invoke < HookStatus > ( 'post_commit_hook' , {
48
- projectId
49
- } ) ;
50
- }
51
-
52
- async message ( projectId : string , message : string ) {
53
- return await this . backend . invoke < MessageHookStatus > ( 'message_hook' , {
54
- projectId,
55
- message
56
- } ) ;
43
+ get message ( ) {
44
+ return this . api . endpoints . message . useMutation ( ) ;
57
45
}
58
46
47
+ // Promise-based wrapper methods with toast handling
59
48
async runPreCommitHooks ( projectId : string , changes : DiffSpec [ ] ) : Promise < void > {
60
49
const loadingToastId = chipToasts . loading ( 'Started pre-commit hooks' ) ;
61
50
62
51
try {
63
- const result = await this . preCommitDiffspecs ( projectId , changes ) ;
52
+ const result = await this . api . endpoints . preCommitDiffspecs . mutate ( {
53
+ projectId,
54
+ changes
55
+ } ) ;
64
56
65
57
if ( result ?. status === 'failure' ) {
66
58
chipToasts . removeChipToast ( loadingToastId ) ;
67
- throw new Error ( result . error ) ;
59
+ throw new Error ( formatError ( result . error ) ) ;
68
60
}
69
61
70
62
chipToasts . removeChipToast ( loadingToastId ) ;
@@ -79,11 +71,13 @@ export class HooksService {
79
71
const loadingToastId = chipToasts . loading ( 'Started post-commit hooks' ) ;
80
72
81
73
try {
82
- const result = await this . postCommit ( projectId ) ;
74
+ const result = await this . api . endpoints . postCommit . mutate ( {
75
+ projectId
76
+ } ) ;
83
77
84
78
if ( result ?. status === 'failure' ) {
85
79
chipToasts . removeChipToast ( loadingToastId ) ;
86
- throw new Error ( result . error ) ;
80
+ throw new Error ( formatError ( result . error ) ) ;
87
81
}
88
82
89
83
chipToasts . removeChipToast ( loadingToastId ) ;
@@ -94,3 +88,26 @@ export class HooksService {
94
88
}
95
89
}
96
90
}
91
+
92
+ function formatError ( error : string ) : string {
93
+ return `${ error } \n\nIf you don't want git hooks to be run, you can disable them in the project settings.` ;
94
+ }
95
+
96
+ function injectEndpoints ( backendApi : BackendApi ) {
97
+ return backendApi . injectEndpoints ( {
98
+ endpoints : ( build ) => ( {
99
+ preCommitDiffspecs : build . mutation < HookStatus , { projectId : string ; changes : DiffSpec [ ] } > ( {
100
+ extraOptions : { command : 'pre_commit_hook_diffspecs' } ,
101
+ query : ( args ) => args
102
+ } ) ,
103
+ postCommit : build . mutation < HookStatus , { projectId : string } > ( {
104
+ extraOptions : { command : 'post_commit_hook' } ,
105
+ query : ( args ) => args
106
+ } ) ,
107
+ message : build . mutation < MessageHookStatus , { projectId : string ; message : string } > ( {
108
+ extraOptions : { command : 'message_hook' } ,
109
+ query : ( args ) => args
110
+ } )
111
+ } )
112
+ } ) ;
113
+ }
0 commit comments