11import { InjectionToken } from '@gitbutler/core/context' ;
22import { 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' ;
44import type { SecretsService } from '$lib/secrets/secretsService' ;
55import type { RepoInfo } from '$lib/url/gitUrl' ;
66
77export const GITLAB_STATE = new InjectionToken < GitLabState > ( 'GitLabState' ) ;
88
99export class GitLabState {
1010 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 ;
1515
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 ) {
2149 // For whatever reason, the token _sometimes_ is incorrectly fetched as null.
2250 // I have no idea why, but this seems to work. There were also some
2351 // weird reactivity issues. Don't touch it as you might make it angry.
2452 const tokenLoading = writable ( true ) ;
2553 let tokenLoadedAsNull = false ;
26- this . token = writable < string | undefined > ( ) ;
2754 this . secretService . get ( `git-lab-token:${ projectId } ` ) . then ( ( fetchedToken ) => {
2855 if ( fetchedToken ) {
2956 this . token . set ( fetchedToken ?? '' ) ;
@@ -50,32 +77,30 @@ export class GitLabState {
5077 return unsubscribe ;
5178 } ) ;
5279
53- const forkProjectId = persisted < string | undefined > (
80+ this . _forkProjectId = persisted < string | undefined > (
5481 undefined ,
5582 `gitlab-project-id:${ projectId } `
5683 ) ;
57- if ( ! get ( forkProjectId ) && repoInfo ) {
58- forkProjectId . set ( `${ repoInfo . owner } /${ repoInfo . name } ` ) ;
59- }
60- this . forkProjectId = forkProjectId ;
6184
62- const upstreamProjectId = persisted < string | undefined > (
85+ this . _upstreamProjectId = persisted < string | undefined > (
6386 undefined ,
6487 `gitlab-upstream-project-id:${ projectId } `
6588 ) ;
66- if ( ! get ( upstreamProjectId ) ) {
67- upstreamProjectId . set ( get ( forkProjectId ) ) ;
89+ if ( ! get ( this . _upstreamProjectId ) ) {
90+ this . _upstreamProjectId . set ( get ( this . _forkProjectId ) ) ;
6891 }
69- this . upstreamProjectId = upstreamProjectId ;
7092
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 } ` ) ;
7394
74- this . configured = derived (
95+ this . _configured = derived (
7596 [ this . token , this . upstreamProjectId , this . forkProjectId , this . instanceUrl ] ,
7697 ( [ token , upstreamProjectId , forkProjectId , instanceUrl ] ) => {
7798 return ! ! token && ! ! upstreamProjectId && ! ! forkProjectId && ! ! instanceUrl ;
7899 }
79100 ) ;
101+
102+ if ( ! get ( this . forkProjectId ) && repoInfo ) {
103+ this . forkProjectId . set ( `${ repoInfo . owner } /${ repoInfo . name } ` ) ;
104+ }
80105 }
81106}
0 commit comments