blob: e6a293eb3fcf12e30ae59430de4454ce627984ea [file] [log] [blame]
Philip Zeyliger272a90e2025-05-16 14:49:51 -07001// sketch-diff-range-picker.ts
2// Component for selecting commit range for diffs
3
banksean2be768e2025-07-18 16:41:39 +00004import { html } from "lit";
Philip Zeyliger272a90e2025-05-16 14:49:51 -07005import { customElement, property, state } from "lit/decorators.js";
philip.zeyliger26bc6592025-06-30 20:15:30 -07006import { GitDataService } from "./git-data-service";
Philip Zeyliger272a90e2025-05-16 14:49:51 -07007import { GitLogEntry } from "../types";
banksean2be768e2025-07-18 16:41:39 +00008import { SketchTailwindElement } from "./sketch-tailwind-element";
Philip Zeyliger272a90e2025-05-16 14:49:51 -07009
10/**
11 * Range type for diff views
12 */
David Crawshaw216d2fc2025-06-15 18:45:53 +000013export type DiffRange = { type: "range"; from: string; to: string };
Philip Zeyliger272a90e2025-05-16 14:49:51 -070014
15/**
16 * Component for selecting commit range for diffs
17 */
18@customElement("sketch-diff-range-picker")
banksean2be768e2025-07-18 16:41:39 +000019export class SketchDiffRangePicker extends SketchTailwindElement {
Philip Zeyliger272a90e2025-05-16 14:49:51 -070020 @property({ type: Array })
21 commits: GitLogEntry[] = [];
22
23 @state()
Autoformatter8c463622025-05-16 21:54:17 +000024 private fromCommit: string = "";
Philip Zeyliger272a90e2025-05-16 14:49:51 -070025
26 @state()
Autoformatter8c463622025-05-16 21:54:17 +000027 private toCommit: string = "";
Philip Zeyliger272a90e2025-05-16 14:49:51 -070028
Philip Zeyliger364b35a2025-06-21 16:39:04 -070029 @state()
30 private dropdownOpen: boolean = false;
31
Philip Zeyliger38499cc2025-06-15 21:17:05 -070032 // Removed commitsExpanded state - always expanded now
Philip Zeyliger272a90e2025-05-16 14:49:51 -070033
34 @state()
35 private loading: boolean = true;
36
37 @state()
38 private error: string | null = null;
Autoformatter8c463622025-05-16 21:54:17 +000039
Philip Zeyliger272a90e2025-05-16 14:49:51 -070040 @property({ attribute: false, type: Object })
41 gitService!: GitDataService;
Autoformatter8c463622025-05-16 21:54:17 +000042
Philip Zeyliger272a90e2025-05-16 14:49:51 -070043 constructor() {
44 super();
Autoformatter8c463622025-05-16 21:54:17 +000045 console.log("SketchDiffRangePicker initialized");
Philip Zeyliger272a90e2025-05-16 14:49:51 -070046 }
47
banksean2be768e2025-07-18 16:41:39 +000048 // Ensure global styles are injected when component is used
49 private ensureGlobalStyles() {
50 if (!document.querySelector("#sketch-diff-range-picker-styles")) {
51 const floatingMessageStyles = document.createElement("style");
52 floatingMessageStyles.id = "sketch-diff-range-picker-styles";
53 floatingMessageStyles.textContent = this.getGlobalStylesContent();
54 document.head.appendChild(floatingMessageStyles);
55 }
56 }
57
58 // Get the global styles content
59 private getGlobalStylesContent(): string {
60 return `
61 sketch-diff-range-picker {
Philip Zeyliger272a90e2025-05-16 14:49:51 -070062 display: block;
63 width: 100%;
64 font-family: var(--font-family, system-ui, sans-serif);
65 color: var(--text-color, #333);
banksean2be768e2025-07-18 16:41:39 +000066 }`;
67 }
Philip Zeyliger272a90e2025-05-16 14:49:51 -070068
69 connectedCallback() {
70 super.connectedCallback();
banksean2be768e2025-07-18 16:41:39 +000071 this.ensureGlobalStyles();
Philip Zeyliger272a90e2025-05-16 14:49:51 -070072 // Wait for DOM to be fully loaded to ensure proper initialization order
Autoformatter8c463622025-05-16 21:54:17 +000073 if (document.readyState === "complete") {
Philip Zeyliger272a90e2025-05-16 14:49:51 -070074 this.loadCommits();
75 } else {
Autoformatter8c463622025-05-16 21:54:17 +000076 window.addEventListener("load", () => {
Philip Zeyliger272a90e2025-05-16 14:49:51 -070077 setTimeout(() => this.loadCommits(), 0); // Give time for provider initialization
78 });
79 }
Autoformatter9abf8032025-06-14 23:24:08 +000080
David Crawshaw938d2dc2025-06-14 22:17:33 +000081 // Listen for popstate events to handle browser back/forward navigation
Autoformatter9abf8032025-06-14 23:24:08 +000082 window.addEventListener("popstate", this.handlePopState.bind(this));
David Crawshaw938d2dc2025-06-14 22:17:33 +000083 }
84
85 disconnectedCallback() {
86 super.disconnectedCallback();
Autoformatter9abf8032025-06-14 23:24:08 +000087 window.removeEventListener("popstate", this.handlePopState.bind(this));
David Crawshaw938d2dc2025-06-14 22:17:33 +000088 }
89
90 /**
91 * Handle browser back/forward navigation
92 */
93 private handlePopState() {
94 // Re-initialize from URL parameters when user navigates
95 if (this.commits.length > 0) {
96 const initializedFromUrl = this.initializeFromUrlParams();
97 if (initializedFromUrl) {
98 // Force re-render and dispatch event
99 this.requestUpdate();
100 this.dispatchRangeEvent();
101 }
102 }
Philip Zeyliger272a90e2025-05-16 14:49:51 -0700103 }
104
105 render() {
106 return html`
banksean3eaa4332025-07-19 02:19:06 +0000107 <div class="block w-full font-system text-gray-800 dark:text-gray-200">
banksean2be768e2025-07-18 16:41:39 +0000108 <div class="flex flex-col gap-3 w-full">
109 ${this.loading
banksean3eaa4332025-07-19 02:19:06 +0000110 ? html`<div class="italic text-gray-500 dark:text-gray-400">
111 Loading commits...
112 </div>`
banksean2be768e2025-07-18 16:41:39 +0000113 : this.error
banksean3eaa4332025-07-19 02:19:06 +0000114 ? html`<div class="text-red-600 dark:text-red-400 text-sm">
115 ${this.error}
116 </div>`
banksean2be768e2025-07-18 16:41:39 +0000117 : this.renderRangePicker()}
118 </div>
Philip Zeyliger272a90e2025-05-16 14:49:51 -0700119 </div>
120 `;
121 }
122
123 renderRangePicker() {
124 return html`
banksean2be768e2025-07-18 16:41:39 +0000125 <div class="flex flex-row items-center gap-3 flex-1">
126 ${this.renderRangeSelectors()}
127 </div>
Philip Zeyliger272a90e2025-05-16 14:49:51 -0700128 `;
129 }
130
131 renderRangeSelectors() {
Philip Zeyliger364b35a2025-06-21 16:39:04 -0700132 // Always diff against uncommitted changes
133 this.toCommit = "";
134
135 const selectedCommit = this.commits.find((c) => c.hash === this.fromCommit);
136 const isDefaultCommit =
137 selectedCommit && this.isSketchBaseCommit(selectedCommit);
138
Philip Zeyliger272a90e2025-05-16 14:49:51 -0700139 return html`
banksean2be768e2025-07-18 16:41:39 +0000140 <div class="flex items-center gap-2 flex-1 relative">
banksean3eaa4332025-07-19 02:19:06 +0000141 <label
142 for="fromCommit"
143 class="font-medium text-sm text-gray-700 dark:text-gray-300"
banksean2be768e2025-07-18 16:41:39 +0000144 >Diff from:</label
145 >
146 <div
147 class="relative w-full min-w-[300px]"
148 @click=${this.toggleDropdown}
149 >
Philip Zeyliger364b35a2025-06-21 16:39:04 -0700150 <button
banksean3eaa4332025-07-19 02:19:06 +0000151 class="w-full py-2 px-3 pr-8 border rounded text-left min-h-[36px] text-sm relative cursor-pointer bg-white dark:bg-gray-800 text-gray-900 dark:text-gray-100 ${isDefaultCommit
152 ? "border-blue-500 bg-blue-50 dark:bg-blue-900/30"
153 : "border-gray-300 dark:border-gray-600 hover:border-gray-400 dark:hover:border-gray-500"} focus:outline-none focus:border-blue-500 focus:ring-2 focus:ring-blue-200 dark:focus:ring-blue-800"
Philip Zeyliger364b35a2025-06-21 16:39:04 -0700154 @click=${this.toggleDropdown}
155 @blur=${this.handleBlur}
156 >
banksean2be768e2025-07-18 16:41:39 +0000157 <div class="flex items-center gap-2 pr-6">
158 ${selectedCommit
159 ? this.renderCommitButton(selectedCommit)
160 : "Select commit..."}
161 </div>
Philip Zeyliger364b35a2025-06-21 16:39:04 -0700162 <svg
banksean2be768e2025-07-18 16:41:39 +0000163 class="absolute right-2 top-1/2 transform -translate-y-1/2 transition-transform duration-200 ${this
164 .dropdownOpen
165 ? "rotate-180"
166 : ""}"
Philip Zeyliger364b35a2025-06-21 16:39:04 -0700167 width="12"
168 height="12"
169 viewBox="0 0 12 12"
170 >
171 <path d="M6 8l-4-4h8z" fill="currentColor" />
172 </svg>
173 </button>
174 ${this.dropdownOpen
175 ? html`
banksean2be768e2025-07-18 16:41:39 +0000176 <div
banksean3eaa4332025-07-19 02:19:06 +0000177 class="absolute top-full left-0 right-0 bg-white dark:bg-gray-800 border border-gray-300 dark:border-gray-600 rounded shadow-lg z-50 max-h-[300px] overflow-y-auto mt-0.5"
banksean2be768e2025-07-18 16:41:39 +0000178 >
Philip Zeyliger364b35a2025-06-21 16:39:04 -0700179 ${this.commits.map(
180 (commit) => html`
181 <div
banksean3eaa4332025-07-19 02:19:06 +0000182 class="px-3 py-2.5 cursor-pointer border-b border-gray-100 dark:border-gray-700 last:border-b-0 flex items-start gap-2 text-sm leading-5 hover:bg-gray-50 dark:hover:bg-gray-700 ${commit.hash ===
banksean2be768e2025-07-18 16:41:39 +0000183 this.fromCommit
banksean3eaa4332025-07-19 02:19:06 +0000184 ? "bg-blue-50 dark:bg-blue-900/30"
Philip Zeyliger364b35a2025-06-21 16:39:04 -0700185 : ""} ${this.isSketchBaseCommit(commit)
banksean3eaa4332025-07-19 02:19:06 +0000186 ? "bg-blue-50 dark:bg-blue-900/30 border-l-4 border-l-blue-500 pl-2"
Philip Zeyliger364b35a2025-06-21 16:39:04 -0700187 : ""}"
188 @click=${() => this.selectCommit(commit.hash)}
189 >
190 ${this.renderCommitOption(commit)}
191 </div>
192 `,
193 )}
194 </div>
195 `
196 : ""}
197 </div>
Philip Zeyliger272a90e2025-05-16 14:49:51 -0700198 </div>
199 `;
200 }
201
Philip Zeyliger272a90e2025-05-16 14:49:51 -0700202 /**
Philip Zeyliger364b35a2025-06-21 16:39:04 -0700203 * Format a commit for display in the dropdown (legacy method, kept for compatibility)
Philip Zeyliger272a90e2025-05-16 14:49:51 -0700204 */
205 formatCommitOption(commit: GitLogEntry): string {
206 const shortHash = commit.hash.substring(0, 7);
Autoformatter8c463622025-05-16 21:54:17 +0000207
David Crawshawdbca8972025-06-14 23:46:58 +0000208 // Truncate subject if it's too long
209 let subject = commit.subject;
210 if (subject.length > 50) {
211 subject = subject.substring(0, 47) + "...";
212 }
213
214 let label = `${shortHash} ${subject}`;
215
216 // Add refs but keep them concise
Philip Zeyliger272a90e2025-05-16 14:49:51 -0700217 if (commit.refs && commit.refs.length > 0) {
David Crawshawdbca8972025-06-14 23:46:58 +0000218 const refs = commit.refs.map((ref) => {
219 // Shorten common prefixes
220 if (ref.startsWith("origin/")) {
221 return ref.substring(7);
222 }
223 if (ref.startsWith("refs/heads/")) {
224 return ref.substring(11);
225 }
226 if (ref.startsWith("refs/remotes/origin/")) {
227 return ref.substring(20);
228 }
229 return ref;
230 });
231
232 // Limit to first 2 refs to avoid overcrowding
233 const displayRefs = refs.slice(0, 2);
234 if (refs.length > 2) {
235 displayRefs.push(`+${refs.length - 2} more`);
236 }
237
238 label += ` (${displayRefs.join(", ")})`;
Philip Zeyliger272a90e2025-05-16 14:49:51 -0700239 }
Autoformatter8c463622025-05-16 21:54:17 +0000240
Philip Zeyliger272a90e2025-05-16 14:49:51 -0700241 return label;
242 }
243
244 /**
245 * Load commits from the Git data service
246 */
247 async loadCommits() {
248 this.loading = true;
249 this.error = null;
250
251 if (!this.gitService) {
Autoformatter8c463622025-05-16 21:54:17 +0000252 console.error("GitService was not provided to sketch-diff-range-picker");
Philip Zeyliger272a90e2025-05-16 14:49:51 -0700253 throw Error();
254 }
255
256 try {
257 // Get the base commit reference
258 const baseCommitRef = await this.gitService.getBaseCommitRef();
Autoformatter8c463622025-05-16 21:54:17 +0000259
Philip Zeyliger272a90e2025-05-16 14:49:51 -0700260 // Load commit history
261 this.commits = await this.gitService.getCommitHistory(baseCommitRef);
Autoformatter8c463622025-05-16 21:54:17 +0000262
David Crawshaw938d2dc2025-06-14 22:17:33 +0000263 // Check if we should initialize from URL parameters first
264 const initializedFromUrl = this.initializeFromUrlParams();
Autoformatter9abf8032025-06-14 23:24:08 +0000265
David Crawshaw938d2dc2025-06-14 22:17:33 +0000266 // Set default selections only if not initialized from URL
267 if (this.commits.length > 0 && !initializedFromUrl) {
Philip Zeyliger272a90e2025-05-16 14:49:51 -0700268 // For range, default is base to HEAD
269 // TODO: is sketch-base right in the unsafe context, where it's sketch-base-...
270 // should this be startswith?
Autoformatter8c463622025-05-16 21:54:17 +0000271 const baseCommit = this.commits.find(
272 (c) => c.refs && c.refs.some((ref) => ref.includes("sketch-base")),
Philip Zeyliger272a90e2025-05-16 14:49:51 -0700273 );
Autoformatter8c463622025-05-16 21:54:17 +0000274
275 this.fromCommit = baseCommit
276 ? baseCommit.hash
277 : this.commits[this.commits.length - 1].hash;
Philip Zeyliger272a90e2025-05-16 14:49:51 -0700278 // Default to Uncommitted Changes by setting toCommit to empty string
Autoformatter8c463622025-05-16 21:54:17 +0000279 this.toCommit = ""; // Empty string represents uncommitted changes
Philip Zeyliger272a90e2025-05-16 14:49:51 -0700280 }
Autoformatter9abf8032025-06-14 23:24:08 +0000281
David Crawshaw938d2dc2025-06-14 22:17:33 +0000282 // Always dispatch range event to ensure diff view is updated
283 this.dispatchRangeEvent();
Philip Zeyliger272a90e2025-05-16 14:49:51 -0700284 } catch (error) {
Autoformatter8c463622025-05-16 21:54:17 +0000285 console.error("Error loading commits:", error);
Philip Zeyliger272a90e2025-05-16 14:49:51 -0700286 this.error = `Error loading commits: ${error.message}`;
287 } finally {
288 this.loading = false;
289 }
290 }
291
Philip Zeyliger272a90e2025-05-16 14:49:51 -0700292 /**
293 * Handle From commit change
294 */
295 handleFromChange(event: Event) {
296 const select = event.target as HTMLSelectElement;
297 this.fromCommit = select.value;
298 this.dispatchRangeEvent();
299 }
300
301 /**
302 * Handle To commit change
303 */
304 handleToChange(event: Event) {
305 const select = event.target as HTMLSelectElement;
306 this.toCommit = select.value;
307 this.dispatchRangeEvent();
308 }
309
Philip Zeyliger364b35a2025-06-21 16:39:04 -0700310 /**
311 * Toggle dropdown open/closed
312 */
313 toggleDropdown(event: Event) {
314 event.stopPropagation();
315 this.dropdownOpen = !this.dropdownOpen;
316
317 if (this.dropdownOpen) {
318 // Close dropdown when clicking outside
319 setTimeout(() => {
320 document.addEventListener("click", this.closeDropdown, { once: true });
321 }, 0);
322 }
323 }
324
325 /**
326 * Close dropdown
327 */
328 closeDropdown = () => {
329 this.dropdownOpen = false;
330 };
331
332 /**
333 * Handle blur event on select button
334 */
philip.zeyliger26bc6592025-06-30 20:15:30 -0700335 handleBlur(_event: FocusEvent) {
Philip Zeyliger364b35a2025-06-21 16:39:04 -0700336 // Small delay to allow click events to process
337 setTimeout(() => {
banksean44320562025-07-21 11:09:38 -0700338 if (!this.closest(".custom-select")) {
Philip Zeyliger364b35a2025-06-21 16:39:04 -0700339 this.dropdownOpen = false;
340 }
341 }, 150);
342 }
343
344 /**
345 * Select a commit from dropdown
346 */
347 selectCommit(hash: string) {
348 this.fromCommit = hash;
349 this.dropdownOpen = false;
350 this.dispatchRangeEvent();
351 }
352
353 /**
354 * Check if a commit is a sketch-base commit
355 */
356 isSketchBaseCommit(commit: GitLogEntry): boolean {
357 return commit.refs?.some((ref) => ref.includes("sketch-base")) || false;
358 }
359
360 /**
361 * Render commit for the dropdown button
362 */
363 renderCommitButton(commit: GitLogEntry) {
364 const shortHash = commit.hash.substring(0, 7);
365 let subject = commit.subject;
366 if (subject.length > 40) {
367 subject = subject.substring(0, 37) + "...";
368 }
369
370 return html`
banksean3eaa4332025-07-19 02:19:06 +0000371 <span class="font-mono text-gray-600 dark:text-gray-400 text-xs"
372 >${shortHash}</span
373 >
374 <span class="text-gray-800 dark:text-gray-200 text-xs truncate"
375 >${subject}</span
376 >
Philip Zeyliger364b35a2025-06-21 16:39:04 -0700377 ${this.isSketchBaseCommit(commit)
banksean2be768e2025-07-18 16:41:39 +0000378 ? html`
379 <span
380 class="bg-blue-600 text-white px-1.5 py-0.5 rounded-full text-xs font-semibold"
381 >base</span
382 >
383 `
Philip Zeyliger364b35a2025-06-21 16:39:04 -0700384 : ""}
385 `;
386 }
387
388 /**
389 * Render commit option in dropdown
390 */
391 renderCommitOption(commit: GitLogEntry) {
392 const shortHash = commit.hash.substring(0, 7);
393 let subject = commit.subject;
394 if (subject.length > 50) {
395 subject = subject.substring(0, 47) + "...";
396 }
397
398 return html`
banksean3eaa4332025-07-19 02:19:06 +0000399 <span class="font-mono text-gray-600 dark:text-gray-400 text-xs"
400 >${shortHash}</span
401 >
402 <span
403 class="text-gray-800 dark:text-gray-200 text-xs flex-1 truncate min-w-[200px]"
banksean2be768e2025-07-18 16:41:39 +0000404 >${subject}</span
405 >
Philip Zeyliger364b35a2025-06-21 16:39:04 -0700406 ${commit.refs && commit.refs.length > 0
banksean2be768e2025-07-18 16:41:39 +0000407 ? html`
408 <div class="flex gap-1 flex-wrap">
409 ${this.renderRefs(commit.refs)}
410 </div>
411 `
Philip Zeyliger364b35a2025-06-21 16:39:04 -0700412 : ""}
413 `;
414 }
415
416 /**
417 * Render all refs naturally without truncation
418 */
419 renderRefs(refs: string[]) {
420 return html`
banksean2be768e2025-07-18 16:41:39 +0000421 <div class="flex gap-1 flex-wrap flex-shrink-0">
Philip Zeyliger364b35a2025-06-21 16:39:04 -0700422 ${refs.map((ref) => {
423 const shortRef = this.getShortRefName(ref);
424 const isSketchBase = ref.includes("sketch-base");
425 const refClass = isSketchBase
banksean2be768e2025-07-18 16:41:39 +0000426 ? "bg-blue-600 text-white font-semibold"
Philip Zeyliger364b35a2025-06-21 16:39:04 -0700427 : ref.includes("tag")
banksean3eaa4332025-07-19 02:19:06 +0000428 ? "bg-amber-100 dark:bg-amber-900 text-amber-800 dark:text-amber-200"
429 : "bg-green-100 dark:bg-green-900 text-green-800 dark:text-green-200";
banksean2be768e2025-07-18 16:41:39 +0000430 return html`<span
431 class="px-1.5 py-0.5 rounded-full text-xs font-medium ${refClass}"
432 >${shortRef}</span
433 >`;
Philip Zeyliger364b35a2025-06-21 16:39:04 -0700434 })}
435 </div>
436 `;
437 }
438
439 /**
440 * Get shortened reference name for compact display
441 */
442 getShortRefName(ref: string): string {
443 if (ref.startsWith("refs/heads/")) {
444 return ref.substring(11);
445 }
446 if (ref.startsWith("refs/remotes/origin/")) {
447 return ref.substring(20);
448 }
449 if (ref.startsWith("refs/tags/")) {
450 return ref.substring(10);
451 }
452 return ref;
453 }
Philip Zeyliger272a90e2025-05-16 14:49:51 -0700454
455 /**
David Crawshaw216d2fc2025-06-15 18:45:53 +0000456 * Get a summary of the current commit range for display
Philip Zeyligere89b3082025-05-29 03:16:06 +0000457 */
David Crawshaw216d2fc2025-06-15 18:45:53 +0000458 getCommitSummary(): string {
459 if (!this.fromCommit && !this.toCommit) {
Autoformatter62554112025-06-15 19:23:33 +0000460 return "No commits selected";
David Crawshaw216d2fc2025-06-15 18:45:53 +0000461 }
462
Autoformatter62554112025-06-15 19:23:33 +0000463 const fromShort = this.fromCommit ? this.fromCommit.substring(0, 7) : "";
464 const toShort = this.toCommit
465 ? this.toCommit.substring(0, 7)
466 : "Uncommitted";
467
David Crawshaw216d2fc2025-06-15 18:45:53 +0000468 return `${fromShort}..${toShort}`;
Philip Zeyligere89b3082025-05-29 03:16:06 +0000469 }
470
471 /**
David Crawshaw938d2dc2025-06-14 22:17:33 +0000472 * Validate that a commit hash exists in the loaded commits
473 */
474 private isValidCommitHash(hash: string): boolean {
Autoformatter9abf8032025-06-14 23:24:08 +0000475 if (!hash || hash.trim() === "") return true; // Empty is valid (uncommitted changes)
476 return this.commits.some(
477 (commit) => commit.hash.startsWith(hash) || commit.hash === hash,
478 );
David Crawshaw938d2dc2025-06-14 22:17:33 +0000479 }
480
481 /**
482 * Dispatch range change event and update URL parameters
Philip Zeyliger272a90e2025-05-16 14:49:51 -0700483 */
484 dispatchRangeEvent() {
Autoformatter62554112025-06-15 19:23:33 +0000485 const range: DiffRange = {
486 type: "range",
487 from: this.fromCommit,
488 to: this.toCommit,
489 };
Autoformatter8c463622025-05-16 21:54:17 +0000490
David Crawshaw938d2dc2025-06-14 22:17:33 +0000491 // Update URL parameters
492 this.updateUrlParams(range);
493
Autoformatter8c463622025-05-16 21:54:17 +0000494 const event = new CustomEvent("range-change", {
Philip Zeyliger272a90e2025-05-16 14:49:51 -0700495 detail: { range },
496 bubbles: true,
Autoformatter8c463622025-05-16 21:54:17 +0000497 composed: true,
Philip Zeyliger272a90e2025-05-16 14:49:51 -0700498 });
Autoformatter8c463622025-05-16 21:54:17 +0000499
Philip Zeyliger272a90e2025-05-16 14:49:51 -0700500 this.dispatchEvent(event);
501 }
David Crawshaw938d2dc2025-06-14 22:17:33 +0000502
503 /**
504 * Update URL parameters for from and to commits
505 */
506 private updateUrlParams(range: DiffRange) {
507 const url = new URL(window.location.href);
Autoformatter9abf8032025-06-14 23:24:08 +0000508
David Crawshaw938d2dc2025-06-14 22:17:33 +0000509 // Remove existing range parameters
Autoformatter9abf8032025-06-14 23:24:08 +0000510 url.searchParams.delete("from");
511 url.searchParams.delete("to");
512 url.searchParams.delete("commit");
513
David Crawshaw216d2fc2025-06-15 18:45:53 +0000514 // Add from parameter if not empty
515 if (range.from && range.from.trim() !== "") {
516 url.searchParams.set("from", range.from);
517 }
518 // Add to parameter if not empty (empty string means uncommitted changes)
519 if (range.to && range.to.trim() !== "") {
520 url.searchParams.set("to", range.to);
David Crawshaw938d2dc2025-06-14 22:17:33 +0000521 }
Autoformatter9abf8032025-06-14 23:24:08 +0000522
David Crawshaw938d2dc2025-06-14 22:17:33 +0000523 // Update the browser history without reloading the page
Autoformatter9abf8032025-06-14 23:24:08 +0000524 window.history.replaceState(window.history.state, "", url.toString());
David Crawshaw938d2dc2025-06-14 22:17:33 +0000525 }
526
527 /**
528 * Initialize from URL parameters if available
529 */
530 private initializeFromUrlParams() {
531 const url = new URL(window.location.href);
Autoformatter9abf8032025-06-14 23:24:08 +0000532 const fromParam = url.searchParams.get("from");
533 const toParam = url.searchParams.get("to");
Autoformatter9abf8032025-06-14 23:24:08 +0000534
David Crawshaw216d2fc2025-06-15 18:45:53 +0000535 // If from or to parameters are present, use them
David Crawshaw938d2dc2025-06-14 22:17:33 +0000536 if (fromParam || toParam) {
David Crawshaw938d2dc2025-06-14 22:17:33 +0000537 if (fromParam) {
538 this.fromCommit = fromParam;
539 }
540 if (toParam) {
541 this.toCommit = toParam;
542 } else {
543 // If no 'to' param, default to uncommitted changes (empty string)
Autoformatter9abf8032025-06-14 23:24:08 +0000544 this.toCommit = "";
David Crawshaw938d2dc2025-06-14 22:17:33 +0000545 }
546 return true; // Indicate that we initialized from URL
547 }
Autoformatter9abf8032025-06-14 23:24:08 +0000548
David Crawshaw938d2dc2025-06-14 22:17:33 +0000549 return false; // No URL params found
550 }
Philip Zeyliger272a90e2025-05-16 14:49:51 -0700551}
552
553declare global {
554 interface HTMLElementTagNameMap {
555 "sketch-diff-range-picker": SketchDiffRangePicker;
556 }
557}