1
1
import { InjectionToken } from '@gitbutler/core/context' ;
2
2
import { persisted } from '@gitbutler/shared/persisted' ;
3
- import { derived , get , writable , type Readable , type Writable } from 'svelte/store' ;
3
+ import { derived , get , readable , writable , type Readable , type Writable } from 'svelte/store' ;
4
4
import type { SecretsService } from '$lib/secrets/secretsService' ;
5
5
import type { RepoInfo } from '$lib/url/gitUrl' ;
6
6
7
7
export const GITLAB_STATE = new InjectionToken < GitLabState > ( 'GitLabState' ) ;
8
8
9
9
export class GitLabState {
10
10
readonly token : Writable < string | undefined > ;
11
- readonly forkProjectId : Writable < string | undefined > ;
12
- readonly upstreamProjectId : Writable < string | undefined > ;
13
- readonly instanceUrl : Writable < string | undefined > ;
14
- readonly configured : Readable < boolean > ;
11
+ private _forkProjectId : Writable < string | undefined > | undefined ;
12
+ private _upstreamProjectId : Writable < string | undefined > | undefined ;
13
+ private _instanceUrl : Writable < string | undefined > | undefined ;
14
+ private _configured : Readable < boolean > | undefined ;
15
15
16
- constructor (
17
- private readonly secretService : SecretsService ,
18
- repoInfo : RepoInfo | undefined ,
19
- projectId : string
20
- ) {
16
+ constructor ( private readonly secretService : SecretsService ) {
17
+ this . token = writable < string | undefined > ( ) ;
18
+ }
19
+
20
+ get forkProjectId ( ) {
21
+ if ( ! this . _forkProjectId ) {
22
+ return writable < string | undefined > ( undefined ) ;
23
+ }
24
+ return this . _forkProjectId ;
25
+ }
26
+
27
+ get upstreamProjectId ( ) {
28
+ if ( ! this . _upstreamProjectId ) {
29
+ return writable < string | undefined > ( undefined ) ;
30
+ }
31
+ return this . _upstreamProjectId ;
32
+ }
33
+
34
+ get instanceUrl ( ) {
35
+ if ( ! this . _instanceUrl ) {
36
+ return writable < string | undefined > ( undefined ) ;
37
+ }
38
+ return this . _instanceUrl ;
39
+ }
40
+
41
+ get configured ( ) {
42
+ if ( ! this . _configured ) {
43
+ return readable ( false ) ;
44
+ }
45
+ return this . _configured ;
46
+ }
47
+
48
+ init ( projectId : string , repoInfo : RepoInfo | undefined ) {
21
49
// For whatever reason, the token _sometimes_ is incorrectly fetched as null.
22
50
// I have no idea why, but this seems to work. There were also some
23
51
// weird reactivity issues. Don't touch it as you might make it angry.
24
52
const tokenLoading = writable ( true ) ;
25
53
let tokenLoadedAsNull = false ;
26
- this . token = writable < string | undefined > ( ) ;
27
54
this . secretService . get ( `git-lab-token:${ projectId } ` ) . then ( ( fetchedToken ) => {
28
55
if ( fetchedToken ) {
29
56
this . token . set ( fetchedToken ?? '' ) ;
@@ -50,32 +77,30 @@ export class GitLabState {
50
77
return unsubscribe ;
51
78
} ) ;
52
79
53
- const forkProjectId = persisted < string | undefined > (
80
+ this . _forkProjectId = persisted < string | undefined > (
54
81
undefined ,
55
82
`gitlab-project-id:${ projectId } `
56
83
) ;
57
- if ( ! get ( forkProjectId ) && repoInfo ) {
58
- forkProjectId . set ( `${ repoInfo . owner } /${ repoInfo . name } ` ) ;
59
- }
60
- this . forkProjectId = forkProjectId ;
61
84
62
- const upstreamProjectId = persisted < string | undefined > (
85
+ this . _upstreamProjectId = persisted < string | undefined > (
63
86
undefined ,
64
87
`gitlab-upstream-project-id:${ projectId } `
65
88
) ;
66
- if ( ! get ( upstreamProjectId ) ) {
67
- upstreamProjectId . set ( get ( forkProjectId ) ) ;
89
+ if ( ! get ( this . _upstreamProjectId ) ) {
90
+ this . _upstreamProjectId . set ( get ( this . _forkProjectId ) ) ;
68
91
}
69
- this . upstreamProjectId = upstreamProjectId ;
70
92
71
- const instanceUrl = persisted < string > ( 'https://gitlab.com' , `gitlab-instance-url:${ projectId } ` ) ;
72
- this . instanceUrl = instanceUrl ;
93
+ this . _instanceUrl = persisted < string > ( 'https://gitlab.com' , `gitlab-instance-url:${ projectId } ` ) ;
73
94
74
- this . configured = derived (
95
+ this . _configured = derived (
75
96
[ this . token , this . upstreamProjectId , this . forkProjectId , this . instanceUrl ] ,
76
97
( [ token , upstreamProjectId , forkProjectId , instanceUrl ] ) => {
77
98
return ! ! token && ! ! upstreamProjectId && ! ! forkProjectId && ! ! instanceUrl ;
78
99
}
79
100
) ;
101
+
102
+ if ( ! get ( this . forkProjectId ) && repoInfo ) {
103
+ this . forkProjectId . set ( `${ repoInfo . owner } /${ repoInfo . name } ` ) ;
104
+ }
80
105
}
81
106
}
0 commit comments