11import { InjectionToken } from '@gitbutler/core/context' ;
22import { chipToasts } from '@gitbutler/ui' ;
3- import type { IBackend } from '$lib/backend' ;
43import type { DiffSpec } from '$lib/hunks/hunk' ;
4+ import type { BackendApi } from '$lib/state/clientState.svelte' ;
55
66export type HookStatus =
77 | {
@@ -34,37 +34,29 @@ export type MessageHookStatus =
3434export const HOOKS_SERVICE = new InjectionToken < HooksService > ( 'HooksService' ) ;
3535
3636export class HooksService {
37- constructor ( private backend : IBackend ) { }
37+ private api : ReturnType < typeof injectEndpoints > ;
3838
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 ) ;
4441 }
4542
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 ( ) ;
5745 }
5846
47+ // Promise-based wrapper methods with toast handling
5948 async runPreCommitHooks ( projectId : string , changes : DiffSpec [ ] ) : Promise < void > {
6049 const loadingToastId = chipToasts . loading ( 'Started pre-commit hooks' ) ;
6150
6251 try {
63- const result = await this . preCommitDiffspecs ( projectId , changes ) ;
52+ const result = await this . api . endpoints . preCommitDiffspecs . mutate ( {
53+ projectId,
54+ changes
55+ } ) ;
6456
6557 if ( result ?. status === 'failure' ) {
6658 chipToasts . removeChipToast ( loadingToastId ) ;
67- throw new Error ( result . error ) ;
59+ throw new Error ( formatError ( result . error ) ) ;
6860 }
6961
7062 chipToasts . removeChipToast ( loadingToastId ) ;
@@ -79,11 +71,13 @@ export class HooksService {
7971 const loadingToastId = chipToasts . loading ( 'Started post-commit hooks' ) ;
8072
8173 try {
82- const result = await this . postCommit ( projectId ) ;
74+ const result = await this . api . endpoints . postCommit . mutate ( {
75+ projectId
76+ } ) ;
8377
8478 if ( result ?. status === 'failure' ) {
8579 chipToasts . removeChipToast ( loadingToastId ) ;
86- throw new Error ( result . error ) ;
80+ throw new Error ( formatError ( result . error ) ) ;
8781 }
8882
8983 chipToasts . removeChipToast ( loadingToastId ) ;
@@ -94,3 +88,26 @@ export class HooksService {
9488 }
9589 }
9690}
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