| 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 | 3eaa433 | 2025-07-19 02:19:06 +0000 | [diff] [blame] | 107 | <div class="block w-full font-system text-gray-800 dark:text-gray-200"> |
| banksean | 2be768e | 2025-07-18 16:41:39 +0000 | [diff] [blame] | 108 | <div class="flex flex-col gap-3 w-full"> |
| 109 | ${this.loading |
| banksean | 3eaa433 | 2025-07-19 02:19:06 +0000 | [diff] [blame] | 110 | ? html`<div class="italic text-gray-500 dark:text-gray-400"> |
| 111 | Loading commits... |
| 112 | </div>` |
| banksean | 2be768e | 2025-07-18 16:41:39 +0000 | [diff] [blame] | 113 | : this.error |
| banksean | 3eaa433 | 2025-07-19 02:19:06 +0000 | [diff] [blame] | 114 | ? html`<div class="text-red-600 dark:text-red-400 text-sm"> |
| 115 | ${this.error} |
| 116 | </div>` |
| banksean | 2be768e | 2025-07-18 16:41:39 +0000 | [diff] [blame] | 117 | : this.renderRangePicker()} |
| 118 | </div> |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 119 | </div> |
| 120 | `; |
| 121 | } |
| 122 | |
| 123 | renderRangePicker() { |
| 124 | return html` |
| banksean | 2be768e | 2025-07-18 16:41:39 +0000 | [diff] [blame] | 125 | <div class="flex flex-row items-center gap-3 flex-1"> |
| 126 | ${this.renderRangeSelectors()} |
| 127 | </div> |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 128 | `; |
| 129 | } |
| 130 | |
| 131 | renderRangeSelectors() { |
| Philip Zeyliger | 364b35a | 2025-06-21 16:39:04 -0700 | [diff] [blame] | 132 | // 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 Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 139 | return html` |
| banksean | 2be768e | 2025-07-18 16:41:39 +0000 | [diff] [blame] | 140 | <div class="flex items-center gap-2 flex-1 relative"> |
| banksean | 3eaa433 | 2025-07-19 02:19:06 +0000 | [diff] [blame] | 141 | <label |
| 142 | for="fromCommit" |
| 143 | class="font-medium text-sm text-gray-700 dark:text-gray-300" |
| banksean | 2be768e | 2025-07-18 16:41:39 +0000 | [diff] [blame] | 144 | >Diff from:</label |
| 145 | > |
| 146 | <div |
| 147 | class="relative w-full min-w-[300px]" |
| 148 | @click=${this.toggleDropdown} |
| 149 | > |
| Philip Zeyliger | 364b35a | 2025-06-21 16:39:04 -0700 | [diff] [blame] | 150 | <button |
| banksean | 3eaa433 | 2025-07-19 02:19:06 +0000 | [diff] [blame] | 151 | 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 Zeyliger | 364b35a | 2025-06-21 16:39:04 -0700 | [diff] [blame] | 154 | @click=${this.toggleDropdown} |
| 155 | @blur=${this.handleBlur} |
| 156 | > |
| banksean | 2be768e | 2025-07-18 16:41:39 +0000 | [diff] [blame] | 157 | <div class="flex items-center gap-2 pr-6"> |
| 158 | ${selectedCommit |
| 159 | ? this.renderCommitButton(selectedCommit) |
| 160 | : "Select commit..."} |
| 161 | </div> |
| Philip Zeyliger | 364b35a | 2025-06-21 16:39:04 -0700 | [diff] [blame] | 162 | <svg |
| banksean | 2be768e | 2025-07-18 16:41:39 +0000 | [diff] [blame] | 163 | class="absolute right-2 top-1/2 transform -translate-y-1/2 transition-transform duration-200 ${this |
| 164 | .dropdownOpen |
| 165 | ? "rotate-180" |
| 166 | : ""}" |
| Philip Zeyliger | 364b35a | 2025-06-21 16:39:04 -0700 | [diff] [blame] | 167 | 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` |
| banksean | 2be768e | 2025-07-18 16:41:39 +0000 | [diff] [blame] | 176 | <div |
| banksean | 3eaa433 | 2025-07-19 02:19:06 +0000 | [diff] [blame] | 177 | 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" |
| banksean | 2be768e | 2025-07-18 16:41:39 +0000 | [diff] [blame] | 178 | > |
| Philip Zeyliger | 364b35a | 2025-06-21 16:39:04 -0700 | [diff] [blame] | 179 | ${this.commits.map( |
| 180 | (commit) => html` |
| 181 | <div |
| banksean | 3eaa433 | 2025-07-19 02:19:06 +0000 | [diff] [blame] | 182 | 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 === |
| banksean | 2be768e | 2025-07-18 16:41:39 +0000 | [diff] [blame] | 183 | this.fromCommit |
| banksean | 3eaa433 | 2025-07-19 02:19:06 +0000 | [diff] [blame] | 184 | ? "bg-blue-50 dark:bg-blue-900/30" |
| Philip Zeyliger | 364b35a | 2025-06-21 16:39:04 -0700 | [diff] [blame] | 185 | : ""} ${this.isSketchBaseCommit(commit) |
| banksean | 3eaa433 | 2025-07-19 02:19:06 +0000 | [diff] [blame] | 186 | ? "bg-blue-50 dark:bg-blue-900/30 border-l-4 border-l-blue-500 pl-2" |
| Philip Zeyliger | 364b35a | 2025-06-21 16:39:04 -0700 | [diff] [blame] | 187 | : ""}" |
| 188 | @click=${() => this.selectCommit(commit.hash)} |
| 189 | > |
| 190 | ${this.renderCommitOption(commit)} |
| 191 | </div> |
| 192 | `, |
| 193 | )} |
| 194 | </div> |
| 195 | ` |
| 196 | : ""} |
| 197 | </div> |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 198 | </div> |
| 199 | `; |
| 200 | } |
| 201 | |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 202 | /** |
| Philip Zeyliger | 364b35a | 2025-06-21 16:39:04 -0700 | [diff] [blame] | 203 | * 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] | 204 | */ |
| 205 | formatCommitOption(commit: GitLogEntry): string { |
| 206 | const shortHash = commit.hash.substring(0, 7); |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 207 | |
| David Crawshaw | dbca897 | 2025-06-14 23:46:58 +0000 | [diff] [blame] | 208 | // 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 Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 217 | if (commit.refs && commit.refs.length > 0) { |
| David Crawshaw | dbca897 | 2025-06-14 23:46:58 +0000 | [diff] [blame] | 218 | 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 Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 239 | } |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 240 | |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 241 | 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) { |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 252 | console.error("GitService was not provided to sketch-diff-range-picker"); |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 253 | throw Error(); |
| 254 | } |
| 255 | |
| 256 | try { |
| 257 | // Get the base commit reference |
| 258 | const baseCommitRef = await this.gitService.getBaseCommitRef(); |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 259 | |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 260 | // Load commit history |
| 261 | this.commits = await this.gitService.getCommitHistory(baseCommitRef); |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 262 | |
| David Crawshaw | 938d2dc | 2025-06-14 22:17:33 +0000 | [diff] [blame] | 263 | // Check if we should initialize from URL parameters first |
| 264 | const initializedFromUrl = this.initializeFromUrlParams(); |
| Autoformatter | 9abf803 | 2025-06-14 23:24:08 +0000 | [diff] [blame] | 265 | |
| David Crawshaw | 938d2dc | 2025-06-14 22:17:33 +0000 | [diff] [blame] | 266 | // Set default selections only if not initialized from URL |
| 267 | if (this.commits.length > 0 && !initializedFromUrl) { |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 268 | // 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? |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 271 | const baseCommit = this.commits.find( |
| 272 | (c) => c.refs && c.refs.some((ref) => ref.includes("sketch-base")), |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 273 | ); |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 274 | |
| 275 | this.fromCommit = baseCommit |
| 276 | ? baseCommit.hash |
| 277 | : this.commits[this.commits.length - 1].hash; |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 278 | // Default to Uncommitted Changes by setting toCommit to empty string |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 279 | this.toCommit = ""; // Empty string represents uncommitted changes |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 280 | } |
| Autoformatter | 9abf803 | 2025-06-14 23:24:08 +0000 | [diff] [blame] | 281 | |
| David Crawshaw | 938d2dc | 2025-06-14 22:17:33 +0000 | [diff] [blame] | 282 | // Always dispatch range event to ensure diff view is updated |
| 283 | this.dispatchRangeEvent(); |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 284 | } catch (error) { |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 285 | console.error("Error loading commits:", error); |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 286 | this.error = `Error loading commits: ${error.message}`; |
| 287 | } finally { |
| 288 | this.loading = false; |
| 289 | } |
| 290 | } |
| 291 | |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 292 | /** |
| 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 Zeyliger | 364b35a | 2025-06-21 16:39:04 -0700 | [diff] [blame] | 310 | /** |
| 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.zeyliger | 26bc659 | 2025-06-30 20:15:30 -0700 | [diff] [blame] | 335 | handleBlur(_event: FocusEvent) { |
| Philip Zeyliger | 364b35a | 2025-06-21 16:39:04 -0700 | [diff] [blame] | 336 | // Small delay to allow click events to process |
| 337 | setTimeout(() => { |
| banksean | 4432056 | 2025-07-21 11:09:38 -0700 | [diff] [blame] | 338 | if (!this.closest(".custom-select")) { |
| Philip Zeyliger | 364b35a | 2025-06-21 16:39:04 -0700 | [diff] [blame] | 339 | 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` |
| banksean | 3eaa433 | 2025-07-19 02:19:06 +0000 | [diff] [blame] | 371 | <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 Zeyliger | 364b35a | 2025-06-21 16:39:04 -0700 | [diff] [blame] | 377 | ${this.isSketchBaseCommit(commit) |
| banksean | 2be768e | 2025-07-18 16:41:39 +0000 | [diff] [blame] | 378 | ? 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 Zeyliger | 364b35a | 2025-06-21 16:39:04 -0700 | [diff] [blame] | 384 | : ""} |
| 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` |
| banksean | 3eaa433 | 2025-07-19 02:19:06 +0000 | [diff] [blame] | 399 | <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]" |
| banksean | 2be768e | 2025-07-18 16:41:39 +0000 | [diff] [blame] | 404 | >${subject}</span |
| 405 | > |
| Philip Zeyliger | 364b35a | 2025-06-21 16:39:04 -0700 | [diff] [blame] | 406 | ${commit.refs && commit.refs.length > 0 |
| banksean | 2be768e | 2025-07-18 16:41:39 +0000 | [diff] [blame] | 407 | ? html` |
| 408 | <div class="flex gap-1 flex-wrap"> |
| 409 | ${this.renderRefs(commit.refs)} |
| 410 | </div> |
| 411 | ` |
| Philip Zeyliger | 364b35a | 2025-06-21 16:39:04 -0700 | [diff] [blame] | 412 | : ""} |
| 413 | `; |
| 414 | } |
| 415 | |
| 416 | /** |
| 417 | * Render all refs naturally without truncation |
| 418 | */ |
| 419 | renderRefs(refs: string[]) { |
| 420 | return html` |
| banksean | 2be768e | 2025-07-18 16:41:39 +0000 | [diff] [blame] | 421 | <div class="flex gap-1 flex-wrap flex-shrink-0"> |
| Philip Zeyliger | 364b35a | 2025-06-21 16:39:04 -0700 | [diff] [blame] | 422 | ${refs.map((ref) => { |
| 423 | const shortRef = this.getShortRefName(ref); |
| 424 | const isSketchBase = ref.includes("sketch-base"); |
| 425 | const refClass = isSketchBase |
| banksean | 2be768e | 2025-07-18 16:41:39 +0000 | [diff] [blame] | 426 | ? "bg-blue-600 text-white font-semibold" |
| Philip Zeyliger | 364b35a | 2025-06-21 16:39:04 -0700 | [diff] [blame] | 427 | : ref.includes("tag") |
| banksean | 3eaa433 | 2025-07-19 02:19:06 +0000 | [diff] [blame] | 428 | ? "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"; |
| banksean | 2be768e | 2025-07-18 16:41:39 +0000 | [diff] [blame] | 430 | return html`<span |
| 431 | class="px-1.5 py-0.5 rounded-full text-xs font-medium ${refClass}" |
| 432 | >${shortRef}</span |
| 433 | >`; |
| Philip Zeyliger | 364b35a | 2025-06-21 16:39:04 -0700 | [diff] [blame] | 434 | })} |
| 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 Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 454 | |
| 455 | /** |
| David Crawshaw | 216d2fc | 2025-06-15 18:45:53 +0000 | [diff] [blame] | 456 | * Get a summary of the current commit range for display |
| Philip Zeyliger | e89b308 | 2025-05-29 03:16:06 +0000 | [diff] [blame] | 457 | */ |
| David Crawshaw | 216d2fc | 2025-06-15 18:45:53 +0000 | [diff] [blame] | 458 | getCommitSummary(): string { |
| 459 | if (!this.fromCommit && !this.toCommit) { |
| Autoformatter | 6255411 | 2025-06-15 19:23:33 +0000 | [diff] [blame] | 460 | return "No commits selected"; |
| David Crawshaw | 216d2fc | 2025-06-15 18:45:53 +0000 | [diff] [blame] | 461 | } |
| 462 | |
| Autoformatter | 6255411 | 2025-06-15 19:23:33 +0000 | [diff] [blame] | 463 | const fromShort = this.fromCommit ? this.fromCommit.substring(0, 7) : ""; |
| 464 | const toShort = this.toCommit |
| 465 | ? this.toCommit.substring(0, 7) |
| 466 | : "Uncommitted"; |
| 467 | |
| David Crawshaw | 216d2fc | 2025-06-15 18:45:53 +0000 | [diff] [blame] | 468 | return `${fromShort}..${toShort}`; |
| Philip Zeyliger | e89b308 | 2025-05-29 03:16:06 +0000 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | /** |
| David Crawshaw | 938d2dc | 2025-06-14 22:17:33 +0000 | [diff] [blame] | 472 | * Validate that a commit hash exists in the loaded commits |
| 473 | */ |
| 474 | private isValidCommitHash(hash: string): boolean { |
| Autoformatter | 9abf803 | 2025-06-14 23:24:08 +0000 | [diff] [blame] | 475 | 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 Crawshaw | 938d2dc | 2025-06-14 22:17:33 +0000 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | /** |
| 482 | * Dispatch range change event and update URL parameters |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 483 | */ |
| 484 | dispatchRangeEvent() { |
| Autoformatter | 6255411 | 2025-06-15 19:23:33 +0000 | [diff] [blame] | 485 | const range: DiffRange = { |
| 486 | type: "range", |
| 487 | from: this.fromCommit, |
| 488 | to: this.toCommit, |
| 489 | }; |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 490 | |
| David Crawshaw | 938d2dc | 2025-06-14 22:17:33 +0000 | [diff] [blame] | 491 | // Update URL parameters |
| 492 | this.updateUrlParams(range); |
| 493 | |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 494 | const event = new CustomEvent("range-change", { |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 495 | detail: { range }, |
| 496 | bubbles: true, |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 497 | composed: true, |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 498 | }); |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 499 | |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 500 | this.dispatchEvent(event); |
| 501 | } |
| David Crawshaw | 938d2dc | 2025-06-14 22:17:33 +0000 | [diff] [blame] | 502 | |
| 503 | /** |
| 504 | * Update URL parameters for from and to commits |
| 505 | */ |
| 506 | private updateUrlParams(range: DiffRange) { |
| 507 | const url = new URL(window.location.href); |
| Autoformatter | 9abf803 | 2025-06-14 23:24:08 +0000 | [diff] [blame] | 508 | |
| David Crawshaw | 938d2dc | 2025-06-14 22:17:33 +0000 | [diff] [blame] | 509 | // Remove existing range parameters |
| Autoformatter | 9abf803 | 2025-06-14 23:24:08 +0000 | [diff] [blame] | 510 | url.searchParams.delete("from"); |
| 511 | url.searchParams.delete("to"); |
| 512 | url.searchParams.delete("commit"); |
| 513 | |
| David Crawshaw | 216d2fc | 2025-06-15 18:45:53 +0000 | [diff] [blame] | 514 | // 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 Crawshaw | 938d2dc | 2025-06-14 22:17:33 +0000 | [diff] [blame] | 521 | } |
| Autoformatter | 9abf803 | 2025-06-14 23:24:08 +0000 | [diff] [blame] | 522 | |
| David Crawshaw | 938d2dc | 2025-06-14 22:17:33 +0000 | [diff] [blame] | 523 | // Update the browser history without reloading the page |
| Autoformatter | 9abf803 | 2025-06-14 23:24:08 +0000 | [diff] [blame] | 524 | window.history.replaceState(window.history.state, "", url.toString()); |
| David Crawshaw | 938d2dc | 2025-06-14 22:17:33 +0000 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | /** |
| 528 | * Initialize from URL parameters if available |
| 529 | */ |
| 530 | private initializeFromUrlParams() { |
| 531 | const url = new URL(window.location.href); |
| Autoformatter | 9abf803 | 2025-06-14 23:24:08 +0000 | [diff] [blame] | 532 | const fromParam = url.searchParams.get("from"); |
| 533 | const toParam = url.searchParams.get("to"); |
| Autoformatter | 9abf803 | 2025-06-14 23:24:08 +0000 | [diff] [blame] | 534 | |
| David Crawshaw | 216d2fc | 2025-06-15 18:45:53 +0000 | [diff] [blame] | 535 | // If from or to parameters are present, use them |
| David Crawshaw | 938d2dc | 2025-06-14 22:17:33 +0000 | [diff] [blame] | 536 | if (fromParam || toParam) { |
| David Crawshaw | 938d2dc | 2025-06-14 22:17:33 +0000 | [diff] [blame] | 537 | 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) |
| Autoformatter | 9abf803 | 2025-06-14 23:24:08 +0000 | [diff] [blame] | 544 | this.toCommit = ""; |
| David Crawshaw | 938d2dc | 2025-06-14 22:17:33 +0000 | [diff] [blame] | 545 | } |
| 546 | return true; // Indicate that we initialized from URL |
| 547 | } |
| Autoformatter | 9abf803 | 2025-06-14 23:24:08 +0000 | [diff] [blame] | 548 | |
| David Crawshaw | 938d2dc | 2025-06-14 22:17:33 +0000 | [diff] [blame] | 549 | return false; // No URL params found |
| 550 | } |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 551 | } |
| 552 | |
| 553 | declare global { |
| 554 | interface HTMLElementTagNameMap { |
| 555 | "sketch-diff-range-picker": SketchDiffRangePicker; |
| 556 | } |
| 557 | } |