|
1 | 1 | <script lang="ts">
|
2 | 2 | import CommitMessageInput from './CommitMessageInput.svelte';
|
| 3 | + import { PostHogWrapper } from '$lib/analytics/posthog'; |
3 | 4 | import ContextMenuItem from '$lib/components/contextmenu/ContextMenuItem.svelte';
|
4 | 5 | import ContextMenuSection from '$lib/components/contextmenu/ContextMenuSection.svelte';
|
5 | 6 | import { persistedCommitMessage } from '$lib/config/config';
|
6 | 7 | import { cloudCommunicationFunctionality } from '$lib/config/uiFeatureFlags';
|
7 | 8 | import { SyncedSnapshotService } from '$lib/history/syncedSnapshotService';
|
| 9 | + import { HooksService } from '$lib/hooks/hooksService'; |
| 10 | + import { showError } from '$lib/notifications/toasts'; |
8 | 11 | import DropDownButton from '$lib/shared/DropDownButton.svelte';
|
9 | 12 | import { intersectionObserver } from '$lib/utils/intersectionObserver';
|
10 | 13 | import { BranchController } from '$lib/vbranches/branchController';
|
|
25 | 28 | const { projectId, expanded, hasSectionsAfter }: Props = $props();
|
26 | 29 |
|
27 | 30 | const branchController = getContext(BranchController);
|
| 31 | + const hooksService = getContext(HooksService); |
| 32 | + const posthog = getContext(PostHogWrapper); |
28 | 33 | const syncedSnapshotService = getContext(SyncedSnapshotService);
|
29 | 34 | const canTakeSnapshot = syncedSnapshotService.canTakeSnapshot;
|
30 | 35 | const selectedOwnership = getContextStore(SelectedOwnership);
|
31 | 36 | const stack = getContextStore(BranchStack);
|
32 | 37 | const commitMessage = persistedCommitMessage(projectId, $stack.id);
|
| 38 | + const canShowCommitAndPublish = $derived($cloudCommunicationFunctionality && $canTakeSnapshot); |
33 | 39 |
|
34 | 40 | let commitMessageInput = $state<CommitMessageInput>();
|
35 | 41 | let isCommitting = $state(false);
|
36 | 42 | let commitMessageValid = $state(false);
|
37 | 43 | let isInViewport = $state(false);
|
38 | 44 |
|
| 45 | + let commitAndPublish = $state(false); |
| 46 | + let commitButton = $state<DropDownButton>(); |
| 47 | +
|
39 | 48 | async function commit() {
|
40 |
| - const message = $commitMessage; |
41 | 49 | isCommitting = true;
|
42 |
| - try { |
43 |
| - await branchController.commitBranch($stack.id, message.trim(), $selectedOwnership.toString()); |
44 |
| - $commitMessage = ''; |
| 50 | + const message = $commitMessage; |
| 51 | + const ownership = $selectedOwnership.toString(); |
45 | 52 |
|
46 |
| - if (commitAndPublish) { |
47 |
| - syncedSnapshotService.takeSyncedSnapshot($stack.id); |
| 53 | + try { |
| 54 | + const preCommitHook = await hooksService.preCommit(projectId, ownership); |
| 55 | + if (preCommitHook.status === 'failure') { |
| 56 | + showError('Pre-commit hook failed', preCommitHook.error); |
| 57 | + return; // Abort commit if hook failed. |
48 | 58 | }
|
| 59 | + await branchController.commit($stack.id, message.trim(), ownership); |
| 60 | + } catch (err: unknown) { |
| 61 | + showError('Failed to commit changes', err); |
| 62 | + posthog.capture('Commit Failed', { error: err }); |
| 63 | + return; |
49 | 64 | } finally {
|
50 | 65 | isCommitting = false;
|
51 | 66 | }
|
| 67 | +
|
| 68 | + // Run both without awaiting unless commit failed. |
| 69 | + runPostCommitActions(); |
| 70 | + runPostCommitHook(); |
| 71 | + } |
| 72 | +
|
| 73 | + async function runPostCommitActions() { |
| 74 | + // Clear the commit message editor. |
| 75 | + commitMessage.set(''); |
| 76 | +
|
| 77 | + // Publishing a snapshot seems to imply posting a bleep. |
| 78 | + if (commitAndPublish) { |
| 79 | + await syncedSnapshotService.takeSyncedSnapshot($stack.id); |
| 80 | + } |
| 81 | + } |
| 82 | +
|
| 83 | + async function runPostCommitHook() { |
| 84 | + const postCommitHook = await hooksService.postCommit(projectId); |
| 85 | + if (postCommitHook.status === 'failure') { |
| 86 | + showError('Post-commit hook failed', postCommitHook.error); |
| 87 | + } |
52 | 88 | }
|
53 | 89 |
|
54 | 90 | function close() {
|
|
60 | 96 | await tick();
|
61 | 97 | commitMessageInput?.focus();
|
62 | 98 | }
|
63 |
| -
|
64 |
| - const canShowCommitAndPublish = $derived($cloudCommunicationFunctionality && $canTakeSnapshot); |
65 |
| -
|
66 |
| - let commitAndPublish = $state(false); |
67 |
| - let commitButton = $state<DropDownButton>(); |
68 | 99 | </script>
|
69 | 100 |
|
70 | 101 | <div
|
|
0 commit comments