|
| 1 | +<script lang="ts"> |
| 2 | + import { goto } from '$app/navigation'; |
| 3 | + import InfoMessage from '$components/InfoMessage.svelte'; |
| 4 | + import ReduxResult from '$components/ReduxResult.svelte'; |
| 5 | + import { CHERRY_APPLY_SERVICE } from '$lib/cherryApply/cherryApplyService'; |
| 6 | + import { workspacePath } from '$lib/routes/routes.svelte'; |
| 7 | + import { getStackName } from '$lib/stacks/stack'; |
| 8 | + import { STACK_SERVICE } from '$lib/stacks/stackService.svelte'; |
| 9 | + import { combineResults } from '$lib/state/helpers'; |
| 10 | + import { inject } from '@gitbutler/core/context'; |
| 11 | + import { Button, Modal, RadioButton, SectionCard } from '@gitbutler/ui'; |
| 12 | +
|
| 13 | + type Props = { |
| 14 | + projectId: string; |
| 15 | + /** The commit hash to cherry-apply */ |
| 16 | + subject?: string; |
| 17 | + }; |
| 18 | +
|
| 19 | + let { projectId, subject }: Props = $props(); |
| 20 | +
|
| 21 | + const cherryApplyService = inject(CHERRY_APPLY_SERVICE); |
| 22 | + const stackService = inject(STACK_SERVICE); |
| 23 | +
|
| 24 | + let modalRef = $state<Modal>(); |
| 25 | +
|
| 26 | + const statusResult = $derived( |
| 27 | + subject ? cherryApplyService.status({ projectId, subject }) : undefined |
| 28 | + ); |
| 29 | + const stacksResult = $derived(stackService.stacks(projectId)); |
| 30 | + const status = $derived(statusResult?.response); |
| 31 | +
|
| 32 | + let selectedStackId = $state<string | undefined>(undefined); |
| 33 | + const [applyCommit, applyResult] = cherryApplyService.apply(); |
| 34 | +
|
| 35 | + $effect(() => { |
| 36 | + if (status?.type === 'lockedToStack') { |
| 37 | + selectedStackId = status.subject; |
| 38 | + } |
| 39 | + }); |
| 40 | +
|
| 41 | + export function close() { |
| 42 | + modalRef?.close(); |
| 43 | + } |
| 44 | +
|
| 45 | + export function open() { |
| 46 | + modalRef?.show(); |
| 47 | + } |
| 48 | +
|
| 49 | + async function handleApply() { |
| 50 | + if (!selectedStackId || !subject) return; |
| 51 | +
|
| 52 | + await applyCommit({ |
| 53 | + projectId, |
| 54 | + subject, |
| 55 | + target: selectedStackId |
| 56 | + }); |
| 57 | +
|
| 58 | + goto(workspacePath(projectId)); |
| 59 | +
|
| 60 | + close(); |
| 61 | + } |
| 62 | +
|
| 63 | + function getStatusMessage(): string { |
| 64 | + if (!status) return ''; |
| 65 | +
|
| 66 | + switch (status.type) { |
| 67 | + case 'applicableToAnyStack': |
| 68 | + return 'This commit can be applied to any stack. Select a stack below.'; |
| 69 | + case 'lockedToStack': |
| 70 | + return 'This commit conflicts when applied to the selected stack, as such it must be applied to the selected stack to avoid a workspace conflict.'; |
| 71 | + case 'causesWorkspaceConflict': |
| 72 | + return "This commit can't be applied since it would cause a workspace."; |
| 73 | + case 'noStacks': |
| 74 | + return 'No stacks are currently applied to the workspace.'; |
| 75 | + } |
| 76 | + } |
| 77 | +
|
| 78 | + const canApply = $derived( |
| 79 | + status?.type === 'applicableToAnyStack' || status?.type === 'lockedToStack' |
| 80 | + ); |
| 81 | + const canSelectStack = $derived(status?.type === 'applicableToAnyStack'); |
| 82 | + const isApplying = $derived(applyResult.current.isLoading); |
| 83 | +
|
| 84 | + function handleStackSelectionChange(form: HTMLFormElement) { |
| 85 | + const formData = new FormData(form); |
| 86 | + const selected = formData.get('stackSelection') as string | null; |
| 87 | + if (selected) { |
| 88 | + selectedStackId = selected; |
| 89 | + } |
| 90 | + } |
| 91 | +
|
| 92 | + const messageStyle = $derived(status?.type === 'causesWorkspaceConflict' ? 'warning' : 'info'); |
| 93 | +</script> |
| 94 | + |
| 95 | +<Modal bind:this={modalRef} title="Cherry-pick commit" width={500}> |
| 96 | + {#if statusResult} |
| 97 | + <ReduxResult {projectId} result={combineResults(statusResult?.result, stacksResult.result)}> |
| 98 | + {#snippet children([_status, stacks], { projectId: _projectId })} |
| 99 | + <div class="cherry-apply-modal"> |
| 100 | + <InfoMessage style={messageStyle} outlined filled> |
| 101 | + {#snippet content()} |
| 102 | + {getStatusMessage()} |
| 103 | + {/snippet} |
| 104 | + </InfoMessage> |
| 105 | + |
| 106 | + {#if canApply && stacks.length > 0} |
| 107 | + <form onchange={(e) => handleStackSelectionChange(e.currentTarget)}> |
| 108 | + {#each stacks as stack, idx (stack.id)} |
| 109 | + {@const isFirst = idx === 0} |
| 110 | + {@const isLast = idx === stacks.length - 1} |
| 111 | + {@const isDisabled = !canSelectStack && selectedStackId !== stack.id} |
| 112 | + <SectionCard |
| 113 | + orientation="row" |
| 114 | + roundedBottom={isLast} |
| 115 | + roundedTop={isFirst} |
| 116 | + labelFor="stack-{stack.id}" |
| 117 | + disabled={isDisabled} |
| 118 | + > |
| 119 | + {#snippet title()} |
| 120 | + {getStackName(stack)} |
| 121 | + {/snippet} |
| 122 | + {#snippet caption()} |
| 123 | + {stack.heads.length} |
| 124 | + {stack.heads.length === 1 ? 'branch' : 'branches'} |
| 125 | + {/snippet} |
| 126 | + {#snippet actions()} |
| 127 | + <RadioButton |
| 128 | + name="stackSelection" |
| 129 | + value={stack.id} |
| 130 | + id="stack-{stack.id}" |
| 131 | + checked={selectedStackId === stack.id} |
| 132 | + disabled={isDisabled} |
| 133 | + /> |
| 134 | + {/snippet} |
| 135 | + </SectionCard> |
| 136 | + {/each} |
| 137 | + </form> |
| 138 | + {/if} |
| 139 | + </div> |
| 140 | + {/snippet} |
| 141 | + </ReduxResult> |
| 142 | + {/if} |
| 143 | + {#snippet controls()} |
| 144 | + <Button kind="outline" onclick={close} disabled={isApplying}>Cancel</Button> |
| 145 | + <Button |
| 146 | + style="pop" |
| 147 | + onclick={handleApply} |
| 148 | + disabled={!canApply || !selectedStackId || isApplying} |
| 149 | + loading={isApplying} |
| 150 | + > |
| 151 | + Apply commit |
| 152 | + </Button> |
| 153 | + {/snippet} |
| 154 | +</Modal> |
| 155 | + |
| 156 | +<style lang="postcss"> |
| 157 | + .cherry-apply-modal { |
| 158 | + display: flex; |
| 159 | + flex-direction: column; |
| 160 | + gap: 16px; |
| 161 | + } |
| 162 | +</style> |
0 commit comments