Ben Rohlfs | 65c2f2b | 2022-09-12 22:35:26 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @license |
| 3 | * Copyright 2022 Google LLC |
| 4 | * SPDX-License-Identifier: Apache-2.0 |
| 5 | */ |
| 6 | import { |
| 7 | NumericChangeId, |
| 8 | RepoName, |
| 9 | RevisionPatchSetNum, |
| 10 | BasePatchSetNum, |
Ben Rohlfs | 38ede01 | 2022-09-21 16:02:46 +0200 | [diff] [blame] | 11 | ChangeInfo, |
Ben Rohlfs | a1d2c0c | 2022-09-29 17:03:26 +0200 | [diff] [blame] | 12 | PatchSetNumber, |
Ben Rohlfs | b91a6a4 | 2023-01-13 09:29:31 +0100 | [diff] [blame] | 13 | EDIT, |
Ben Rohlfs | 65c2f2b | 2022-09-12 22:35:26 +0200 | [diff] [blame] | 14 | } from '../../api/rest-api'; |
Ben Rohlfs | 7cd327d | 2022-09-29 17:37:28 +0200 | [diff] [blame] | 15 | import {Tab} from '../../constants/constants'; |
Ben Rohlfs | 65c2f2b | 2022-09-12 22:35:26 +0200 | [diff] [blame] | 16 | import {GerritView} from '../../services/router/router-model'; |
| 17 | import {UrlEncodedCommentId} from '../../types/common'; |
Ben Rohlfs | 20fe8e8 | 2022-10-14 11:13:10 +0200 | [diff] [blame] | 18 | import {toggleSet} from '../../utils/common-util'; |
Ben Rohlfs | 7b22a29 | 2022-09-29 12:52:01 +0200 | [diff] [blame] | 19 | import {select} from '../../utils/observable-util'; |
Ben Rohlfs | 7f8ba52 | 2022-09-16 11:02:01 +0200 | [diff] [blame] | 20 | import { |
| 21 | encodeURL, |
| 22 | getBaseUrl, |
| 23 | getPatchRangeExpression, |
| 24 | } from '../../utils/url-util'; |
Ben Rohlfs | 65c2f2b | 2022-09-12 22:35:26 +0200 | [diff] [blame] | 25 | import {AttemptChoice} from '../checks/checks-util'; |
Ben Rohlfs | 2586e57 | 2022-09-16 12:55:37 +0200 | [diff] [blame] | 26 | import {define} from '../dependency'; |
Ben Rohlfs | 65c2f2b | 2022-09-12 22:35:26 +0200 | [diff] [blame] | 27 | import {Model} from '../model'; |
| 28 | import {ViewState} from './base'; |
Ben Rohlfs | ecc6799 | 2022-12-16 10:10:16 +0100 | [diff] [blame] | 29 | |
| 30 | export enum ChangeChildView { |
| 31 | OVERVIEW = 'OVERVIEW', |
| 32 | DIFF = 'DIFF', |
| 33 | EDIT = 'EDIT', |
| 34 | } |
Ben Rohlfs | 65c2f2b | 2022-09-12 22:35:26 +0200 | [diff] [blame] | 35 | |
| 36 | export interface ChangeViewState extends ViewState { |
| 37 | view: GerritView.CHANGE; |
Ben Rohlfs | ecc6799 | 2022-12-16 10:10:16 +0100 | [diff] [blame] | 38 | childView: ChangeChildView; |
Ben Rohlfs | a1d2c0c | 2022-09-29 17:03:26 +0200 | [diff] [blame] | 39 | |
Ben Rohlfs | 38ede01 | 2022-09-21 16:02:46 +0200 | [diff] [blame] | 40 | changeNum: NumericChangeId; |
Ben Rohlfs | bfc688b | 2022-10-21 12:38:37 +0200 | [diff] [blame] | 41 | repo: RepoName; |
Ben Rohlfs | 65c2f2b | 2022-09-12 22:35:26 +0200 | [diff] [blame] | 42 | patchNum?: RevisionPatchSetNum; |
| 43 | basePatchNum?: BasePatchSetNum; |
Ben Rohlfs | 196dc72 | 2022-12-20 18:01:33 +0100 | [diff] [blame] | 44 | /** Refers to comment on COMMENTS tab in OVERVIEW. */ |
Ben Rohlfs | 65c2f2b | 2022-09-12 22:35:26 +0200 | [diff] [blame] | 45 | commentId?: UrlEncodedCommentId; |
Ben Rohlfs | ecc6799 | 2022-12-16 10:10:16 +0100 | [diff] [blame] | 46 | |
| 47 | // TODO: Move properties that only apply to OVERVIEW into a submessage. |
| 48 | |
| 49 | edit?: boolean; |
Ben Rohlfs | 7cd327d | 2022-09-29 17:37:28 +0200 | [diff] [blame] | 50 | /** This can be a string only for plugin provided tabs. */ |
| 51 | tab?: Tab | string; |
Ben Rohlfs | a1d2c0c | 2022-09-29 17:03:26 +0200 | [diff] [blame] | 52 | |
Ben Rohlfs | ecc6799 | 2022-12-16 10:10:16 +0100 | [diff] [blame] | 53 | // TODO: Move properties that only apply to CHECKS tab into a submessage. |
| 54 | |
Ben Rohlfs | a1d2c0c | 2022-09-29 17:03:26 +0200 | [diff] [blame] | 55 | /** Checks related view state */ |
| 56 | |
| 57 | /** selected patchset for check runs (undefined=latest) */ |
| 58 | checksPatchset?: PatchSetNumber; |
Ben Rohlfs | 65c2f2b | 2022-09-12 22:35:26 +0200 | [diff] [blame] | 59 | /** regular expression for filtering check runs */ |
| 60 | filter?: string; |
Ben Rohlfs | a1d2c0c | 2022-09-29 17:03:26 +0200 | [diff] [blame] | 61 | /** selected attempt for check runs (undefined=latest) */ |
Ben Rohlfs | 65c2f2b | 2022-09-12 22:35:26 +0200 | [diff] [blame] | 62 | attempt?: AttemptChoice; |
Ben Rohlfs | 6485eb8 | 2022-09-30 11:26:08 +0200 | [diff] [blame] | 63 | /** selected check runs identified by `checkName` */ |
Ben Rohlfs | 20fe8e8 | 2022-10-14 11:13:10 +0200 | [diff] [blame] | 64 | checksRunsSelected?: Set<string>; |
Ben Rohlfs | 209f141 | 2022-09-30 12:25:46 +0200 | [diff] [blame] | 65 | /** regular expression for filtering check results */ |
| 66 | checksResultsFilter?: string; |
Ben Rohlfs | 804e724 | 2022-09-15 16:16:12 +0200 | [diff] [blame] | 67 | |
Ben Rohlfs | a1d2c0c | 2022-09-29 17:03:26 +0200 | [diff] [blame] | 68 | /** State properties that trigger one-time actions */ |
| 69 | |
| 70 | /** for scrolling a Change Log message into view in gr-change-view */ |
Ben Rohlfs | 804e724 | 2022-09-15 16:16:12 +0200 | [diff] [blame] | 71 | messageHash?: string; |
Ben Rohlfs | 0d12586 | 2023-04-20 22:40:04 +0200 | [diff] [blame] | 72 | /** |
| 73 | * For logging where the user came from. This is handled by the router, so |
| 74 | * this is not inspected by the model. |
| 75 | */ |
Ben Rohlfs | 804e724 | 2022-09-15 16:16:12 +0200 | [diff] [blame] | 76 | usp?: string; |
Ben Rohlfs | 0d12586 | 2023-04-20 22:40:04 +0200 | [diff] [blame] | 77 | /** |
| 78 | * Triggers all change related data to be reloaded. This is implemented by |
| 79 | * intercepting change view state updates and `forceReload` causing the view |
| 80 | * state to be wiped clean as `undefined` in an intermediate update. |
| 81 | */ |
Ben Rohlfs | a1d2c0c | 2022-09-29 17:03:26 +0200 | [diff] [blame] | 82 | forceReload?: boolean; |
| 83 | /** triggers opening the reply dialog */ |
| 84 | openReplyDialog?: boolean; |
Ben Rohlfs | ecc6799 | 2022-12-16 10:10:16 +0100 | [diff] [blame] | 85 | |
| 86 | /** These properties apply to the DIFF child view only. */ |
| 87 | diffView?: { |
| 88 | path?: string; |
Ben Rohlfs | c368435 | 2023-02-17 16:01:39 +0100 | [diff] [blame] | 89 | // TODO: Use LineNumber as a type, i.e. accept FILE and LOST. |
Ben Rohlfs | ecc6799 | 2022-12-16 10:10:16 +0100 | [diff] [blame] | 90 | lineNum?: number; |
| 91 | leftSide?: boolean; |
Ben Rohlfs | ecc6799 | 2022-12-16 10:10:16 +0100 | [diff] [blame] | 92 | }; |
| 93 | |
| 94 | /** These properties apply to the EDIT child view only. */ |
| 95 | editView?: { |
| 96 | path?: string; |
| 97 | lineNum?: number; |
| 98 | }; |
Ben Rohlfs | 65c2f2b | 2022-09-12 22:35:26 +0200 | [diff] [blame] | 99 | } |
| 100 | |
Ben Rohlfs | 38ede01 | 2022-09-21 16:02:46 +0200 | [diff] [blame] | 101 | /** |
| 102 | * This is a convenience type such that you can pass a `ChangeInfo` object |
| 103 | * as the `change` property instead of having to set both the `changeNum` and |
| 104 | * `project` properties explicitly. |
| 105 | */ |
| 106 | export type CreateChangeUrlObject = Omit< |
| 107 | ChangeViewState, |
Ben Rohlfs | ecc6799 | 2022-12-16 10:10:16 +0100 | [diff] [blame] | 108 | 'view' | 'childView' | 'changeNum' | 'repo' |
Ben Rohlfs | 38ede01 | 2022-09-21 16:02:46 +0200 | [diff] [blame] | 109 | > & { |
| 110 | change: Pick<ChangeInfo, '_number' | 'project'>; |
| 111 | }; |
| 112 | |
| 113 | export function isCreateChangeUrlObject( |
| 114 | state: CreateChangeUrlObject | Omit<ChangeViewState, 'view'> |
| 115 | ): state is CreateChangeUrlObject { |
| 116 | return !!(state as CreateChangeUrlObject).change; |
| 117 | } |
| 118 | |
| 119 | export function objToState( |
Ben Rohlfs | ecc6799 | 2022-12-16 10:10:16 +0100 | [diff] [blame] | 120 | obj: |
| 121 | | (CreateChangeUrlObject & {childView: ChangeChildView}) |
| 122 | | Omit<ChangeViewState, 'view'> |
Ben Rohlfs | 38ede01 | 2022-09-21 16:02:46 +0200 | [diff] [blame] | 123 | ): ChangeViewState { |
| 124 | if (isCreateChangeUrlObject(obj)) { |
| 125 | return { |
| 126 | ...obj, |
| 127 | view: GerritView.CHANGE, |
| 128 | changeNum: obj.change._number, |
Ben Rohlfs | bfc688b | 2022-10-21 12:38:37 +0200 | [diff] [blame] | 129 | repo: obj.change.project, |
Ben Rohlfs | 38ede01 | 2022-09-21 16:02:46 +0200 | [diff] [blame] | 130 | }; |
| 131 | } |
| 132 | return {...obj, view: GerritView.CHANGE}; |
| 133 | } |
| 134 | |
Ben Rohlfs | ecc6799 | 2022-12-16 10:10:16 +0100 | [diff] [blame] | 135 | export function createChangeViewUrl(state: ChangeViewState): string { |
| 136 | switch (state.childView) { |
| 137 | case ChangeChildView.OVERVIEW: |
| 138 | return createChangeUrl(state); |
| 139 | case ChangeChildView.DIFF: |
| 140 | return createDiffUrl(state); |
| 141 | case ChangeChildView.EDIT: |
| 142 | return createEditUrl(state); |
| 143 | } |
| 144 | } |
| 145 | |
Ben Rohlfs | 38ede01 | 2022-09-21 16:02:46 +0200 | [diff] [blame] | 146 | export function createChangeUrl( |
Ben Rohlfs | ecc6799 | 2022-12-16 10:10:16 +0100 | [diff] [blame] | 147 | obj: CreateChangeUrlObject | Omit<ChangeViewState, 'view' | 'childView'> |
Ben Rohlfs | 38ede01 | 2022-09-21 16:02:46 +0200 | [diff] [blame] | 148 | ) { |
Ben Rohlfs | ecc6799 | 2022-12-16 10:10:16 +0100 | [diff] [blame] | 149 | const state: ChangeViewState = objToState({ |
| 150 | ...obj, |
| 151 | childView: ChangeChildView.OVERVIEW, |
| 152 | }); |
Ben Rohlfs | b91a6a4 | 2023-01-13 09:29:31 +0100 | [diff] [blame] | 153 | |
| 154 | let suffix = ''; |
Ben Rohlfs | 804e724 | 2022-09-15 16:16:12 +0200 | [diff] [blame] | 155 | const queries = []; |
Ben Rohlfs | a1d2c0c | 2022-09-29 17:03:26 +0200 | [diff] [blame] | 156 | if (state.checksPatchset && state.checksPatchset > 0) { |
| 157 | queries.push(`checksPatchset=${state.checksPatchset}`); |
| 158 | } |
Ben Rohlfs | 7b22a29 | 2022-09-29 12:52:01 +0200 | [diff] [blame] | 159 | if (state.attempt) { |
| 160 | if (state.attempt !== 'latest') queries.push(`attempt=${state.attempt}`); |
| 161 | } |
| 162 | if (state.filter) { |
| 163 | queries.push(`filter=${state.filter}`); |
| 164 | } |
Ben Rohlfs | 209f141 | 2022-09-30 12:25:46 +0200 | [diff] [blame] | 165 | if (state.checksResultsFilter) { |
| 166 | queries.push(`checksResultsFilter=${state.checksResultsFilter}`); |
| 167 | } |
Ben Rohlfs | 20fe8e8 | 2022-10-14 11:13:10 +0200 | [diff] [blame] | 168 | if (state.checksRunsSelected && state.checksRunsSelected.size > 0) { |
Ben Rohlfs | 6485eb8 | 2022-09-30 11:26:08 +0200 | [diff] [blame] | 169 | queries.push(`checksRunsSelected=${[...state.checksRunsSelected].sort()}`); |
| 170 | } |
Ben Rohlfs | 7cd327d | 2022-09-29 17:37:28 +0200 | [diff] [blame] | 171 | if (state.tab && state.tab !== Tab.FILES) { |
| 172 | queries.push(`tab=${state.tab}`); |
| 173 | } |
Ben Rohlfs | 804e724 | 2022-09-15 16:16:12 +0200 | [diff] [blame] | 174 | if (state.forceReload) { |
| 175 | queries.push('forceReload=true'); |
| 176 | } |
| 177 | if (state.openReplyDialog) { |
| 178 | queries.push('openReplyDialog=true'); |
| 179 | } |
| 180 | if (state.usp) { |
| 181 | queries.push(`usp=${state.usp}`); |
| 182 | } |
| 183 | if (state.edit) { |
| 184 | suffix += ',edit'; |
| 185 | } |
| 186 | if (state.commentId) { |
Ben Rohlfs | b91a6a4 | 2023-01-13 09:29:31 +0100 | [diff] [blame] | 187 | suffix += `/comments/${state.commentId}`; |
Ben Rohlfs | 804e724 | 2022-09-15 16:16:12 +0200 | [diff] [blame] | 188 | } |
| 189 | if (queries.length > 0) { |
| 190 | suffix += '?' + queries.join('&'); |
| 191 | } |
| 192 | if (state.messageHash) { |
| 193 | suffix += state.messageHash; |
| 194 | } |
Ben Rohlfs | b91a6a4 | 2023-01-13 09:29:31 +0100 | [diff] [blame] | 195 | |
| 196 | return `${createChangeUrlCommon(state)}${suffix}`; |
| 197 | } |
| 198 | |
| 199 | export function createDiffUrl( |
| 200 | obj: CreateChangeUrlObject | Omit<ChangeViewState, 'view' | 'childView'> |
| 201 | ) { |
| 202 | const state: ChangeViewState = objToState({ |
| 203 | ...obj, |
| 204 | childView: ChangeChildView.DIFF, |
| 205 | }); |
| 206 | |
Ben Rohlfs | c02facb | 2023-01-27 18:46:02 +0100 | [diff] [blame] | 207 | const path = `/${encodeURL(state.diffView?.path ?? '')}`; |
Ben Rohlfs | b91a6a4 | 2023-01-13 09:29:31 +0100 | [diff] [blame] | 208 | |
| 209 | let suffix = ''; |
| 210 | // TODO: Move creating of comment URLs to a separate function. We are |
| 211 | // "abusing" the `commentId` property, which should only be used for pointing |
| 212 | // to comment in the COMMENTS tab of the OVERVIEW page. |
| 213 | if (state.commentId) { |
| 214 | suffix += `comment/${state.commentId}/`; |
Ben Rohlfs | 804e724 | 2022-09-15 16:16:12 +0200 | [diff] [blame] | 215 | } |
Ben Rohlfs | b91a6a4 | 2023-01-13 09:29:31 +0100 | [diff] [blame] | 216 | |
| 217 | if (state.diffView?.lineNum) { |
| 218 | suffix += '#'; |
| 219 | if (state.diffView?.leftSide) { |
| 220 | suffix += 'b'; |
| 221 | } |
| 222 | suffix += state.diffView.lineNum; |
| 223 | } |
| 224 | |
| 225 | return `${createChangeUrlCommon(state)}${path}${suffix}`; |
| 226 | } |
| 227 | |
| 228 | export function createEditUrl( |
| 229 | obj: Omit<ChangeViewState, 'view' | 'childView'> |
| 230 | ): string { |
| 231 | const state: ChangeViewState = objToState({ |
| 232 | ...obj, |
| 233 | childView: ChangeChildView.DIFF, |
| 234 | patchNum: obj.patchNum ?? EDIT, |
| 235 | }); |
| 236 | |
Ben Rohlfs | c02facb | 2023-01-27 18:46:02 +0100 | [diff] [blame] | 237 | const path = `/${encodeURL(state.editView?.path ?? '')}`; |
Ben Rohlfs | b91a6a4 | 2023-01-13 09:29:31 +0100 | [diff] [blame] | 238 | const line = state.editView?.lineNum; |
| 239 | const suffix = line ? `#${line}` : ''; |
| 240 | |
| 241 | return `${createChangeUrlCommon(state)}${path},edit${suffix}`; |
| 242 | } |
| 243 | |
| 244 | /** |
| 245 | * The shared part of creating a change URL between OVERVIEW, DIFF and EDIT |
| 246 | * child views. |
| 247 | */ |
| 248 | function createChangeUrlCommon(state: ChangeViewState) { |
| 249 | let range = getPatchRangeExpression(state); |
| 250 | if (range.length) range = '/' + range; |
| 251 | |
| 252 | let repo = ''; |
Ben Rohlfs | c02facb | 2023-01-27 18:46:02 +0100 | [diff] [blame] | 253 | if (state.repo) repo = `${encodeURL(state.repo)}/+/`; |
Ben Rohlfs | b91a6a4 | 2023-01-13 09:29:31 +0100 | [diff] [blame] | 254 | |
| 255 | return `${getBaseUrl()}/c/${repo}${state.changeNum}${range}`; |
Ben Rohlfs | 804e724 | 2022-09-15 16:16:12 +0200 | [diff] [blame] | 256 | } |
| 257 | |
Ben Rohlfs | 2586e57 | 2022-09-16 12:55:37 +0200 | [diff] [blame] | 258 | export const changeViewModelToken = |
| 259 | define<ChangeViewModel>('change-view-model'); |
| 260 | |
Ben Rohlfs | 10dfa93 | 2022-09-19 13:43:32 +0200 | [diff] [blame] | 261 | export class ChangeViewModel extends Model<ChangeViewState | undefined> { |
Ben Rohlfs | d1009f8 | 2022-12-16 12:19:13 +0100 | [diff] [blame] | 262 | public readonly changeNum$ = select(this.state$, state => state?.changeNum); |
| 263 | |
| 264 | public readonly patchNum$ = select(this.state$, state => state?.patchNum); |
| 265 | |
| 266 | public readonly basePatchNum$ = select( |
| 267 | this.state$, |
| 268 | state => state?.basePatchNum |
| 269 | ); |
| 270 | |
Ben Rohlfs | 3c74cee | 2023-02-23 16:32:43 +0100 | [diff] [blame] | 271 | public readonly openReplyDialog$ = select( |
| 272 | this.state$, |
| 273 | state => state?.openReplyDialog |
| 274 | ); |
| 275 | |
Ben Rohlfs | b5eba16 | 2023-03-03 12:28:08 +0100 | [diff] [blame] | 276 | public readonly commentId$ = select(this.state$, state => state?.commentId); |
| 277 | |
Ben Rohlfs | 05d785e | 2023-04-19 15:45:06 +0200 | [diff] [blame] | 278 | public readonly edit$ = select(this.state$, state => !!state?.edit); |
| 279 | |
Ben Rohlfs | 55617a3 | 2023-02-28 21:43:22 +0100 | [diff] [blame] | 280 | public readonly editPath$ = select( |
| 281 | this.state$, |
| 282 | state => state?.editView?.path |
| 283 | ); |
| 284 | |
Ben Rohlfs | e740884 | 2023-01-09 22:47:53 +0100 | [diff] [blame] | 285 | public readonly diffPath$ = select( |
| 286 | this.state$, |
| 287 | state => state?.diffView?.path |
| 288 | ); |
| 289 | |
| 290 | public readonly diffLine$ = select( |
| 291 | this.state$, |
| 292 | state => state?.diffView?.lineNum |
| 293 | ); |
| 294 | |
| 295 | public readonly diffLeftSide$ = select( |
| 296 | this.state$, |
| 297 | state => state?.diffView?.leftSide ?? false |
| 298 | ); |
| 299 | |
Ben Rohlfs | ecc6799 | 2022-12-16 10:10:16 +0100 | [diff] [blame] | 300 | public readonly childView$ = select(this.state$, state => state?.childView); |
| 301 | |
Ben Rohlfs | 583b434 | 2023-04-19 13:15:13 +0200 | [diff] [blame] | 302 | public readonly tab$ = select(this.state$, state => { |
Ben Rohlfs | 583b434 | 2023-04-19 13:15:13 +0200 | [diff] [blame] | 303 | if (state?.tab) return state.tab; |
Dhruv Srivastava | fd905c4 | 2023-05-30 13:10:07 +0200 | [diff] [blame] | 304 | if (state?.commentId) return Tab.COMMENT_THREADS; |
Ben Rohlfs | 583b434 | 2023-04-19 13:15:13 +0200 | [diff] [blame] | 305 | return Tab.FILES; |
| 306 | }); |
Ben Rohlfs | 7b22a29 | 2022-09-29 12:52:01 +0200 | [diff] [blame] | 307 | |
Ben Rohlfs | a1d2c0c | 2022-09-29 17:03:26 +0200 | [diff] [blame] | 308 | public readonly checksPatchset$ = select( |
| 309 | this.state$, |
| 310 | state => state?.checksPatchset |
| 311 | ); |
| 312 | |
Ben Rohlfs | 7b22a29 | 2022-09-29 12:52:01 +0200 | [diff] [blame] | 313 | public readonly attempt$ = select(this.state$, state => state?.attempt); |
| 314 | |
| 315 | public readonly filter$ = select(this.state$, state => state?.filter); |
| 316 | |
Ben Rohlfs | 209f141 | 2022-09-30 12:25:46 +0200 | [diff] [blame] | 317 | public readonly checksResultsFilter$ = select( |
| 318 | this.state$, |
| 319 | state => state?.checksResultsFilter ?? '' |
| 320 | ); |
| 321 | |
Ben Rohlfs | 6485eb8 | 2022-09-30 11:26:08 +0200 | [diff] [blame] | 322 | public readonly checksRunsSelected$ = select( |
| 323 | this.state$, |
Ben Rohlfs | 20fe8e8 | 2022-10-14 11:13:10 +0200 | [diff] [blame] | 324 | state => state?.checksRunsSelected ?? new Set<string>() |
Ben Rohlfs | 6485eb8 | 2022-09-30 11:26:08 +0200 | [diff] [blame] | 325 | ); |
| 326 | |
Ben Rohlfs | 65c2f2b | 2022-09-12 22:35:26 +0200 | [diff] [blame] | 327 | constructor() { |
Ben Rohlfs | 10dfa93 | 2022-09-19 13:43:32 +0200 | [diff] [blame] | 328 | super(undefined); |
Ben Rohlfs | 7b22a29 | 2022-09-29 12:52:01 +0200 | [diff] [blame] | 329 | this.state$.subscribe(s => { |
| 330 | if (s?.usp || s?.forceReload || s?.openReplyDialog) { |
| 331 | this.updateState({ |
| 332 | usp: undefined, |
| 333 | forceReload: undefined, |
| 334 | openReplyDialog: undefined, |
| 335 | }); |
| 336 | } |
| 337 | }); |
Ben Rohlfs | 0d12586 | 2023-04-20 22:40:04 +0200 | [diff] [blame] | 338 | document.addEventListener('reload', this.reload); |
| 339 | } |
| 340 | |
| 341 | override finalize(): void { |
| 342 | document.removeEventListener('reload', this.reload); |
| 343 | } |
| 344 | |
| 345 | /** |
| 346 | * Calling this is the same as firing the 'reload' event. This is also the |
| 347 | * same as adding `forceReload` parameter in the URL. See below. |
| 348 | */ |
| 349 | reload = () => { |
| 350 | const state = this.getState(); |
| 351 | if (state !== undefined) this.forceLoad(state); |
| 352 | }; |
| 353 | |
| 354 | /** |
| 355 | * This is the destination of where the `reload()` method, the `reload` event |
| 356 | * and the `forceReload` URL parameter all end up. |
| 357 | */ |
| 358 | private forceLoad(state: ChangeViewState) { |
| 359 | this.setState(undefined); |
| 360 | // We have to do this in a timeout, because we need the `undefined` value to |
| 361 | // be processed by all observers first and thus have the "reset" completed. |
| 362 | setTimeout(() => this.setState({...state, forceReload: undefined})); |
| 363 | } |
| 364 | |
| 365 | override setState(state: ChangeViewState | undefined): void { |
| 366 | if (state?.forceReload) { |
| 367 | this.forceLoad(state); |
| 368 | } else { |
| 369 | super.setState(state); |
| 370 | } |
Ben Rohlfs | 65c2f2b | 2022-09-12 22:35:26 +0200 | [diff] [blame] | 371 | } |
Ben Rohlfs | 6485eb8 | 2022-09-30 11:26:08 +0200 | [diff] [blame] | 372 | |
| 373 | toggleSelectedCheckRun(checkName: string) { |
Ben Rohlfs | 20fe8e8 | 2022-10-14 11:13:10 +0200 | [diff] [blame] | 374 | const current = this.getState()?.checksRunsSelected ?? new Set(); |
| 375 | const next = new Set(current); |
| 376 | toggleSet(next, checkName); |
| 377 | this.updateState({checksRunsSelected: next}); |
Ben Rohlfs | 6485eb8 | 2022-09-30 11:26:08 +0200 | [diff] [blame] | 378 | } |
Ben Rohlfs | 65c2f2b | 2022-09-12 22:35:26 +0200 | [diff] [blame] | 379 | } |