|
6 | 6 |
|
7 | 7 | interface Props { |
8 | 8 | branchName: string; |
| 9 | + currentUpstream?: string; |
9 | 10 | onClose: () => void; |
10 | | - onSet: (remote: string, remoteBranch: string) => void; |
| 11 | + onSet: (remote: string, remoteBranch: string, createRemote: boolean) => void; |
11 | 12 | } |
12 | 13 |
|
13 | | - let { branchName, onClose, onSet }: Props = $props(); |
| 14 | + let { branchName, currentUpstream = '', onClose, onSet }: Props = $props(); |
14 | 15 |
|
15 | 16 | const remoteNames = $derived([...new Set(branchStore.remotes.map(r => r.name))]); |
| 17 | +
|
| 18 | + function parseUpstream(upstream: string) { |
| 19 | + const slashIdx = upstream.indexOf('/'); |
| 20 | + if (slashIdx === -1) return { remote: '', branch: upstream }; |
| 21 | + return { remote: upstream.slice(0, slashIdx), branch: upstream.slice(slashIdx + 1) }; |
| 22 | + } |
| 23 | +
|
16 | 24 | // svelte-ignore state_referenced_locally |
17 | | - let selectedRemote = $state(remoteNames[0] ?? 'origin'); |
| 25 | + const parsed = parseUpstream(currentUpstream); |
| 26 | + // svelte-ignore state_referenced_locally |
| 27 | + let selectedRemote = $state(parsed.remote || remoteNames[0] || 'origin'); |
18 | 28 |
|
19 | 29 | const remoteBranchesForRemote = $derived( |
20 | 30 | branchStore.remoteBranches |
21 | 31 | .filter(b => b.name.startsWith(selectedRemote + '/')) |
22 | 32 | .map(b => b.name.substring(selectedRemote.length + 1)) |
23 | 33 | ); |
24 | 34 |
|
| 35 | + const remoteOptions = $derived(remoteNames.map(r => ({ value: r, label: r, color: '' }))); |
| 36 | + const branchOptions = $derived( |
| 37 | + remoteBranchesForRemote.map(b => ({ value: b, label: `${selectedRemote}/${b}`, color: '' })) |
| 38 | + ); |
| 39 | +
|
| 40 | + const hasBranchOptions = $derived(remoteBranchesForRemote.length > 0); |
| 41 | +
|
| 42 | + // svelte-ignore state_referenced_locally |
| 43 | + let dropdownBranch = $state(parsed.branch || branchName); |
25 | 44 | // svelte-ignore state_referenced_locally |
26 | | - let selectedRemoteBranch = $state(branchName); |
| 45 | + let textBranch = $state(currentUpstream ? '' : branchName); |
| 46 | + // upstream이 없으면 새로 만드는 케이스 → 텍스트 입력으로 시작 |
| 47 | + // svelte-ignore state_referenced_locally |
| 48 | + let manualInput = $state(!currentUpstream); |
27 | 49 |
|
28 | | - const remoteOptions = $derived(remoteNames.map(r => ({ value: r, label: r, color: '' }))); |
29 | | - const branchOptions = $derived(remoteBranchesForRemote.map(b => ({ value: b, label: `${selectedRemote}/${b}`, color: '' }))); |
| 50 | + const useDropdown = $derived(hasBranchOptions && !manualInput); |
| 51 | + const activeBranch = $derived(manualInput ? textBranch : dropdownBranch); |
| 52 | +
|
| 53 | + const remoteBranchExists = $derived( |
| 54 | + useDropdown || remoteBranchesForRemote.includes(activeBranch.trim()) |
| 55 | + ); |
30 | 56 |
|
31 | 57 | function handleSubmit() { |
32 | | - if (selectedRemote && selectedRemoteBranch.trim()) { |
33 | | - onSet(selectedRemote, selectedRemoteBranch.trim()); |
| 58 | + if (selectedRemote && activeBranch.trim()) { |
| 59 | + onSet(selectedRemote, activeBranch.trim(), !remoteBranchExists); |
34 | 60 | } |
35 | 61 | } |
36 | 62 | </script> |
|
40 | 66 | <div class="modal-context-card"> |
41 | 67 | <span class="modal-pill modal-pill--target"><i class="codicon codicon-git-branch"></i>{branchName}</span> |
42 | 68 | <i class="codicon codicon-arrow-both" style="color: var(--text-secondary);"></i> |
43 | | - <span class="modal-pill modal-pill--source"><i class="codicon codicon-cloud"></i>{selectedRemote}/{selectedRemoteBranch || branchName}</span> |
| 69 | + <span class="modal-pill modal-pill--source"><i class="codicon codicon-cloud"></i>{selectedRemote}/{activeBranch || branchName}</span> |
44 | 70 | </div> |
45 | 71 |
|
46 | 72 | {#if remoteNames.length > 1} |
|
56 | 82 | {/if} |
57 | 83 |
|
58 | 84 | <div class="modal-form-group"> |
59 | | - <div class="modal-field-label">{t('setUpstream.remoteBranch')}</div> |
60 | | - <ColorSelect |
61 | | - options={branchOptions} |
62 | | - value={selectedRemoteBranch} |
63 | | - onChange={(v) => { selectedRemoteBranch = v; }} |
64 | | - showDot={false} |
65 | | - /> |
| 85 | + <div class="modal-field-label-row"> |
| 86 | + <span class="modal-field-label">{t('setUpstream.remoteBranch')}</span> |
| 87 | + {#if hasBranchOptions} |
| 88 | + <button class="toggle-btn" onclick={() => { manualInput = !manualInput; }}> |
| 89 | + <i class="codicon {manualInput ? 'codicon-list-unordered' : 'codicon-edit'}"></i> |
| 90 | + <span>{manualInput ? t('setUpstream.selectFromList') : t('setUpstream.typeManually')}</span> |
| 91 | + </button> |
| 92 | + {/if} |
| 93 | + </div> |
| 94 | + {#if useDropdown} |
| 95 | + <ColorSelect |
| 96 | + options={branchOptions} |
| 97 | + value={dropdownBranch} |
| 98 | + onChange={(v) => { dropdownBranch = v; }} |
| 99 | + showDot={false} |
| 100 | + /> |
| 101 | + {:else} |
| 102 | + <input |
| 103 | + class="modal-input" |
| 104 | + type="text" |
| 105 | + bind:value={textBranch} |
| 106 | + /> |
| 107 | + {/if} |
66 | 108 | </div> |
67 | 109 |
|
| 110 | + {#if activeBranch.trim() && !remoteBranchExists} |
| 111 | + <p class="notice"><i class="codicon codicon-info"></i>{t('setUpstream.willCreate')}</p> |
| 112 | + {/if} |
| 113 | + |
68 | 114 | <div class="form-actions"> |
69 | 115 | <button onclick={onClose}>{t('common.cancel')}</button> |
70 | | - <button class="primary" onclick={handleSubmit} disabled={!selectedRemoteBranch.trim()}>{t('setUpstream.set')}</button> |
| 116 | + <button class="primary" onclick={handleSubmit} disabled={!activeBranch.trim()}>{t('setUpstream.set')}</button> |
71 | 117 | </div> |
72 | 118 | </Modal> |
73 | 119 |
|
| 120 | +<style> |
| 121 | + .modal-field-label-row { |
| 122 | + display: flex; |
| 123 | + align-items: center; |
| 124 | + justify-content: space-between; |
| 125 | + margin-bottom: 6px; |
| 126 | + } |
| 127 | +
|
| 128 | + .modal-field-label-row .modal-field-label { |
| 129 | + margin-bottom: 0; |
| 130 | + } |
| 131 | +
|
| 132 | + .toggle-btn { |
| 133 | + display: flex; |
| 134 | + align-items: center; |
| 135 | + gap: 3px; |
| 136 | + background: none; |
| 137 | + border: none; |
| 138 | + padding: 0; |
| 139 | + font-size: 11px; |
| 140 | + color: var(--text-secondary); |
| 141 | + cursor: pointer; |
| 142 | + font-family: inherit; |
| 143 | + } |
| 144 | +
|
| 145 | + .toggle-btn:hover { |
| 146 | + color: var(--text-primary); |
| 147 | + } |
| 148 | +
|
| 149 | + .toggle-btn:hover span { |
| 150 | + text-decoration: underline; |
| 151 | + } |
| 152 | +
|
| 153 | + .toggle-btn .codicon { |
| 154 | + font-size: 11px; |
| 155 | + } |
| 156 | +
|
| 157 | + .notice { |
| 158 | + display: flex; |
| 159 | + align-items: center; |
| 160 | + gap: 6px; |
| 161 | + font-size: 12px; |
| 162 | + color: var(--text-secondary); |
| 163 | + margin: 0; |
| 164 | + } |
| 165 | +</style> |
0 commit comments