Skip to content

Commit 37fd257

Browse files
committed
Provide correctly the GitLab state
Since now the GitLab state needs to be available everywhere, provide it from the root. Change it so that setting the project specific information doesn’t involve recreating it, but rather updating it’s information
1 parent d851b59 commit 37fd257

File tree

4 files changed

+62
-42
lines changed

4 files changed

+62
-42
lines changed

apps/desktop/src/components/ForgeForm.svelte

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,20 @@
22
import { DEFAULT_FORGE_FACTORY } from '$lib/forge/forgeFactory.svelte';
33
import { GITLAB_STATE } from '$lib/forge/gitlab/gitlabState.svelte';
44
import { PROJECTS_SERVICE } from '$lib/project/projectsService';
5-
import { inject, injectOptional } from '@gitbutler/core/context';
5+
import { inject } from '@gitbutler/core/context';
66
import { Link, SectionCard, Select, SelectItem, Spacer, Textbox } from '@gitbutler/ui';
7-
import { writable } from 'svelte/store';
87
98
import type { ForgeName } from '$lib/forge/interface/forge';
109
import type { Project } from '$lib/project/project';
1110
1211
const { projectId }: { projectId: string } = $props();
1312
1413
const forge = inject(DEFAULT_FORGE_FACTORY);
15-
const gitLabState = injectOptional(GITLAB_STATE, null);
16-
const token = gitLabState?.token ?? writable<string | undefined>('');
17-
const forkProjectId = gitLabState?.forkProjectId ?? writable<string | undefined>('');
18-
const upstreamProjectId = gitLabState?.upstreamProjectId ?? writable<string | undefined>('');
19-
const instanceUrl = gitLabState?.instanceUrl ?? writable<string | undefined>('');
14+
const gitLabState = inject(GITLAB_STATE);
15+
const token = gitLabState.token;
16+
const forkProjectId = gitLabState.forkProjectId;
17+
const upstreamProjectId = gitLabState.upstreamProjectId;
18+
const instanceUrl = gitLabState.instanceUrl;
2019
2120
const projectsService = inject(PROJECTS_SERVICE);
2221
const projectResult = $derived(projectsService.getProject(projectId));

apps/desktop/src/lib/bootstrap/deps.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import { DefaultForgeFactory, DEFAULT_FORGE_FACTORY } from '$lib/forge/forgeFact
2828
import { GITHUB_CLIENT, GitHubClient } from '$lib/forge/github/githubClient';
2929
import { GitHubUserService, GITHUB_USER_SERVICE } from '$lib/forge/github/githubUserService.svelte';
3030
import { GITLAB_CLIENT, GitLabClient } from '$lib/forge/gitlab/gitlabClient.svelte';
31+
import { GITLAB_STATE, GitLabState } from '$lib/forge/gitlab/gitlabState.svelte';
3132
import { GitService, GIT_SERVICE } from '$lib/git/gitService';
3233
import { HISTORY_SERVICE, HistoryService } from '$lib/history/history';
3334
import { OplogService, OPLOG_SERVICE } from '$lib/history/oplogService.svelte';
@@ -124,6 +125,8 @@ export function initDependencies(args: {
124125

125126
const gitHubClient = new GitHubClient();
126127
const gitLabClient = new GitLabClient();
128+
const gitLabState = new GitLabState(secretsService);
129+
gitLabClient.set(gitLabState);
127130

128131
// ============================================================================
129132
// EXPERIMENTAL STUFF
@@ -325,6 +328,7 @@ export function initDependencies(args: {
325328
[GITHUB_CLIENT, gitHubClient],
326329
[GITHUB_USER_SERVICE, githubUserService],
327330
[GITLAB_CLIENT, gitLabClient],
331+
[GITLAB_STATE, gitLabState],
328332
[GIT_CONFIG_SERVICE, gitConfig],
329333
[GIT_SERVICE, gitService],
330334
[HISTORY_SERVICE, historyService],
Lines changed: 48 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,56 @@
11
import { InjectionToken } from '@gitbutler/core/context';
22
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';
44
import type { SecretsService } from '$lib/secrets/secretsService';
55
import type { RepoInfo } from '$lib/url/gitUrl';
66

77
export const GITLAB_STATE = new InjectionToken<GitLabState>('GitLabState');
88

99
export 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
}

apps/desktop/src/routes/[projectId]/+layout.svelte

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,11 @@
1818
import { FEED_FACTORY } from '$lib/feed/feed';
1919
import { DEFAULT_FORGE_FACTORY } from '$lib/forge/forgeFactory.svelte';
2020
import { GITHUB_CLIENT } from '$lib/forge/github/githubClient';
21-
import { GITLAB_CLIENT } from '$lib/forge/gitlab/gitlabClient.svelte';
22-
import { GitLabState, GITLAB_STATE } from '$lib/forge/gitlab/gitlabState.svelte';
21+
import { GITLAB_STATE } from '$lib/forge/gitlab/gitlabState.svelte';
2322
import { GIT_SERVICE } from '$lib/git/gitService';
2423
import { MODE_SERVICE } from '$lib/mode/modeService';
2524
import { showError, showInfo, showWarning } from '$lib/notifications/toasts';
2625
import { PROJECTS_SERVICE } from '$lib/project/projectsService';
27-
import { SECRET_SERVICE } from '$lib/secrets/secretsService';
2826
import { FILE_SELECTION_MANAGER } from '$lib/selection/fileSelectionManager.svelte';
2927
import { UNCOMMITTED_SERVICE } from '$lib/selection/uncommittedService.svelte';
3028
import { STACK_SERVICE } from '$lib/stacks/stackService.svelte';
@@ -33,7 +31,7 @@
3331
import { USER_SERVICE } from '$lib/user/userService';
3432
import { debounce } from '$lib/utils/debounce';
3533
import { WORKTREE_SERVICE } from '$lib/worktree/worktreeService.svelte';
36-
import { inject, provide } from '@gitbutler/core/context';
34+
import { inject } from '@gitbutler/core/context';
3735
import { onDestroy, untrack, type Snippet } from 'svelte';
3836
import type { LayoutData } from './$types';
3937
@@ -102,22 +100,16 @@
102100
// =============================================================================
103101
104102
const gitHubClient = inject(GITHUB_CLIENT);
105-
const gitLabClient = inject(GITLAB_CLIENT);
106-
const secretService = inject(SECRET_SERVICE);
103+
const gitLabState = inject(GITLAB_STATE);
107104
const forgeFactory = inject(DEFAULT_FORGE_FACTORY);
108105
109106
// GitHub setup
110107
$effect.pre(() => gitHubClient.setToken(accessToken));
111108
$effect.pre(() => gitHubClient.setRepo({ owner: repoInfo?.owner, repo: repoInfo?.name }));
112109
113110
// GitLab setup
114-
const gitLabState = $derived(new GitLabState(secretService, repoInfo, projectId));
115111
const gitlabConfigured = $derived(gitLabState.configured);
116-
117-
$effect.pre(() => {
118-
provide(GITLAB_STATE, gitLabState);
119-
gitLabClient.set(gitLabState);
120-
});
112+
$effect.pre(() => gitLabState.init(projectId, repoInfo));
121113
122114
// Forge factory configuration
123115
$effect(() => {

0 commit comments

Comments
 (0)