Skip to content

Commit 75a74bc

Browse files
committed
Refactor projectId usage
1 parent 3932512 commit 75a74bc

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+944
-1138
lines changed

apps/desktop/cypress/e2e/support/mock/projects.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,18 @@ export function listProjects() {
1919
}
2020

2121
type GetProjectArgs = {
22-
id: string;
22+
projectId: string;
2323
};
2424

2525
export function isGetProjectArgs(args: unknown): args is GetProjectArgs {
26-
return typeof args === 'object' && args !== null && 'id' in args && typeof args.id === 'string';
26+
return (
27+
typeof args === 'object' &&
28+
args !== null &&
29+
'projectId' in args &&
30+
typeof args.projectId === 'string'
31+
);
2732
}
2833

2934
export function getProject(args: GetProjectArgs) {
30-
return MOCK_PROJECT_A.id === args.id ? MOCK_PROJECT_A : undefined;
35+
return MOCK_PROJECT_A.id === args.projectId ? MOCK_PROJECT_A : undefined;
3136
}

apps/desktop/src/components/AIPromptSelect.svelte

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,30 @@
11
<script lang="ts">
22
import { PromptService } from '$lib/ai/promptService';
3-
import { Project } from '$lib/project/project';
43
import { getContext } from '@gitbutler/shared/context';
54
import Select from '@gitbutler/ui/select/Select.svelte';
65
import SelectItem from '@gitbutler/ui/select/SelectItem.svelte';
76
import { onMount } from 'svelte';
87
import type { Prompts, UserPrompt } from '$lib/ai/types';
98
import type { Persisted } from '@gitbutler/shared/persisted';
109
11-
interface Props {
10+
type Props = {
11+
projectId: string;
1212
promptUse: 'commits' | 'branches';
13-
}
13+
};
1414
15-
const { promptUse }: Props = $props();
15+
const { projectId, promptUse }: Props = $props();
1616
17-
const project = getContext(Project);
1817
const promptService = getContext(PromptService);
1918
2019
let prompts: Prompts;
2120
let selectedPromptId = $state<Persisted<string | undefined>>();
2221
2322
if (promptUse === 'commits') {
2423
prompts = promptService.commitPrompts;
25-
selectedPromptId = promptService.selectedCommitPromptId(project.id);
24+
selectedPromptId = promptService.selectedCommitPromptId(projectId);
2625
} else {
2726
prompts = promptService.branchPrompts;
28-
selectedPromptId = promptService.selectedBranchPromptId(project.id);
27+
selectedPromptId = promptService.selectedBranchPromptId(projectId);
2928
}
3029
3130
let userPrompts = prompts.userPrompts;

apps/desktop/src/components/AnalyticsMonitor.svelte

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,23 @@ attached to posthog events.
66
<script lang="ts">
77
import { EventContext } from '$lib/analytics/eventContext';
88
import { SettingsService } from '$lib/config/appSettingsV2';
9-
import { ProjectService } from '$lib/project/projectService';
9+
import { gitAuthType } from '$lib/project/project';
10+
import { ProjectsService } from '$lib/project/projectsService';
1011
import { SETTINGS, type Settings } from '$lib/settings/userSettings';
1112
import { UiState } from '$lib/state/uiState.svelte';
1213
import { getContextStoreBySymbol, inject } from '@gitbutler/shared/context';
1314
import type { Writable } from 'svelte/store';
1415
1516
const { projectId }: { projectId: string } = $props();
1617
17-
const [uiState, eventContext, projectService, settingsService] = inject(
18+
const [uiState, eventContext, projectsService, settingsService] = inject(
1819
UiState,
1920
EventContext,
20-
ProjectService,
21+
ProjectsService,
2122
SettingsService
2223
);
2324
25+
const projectResult = $derived(projectsService.getProject(projectId));
2426
const globalState = uiState.global;
2527
const projectState = $derived(uiState.project(projectId));
2628
@@ -52,10 +54,13 @@ attached to posthog events.
5254
});
5355
5456
$effect(() => {
55-
eventContext.update({
56-
forcePushAllowed: $projectService?.ok_with_force_push,
57-
gitAuthType: $projectService?.gitAuthType()
58-
});
57+
const project = projectResult.current.data;
58+
if (project) {
59+
eventContext.update({
60+
forcePushAllowed: project.ok_with_force_push,
61+
gitAuthType: gitAuthType(project.preferred_key)
62+
});
63+
}
5964
});
6065
6166
$effect(() => {

apps/desktop/src/components/BaseBranchSwitch.svelte

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,15 @@
22
import InfoMessage from '$components/InfoMessage.svelte';
33
import { BaseBranch } from '$lib/baseBranch/baseBranch';
44
import BaseBranchService from '$lib/baseBranch/baseBranchService.svelte';
5-
import { Project } from '$lib/project/project';
65
import { StackService } from '$lib/stacks/stackService.svelte';
76
import { getContext } from '@gitbutler/shared/context';
87
import Button from '@gitbutler/ui/Button.svelte';
98
import SectionCard from '@gitbutler/ui/SectionCard.svelte';
109
import Select from '@gitbutler/ui/select/Select.svelte';
1110
import SelectItem from '@gitbutler/ui/select/SelectItem.svelte';
1211
13-
const project = getContext(Project);
14-
const projectId = $derived(project.id);
12+
const { projectId }: { projectId: string } = $props();
13+
1514
const baseBranch = getContext(BaseBranch);
1615
const stackService = getContext(StackService);
1716
const baseBranchService = getContext(BaseBranchService);
@@ -31,12 +30,8 @@
3130
}));
3231
}
3332
34-
async function switchTarget(branch: string, remote?: string) {
35-
await setBaseBranchTarget({
36-
projectId: project.id,
37-
branch,
38-
pushRemote: remote
39-
});
33+
async function switchTarget(branch: string, pushRemote?: string) {
34+
await setBaseBranchTarget({ projectId, branch, pushRemote });
4035
}
4136
4237
async function onSetBaseBranchClick() {

apps/desktop/src/components/Chrome.svelte

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,47 @@
11
<script lang="ts">
22
import ChromeHeader from '$components/ChromeHeader.svelte';
33
import ChromeSidebar from '$components/ChromeSidebar.svelte';
4+
import ProjectNotFound from '$components/ProjectNotFound.svelte';
5+
import ReduxResult from '$components/ReduxResult.svelte';
6+
import { Code, isTauriCommandError } from '$lib/backend/ipc';
7+
import { ProjectsService } from '$lib/project/projectsService';
8+
import { inject } from '@gitbutler/shared/context';
49
import type { Snippet } from 'svelte';
510
611
const {
712
projectId,
8-
children,
13+
children: children2,
914
sidebarDisabled = false
1015
}: { projectId: string; children: Snippet; sidebarDisabled?: boolean } = $props();
16+
17+
const [projectService] = inject(ProjectsService);
18+
const projectResult = $derived(projectService.getProject(projectId));
1119
</script>
1220

13-
<div class="chrome">
14-
<ChromeHeader {projectId} actionsDisabled={sidebarDisabled} />
15-
<div class="chrome-body">
16-
<ChromeSidebar disabled={sidebarDisabled} />
17-
<div class="chrome-content">
18-
{@render children()}
21+
<ReduxResult {projectId} result={projectResult.current}>
22+
{#snippet children(project, env)}
23+
<div class="chrome">
24+
<ChromeHeader
25+
projectId={env.projectId}
26+
projectTitle={project.title}
27+
actionsDisabled={sidebarDisabled}
28+
/>
29+
<div class="chrome-body">
30+
<ChromeSidebar projectId={env.projectId} disabled={sidebarDisabled} />
31+
<div class="chrome-content">
32+
{@render children2()}
33+
</div>
34+
</div>
1935
</div>
20-
</div>
21-
</div>
36+
{/snippet}
37+
{#snippet error(e)}
38+
{#if isTauriCommandError(e)}
39+
{#if e.code === Code.ProjectMissing}
40+
<ProjectNotFound {projectId} />
41+
{/if}
42+
{/if}
43+
{/snippet}
44+
</ReduxResult>
2245

2346
<style>
2447
.chrome {

apps/desktop/src/components/ChromeHeader.svelte

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,11 @@
77
import { ircEnabled } from '$lib/config/uiFeatureFlags';
88
import { IrcService } from '$lib/irc/ircService.svelte';
99
import { platformName } from '$lib/platform/platform';
10-
import { Project } from '$lib/project/project';
1110
import { ProjectsService } from '$lib/project/projectsService';
1211
import { ircPath, projectPath } from '$lib/routes/routes.svelte';
1312
import { UiState } from '$lib/state/uiState.svelte';
1413
import { TestId } from '$lib/testing/testIds';
15-
import { getContext, maybeGetContext } from '@gitbutler/shared/context';
14+
import { getContext } from '@gitbutler/shared/context';
1615
import Button from '@gitbutler/ui/Button.svelte';
1716
import Icon from '@gitbutler/ui/Icon.svelte';
1817
import NotificationButton from '@gitbutler/ui/NotificationButton.svelte';
@@ -22,33 +21,31 @@
2221
2322
type Props = {
2423
projectId: string;
24+
projectTitle: string;
2525
actionsDisabled?: boolean;
2626
};
2727
28-
const { projectId, actionsDisabled = false }: Props = $props();
28+
const { projectId, projectTitle, actionsDisabled = false }: Props = $props();
2929
3030
const projectsService = getContext(ProjectsService);
3131
const baseBranchService = getContext(BaseBranchService);
3232
const ircService = getContext(IrcService);
33-
const project = maybeGetContext(Project);
3433
const settingsService = getContext(SettingsService);
3534
const uiState = getContext(UiState);
36-
const selectedProjectId: string | undefined = $derived(project ? project.id : undefined);
37-
const baseReponse = $derived(
38-
selectedProjectId ? baseBranchService.baseBranch(selectedProjectId) : undefined
39-
);
35+
const baseReponse = $derived(projectId ? baseBranchService.baseBranch(projectId) : undefined);
4036
const base = $derived(baseReponse?.current.data);
4137
const settingsStore = $derived(settingsService.appSettings);
4238
const canUseActions = $derived($settingsStore?.featureFlags.actions ?? false);
4339
44-
const projects = $derived(projectsService.projects);
4540
const upstreamCommits = $derived(base?.behind ?? 0);
4641
const isHasUpstreamCommits = $derived(upstreamCommits > 0);
4742
4843
let modal = $state<ReturnType<typeof IntegrateUpstreamModal>>();
4944
45+
const projects = $derived(projectsService.projects());
46+
5047
const mappedProjects = $derived(
51-
$projects?.map((project) => ({
48+
projects.current.data?.map((project) => ({
5249
value: project.id,
5350
label: project.title
5451
})) || []
@@ -72,8 +69,8 @@
7269
}
7370
</script>
7471

75-
{#if selectedProjectId}
76-
<IntegrateUpstreamModal bind:this={modal} projectId={selectedProjectId} />
72+
{#if projectId}
73+
<IntegrateUpstreamModal bind:this={modal} {projectId} />
7774
{/if}
7875

7976
<div class="chrome-header" class:mac={platformName === 'macos'} data-tauri-drag-region>
@@ -85,7 +82,7 @@
8582
testId={TestId.IntegrateUpstreamCommitsButton}
8683
style="pop"
8784
onclick={openModal}
88-
disabled={!selectedProjectId || actionsDisabled}
85+
disabled={!projectId || actionsDisabled}
8986
>
9087
{upstreamCommits} upstream commits
9188
</Button>
@@ -100,7 +97,7 @@
10097
<div class="chrome-center" data-tauri-drag-region>
10198
<Select
10299
searchable
103-
value={selectedProjectId}
100+
value={projectId}
104101
options={mappedProjects}
105102
loading={newProjectLoading || cloneProjectLoading}
106103
disabled={newProjectLoading || cloneProjectLoading}
@@ -112,13 +109,13 @@
112109
>
113110
{#snippet customSelectButton()}
114111
<div class="selector-series-select">
115-
<span class="text-13 text-bold">{project?.title}</span>
112+
<span class="text-13 text-bold">{projectTitle}</span>
116113
<div class="selector-series-select__icon"><Icon name="chevron-down-small" /></div>
117114
</div>
118115
{/snippet}
119116

120117
{#snippet itemSnippet({ item, highlighted })}
121-
<SelectItem selected={item.value === selectedProjectId} {highlighted}>
118+
<SelectItem selected={item.value === projectId} {highlighted}>
122119
{item.label}
123120
</SelectItem>
124121
{/snippet}

apps/desktop/src/components/ChromeSidebar.svelte

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import KeyboardShortcutsModal from '$components/KeyboardShortcutsModal.svelte';
44
import ShareIssueModal from '$components/ShareIssueModal.svelte';
55
import { ircEnabled } from '$lib/config/uiFeatureFlags';
6-
import { Project } from '$lib/project/project';
76
import {
87
branchesPath,
98
ircPath,
@@ -32,9 +31,8 @@
3231
import { slide } from 'svelte/transition';
3332
import type { Writable } from 'svelte/store';
3433
35-
const { disabled = false }: { disabled?: boolean } = $props();
34+
const { projectId, disabled = false }: { projectId: string; disabled?: boolean } = $props();
3635
37-
const project = getContext(Project);
3836
const user = getContextStore(User);
3937
4038
let contextTriggerButton = $state<HTMLButtonElement | undefined>();
@@ -55,7 +53,7 @@
5553
<Button
5654
testId={TestId.NavigationWorkspaceButton}
5755
kind="outline"
58-
onclick={() => goto(workspacePath(project.id))}
56+
onclick={() => goto(workspacePath(projectId))}
5957
width={34}
6058
class={['btn-square', isWorkspacePath() && 'btn-active']}
6159
tooltip="Workspace"
@@ -85,7 +83,7 @@
8583
<Button
8684
testId={TestId.NavigationBranchesButton}
8785
kind="outline"
88-
onclick={() => goto(branchesPath(project.id))}
86+
onclick={() => goto(branchesPath(projectId))}
8987
width={34}
9088
class={['btn-square', isBranchesPath() && 'btn-active']}
9189
tooltip="Branches"
@@ -139,7 +137,7 @@
139137
{/if}
140138
<Button
141139
kind="outline"
142-
onclick={() => goto(historyPath(project.id))}
140+
onclick={() => goto(historyPath(projectId))}
143141
width={34}
144142
class={['btn-square', isHistoryPath() && 'btn-active']}
145143
tooltip="Operations history"
@@ -187,7 +185,7 @@
187185
{/if}
188186
<Button
189187
kind="outline"
190-
onclick={() => goto(ircPath(project.id))}
188+
onclick={() => goto(ircPath(projectId))}
191189
icon="chat"
192190
width={34}
193191
class={['btn-square', isIrcPath() && 'btn-active']}
@@ -205,7 +203,7 @@
205203
{/if}
206204
<Button
207205
kind="outline"
208-
onclick={() => goto(newProjectSettingsPath(project.id))}
206+
onclick={() => goto(newProjectSettingsPath(projectId))}
209207
width={34}
210208
class={['btn-square', isNewProjectSettingsPath() && 'btn-active']}
211209
tooltipPosition="top"

apps/desktop/src/components/CloudForm.svelte

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,20 @@
44
import Section from '$components/Section.svelte';
55
import WelcomeSigninAction from '$components/WelcomeSigninAction.svelte';
66
import { projectAiExperimentalFeaturesEnabled, projectAiGenEnabled } from '$lib/config/config';
7-
import { Project } from '$lib/project/project';
87
import { UserService } from '$lib/user/userService';
98
import { getContext } from '@gitbutler/shared/context';
109
import Button from '@gitbutler/ui/Button.svelte';
1110
import SectionCard from '@gitbutler/ui/SectionCard.svelte';
1211
import Spacer from '@gitbutler/ui/Spacer.svelte';
1312
import Toggle from '@gitbutler/ui/Toggle.svelte';
1413
14+
const { projectId }: { projectId: string } = $props();
15+
1516
const userService = getContext(UserService);
16-
const project = getContext(Project);
1717
const user = userService.user;
1818
19-
const aiGenEnabled = $derived(projectAiGenEnabled(project.id));
20-
const experimentalAiGenEnabled = $derived(projectAiExperimentalFeaturesEnabled(project.id));
19+
const aiGenEnabled = $derived(projectAiGenEnabled(projectId));
20+
const experimentalAiGenEnabled = $derived(projectAiExperimentalFeaturesEnabled(projectId));
2121
</script>
2222

2323
<Section>
@@ -87,8 +87,8 @@
8787
Custom prompts
8888
{/snippet}
8989

90-
<AiPromptSelect promptUse="commits" />
91-
<AiPromptSelect promptUse="branches" />
90+
<AiPromptSelect {projectId} promptUse="commits" />
91+
<AiPromptSelect {projectId} promptUse="branches" />
9292

9393
<Spacer margin={8} />
9494

0 commit comments

Comments
 (0)