| 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 | |
| banksean | 2be768e | 2025-07-18 16:41:39 +0000 | [diff] [blame^] | 4 | import { html } from "lit"; |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 5 | import { customElement, property, state } from "lit/decorators.js"; |
| philip.zeyliger | 26bc659 | 2025-06-30 20:15:30 -0700 | [diff] [blame] | 6 | import { GitDataService } from "./git-data-service"; |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 7 | import { GitLogEntry } from "../types"; |
| banksean | 2be768e | 2025-07-18 16:41:39 +0000 | [diff] [blame^] | 8 | import { SketchTailwindElement } from "./sketch-tailwind-element"; |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 9 | |
| 10 | /** |
| 11 | * Range type for diff views |
| 12 | */ |
| David Crawshaw | 216d2fc | 2025-06-15 18:45:53 +0000 | [diff] [blame] | 13 | export type DiffRange = { type: "range"; from: string; to: string }; |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 14 | |
| 15 | /** |
| 16 | * Component for selecting commit range for diffs |
| 17 | */ |
| 18 | @customElement("sketch-diff-range-picker") |
| banksean | 2be768e | 2025-07-18 16:41:39 +0000 | [diff] [blame^] | 19 | export class SketchDiffRangePicker extends SketchTailwindElement { |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 20 | @property({ type: Array }) |
| 21 | commits: GitLogEntry[] = []; |
| 22 | |
| 23 | @state() |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 24 | private fromCommit: string = ""; |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 25 | |
| 26 | @state() |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 27 | private toCommit: string = ""; |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 28 | |
| Philip Zeyliger | 364b35a | 2025-06-21 16:39:04 -0700 | [diff] [blame] | 29 | @state() |
| 30 | private dropdownOpen: boolean = false; |
| 31 | |
| Philip Zeyliger | 38499cc | 2025-06-15 21:17:05 -0700 | [diff] [blame] | 32 | // Removed commitsExpanded state - always expanded now |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 33 | |
| 34 | @state() |
| 35 | private loading: boolean = true; |
| 36 | |
| 37 | @state() |
| 38 | private error: string | null = null; |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 39 | |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 40 | @property({ attribute: false, type: Object }) |
| 41 | gitService!: GitDataService; |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 42 | |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 43 | constructor() { |
| 44 | super(); |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 45 | console.log("SketchDiffRangePicker initialized"); |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 46 | } |
| 47 | |
| banksean | 2be768e | 2025-07-18 16:41:39 +0000 | [diff] [blame^] | 48 | // 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 Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 62 | display: block; |
| 63 | width: 100%; |
| 64 | font-family: var(--font-family, system-ui, sans-serif); |
| 65 | color: var(--text-color, #333); |
| banksean | 2be768e | 2025-07-18 16:41:39 +0000 | [diff] [blame^] | 66 | }`; |
| 67 | } |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 68 | |
| 69 | connectedCallback() { |
| 70 | super.connectedCallback(); |
| banksean | 2be768e | 2025-07-18 16:41:39 +0000 | [diff] [blame^] | 71 | this.ensureGlobalStyles(); |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 72 | // Wait for DOM to be fully loaded to ensure proper initialization order |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 73 | if (document.readyState === "complete") { |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 74 | this.loadCommits(); |
| 75 | } else { |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 76 | window.addEventListener("load", () => { |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 77 | setTimeout(() => this.loadCommits(), 0); // Give time for provider initialization |
| 78 | }); |
| 79 | } |
| Autoformatter | 9abf803 | 2025-06-14 23:24:08 +0000 | [diff] [blame] | 80 | |
| David Crawshaw | 938d2dc | 2025-06-14 22:17:33 +0000 | [diff] [blame] | 81 | // Listen for popstate events to handle browser back/forward navigation |
| Autoformatter | 9abf803 | 2025-06-14 23:24:08 +0000 | [diff] [blame] | 82 | window.addEventListener("popstate", this.handlePopState.bind(this)); |
| David Crawshaw | 938d2dc | 2025-06-14 22:17:33 +0000 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | disconnectedCallback() { |
| 86 | super.disconnectedCallback(); |
| Autoformatter | 9abf803 | 2025-06-14 23:24:08 +0000 | [diff] [blame] | 87 | window.removeEventListener("popstate", this.handlePopState.bind(this)); |
| David Crawshaw | 938d2dc | 2025-06-14 22:17:33 +0000 | [diff] [blame] | 88 | } |
| 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 Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | render() { |
| 106 | return html` |
| banksean | 2be768e | 2025-07-18 16:41:39 +0000 | [diff] [blame^] | 107 | <div class="block w-full font-system text-gray-800"> |
| 108 | <div class="flex flex-col gap-3 w-full"> |
| 109 | ${this.loading |
| 110 | ? html`<div class="italic text-gray-500">Loading commits...</div>` |
| 111 | : this.error |
| 112 | ? html`<div class="text-red-600 text-sm">${this.error}</div>` |
| 113 | : this.renderRangePicker()} |
| 114 | </div> |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 115 | </div> |
| 116 | `; |
| 117 | } |
| 118 | |
| 119 | renderRangePicker() { |
| 120 | return html` |
| banksean | 2be768e | 2025-07-18 16:41:39 +0000 | [diff] [blame^] | 121 | <div class="flex flex-row items-center gap-3 flex-1"> |
| 122 | ${this.renderRangeSelectors()} |
| 123 | </div> |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 124 | `; |
| 125 | } |
| 126 | |
| 127 | renderRangeSelectors() { |
| Philip Zeyliger | 364b35a | 2025-06-21 16:39:04 -0700 | [diff] [blame] | 128 | // Always diff against uncommitted changes |
| 129 | this.toCommit = ""; |
| 130 | |
| 131 | const selectedCommit = this.commits.find((c) => c.hash === this.fromCommit); |
| 132 | const isDefaultCommit = |
| 133 | selectedCommit && this.isSketchBaseCommit(selectedCommit); |
| 134 | |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 135 | return html` |
| banksean | 2be768e | 2025-07-18 16:41:39 +0000 | [diff] [blame^] | 136 | <div class="flex items-center gap-2 flex-1 relative"> |
| 137 | <label for="fromCommit" class="font-medium text-sm text-gray-700" |
| 138 | >Diff from:</label |
| 139 | > |
| 140 | <div |
| 141 | class="relative w-full min-w-[300px]" |
| 142 | @click=${this.toggleDropdown} |
| 143 | > |
| Philip Zeyliger | 364b35a | 2025-06-21 16:39:04 -0700 | [diff] [blame] | 144 | <button |
| banksean | 2be768e | 2025-07-18 16:41:39 +0000 | [diff] [blame^] | 145 | class="w-full py-2 px-3 pr-8 border rounded text-left min-h-[36px] text-sm relative cursor-pointer bg-white ${isDefaultCommit |
| 146 | ? "border-blue-500 bg-blue-50" |
| 147 | : "border-gray-300 hover:border-gray-400"} focus:outline-none focus:border-blue-500 focus:ring-2 focus:ring-blue-200" |
| Philip Zeyliger | 364b35a | 2025-06-21 16:39:04 -0700 | [diff] [blame] | 148 | @click=${this.toggleDropdown} |
| 149 | @blur=${this.handleBlur} |
| 150 | > |
| banksean | 2be768e | 2025-07-18 16:41:39 +0000 | [diff] [blame^] | 151 | <div class="flex items-center gap-2 pr-6"> |
| 152 | ${selectedCommit |
| 153 | ? this.renderCommitButton(selectedCommit) |
| 154 | : "Select commit..."} |
| 155 | </div> |
| Philip Zeyliger | 364b35a | 2025-06-21 16:39:04 -0700 | [diff] [blame] | 156 | <svg |
| banksean | 2be768e | 2025-07-18 16:41:39 +0000 | [diff] [blame^] | 157 | class="absolute right-2 top-1/2 transform -translate-y-1/2 transition-transform duration-200 ${this |
| 158 | .dropdownOpen |
| 159 | ? "rotate-180" |
| 160 | : ""}" |
| Philip Zeyliger | 364b35a | 2025-06-21 16:39:04 -0700 | [diff] [blame] | 161 | width="12" |
| 162 | height="12" |
| 163 | viewBox="0 0 12 12" |
| 164 | > |
| 165 | <path d="M6 8l-4-4h8z" fill="currentColor" /> |
| 166 | </svg> |
| 167 | </button> |
| 168 | ${this.dropdownOpen |
| 169 | ? html` |
| banksean | 2be768e | 2025-07-18 16:41:39 +0000 | [diff] [blame^] | 170 | <div |
| 171 | class="absolute top-full left-0 right-0 bg-white border border-gray-300 rounded shadow-lg z-50 max-h-[300px] overflow-y-auto mt-0.5" |
| 172 | > |
| Philip Zeyliger | 364b35a | 2025-06-21 16:39:04 -0700 | [diff] [blame] | 173 | ${this.commits.map( |
| 174 | (commit) => html` |
| 175 | <div |
| banksean | 2be768e | 2025-07-18 16:41:39 +0000 | [diff] [blame^] | 176 | class="px-3 py-2.5 cursor-pointer border-b border-gray-100 last:border-b-0 flex items-start gap-2 text-sm leading-5 hover:bg-gray-50 ${commit.hash === |
| 177 | this.fromCommit |
| 178 | ? "bg-blue-50" |
| Philip Zeyliger | 364b35a | 2025-06-21 16:39:04 -0700 | [diff] [blame] | 179 | : ""} ${this.isSketchBaseCommit(commit) |
| banksean | 2be768e | 2025-07-18 16:41:39 +0000 | [diff] [blame^] | 180 | ? "bg-blue-50 border-l-4 border-l-blue-500 pl-2" |
| Philip Zeyliger | 364b35a | 2025-06-21 16:39:04 -0700 | [diff] [blame] | 181 | : ""}" |
| 182 | @click=${() => this.selectCommit(commit.hash)} |
| 183 | > |
| 184 | ${this.renderCommitOption(commit)} |
| 185 | </div> |
| 186 | `, |
| 187 | )} |
| 188 | </div> |
| 189 | ` |
| 190 | : ""} |
| 191 | </div> |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 192 | </div> |
| 193 | `; |
| 194 | } |
| 195 | |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 196 | /** |
| Philip Zeyliger | 364b35a | 2025-06-21 16:39:04 -0700 | [diff] [blame] | 197 | * Format a commit for display in the dropdown (legacy method, kept for compatibility) |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 198 | */ |
| 199 | formatCommitOption(commit: GitLogEntry): string { |
| 200 | const shortHash = commit.hash.substring(0, 7); |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 201 | |
| David Crawshaw | dbca897 | 2025-06-14 23:46:58 +0000 | [diff] [blame] | 202 | // Truncate subject if it's too long |
| 203 | let subject = commit.subject; |
| 204 | if (subject.length > 50) { |
| 205 | subject = subject.substring(0, 47) + "..."; |
| 206 | } |
| 207 | |
| 208 | let label = `${shortHash} ${subject}`; |
| 209 | |
| 210 | // Add refs but keep them concise |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 211 | if (commit.refs && commit.refs.length > 0) { |
| David Crawshaw | dbca897 | 2025-06-14 23:46:58 +0000 | [diff] [blame] | 212 | const refs = commit.refs.map((ref) => { |
| 213 | // Shorten common prefixes |
| 214 | if (ref.startsWith("origin/")) { |
| 215 | return ref.substring(7); |
| 216 | } |
| 217 | if (ref.startsWith("refs/heads/")) { |
| 218 | return ref.substring(11); |
| 219 | } |
| 220 | if (ref.startsWith("refs/remotes/origin/")) { |
| 221 | return ref.substring(20); |
| 222 | } |
| 223 | return ref; |
| 224 | }); |
| 225 | |
| 226 | // Limit to first 2 refs to avoid overcrowding |
| 227 | const displayRefs = refs.slice(0, 2); |
| 228 | if (refs.length > 2) { |
| 229 | displayRefs.push(`+${refs.length - 2} more`); |
| 230 | } |
| 231 | |
| 232 | label += ` (${displayRefs.join(", ")})`; |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 233 | } |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 234 | |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 235 | return label; |
| 236 | } |
| 237 | |
| 238 | /** |
| 239 | * Load commits from the Git data service |
| 240 | */ |
| 241 | async loadCommits() { |
| 242 | this.loading = true; |
| 243 | this.error = null; |
| 244 | |
| 245 | if (!this.gitService) { |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 246 | console.error("GitService was not provided to sketch-diff-range-picker"); |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 247 | throw Error(); |
| 248 | } |
| 249 | |
| 250 | try { |
| 251 | // Get the base commit reference |
| 252 | const baseCommitRef = await this.gitService.getBaseCommitRef(); |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 253 | |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 254 | // Load commit history |
| 255 | this.commits = await this.gitService.getCommitHistory(baseCommitRef); |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 256 | |
| David Crawshaw | 938d2dc | 2025-06-14 22:17:33 +0000 | [diff] [blame] | 257 | // Check if we should initialize from URL parameters first |
| 258 | const initializedFromUrl = this.initializeFromUrlParams(); |
| Autoformatter | 9abf803 | 2025-06-14 23:24:08 +0000 | [diff] [blame] | 259 | |
| David Crawshaw | 938d2dc | 2025-06-14 22:17:33 +0000 | [diff] [blame] | 260 | // Set default selections only if not initialized from URL |
| 261 | if (this.commits.length > 0 && !initializedFromUrl) { |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 262 | // For range, default is base to HEAD |
| 263 | // TODO: is sketch-base right in the unsafe context, where it's sketch-base-... |
| 264 | // should this be startswith? |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 265 | const baseCommit = this.commits.find( |
| 266 | (c) => c.refs && c.refs.some((ref) => ref.includes("sketch-base")), |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 267 | ); |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 268 | |
| 269 | this.fromCommit = baseCommit |
| 270 | ? baseCommit.hash |
| 271 | : this.commits[this.commits.length - 1].hash; |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 272 | // Default to Uncommitted Changes by setting toCommit to empty string |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 273 | this.toCommit = ""; // Empty string represents uncommitted changes |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 274 | } |
| Autoformatter | 9abf803 | 2025-06-14 23:24:08 +0000 | [diff] [blame] | 275 | |
| David Crawshaw | 938d2dc | 2025-06-14 22:17:33 +0000 | [diff] [blame] | 276 | // Always dispatch range event to ensure diff view is updated |
| 277 | this.dispatchRangeEvent(); |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 278 | } catch (error) { |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 279 | console.error("Error loading commits:", error); |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 280 | this.error = `Error loading commits: ${error.message}`; |
| 281 | } finally { |
| 282 | this.loading = false; |
| 283 | } |
| 284 | } |
| 285 | |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 286 | /** |
| 287 | * Handle From commit change |
| 288 | */ |
| 289 | handleFromChange(event: Event) { |
| 290 | const select = event.target as HTMLSelectElement; |
| 291 | this.fromCommit = select.value; |
| 292 | this.dispatchRangeEvent(); |
| 293 | } |
| 294 | |
| 295 | /** |
| 296 | * Handle To commit change |
| 297 | */ |
| 298 | handleToChange(event: Event) { |
| 299 | const select = event.target as HTMLSelectElement; |
| 300 | this.toCommit = select.value; |
| 301 | this.dispatchRangeEvent(); |
| 302 | } |
| 303 | |
| Philip Zeyliger | 364b35a | 2025-06-21 16:39:04 -0700 | [diff] [blame] | 304 | /** |
| 305 | * Toggle dropdown open/closed |
| 306 | */ |
| 307 | toggleDropdown(event: Event) { |
| 308 | event.stopPropagation(); |
| 309 | this.dropdownOpen = !this.dropdownOpen; |
| 310 | |
| 311 | if (this.dropdownOpen) { |
| 312 | // Close dropdown when clicking outside |
| 313 | setTimeout(() => { |
| 314 | document.addEventListener("click", this.closeDropdown, { once: true }); |
| 315 | }, 0); |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | /** |
| 320 | * Close dropdown |
| 321 | */ |
| 322 | closeDropdown = () => { |
| 323 | this.dropdownOpen = false; |
| 324 | }; |
| 325 | |
| 326 | /** |
| 327 | * Handle blur event on select button |
| 328 | */ |
| philip.zeyliger | 26bc659 | 2025-06-30 20:15:30 -0700 | [diff] [blame] | 329 | handleBlur(_event: FocusEvent) { |
| Philip Zeyliger | 364b35a | 2025-06-21 16:39:04 -0700 | [diff] [blame] | 330 | // Small delay to allow click events to process |
| 331 | setTimeout(() => { |
| 332 | if (!this.shadowRoot?.activeElement?.closest(".custom-select")) { |
| 333 | this.dropdownOpen = false; |
| 334 | } |
| 335 | }, 150); |
| 336 | } |
| 337 | |
| 338 | /** |
| 339 | * Select a commit from dropdown |
| 340 | */ |
| 341 | selectCommit(hash: string) { |
| 342 | this.fromCommit = hash; |
| 343 | this.dropdownOpen = false; |
| 344 | this.dispatchRangeEvent(); |
| 345 | } |
| 346 | |
| 347 | /** |
| 348 | * Check if a commit is a sketch-base commit |
| 349 | */ |
| 350 | isSketchBaseCommit(commit: GitLogEntry): boolean { |
| 351 | return commit.refs?.some((ref) => ref.includes("sketch-base")) || false; |
| 352 | } |
| 353 | |
| 354 | /** |
| 355 | * Render commit for the dropdown button |
| 356 | */ |
| 357 | renderCommitButton(commit: GitLogEntry) { |
| 358 | const shortHash = commit.hash.substring(0, 7); |
| 359 | let subject = commit.subject; |
| 360 | if (subject.length > 40) { |
| 361 | subject = subject.substring(0, 37) + "..."; |
| 362 | } |
| 363 | |
| 364 | return html` |
| banksean | 2be768e | 2025-07-18 16:41:39 +0000 | [diff] [blame^] | 365 | <span class="font-mono text-gray-600 text-xs">${shortHash}</span> |
| 366 | <span class="text-gray-800 text-xs truncate">${subject}</span> |
| Philip Zeyliger | 364b35a | 2025-06-21 16:39:04 -0700 | [diff] [blame] | 367 | ${this.isSketchBaseCommit(commit) |
| banksean | 2be768e | 2025-07-18 16:41:39 +0000 | [diff] [blame^] | 368 | ? html` |
| 369 | <span |
| 370 | class="bg-blue-600 text-white px-1.5 py-0.5 rounded-full text-xs font-semibold" |
| 371 | >base</span |
| 372 | > |
| 373 | ` |
| Philip Zeyliger | 364b35a | 2025-06-21 16:39:04 -0700 | [diff] [blame] | 374 | : ""} |
| 375 | `; |
| 376 | } |
| 377 | |
| 378 | /** |
| 379 | * Render commit option in dropdown |
| 380 | */ |
| 381 | renderCommitOption(commit: GitLogEntry) { |
| 382 | const shortHash = commit.hash.substring(0, 7); |
| 383 | let subject = commit.subject; |
| 384 | if (subject.length > 50) { |
| 385 | subject = subject.substring(0, 47) + "..."; |
| 386 | } |
| 387 | |
| 388 | return html` |
| banksean | 2be768e | 2025-07-18 16:41:39 +0000 | [diff] [blame^] | 389 | <span class="font-mono text-gray-600 text-xs">${shortHash}</span> |
| 390 | <span class="text-gray-800 text-xs flex-1 truncate min-w-[200px]" |
| 391 | >${subject}</span |
| 392 | > |
| Philip Zeyliger | 364b35a | 2025-06-21 16:39:04 -0700 | [diff] [blame] | 393 | ${commit.refs && commit.refs.length > 0 |
| banksean | 2be768e | 2025-07-18 16:41:39 +0000 | [diff] [blame^] | 394 | ? html` |
| 395 | <div class="flex gap-1 flex-wrap"> |
| 396 | ${this.renderRefs(commit.refs)} |
| 397 | </div> |
| 398 | ` |
| Philip Zeyliger | 364b35a | 2025-06-21 16:39:04 -0700 | [diff] [blame] | 399 | : ""} |
| 400 | `; |
| 401 | } |
| 402 | |
| 403 | /** |
| 404 | * Render all refs naturally without truncation |
| 405 | */ |
| 406 | renderRefs(refs: string[]) { |
| 407 | return html` |
| banksean | 2be768e | 2025-07-18 16:41:39 +0000 | [diff] [blame^] | 408 | <div class="flex gap-1 flex-wrap flex-shrink-0"> |
| Philip Zeyliger | 364b35a | 2025-06-21 16:39:04 -0700 | [diff] [blame] | 409 | ${refs.map((ref) => { |
| 410 | const shortRef = this.getShortRefName(ref); |
| 411 | const isSketchBase = ref.includes("sketch-base"); |
| 412 | const refClass = isSketchBase |
| banksean | 2be768e | 2025-07-18 16:41:39 +0000 | [diff] [blame^] | 413 | ? "bg-blue-600 text-white font-semibold" |
| Philip Zeyliger | 364b35a | 2025-06-21 16:39:04 -0700 | [diff] [blame] | 414 | : ref.includes("tag") |
| banksean | 2be768e | 2025-07-18 16:41:39 +0000 | [diff] [blame^] | 415 | ? "bg-amber-100 text-amber-800" |
| 416 | : "bg-green-100 text-green-800"; |
| 417 | return html`<span |
| 418 | class="px-1.5 py-0.5 rounded-full text-xs font-medium ${refClass}" |
| 419 | >${shortRef}</span |
| 420 | >`; |
| Philip Zeyliger | 364b35a | 2025-06-21 16:39:04 -0700 | [diff] [blame] | 421 | })} |
| 422 | </div> |
| 423 | `; |
| 424 | } |
| 425 | |
| 426 | /** |
| 427 | * Get shortened reference name for compact display |
| 428 | */ |
| 429 | getShortRefName(ref: string): string { |
| 430 | if (ref.startsWith("refs/heads/")) { |
| 431 | return ref.substring(11); |
| 432 | } |
| 433 | if (ref.startsWith("refs/remotes/origin/")) { |
| 434 | return ref.substring(20); |
| 435 | } |
| 436 | if (ref.startsWith("refs/tags/")) { |
| 437 | return ref.substring(10); |
| 438 | } |
| 439 | return ref; |
| 440 | } |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 441 | |
| 442 | /** |
| David Crawshaw | 216d2fc | 2025-06-15 18:45:53 +0000 | [diff] [blame] | 443 | * Get a summary of the current commit range for display |
| Philip Zeyliger | e89b308 | 2025-05-29 03:16:06 +0000 | [diff] [blame] | 444 | */ |
| David Crawshaw | 216d2fc | 2025-06-15 18:45:53 +0000 | [diff] [blame] | 445 | getCommitSummary(): string { |
| 446 | if (!this.fromCommit && !this.toCommit) { |
| Autoformatter | 6255411 | 2025-06-15 19:23:33 +0000 | [diff] [blame] | 447 | return "No commits selected"; |
| David Crawshaw | 216d2fc | 2025-06-15 18:45:53 +0000 | [diff] [blame] | 448 | } |
| 449 | |
| Autoformatter | 6255411 | 2025-06-15 19:23:33 +0000 | [diff] [blame] | 450 | const fromShort = this.fromCommit ? this.fromCommit.substring(0, 7) : ""; |
| 451 | const toShort = this.toCommit |
| 452 | ? this.toCommit.substring(0, 7) |
| 453 | : "Uncommitted"; |
| 454 | |
| David Crawshaw | 216d2fc | 2025-06-15 18:45:53 +0000 | [diff] [blame] | 455 | return `${fromShort}..${toShort}`; |
| Philip Zeyliger | e89b308 | 2025-05-29 03:16:06 +0000 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | /** |
| David Crawshaw | 938d2dc | 2025-06-14 22:17:33 +0000 | [diff] [blame] | 459 | * Validate that a commit hash exists in the loaded commits |
| 460 | */ |
| 461 | private isValidCommitHash(hash: string): boolean { |
| Autoformatter | 9abf803 | 2025-06-14 23:24:08 +0000 | [diff] [blame] | 462 | if (!hash || hash.trim() === "") return true; // Empty is valid (uncommitted changes) |
| 463 | return this.commits.some( |
| 464 | (commit) => commit.hash.startsWith(hash) || commit.hash === hash, |
| 465 | ); |
| David Crawshaw | 938d2dc | 2025-06-14 22:17:33 +0000 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | /** |
| 469 | * Dispatch range change event and update URL parameters |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 470 | */ |
| 471 | dispatchRangeEvent() { |
| Autoformatter | 6255411 | 2025-06-15 19:23:33 +0000 | [diff] [blame] | 472 | const range: DiffRange = { |
| 473 | type: "range", |
| 474 | from: this.fromCommit, |
| 475 | to: this.toCommit, |
| 476 | }; |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 477 | |
| David Crawshaw | 938d2dc | 2025-06-14 22:17:33 +0000 | [diff] [blame] | 478 | // Update URL parameters |
| 479 | this.updateUrlParams(range); |
| 480 | |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 481 | const event = new CustomEvent("range-change", { |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 482 | detail: { range }, |
| 483 | bubbles: true, |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 484 | composed: true, |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 485 | }); |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 486 | |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 487 | this.dispatchEvent(event); |
| 488 | } |
| David Crawshaw | 938d2dc | 2025-06-14 22:17:33 +0000 | [diff] [blame] | 489 | |
| 490 | /** |
| 491 | * Update URL parameters for from and to commits |
| 492 | */ |
| 493 | private updateUrlParams(range: DiffRange) { |
| 494 | const url = new URL(window.location.href); |
| Autoformatter | 9abf803 | 2025-06-14 23:24:08 +0000 | [diff] [blame] | 495 | |
| David Crawshaw | 938d2dc | 2025-06-14 22:17:33 +0000 | [diff] [blame] | 496 | // Remove existing range parameters |
| Autoformatter | 9abf803 | 2025-06-14 23:24:08 +0000 | [diff] [blame] | 497 | url.searchParams.delete("from"); |
| 498 | url.searchParams.delete("to"); |
| 499 | url.searchParams.delete("commit"); |
| 500 | |
| David Crawshaw | 216d2fc | 2025-06-15 18:45:53 +0000 | [diff] [blame] | 501 | // Add from parameter if not empty |
| 502 | if (range.from && range.from.trim() !== "") { |
| 503 | url.searchParams.set("from", range.from); |
| 504 | } |
| 505 | // Add to parameter if not empty (empty string means uncommitted changes) |
| 506 | if (range.to && range.to.trim() !== "") { |
| 507 | url.searchParams.set("to", range.to); |
| David Crawshaw | 938d2dc | 2025-06-14 22:17:33 +0000 | [diff] [blame] | 508 | } |
| Autoformatter | 9abf803 | 2025-06-14 23:24:08 +0000 | [diff] [blame] | 509 | |
| David Crawshaw | 938d2dc | 2025-06-14 22:17:33 +0000 | [diff] [blame] | 510 | // Update the browser history without reloading the page |
| Autoformatter | 9abf803 | 2025-06-14 23:24:08 +0000 | [diff] [blame] | 511 | window.history.replaceState(window.history.state, "", url.toString()); |
| David Crawshaw | 938d2dc | 2025-06-14 22:17:33 +0000 | [diff] [blame] | 512 | } |
| 513 | |
| 514 | /** |
| 515 | * Initialize from URL parameters if available |
| 516 | */ |
| 517 | private initializeFromUrlParams() { |
| 518 | const url = new URL(window.location.href); |
| Autoformatter | 9abf803 | 2025-06-14 23:24:08 +0000 | [diff] [blame] | 519 | const fromParam = url.searchParams.get("from"); |
| 520 | const toParam = url.searchParams.get("to"); |
| Autoformatter | 9abf803 | 2025-06-14 23:24:08 +0000 | [diff] [blame] | 521 | |
| David Crawshaw | 216d2fc | 2025-06-15 18:45:53 +0000 | [diff] [blame] | 522 | // If from or to parameters are present, use them |
| David Crawshaw | 938d2dc | 2025-06-14 22:17:33 +0000 | [diff] [blame] | 523 | if (fromParam || toParam) { |
| David Crawshaw | 938d2dc | 2025-06-14 22:17:33 +0000 | [diff] [blame] | 524 | if (fromParam) { |
| 525 | this.fromCommit = fromParam; |
| 526 | } |
| 527 | if (toParam) { |
| 528 | this.toCommit = toParam; |
| 529 | } else { |
| 530 | // If no 'to' param, default to uncommitted changes (empty string) |
| Autoformatter | 9abf803 | 2025-06-14 23:24:08 +0000 | [diff] [blame] | 531 | this.toCommit = ""; |
| David Crawshaw | 938d2dc | 2025-06-14 22:17:33 +0000 | [diff] [blame] | 532 | } |
| 533 | return true; // Indicate that we initialized from URL |
| 534 | } |
| Autoformatter | 9abf803 | 2025-06-14 23:24:08 +0000 | [diff] [blame] | 535 | |
| David Crawshaw | 938d2dc | 2025-06-14 22:17:33 +0000 | [diff] [blame] | 536 | return false; // No URL params found |
| 537 | } |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 538 | } |
| 539 | |
| 540 | declare global { |
| 541 | interface HTMLElementTagNameMap { |
| 542 | "sketch-diff-range-picker": SketchDiffRangePicker; |
| 543 | } |
| 544 | } |