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 | 96x 131x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 209x 209x 209x 209x 209x 209x 209x 209x 209x 209x 209x 209x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 279x 279x 279x 96x 4x 4x 4x 4x 1x 96x 268x 268x 268x 3x 3x 268x 3x 268x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 268x 268x 268x 268x 22x 22x 22x 22x 22x 22x 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 2x 12x 22x 268x 96x 96x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 10x 10x 10x 10x 10x 11x 11x 11x 11x 11x 11x 96x 10x 10x 10x 18x 8x 8x 10x 10x 10x 8x 8x 8x 8x 8x 8x 10x 10x 16x 11x 11x 16x 11x 16x 16x 6x 10x 6x 16x 4x 4x 4x 4x 4x 4x 4x 4x 16x 2x 10x 10x 96x 96x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 96x 96x 96x 96x 96x 96x | 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 {
RevisionChange,
isSignificantChange,
summarizeChanges
} from './revision-summary';
const GROUP_WINDOW_MS = 15 * 60 * 1000;
export interface Revision {
id: number;
user: {
id: number;
username: string;
first_name: string;
last_name: string;
name?: string;
};
created_on: string;
comment?: string;
changes?: RevisionChange[] | 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>
ยท ${rev.user.name || rev.user.username}
</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 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;
}
}
}
private collapseRevisions(revisions: Revision[]): Revision[] {
const result: Revision[] = [];
let group: Revision[] = [];
const flush = () => {
if (group.length === 0) return;
const head = group[0];
const allChanges: RevisionChange[] = [];
for (const r of group) {
if (r.changes) allChanges.push(...r.changes);
}
result.push({ ...head, changes: allChanges });
group = [];
};
for (const rev of revisions) {
// Significant revisions and revisions with no recorded changes (legacy/opaque
// entries) always stand alone so the user can click in to inspect them.
const opaque = !rev.changes || rev.changes.length === 0;
if (opaque || isSignificantChange(rev.changes)) {
flush();
result.push(rev);
continue;
}
if (group.length === 0) {
group.push(rev);
continue;
}
const headTime = new Date(group[0].created_on).getTime();
const revTime = new Date(rev.created_on).getTime();
if (headTime - revTime < GROUP_WINDOW_MS) {
group.push(rev);
} else {
flush();
group.push(rev);
}
}
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;
}
}
}
|