Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 | 129x 165x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 229x 229x 229x 229x 229x 229x 229x 229x 229x 229x 229x 229x 229x 129x 129x 129x 129x 129x 129x 129x 129x 129x 323x 323x 323x 129x 6x 6x 6x 6x 1x 129x 316x 316x 316x 4x 4x 316x 4x 316x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 316x 316x 316x 316x 34x 34x 34x 34x 34x 34x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 34x 129x 129x 129x 34x 2x 2x 2x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 20x 20x 20x 20x 21x 21x 21x 21x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 20x 20x 20x 20x 48x 48x 20x 33x 33x 33x 20x 20x 20x 20x 20x 20x 20x 27x 22x 22x 22x 22x 27x 20x 20x 20x 27x 27x 48x 48x 48x 48x 48x 48x 48x 48x 34x 34x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 20x 48x 48x 15x 15x 48x 33x 33x 33x 33x 33x 33x 33x 33x 33x 48x 48x 48x 48x 48x 48x 48x 33x 48x 18x 48x 48x 24x 48x 48x 48x 48x 48x 48x 48x 48x 48x 48x 48x 48x 21x 48x 48x 26x 26x 26x 48x 7x 7x 7x 7x 7x 7x 7x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 128x 128x 128x 128x 129x 129x 129x 129x 129x 129x 129x 129x | import { html, TemplateResult } from 'lit-html';
import { css, PropertyValues } from 'lit';
import { property, state } from 'lit/decorators.js';
import { RapidElement } from '../RapidElement';
import { CustomEventType } from '../interfaces';
import { getStore } from '../store/Store';
import { FlowDefinition } from '../store/flow-definition';
import { fetchResults } from '../utils';
import { FLOW_SPEC_VERSION } from '../store/AppState';
import {
labelsFor,
normalizeChanges,
RevisionChanges,
summarizeChanges
} from './revision-summary';
const GROUP_WINDOW_MS = 15 * 60 * 1000;
const MAX_GROUP_LABELS = 3;
export interface Revision {
id: number;
user: {
id?: number;
email?: string;
username?: string;
first_name?: string;
last_name?: string;
name?: string;
};
created_on: string;
comment?: string;
changes?: RevisionChanges | null;
}
export class RevisionsWindow extends RapidElement {
static get styles() {
return css`
:host {
display: contents;
}
`;
}
@property({ type: String })
flow = '';
@property({ type: Boolean })
hidden = true;
@property({ type: Boolean })
saving = false;
@state()
private revisions: Revision[] = [];
@state()
private viewingRevision: Revision | null = null;
@state()
private isLoading = false;
private preRevertState: {
definition: FlowDefinition;
dirtyDate: Date | null;
} | null = null;
private browseLanguageCode: string | null = null;
private fetchRequestId = 0;
public get isViewingRevision(): boolean {
return this.viewingRevision !== null;
}
public refresh(): void {
if (!this.hidden) {
this.fetchRevisions();
}
}
protected updated(changes: PropertyValues): void {
super.updated(changes);
if (
changes.has('hidden') &&
!this.hidden &&
changes.get('hidden') === true
) {
this.fetchRevisions();
}
}
public close(): void {
this.resetScroll();
if (this.viewingRevision) {
this.cancelRevisionView();
}
this.fireCustomEvent(CustomEventType.RevisionsClosed);
}
public render(): TemplateResult {
return html`
<temba-floating-window
id="revisions-window"
name="revisions"
header="Revisions"
icon="revisions"
.width=${340}
.maxHeight=${500}
.top=${120}
color="rgb(142, 94, 167)"
.saving=${this.saving}
.hidden=${this.hidden}
@temba-dialog-hidden=${() => this.close()}
>
<div class="localization-window-content">
<div
class="revisions-list"
style="display:flex; flex-direction:column; gap:8px; overflow-y:auto; padding-bottom:10px;"
>
${this.isLoading && !this.revisions.length
? html`<temba-loading></temba-loading>`
: this.revisions.map((rev, index) => {
const isCurrent = index === 0;
const isSelected = this.viewingRevision?.id === rev.id;
const summary = summarizeChanges(rev.changes);
return html`
<div
class="revision-item ${isSelected
? 'selected'
: ''} ${isCurrent ? 'current' : ''}"
style="padding:8px; border-radius:4px; cursor:pointer; background:${isSelected
? '#f0f6ff'
: '#f9fafb'}; border:1px solid ${isSelected
? '#a4cafe'
: '#e5e7eb'}; transition: all 0.2s ease;"
@click=${() => this.handleRevisionClick(rev)}
>
${summary
? html`<div
class="revision-summary"
style="font-size:13px; color:#111827; line-height:1.3;"
>
${summary}
</div>`
: ''}
<div
class="revision-meta"
style="display:flex; justify-content:space-between; align-items:center; gap:8px; min-height:20px; font-size:11px; color:#6b7280; margin-top:${summary
? '2px'
: '0'};"
>
<div style="flex:1; min-width:0;">
<temba-date
value=${rev.created_on}
display="duration"
></temba-date>
· ${this.renderUser(rev.user)}
</div>
${isCurrent
? html`<div
class="current-label"
style="font-size:10px; font-weight:600; text-transform:uppercase; color:#6b7280; background:#e5e7eb; padding:2px 6px; border-radius:10px; letter-spacing:0.5px; flex-shrink:0;"
>
Current
</div>`
: isSelected
? html`<button
class="revert-button"
style="font-size:10px; font-weight:600; text-transform:uppercase; color:#1e3a8a; background:#a4cafe; padding:2px 6px; border-radius:10px; letter-spacing:0.5px; border:none; cursor:pointer; flex-shrink:0;"
@click=${(e: Event) => {
e.stopPropagation();
this.handleRevertClick();
}}
>
Revert
</button>`
: html``}
</div>
${rev.comment
? html`<div
style="font-size:12px; color:#4b5563; margin-top:4px;"
>
${rev.comment}
</div>`
: ''}
</div>
`;
})}
</div>
</div>
</temba-floating-window>
`;
}
// --- Private ---
private renderUser(user: Revision['user']): TemplateResult | string {
if (user?.email === 'system') {
return html`<em>System update</em>`;
}
return user?.name || user?.username || '';
}
private async fetchRevisions() {
const requestId = ++this.fetchRequestId;
this.isLoading = true;
try {
const results = await fetchResults(
`/flow/revisions/${this.flow}/?version=${FLOW_SPEC_VERSION}`
);
if (requestId !== this.fetchRequestId) return;
this.revisions = this.collapseRevisions(results);
} catch (e) {
if (requestId !== this.fetchRequestId) return;
console.error('Error fetching revisions', e);
} finally {
if (requestId === this.fetchRequestId) {
this.isLoading = false;
}
}
}
// Lump revisions made in a continuous editing session (within 15 minutes
// of each other, by the same author) onto their most recent member,
// merging the tag sets so the summary covers everything that happened in
// the window. The merged revision is capped at three distinct displayed
// labels — once a fourth would be introduced we break out into a new row.
private collapseRevisions(revisions: Revision[]): Revision[] {
// Normalize at the boundary so the rest of the logic reasons about real
// edits only. After this step, `changes === null` is the single signal
// for "no-op" — used both for the author-barrier bypass and for keeping
// housekeeping tags out of the merged tag set and label cap.
const cleaned = revisions.map((r) => ({
...r,
changes: normalizeChanges(r.changes)
}));
// The API returns newest-first today; sort defensively so the head/window
// logic stays correct if that ever changes.
const sorted = [...cleaned].sort(
(a, b) =>
new Date(b.created_on).getTime() - new Date(a.created_on).getTime()
);
const result: Revision[] = [];
let group: Revision[] = [];
let groupLabels = new Set<string>();
let groupHasRealChange = false;
const flush = () => {
if (group.length === 0) return;
const head = group[0];
// Pick the user from the most recent real-change revision in the
// group. No-op authors (typically the system, doing spec bumps)
// shouldn't appear as the editor when a real user's edit was
// absorbed into the row — that would mislabel the change as
// "System update" even though a real person did the work. Fall back
// to the head if every revision was a no-op.
const realChange = group.find((r) => r.changes);
const displayUser = realChange?.user ?? head.user;
const tagSet = new Set<string>();
let anyKnown = false;
for (const r of group) {
if (r.changes) {
anyKnown = true;
for (const tag of r.changes.tags || []) tagSet.add(tag);
}
}
result.push({
...head,
user: displayUser,
changes: anyKnown ? { tags: Array.from(tagSet) } : null
});
group = [];
groupLabels = new Set();
groupHasRealChange = false;
};
for (const rev of sorted) {
if (group.length === 0) {
group.push(rev);
groupLabels = labelsFor(rev.changes);
groupHasRealChange = !!rev.changes;
continue;
}
const head = group[0];
const headTime = new Date(head.created_on).getTime();
const revTime = new Date(rev.created_on).getTime();
// Compare on whichever identifier the server provides — real data
// arrives with `email`, while test fixtures use `username`. Falling
// back through the chain keeps both shapes working.
const headId = head.user?.email ?? head.user?.username;
const revId = rev.user?.email ?? rev.user?.username;
// Two conditions bypass the time/author barriers:
// 1. The incoming rev is itself a no-op — it carries no editorial
// intent and should disappear into whichever group it neighbors.
// 2. The group hasn't accumulated a real change yet — we never want
// to surface a row showing "nothing changed", so a no-op-only
// chain reaches forward to absorb the first real edit even if
// that edit is far away in time or by a different author.
const isNoOp = !rev.changes;
const bypassBarriers = isNoOp || !groupHasRealChange;
const withinWindow =
bypassBarriers || headTime - revTime < GROUP_WINDOW_MS;
const sameAuthor = bypassBarriers || headId === revId;
const prospective = new Set([...groupLabels, ...labelsFor(rev.changes)]);
// The label cap is meaningful only when adding a real change to a
// group that already has one. A no-op contributes zero labels by
// construction, so it never trips the cap; and a no-op-only chain
// reaching forward to absorb its first real edit must ignore the cap
// too, or a sweeping edit (4+ label areas) would still strand the
// no-op group as an empty-summary row.
const fitsLabelCap =
isNoOp || !groupHasRealChange || prospective.size <= MAX_GROUP_LABELS;
if (withinWindow && sameAuthor && fitsLabelCap) {
group.push(rev);
groupLabels = prospective;
if (!isNoOp) groupHasRealChange = true;
} else {
flush();
group.push(rev);
groupLabels = labelsFor(rev.changes);
groupHasRealChange = !!rev.changes;
}
}
flush();
return result;
}
private async handleRevisionClick(revision: Revision) {
if (this.viewingRevision?.id === revision.id) {
return;
}
const store = getStore().getState();
if (!this.viewingRevision) {
this.preRevertState = {
definition: store.flowDefinition,
dirtyDate: store.dirtyDate
};
this.browseLanguageCode = store.languageCode;
}
this.viewingRevision = revision;
this.isLoading = true;
this.fireCustomEvent(CustomEventType.RevisionViewed, { revision });
try {
await store.fetchRevision(
`/flow/revisions/${this.flow}`,
revision.id.toString()
);
if (this.browseLanguageCode) {
store.setLanguageCode(this.browseLanguageCode);
}
} catch (e) {
console.error('Error fetching revision details', e);
this.cancelRevisionView();
} finally {
this.isLoading = false;
}
}
private cancelRevisionView() {
const store = getStore().getState();
const preservedLanguageCode = this.browseLanguageCode || store.languageCode;
if (this.preRevertState) {
const currentInfo = store.flowInfo;
store.setFlowContents({
definition: this.preRevertState.definition,
info: currentInfo
});
if (this.preRevertState.dirtyDate) {
store.setDirtyDate(this.preRevertState.dirtyDate);
}
if (preservedLanguageCode) {
store.setLanguageCode(preservedLanguageCode);
}
} else {
store.fetchRevision(`/flow/revisions/${this.flow}`).finally(() => {
if (preservedLanguageCode) {
store.setLanguageCode(preservedLanguageCode);
}
});
}
this.viewingRevision = null;
this.preRevertState = null;
this.browseLanguageCode = null;
this.fireCustomEvent(CustomEventType.RevisionCancelled);
}
private async handleRevertClick() {
if (!this.viewingRevision || !this.preRevertState) return;
const store = getStore().getState();
const preservedLanguageCode = this.browseLanguageCode || store.languageCode;
const definitionToSave = {
...store.flowDefinition,
revision: this.preRevertState.definition.revision
};
this.viewingRevision = null;
this.preRevertState = null;
this.browseLanguageCode = null;
this.fireCustomEvent(CustomEventType.RevisionReverted, {
definition: definitionToSave,
languageCode: preservedLanguageCode
});
}
private resetScroll() {
const win = this.shadowRoot?.querySelector('temba-floating-window');
const list = win?.shadowRoot?.querySelector('.body');
if (list) {
list.scrollTop = 0;
}
}
}
|