1
- import { getTemplateVersionByName } from "api/api"
1
+ import {
2
+ getPreviousTemplateVersionByName ,
3
+ getTemplateVersionByName ,
4
+ } from "api/api"
2
5
import { TemplateVersion } from "api/typesGenerated"
3
6
import {
4
7
getTemplateVersionFiles ,
5
8
TemplateVersionFiles ,
6
9
} from "util/templateVersion"
7
10
import { assign , createMachine } from "xstate"
8
-
9
11
export interface TemplateVersionMachineContext {
10
12
orgId : string
11
13
versionName : string
12
- version ?: TemplateVersion
13
- files ?: TemplateVersionFiles
14
+ currentVersion ?: TemplateVersion
15
+ currentFiles ?: TemplateVersionFiles
14
16
error ?: Error | unknown
17
+ // Get file diffs
18
+ previousVersion ?: TemplateVersion
19
+ previousFiles ?: TemplateVersionFiles
15
20
}
16
21
17
22
export const templateVersionMachine = createMachine (
@@ -21,23 +26,29 @@ export const templateVersionMachine = createMachine(
21
26
schema : {
22
27
context : { } as TemplateVersionMachineContext ,
23
28
services : { } as {
24
- loadVersion : {
25
- data : TemplateVersion
29
+ loadVersions : {
30
+ data : {
31
+ currentVersion : TemplateVersion
32
+ previousVersion : TemplateVersion
33
+ }
26
34
}
27
35
loadFiles : {
28
- data : TemplateVersionFiles
36
+ data : {
37
+ currentFiles : TemplateVersionFiles
38
+ previousFiles : TemplateVersionFiles
39
+ }
29
40
}
30
41
} ,
31
42
} ,
32
43
tsTypes : { } as import ( "./templateVersionXService.typegen" ) . Typegen0 ,
33
- initial : "loadingVersion " ,
44
+ initial : "loadingVersions " ,
34
45
states : {
35
- loadingVersion : {
46
+ loadingVersions : {
36
47
invoke : {
37
- src : "loadVersion " ,
48
+ src : "loadVersions " ,
38
49
onDone : {
39
50
target : "loadingFiles" ,
40
- actions : [ "assignVersion " ] ,
51
+ actions : [ "assignVersions " ] ,
41
52
} ,
42
53
onError : {
43
54
target : "done.error" ,
@@ -71,21 +82,43 @@ export const templateVersionMachine = createMachine(
71
82
assignError : assign ( {
72
83
error : ( _ , { data } ) => data ,
73
84
} ) ,
74
- assignVersion : assign ( {
75
- version : ( _ , { data } ) => data ,
85
+ assignVersions : assign ( {
86
+ currentVersion : ( _ , { data } ) => data . currentVersion ,
87
+ previousVersion : ( _ , { data } ) => data . previousVersion ,
76
88
} ) ,
77
89
assignFiles : assign ( {
78
- files : ( _ , { data } ) => data ,
90
+ currentFiles : ( _ , { data } ) => data . currentFiles ,
91
+ previousFiles : ( _ , { data } ) => data . previousFiles ,
79
92
} ) ,
80
93
} ,
81
94
services : {
82
- loadVersion : ( { orgId, versionName } ) =>
83
- getTemplateVersionByName ( orgId , versionName ) ,
84
- loadFiles : async ( { version } ) => {
85
- if ( ! version ) {
95
+ loadVersions : async ( { orgId, versionName } ) => {
96
+ const [ currentVersion , previousVersion ] = await Promise . all ( [
97
+ getTemplateVersionByName ( orgId , versionName ) ,
98
+ getPreviousTemplateVersionByName ( orgId , versionName ) ,
99
+ ] )
100
+
101
+ return {
102
+ currentVersion,
103
+ previousVersion,
104
+ }
105
+ } ,
106
+ loadFiles : async ( { currentVersion, previousVersion } ) => {
107
+ if ( ! currentVersion ) {
86
108
throw new Error ( "Version is not defined" )
87
109
}
88
- return getTemplateVersionFiles ( version , [ "tf" , "md" ] )
110
+ if ( ! previousVersion ) {
111
+ throw new Error ( "Previous version is not defined" )
112
+ }
113
+ const allowedExtensions = [ "tf" , "md" ]
114
+ const [ currentFiles , previousFiles ] = await Promise . all ( [
115
+ getTemplateVersionFiles ( currentVersion , allowedExtensions ) ,
116
+ getTemplateVersionFiles ( previousVersion , allowedExtensions ) ,
117
+ ] )
118
+ return {
119
+ currentFiles,
120
+ previousFiles,
121
+ }
89
122
} ,
90
123
} ,
91
124
} ,
0 commit comments