| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 1 | // sketch-diff-range-picker.ts |
| 2 | // Component for selecting commit range for diffs |
| 3 | |
| 4 | import { css, html, LitElement } from "lit"; |
| 5 | import { customElement, property, state } from "lit/decorators.js"; |
| 6 | import { GitDataService, DefaultGitDataService } from "./git-data-service"; |
| 7 | import { GitLogEntry } from "../types"; |
| 8 | |
| 9 | /** |
| 10 | * Range type for diff views |
| 11 | */ |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 12 | export type DiffRange = |
| 13 | | { type: "range"; from: string; to: string } |
| 14 | | { type: "single"; commit: string }; |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 15 | |
| 16 | /** |
| 17 | * Component for selecting commit range for diffs |
| 18 | */ |
| 19 | @customElement("sketch-diff-range-picker") |
| 20 | export class SketchDiffRangePicker extends LitElement { |
| 21 | @property({ type: Array }) |
| 22 | commits: GitLogEntry[] = []; |
| 23 | |
| 24 | @state() |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 25 | private rangeType: "range" | "single" = "range"; |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 26 | |
| 27 | @state() |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 28 | private fromCommit: string = ""; |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 29 | |
| 30 | @state() |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 31 | private toCommit: string = ""; |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 32 | |
| 33 | @state() |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 34 | private singleCommit: string = ""; |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 35 | |
| 36 | @state() |
| 37 | private loading: boolean = true; |
| 38 | |
| 39 | @state() |
| 40 | private error: string | null = null; |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 41 | |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 42 | @property({ attribute: false, type: Object }) |
| 43 | gitService!: GitDataService; |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 44 | |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 45 | constructor() { |
| 46 | super(); |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 47 | console.log("SketchDiffRangePicker initialized"); |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | static styles = css` |
| 51 | :host { |
| 52 | display: block; |
| 53 | width: 100%; |
| 54 | font-family: var(--font-family, system-ui, sans-serif); |
| 55 | color: var(--text-color, #333); |
| 56 | } |
| 57 | |
| 58 | .range-picker { |
| 59 | display: flex; |
| 60 | flex-direction: row; |
| 61 | align-items: center; |
| 62 | gap: 12px; |
| 63 | padding: 12px; |
| 64 | background-color: var(--background-light, #f8f8f8); |
| 65 | border-radius: 4px; |
| 66 | border: 1px solid var(--border-color, #e0e0e0); |
| 67 | flex-wrap: wrap; /* Allow wrapping on small screens */ |
| 68 | width: 100%; |
| 69 | box-sizing: border-box; |
| 70 | } |
| 71 | |
| 72 | .range-type-selector { |
| 73 | display: flex; |
| 74 | gap: 16px; |
| 75 | flex-shrink: 0; |
| 76 | } |
| 77 | |
| 78 | .range-type-option { |
| 79 | display: flex; |
| 80 | align-items: center; |
| 81 | gap: 6px; |
| 82 | cursor: pointer; |
| 83 | white-space: nowrap; |
| 84 | } |
| 85 | |
| 86 | .commit-selectors { |
| 87 | display: flex; |
| 88 | flex-direction: row; |
| 89 | align-items: center; |
| 90 | gap: 12px; |
| 91 | flex: 1; |
| 92 | flex-wrap: wrap; /* Allow wrapping on small screens */ |
| 93 | } |
| 94 | |
| 95 | .commit-selector { |
| 96 | display: flex; |
| 97 | align-items: center; |
| 98 | gap: 8px; |
| 99 | flex: 1; |
| 100 | min-width: 200px; |
| 101 | max-width: calc(50% - 12px); /* Half width minus half the gap */ |
| 102 | overflow: hidden; |
| 103 | } |
| 104 | |
| 105 | select { |
| 106 | padding: 6px 8px; |
| 107 | border-radius: 4px; |
| 108 | border: 1px solid var(--border-color, #e0e0e0); |
| 109 | background-color: var(--background, #fff); |
| 110 | max-width: 100%; |
| 111 | overflow: hidden; |
| 112 | text-overflow: ellipsis; |
| 113 | white-space: nowrap; |
| 114 | } |
| 115 | |
| 116 | label { |
| 117 | font-weight: 500; |
| 118 | font-size: 14px; |
| 119 | } |
| 120 | |
| 121 | .loading { |
| 122 | font-style: italic; |
| 123 | color: var(--text-muted, #666); |
| 124 | } |
| 125 | |
| 126 | .error { |
| 127 | color: var(--error-color, #dc3545); |
| 128 | font-size: 14px; |
| 129 | } |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 130 | |
| Philip Zeyliger | e89b308 | 2025-05-29 03:16:06 +0000 | [diff] [blame] | 131 | .refresh-button { |
| 132 | padding: 6px 12px; |
| 133 | background-color: #f0f0f0; |
| 134 | color: var(--text-color, #333); |
| 135 | border: 1px solid var(--border-color, #e0e0e0); |
| 136 | border-radius: 4px; |
| 137 | cursor: pointer; |
| 138 | font-size: 14px; |
| 139 | transition: background-color 0.2s; |
| 140 | white-space: nowrap; |
| 141 | display: flex; |
| 142 | align-items: center; |
| 143 | gap: 4px; |
| 144 | } |
| 145 | |
| 146 | .refresh-button:hover { |
| 147 | background-color: #e0e0e0; |
| 148 | } |
| 149 | |
| 150 | .refresh-button:disabled { |
| 151 | background-color: #f8f8f8; |
| 152 | color: #999; |
| 153 | cursor: not-allowed; |
| 154 | } |
| 155 | |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 156 | @media (max-width: 768px) { |
| 157 | .commit-selector { |
| 158 | max-width: 100%; |
| 159 | } |
| 160 | } |
| 161 | `; |
| 162 | |
| 163 | connectedCallback() { |
| 164 | super.connectedCallback(); |
| 165 | // Wait for DOM to be fully loaded to ensure proper initialization order |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 166 | if (document.readyState === "complete") { |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 167 | this.loadCommits(); |
| 168 | } else { |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 169 | window.addEventListener("load", () => { |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 170 | setTimeout(() => this.loadCommits(), 0); // Give time for provider initialization |
| 171 | }); |
| 172 | } |
| Autoformatter | 9abf803 | 2025-06-14 23:24:08 +0000 | [diff] [blame^] | 173 | |
| David Crawshaw | 938d2dc | 2025-06-14 22:17:33 +0000 | [diff] [blame] | 174 | // Listen for popstate events to handle browser back/forward navigation |
| Autoformatter | 9abf803 | 2025-06-14 23:24:08 +0000 | [diff] [blame^] | 175 | window.addEventListener("popstate", this.handlePopState.bind(this)); |
| David Crawshaw | 938d2dc | 2025-06-14 22:17:33 +0000 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | disconnectedCallback() { |
| 179 | super.disconnectedCallback(); |
| Autoformatter | 9abf803 | 2025-06-14 23:24:08 +0000 | [diff] [blame^] | 180 | window.removeEventListener("popstate", this.handlePopState.bind(this)); |
| David Crawshaw | 938d2dc | 2025-06-14 22:17:33 +0000 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | /** |
| 184 | * Handle browser back/forward navigation |
| 185 | */ |
| 186 | private handlePopState() { |
| 187 | // Re-initialize from URL parameters when user navigates |
| 188 | if (this.commits.length > 0) { |
| 189 | const initializedFromUrl = this.initializeFromUrlParams(); |
| 190 | if (initializedFromUrl) { |
| 191 | // Force re-render and dispatch event |
| 192 | this.requestUpdate(); |
| 193 | this.dispatchRangeEvent(); |
| 194 | } |
| 195 | } |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | render() { |
| 199 | return html` |
| 200 | <div class="range-picker"> |
| 201 | ${this.loading |
| 202 | ? html`<div class="loading">Loading commits...</div>` |
| 203 | : this.error |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 204 | ? html`<div class="error">${this.error}</div>` |
| 205 | : this.renderRangePicker()} |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 206 | </div> |
| 207 | `; |
| 208 | } |
| 209 | |
| 210 | renderRangePicker() { |
| 211 | return html` |
| 212 | <div class="range-type-selector"> |
| 213 | <label class="range-type-option"> |
| 214 | <input |
| 215 | type="radio" |
| 216 | name="rangeType" |
| 217 | value="range" |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 218 | ?checked=${this.rangeType === "range"} |
| 219 | @change=${() => this.setRangeType("range")} |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 220 | /> |
| 221 | Commit Range |
| 222 | </label> |
| 223 | <label class="range-type-option"> |
| 224 | <input |
| 225 | type="radio" |
| 226 | name="rangeType" |
| 227 | value="single" |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 228 | ?checked=${this.rangeType === "single"} |
| 229 | @change=${() => this.setRangeType("single")} |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 230 | /> |
| 231 | Single Commit |
| 232 | </label> |
| 233 | </div> |
| 234 | |
| 235 | <div class="commit-selectors"> |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 236 | ${this.rangeType === "range" |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 237 | ? this.renderRangeSelectors() |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 238 | : this.renderSingleSelector()} |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 239 | </div> |
| Philip Zeyliger | e89b308 | 2025-05-29 03:16:06 +0000 | [diff] [blame] | 240 | |
| 241 | <button |
| 242 | class="refresh-button" |
| 243 | @click="${this.handleRefresh}" |
| 244 | ?disabled="${this.loading}" |
| 245 | title="Refresh commit list" |
| 246 | > |
| 247 | 🔄 Refresh |
| 248 | </button> |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 249 | `; |
| 250 | } |
| 251 | |
| 252 | renderRangeSelectors() { |
| 253 | return html` |
| 254 | <div class="commit-selector"> |
| 255 | <label for="fromCommit">From:</label> |
| 256 | <select |
| 257 | id="fromCommit" |
| 258 | .value=${this.fromCommit} |
| 259 | @change=${this.handleFromChange} |
| 260 | > |
| 261 | ${this.commits.map( |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 262 | (commit) => html` |
| 263 | <option |
| 264 | value=${commit.hash} |
| 265 | ?selected=${commit.hash === this.fromCommit} |
| 266 | > |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 267 | ${this.formatCommitOption(commit)} |
| 268 | </option> |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 269 | `, |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 270 | )} |
| 271 | </select> |
| 272 | </div> |
| 273 | <div class="commit-selector"> |
| 274 | <label for="toCommit">To:</label> |
| 275 | <select |
| 276 | id="toCommit" |
| 277 | .value=${this.toCommit} |
| 278 | @change=${this.handleToChange} |
| 279 | > |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 280 | <option value="" ?selected=${this.toCommit === ""}> |
| 281 | Uncommitted Changes |
| 282 | </option> |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 283 | ${this.commits.map( |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 284 | (commit) => html` |
| 285 | <option |
| 286 | value=${commit.hash} |
| 287 | ?selected=${commit.hash === this.toCommit} |
| 288 | > |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 289 | ${this.formatCommitOption(commit)} |
| 290 | </option> |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 291 | `, |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 292 | )} |
| 293 | </select> |
| 294 | </div> |
| 295 | `; |
| 296 | } |
| 297 | |
| 298 | renderSingleSelector() { |
| 299 | return html` |
| 300 | <div class="commit-selector"> |
| 301 | <label for="singleCommit">Commit:</label> |
| 302 | <select |
| 303 | id="singleCommit" |
| 304 | .value=${this.singleCommit} |
| 305 | @change=${this.handleSingleChange} |
| 306 | > |
| 307 | ${this.commits.map( |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 308 | (commit) => html` |
| 309 | <option |
| 310 | value=${commit.hash} |
| 311 | ?selected=${commit.hash === this.singleCommit} |
| 312 | > |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 313 | ${this.formatCommitOption(commit)} |
| 314 | </option> |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 315 | `, |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 316 | )} |
| 317 | </select> |
| 318 | </div> |
| 319 | `; |
| 320 | } |
| 321 | |
| 322 | /** |
| 323 | * Format a commit for display in the dropdown |
| 324 | */ |
| 325 | formatCommitOption(commit: GitLogEntry): string { |
| 326 | const shortHash = commit.hash.substring(0, 7); |
| 327 | let label = `${shortHash} ${commit.subject}`; |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 328 | |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 329 | if (commit.refs && commit.refs.length > 0) { |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 330 | label += ` (${commit.refs.join(", ")})`; |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 331 | } |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 332 | |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 333 | return label; |
| 334 | } |
| 335 | |
| 336 | /** |
| 337 | * Load commits from the Git data service |
| 338 | */ |
| 339 | async loadCommits() { |
| 340 | this.loading = true; |
| 341 | this.error = null; |
| 342 | |
| 343 | if (!this.gitService) { |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 344 | console.error("GitService was not provided to sketch-diff-range-picker"); |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 345 | throw Error(); |
| 346 | } |
| 347 | |
| 348 | try { |
| 349 | // Get the base commit reference |
| 350 | const baseCommitRef = await this.gitService.getBaseCommitRef(); |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 351 | |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 352 | // Load commit history |
| 353 | this.commits = await this.gitService.getCommitHistory(baseCommitRef); |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 354 | |
| David Crawshaw | 938d2dc | 2025-06-14 22:17:33 +0000 | [diff] [blame] | 355 | // Check if we should initialize from URL parameters first |
| 356 | const initializedFromUrl = this.initializeFromUrlParams(); |
| Autoformatter | 9abf803 | 2025-06-14 23:24:08 +0000 | [diff] [blame^] | 357 | |
| David Crawshaw | 938d2dc | 2025-06-14 22:17:33 +0000 | [diff] [blame] | 358 | // Set default selections only if not initialized from URL |
| 359 | if (this.commits.length > 0 && !initializedFromUrl) { |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 360 | // For range, default is base to HEAD |
| 361 | // TODO: is sketch-base right in the unsafe context, where it's sketch-base-... |
| 362 | // should this be startswith? |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 363 | const baseCommit = this.commits.find( |
| 364 | (c) => c.refs && c.refs.some((ref) => ref.includes("sketch-base")), |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 365 | ); |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 366 | |
| 367 | this.fromCommit = baseCommit |
| 368 | ? baseCommit.hash |
| 369 | : this.commits[this.commits.length - 1].hash; |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 370 | // Default to Uncommitted Changes by setting toCommit to empty string |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 371 | this.toCommit = ""; // Empty string represents uncommitted changes |
| 372 | |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 373 | // For single, default to HEAD |
| 374 | this.singleCommit = this.commits[0].hash; |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 375 | } |
| Autoformatter | 9abf803 | 2025-06-14 23:24:08 +0000 | [diff] [blame^] | 376 | |
| David Crawshaw | 938d2dc | 2025-06-14 22:17:33 +0000 | [diff] [blame] | 377 | // Always dispatch range event to ensure diff view is updated |
| 378 | this.dispatchRangeEvent(); |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 379 | } catch (error) { |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 380 | console.error("Error loading commits:", error); |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 381 | this.error = `Error loading commits: ${error.message}`; |
| 382 | } finally { |
| 383 | this.loading = false; |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | /** |
| 388 | * Handle range type change |
| 389 | */ |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 390 | setRangeType(type: "range" | "single") { |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 391 | this.rangeType = type; |
| Autoformatter | 9abf803 | 2025-06-14 23:24:08 +0000 | [diff] [blame^] | 392 | |
| David Crawshaw | 938d2dc | 2025-06-14 22:17:33 +0000 | [diff] [blame] | 393 | // If switching to range mode and we don't have valid commits set, |
| 394 | // initialize with sensible defaults |
| Autoformatter | 9abf803 | 2025-06-14 23:24:08 +0000 | [diff] [blame^] | 395 | if ( |
| 396 | type === "range" && |
| 397 | (!this.fromCommit || !this.toCommit === undefined) |
| 398 | ) { |
| David Crawshaw | 938d2dc | 2025-06-14 22:17:33 +0000 | [diff] [blame] | 399 | if (this.commits.length > 0) { |
| 400 | const baseCommit = this.commits.find( |
| 401 | (c) => c.refs && c.refs.some((ref) => ref.includes("sketch-base")), |
| 402 | ); |
| 403 | if (!this.fromCommit) { |
| 404 | this.fromCommit = baseCommit |
| 405 | ? baseCommit.hash |
| 406 | : this.commits[this.commits.length - 1].hash; |
| 407 | } |
| 408 | if (this.toCommit === undefined) { |
| Autoformatter | 9abf803 | 2025-06-14 23:24:08 +0000 | [diff] [blame^] | 409 | this.toCommit = ""; // Default to uncommitted changes |
| David Crawshaw | 938d2dc | 2025-06-14 22:17:33 +0000 | [diff] [blame] | 410 | } |
| 411 | } |
| 412 | } |
| Autoformatter | 9abf803 | 2025-06-14 23:24:08 +0000 | [diff] [blame^] | 413 | |
| David Crawshaw | 938d2dc | 2025-06-14 22:17:33 +0000 | [diff] [blame] | 414 | // If switching to single mode and we don't have a valid commit set, |
| 415 | // initialize with HEAD |
| Autoformatter | 9abf803 | 2025-06-14 23:24:08 +0000 | [diff] [blame^] | 416 | if (type === "single" && !this.singleCommit && this.commits.length > 0) { |
| David Crawshaw | 938d2dc | 2025-06-14 22:17:33 +0000 | [diff] [blame] | 417 | this.singleCommit = this.commits[0].hash; |
| 418 | } |
| Autoformatter | 9abf803 | 2025-06-14 23:24:08 +0000 | [diff] [blame^] | 419 | |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 420 | this.dispatchRangeEvent(); |
| 421 | } |
| 422 | |
| 423 | /** |
| 424 | * Handle From commit change |
| 425 | */ |
| 426 | handleFromChange(event: Event) { |
| 427 | const select = event.target as HTMLSelectElement; |
| 428 | this.fromCommit = select.value; |
| 429 | this.dispatchRangeEvent(); |
| 430 | } |
| 431 | |
| 432 | /** |
| 433 | * Handle To commit change |
| 434 | */ |
| 435 | handleToChange(event: Event) { |
| 436 | const select = event.target as HTMLSelectElement; |
| 437 | this.toCommit = select.value; |
| 438 | this.dispatchRangeEvent(); |
| 439 | } |
| 440 | |
| 441 | /** |
| 442 | * Handle Single commit change |
| 443 | */ |
| 444 | handleSingleChange(event: Event) { |
| 445 | const select = event.target as HTMLSelectElement; |
| 446 | this.singleCommit = select.value; |
| 447 | this.dispatchRangeEvent(); |
| 448 | } |
| 449 | |
| 450 | /** |
| Philip Zeyliger | e89b308 | 2025-05-29 03:16:06 +0000 | [diff] [blame] | 451 | * Handle refresh button click |
| 452 | */ |
| 453 | handleRefresh() { |
| 454 | this.loadCommits(); |
| 455 | } |
| 456 | |
| 457 | /** |
| David Crawshaw | 938d2dc | 2025-06-14 22:17:33 +0000 | [diff] [blame] | 458 | * Validate that a commit hash exists in the loaded commits |
| 459 | */ |
| 460 | private isValidCommitHash(hash: string): boolean { |
| Autoformatter | 9abf803 | 2025-06-14 23:24:08 +0000 | [diff] [blame^] | 461 | if (!hash || hash.trim() === "") return true; // Empty is valid (uncommitted changes) |
| 462 | return this.commits.some( |
| 463 | (commit) => commit.hash.startsWith(hash) || commit.hash === hash, |
| 464 | ); |
| David Crawshaw | 938d2dc | 2025-06-14 22:17:33 +0000 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | /** |
| 468 | * Dispatch range change event and update URL parameters |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 469 | */ |
| 470 | dispatchRangeEvent() { |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 471 | const range: DiffRange = |
| 472 | this.rangeType === "range" |
| 473 | ? { type: "range", from: this.fromCommit, to: this.toCommit } |
| 474 | : { type: "single", commit: this.singleCommit }; |
| 475 | |
| David Crawshaw | 938d2dc | 2025-06-14 22:17:33 +0000 | [diff] [blame] | 476 | // Update URL parameters |
| 477 | this.updateUrlParams(range); |
| 478 | |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 479 | const event = new CustomEvent("range-change", { |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 480 | detail: { range }, |
| 481 | bubbles: true, |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 482 | composed: true, |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 483 | }); |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 484 | |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 485 | this.dispatchEvent(event); |
| 486 | } |
| David Crawshaw | 938d2dc | 2025-06-14 22:17:33 +0000 | [diff] [blame] | 487 | |
| 488 | /** |
| 489 | * Update URL parameters for from and to commits |
| 490 | */ |
| 491 | private updateUrlParams(range: DiffRange) { |
| 492 | const url = new URL(window.location.href); |
| Autoformatter | 9abf803 | 2025-06-14 23:24:08 +0000 | [diff] [blame^] | 493 | |
| David Crawshaw | 938d2dc | 2025-06-14 22:17:33 +0000 | [diff] [blame] | 494 | // Remove existing range parameters |
| Autoformatter | 9abf803 | 2025-06-14 23:24:08 +0000 | [diff] [blame^] | 495 | url.searchParams.delete("from"); |
| 496 | url.searchParams.delete("to"); |
| 497 | url.searchParams.delete("commit"); |
| 498 | |
| 499 | if (range.type === "range") { |
| David Crawshaw | 938d2dc | 2025-06-14 22:17:33 +0000 | [diff] [blame] | 500 | // Add from parameter if not empty |
| Autoformatter | 9abf803 | 2025-06-14 23:24:08 +0000 | [diff] [blame^] | 501 | if (range.from && range.from.trim() !== "") { |
| 502 | url.searchParams.set("from", range.from); |
| David Crawshaw | 938d2dc | 2025-06-14 22:17:33 +0000 | [diff] [blame] | 503 | } |
| 504 | // Add to parameter if not empty (empty string means uncommitted changes) |
| Autoformatter | 9abf803 | 2025-06-14 23:24:08 +0000 | [diff] [blame^] | 505 | if (range.to && range.to.trim() !== "") { |
| 506 | url.searchParams.set("to", range.to); |
| David Crawshaw | 938d2dc | 2025-06-14 22:17:33 +0000 | [diff] [blame] | 507 | } |
| 508 | } else { |
| 509 | // Single commit mode |
| Autoformatter | 9abf803 | 2025-06-14 23:24:08 +0000 | [diff] [blame^] | 510 | if (range.commit && range.commit.trim() !== "") { |
| 511 | url.searchParams.set("commit", range.commit); |
| David Crawshaw | 938d2dc | 2025-06-14 22:17:33 +0000 | [diff] [blame] | 512 | } |
| 513 | } |
| Autoformatter | 9abf803 | 2025-06-14 23:24:08 +0000 | [diff] [blame^] | 514 | |
| David Crawshaw | 938d2dc | 2025-06-14 22:17:33 +0000 | [diff] [blame] | 515 | // Update the browser history without reloading the page |
| Autoformatter | 9abf803 | 2025-06-14 23:24:08 +0000 | [diff] [blame^] | 516 | window.history.replaceState(window.history.state, "", url.toString()); |
| David Crawshaw | 938d2dc | 2025-06-14 22:17:33 +0000 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | /** |
| 520 | * Initialize from URL parameters if available |
| 521 | */ |
| 522 | private initializeFromUrlParams() { |
| 523 | const url = new URL(window.location.href); |
| Autoformatter | 9abf803 | 2025-06-14 23:24:08 +0000 | [diff] [blame^] | 524 | const fromParam = url.searchParams.get("from"); |
| 525 | const toParam = url.searchParams.get("to"); |
| 526 | const commitParam = url.searchParams.get("commit"); |
| 527 | |
| David Crawshaw | 938d2dc | 2025-06-14 22:17:33 +0000 | [diff] [blame] | 528 | // If commit parameter is present, switch to single commit mode |
| 529 | if (commitParam) { |
| Autoformatter | 9abf803 | 2025-06-14 23:24:08 +0000 | [diff] [blame^] | 530 | this.rangeType = "single"; |
| David Crawshaw | 938d2dc | 2025-06-14 22:17:33 +0000 | [diff] [blame] | 531 | this.singleCommit = commitParam; |
| 532 | return true; // Indicate that we initialized from URL |
| 533 | } |
| Autoformatter | 9abf803 | 2025-06-14 23:24:08 +0000 | [diff] [blame^] | 534 | |
| David Crawshaw | 938d2dc | 2025-06-14 22:17:33 +0000 | [diff] [blame] | 535 | // If from or to parameters are present, use range mode |
| 536 | if (fromParam || toParam) { |
| Autoformatter | 9abf803 | 2025-06-14 23:24:08 +0000 | [diff] [blame^] | 537 | this.rangeType = "range"; |
| David Crawshaw | 938d2dc | 2025-06-14 22:17:33 +0000 | [diff] [blame] | 538 | if (fromParam) { |
| 539 | this.fromCommit = fromParam; |
| 540 | } |
| 541 | if (toParam) { |
| 542 | this.toCommit = toParam; |
| 543 | } else { |
| 544 | // If no 'to' param, default to uncommitted changes (empty string) |
| Autoformatter | 9abf803 | 2025-06-14 23:24:08 +0000 | [diff] [blame^] | 545 | this.toCommit = ""; |
| David Crawshaw | 938d2dc | 2025-06-14 22:17:33 +0000 | [diff] [blame] | 546 | } |
| 547 | return true; // Indicate that we initialized from URL |
| 548 | } |
| Autoformatter | 9abf803 | 2025-06-14 23:24:08 +0000 | [diff] [blame^] | 549 | |
| David Crawshaw | 938d2dc | 2025-06-14 22:17:33 +0000 | [diff] [blame] | 550 | return false; // No URL params found |
| 551 | } |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | declare global { |
| 555 | interface HTMLElementTagNameMap { |
| 556 | "sketch-diff-range-picker": SketchDiffRangePicker; |
| 557 | } |
| 558 | } |