All files / app/store view.ts

50.42% Statements 118/234
29.48% Branches 97/329
64.7% Functions 55/85
56.12% Lines 110/196

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 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487158x                   158x   158x                                 158x   279x 279x 279x 279x 279x 279x 279x 279x 279x 279x 279x 279x 279x 279x 279x 279x 279x 279x 279x     279x 279x   279x                   279x                           294x 294x 294x 294x 294x 294x 294x 294x 294x       1x 1x 1x                                     1x       2x                       11x       1x 1x                           42x                         1x                             42x 1x 1x       1x       72x       2x 2x       1x 1x       1x                 1x 1x       1x         40x 40x                                                                               40x       6x       6x 6x       6x                                     1x       18x                       1x       47x                     42x             40x 40x                             4x       1x               1x       1x       4x       42x     5x       2x               3x 3x 3x 3x       11x       26x         1x         30x 30x 30x 30x                         4x       49x 49x 4x 4x       42x 7x 5x       31x       1x       29x       35x     35x 35x               42x       1x       38x               6x                
import { isEqual, uniq } from 'lodash-es';
import { action, makeAutoObservable, observable } from 'mobx';
import { RouterStore } from 'mobx-angular';
import { Ext } from '../model/ext';
import { Plugin } from '../model/plugin';
import { Ref, RefSort } from '../model/ref';
import { TagSort } from '../model/tag';
import { Template } from '../model/template';
import { User } from '../model/user';
import { RootConfig } from '../mods/root';
import { getPageTitle } from '../util/format';
import { UrlFilter } from '../util/query';
import { hasPrefix, hasTag, isQuery, localTag, queryPrefix, top, topAnds } from '../util/tag';
import { AccountStore } from './account';
 
/**
 * ID for current view. Only includes pages that make queries.
 * For example, the alt refs and missing refs pages are not included since
 * they do not make queries.
 */
export type View =
  'home' | 'all' | 'local' |
  'tag' | 'tags' | 'query' |
  'inbox/all' | 'inbox/sent' | 'inbox/alarms' | 'inbox/dms' | 'inbox/modlist' | 'inbox/reports' | 'inbox/ref' |
  'ref/summary' | 'ref/comments' | 'ref/thread' | 'ref/responses' | 'ref/errors' | 'ref/sources' | 'ref/alts' | 'ref/versions' |
  'settings/user' | 'settings/plugin' | 'settings/template' | 'settings/ref';
 
export type Type = 'ref' | 'ext' | 'user' | 'plugin' | 'template';
 
export class ViewStore {
 
  floatingSidebar = true;
  sidebarExpanded = true;
  defaultPageSize = 24;
  defaultKanbanLoadSize = 8;
  defaultBlogPageSize = 5;
  defaultSort: RefSort[] | TagSort[] = ['published'];
  defaultSearchSort: RefSort[] | TagSort[] = ['rank'];
  defaultPageNumber = 0;
  ref?: Ref = {} as any;
  top?: Ref = {} as any;
  lastSelected?: Ref = {} as any;
  versions = 0;
  exts: Ext[] = [];
  extTemplates: Template[] = [];
  selectedUser?: User = {} as any;
  modChanges = new Map<string, boolean>();
  modUpdates = new Set<string>();
  inboxTabs: Plugin[] = [];
  settingsTabs: Plugin[] = [];
 
  constructor(
    public route: RouterStore,
    private account: AccountStore,
  ) {
    makeAutoObservable(this, {
      clear: action,
      setRef: action,
      preloadRef: action,
      setLastSelected: action,
      exts: observable.shallow,
      extTemplates: observable.shallow,
      inboxTabs: observable.shallow,
      settingsTabs: observable.shallow,
    });
    this.clear(); // Initial observables may not be null for MobX
  }
 
  setLastSelected(ref?: Ref) {
    this.lastSelected = ref;
  }
 
  clearLastSelected(url?: string) {
    if (!url || url === this.lastSelected?.url) {
      this.lastSelected = undefined;
    }
  }
 
  clear(defaultSort: RefSort[] | TagSort[] = ['published'], defaultSearchSort: RefSort[] | TagSort[] = ['rank'], defaultPageNumber = 0) {
    this.ref = undefined;
    this.top = undefined;
    this.versions = 0;
    this.exts = [];
    this.extTemplates = [];
    this.selectedUser = undefined;
    this.defaultSort = defaultSort;
    this.defaultSearchSort = defaultSearchSort;
    this.defaultPageNumber = defaultPageNumber;
  }
 
  clearRef(ref?: Ref) {
    Iif (this.ref && (!ref || ref.url !== this.ref?.url)) this.lastSelected = this.ref;
    this.ref = undefined;
    this.top = undefined;
  }
 
  setRef(ref?: Ref, top?: Ref) {
    this.clearRef(ref);
    this.ref = ref;
    this.top = top;
    this.exts = [];
    this.extTemplates = [];
    this.selectedUser = undefined;
  }
 
  preloadRef(ref: Ref, topRef?: Ref) {
    this.clearRef(ref);
    if (ref?.created) this.ref = ref;
    if (topRef || top(ref) !== this.top?.url) this.top = topRef;
  }
 
  get pageTitle() {
    return getPageTitle(this.ref, this.top);
  }
 
  get ext() {
    return this.exts[0];
  }
 
  get extTemplate() {
    return this.extTemplates.find(t => this.exts.find(x => hasPrefix(x.tag, t.tag)));
  }
 
  get config(): RootConfig | undefined {
    return this.viewExt?.config;
  }
 
  get url() {
    return this.route.routeSnapshot?.firstChild?.params['url'];
  }
 
  get summary() {
    const s = this.route.routeSnapshot?.firstChild;
    Eif (s?.url[0].path !== 'ref') return false;
    return !s.firstChild?.routeConfig?.path;
  }
 
  get alternateUrls() {
    const s = this.route.routeSnapshot?.firstChild;
    if (s?.url[0].path !== 'ref') return false;
    return s.firstChild?.routeConfig?.path === 'alts';
  }
 
  /**
   * Exts for all active Templates. If no Ext is found a default will be created.
   */
  get activeExts(): Ext[] {
    return uniq(this.activeTemplates
        .flatMap(t => {
          const exts = this.exts.filter(x => x.modifiedString && hasPrefix(x.tag, t.tag));
          if (exts.length) return exts;
          return [{ tag: t.tag, origin: t.origin, name: t.name, config: { ...t.defaults, tab: t?.config?.tab, view: t?.config?.view } }];
        })
        .filter(x => !!x));
  }
 
  /**
   * Exts for all global Templates. If no Ext is found a default will be created.
   */
  get globalExts(): Ext[] {
    return uniq(this.globalTemplates
        .flatMap(t => {
          if (this.exts.find(x => hasPrefix(x.tag, t.tag))) {
            // Already an active ext so ignore global
            return [];
          }
          return [{ tag: t.tag, origin: t.origin, name: t.name, config: { ...t.defaults, tab: t.config?.tab, view: t.config?.view } }];
        })
        .filter(x => !!x));
  }
 
  /**
   * Templates found in top ands of query or filters.
   */
  get activeTemplates(): Template[] {
    return uniq(this.queryTags
        .map(tag => this.extTemplates.find(t => hasPrefix(tag, t.tag))!)
        .filter(t => !!t));
  }
 
  get globalTemplates(): Template[] {
    return this.extTemplates.filter(t => t.config?.global);
  }
 
  isTemplate(template: string) {
    return hasPrefix(this.viewExt?.tag, template);
  }
 
  get tags(): boolean {
    const s = this.route.routeSnapshot?.firstChild;
    return s?.url[0].path === 'tags';
  }
 
  get settings() {
    const s = this.route.routeSnapshot?.firstChild;
    return s?.routeConfig?.path === 'settings';
  }
 
  get settingsTag() {
    Eif (!this.settings) return '';
    return this.childTag;
  }
 
  get settingsExt() {
    return this.settingsTabs.find(t => t.tag === this.settingsTag) as Ext;
  }
 
  get inbox() {
    const s = this.route.routeSnapshot?.firstChild;
    return s?.routeConfig?.path === 'inbox';
  }
 
  get inboxTag() {
    Eif (!this.inbox) return '';
    return this.childTag;
  }
 
  get current(): View | undefined {
    const s = this.route.routeSnapshot?.firstChild;
    switch (s?.url[0].path) {
      case 'home': return 'home';
      case 'tags': return 'tags';
      case 'tag':
        if (this.tag === '@*') return 'all';
        if (this.tag === '*') return 'local';
        if (isQuery(this.tag)) return 'query';
        return 'tag';
      case 'ref':
        switch (s.firstChild?.routeConfig?.path) {
          case '': return 'ref/summary';
          case 'comments': return 'ref/comments';
          case 'thread': return 'ref/thread';
          case 'responses': return 'ref/responses';
          case 'errors': return 'ref/errors';
          case 'sources': return 'ref/sources';
          case 'versions': return 'ref/versions';
          case 'alts': return 'ref/alts';
        }
        return undefined;
      case 'settings':
        switch (s.firstChild?.routeConfig?.path) {
          case 'user': return 'settings/user';
          case 'plugin': return 'settings/plugin';
          case 'template': return 'settings/template';
          case 'ref/:tag': return 'settings/ref';
        }
        return undefined;
      case 'inbox':
        switch (s.firstChild?.routeConfig?.path) {
          case 'all': return 'inbox/all';
          case 'sent': return 'inbox/sent';
          case 'alarms': return 'inbox/alarms';
          case 'dms': return 'inbox/dms';
          case 'modlist': return 'inbox/modlist';
          case 'reports': return 'inbox/reports';
          case 'ref/:tag': return 'inbox/ref';
        }
        return undefined;
    }
    return undefined;
  }
 
  get originFilter() {
    return this.filter?.some(f => f.startsWith('query/@') || f === 'query/*');
  }
 
  get showRemotesCheckbox() {
    Iif (this.originFilter) return false;
    return ['tags', 'settings/user', 'settings/plugin', 'settings/template', 'settings/ref', 'inbox/ref'].includes(this.current!);
  }
 
  get type(): Type | undefined {
    Eif (!this.current) return undefined;
    if (this.current === 'ref/summary') return undefined;
    if (this.current === 'tags') return 'ext';
    if (this.current.startsWith('ref/') ||
      this.current.startsWith('inbox/') ||
      this.current ==='home' ||
      this.current ==='all' ||
      this.current ==='local' ||
      this.current ==='tag' ||
      this.current ==='query' ) {
      return 'ref';
    }
    if (this.current.startsWith('settings/')) {
      return this.current.substring('settings/'.length) as Type;
    }
    return this.current as Type;
  }
 
  get forYou() {
    return !!this.route.routeSnapshot?.queryParams['forYou'];
  }
 
  get origin() {
    return this.route.routeSnapshot?.queryParams['origin'];
  }
 
  get depth() {
    return this.route.routeSnapshot?.queryParams['depth'];
  }
 
  get isTextPost() {
    return this.url?.startsWith('comment:');
  }
 
  get alarm(): boolean {
    return this.account.alarms.includes(this.tag);
  }
 
  get tag(): string {
    return this.route.routeSnapshot?.firstChild?.params['tag'] || '';
  }
 
  get childTag(): string {
    return this.route.routeSnapshot?.firstChild?.firstChild?.params['tag'] || '';
  }
 
  /**
   * The main tag associated with this view.
   */
  get viewTag(): string {
    return this.view || this.activeExts[0]?.tag || '';
  }
 
  /**
   * The main Ext associated with this view.
   */
  get viewExt() {
    Iif (this.list) return undefined;
    return this.viewTag && [...this.activeExts, ...this.globalExts].find(x => hasPrefix(x.tag, this.viewTag)) || this.exts[0];
  }
 
  get homeExt() {
    if (this.list) return this.ext;
    return {
      ...this.ext || {},
      config: {
        ...this.ext?.config || {},
        noFloatingSidebar: this.viewExt?.config?.noFloatingSidebar ?? this.ext?.config?.noFloatingSidebar,
      },
    };
  }
 
  get template(): string {
    return this.route.routeSnapshot?.firstChild?.params['template'] || '';
  }
 
  get localTemplate(): string {
    return localTag(this.template);
  }
 
  get userTemplate() {
    return hasPrefix(this.localTemplate, 'user');
  }
 
  get noTemplate(): boolean {
    return this.route.routeSnapshot?.queryParams['noTemplate'] !== undefined && this.route.routeSnapshot?.queryParams['noTemplate'] !== 'false';
  }
 
  get home(): boolean {
    return this.route.routeSnapshot?.queryParams['home'] !== undefined && this.route.routeSnapshot?.queryParams['home'] !== 'false';
  }
 
  get query() {
    return isQuery(this.tag) ? this.tag : '';
  }
 
  get queryTags() {
    return uniq([
        ...topAnds(this.tag).map(queryPrefix),
        ...this.queryFilters.map(queryPrefix),
    ].filter(t => t && !isQuery(t)));
  }
 
  get noQuery() {
    return isQuery(this.tag) ? '' : this.tag;
  }
 
  get localTag() {
    return localTag(this.tag);
  }
 
  get name() {
    Iif (this.tag === '@*') return $localize`All`;
    Iif (this.tag === '*') return $localize`Local`;
    Iif (isQuery(this.tag)) return $localize`Query`;
    return this.exts[0]?.name || this.viewExt?.name || (this.viewExt?.tag || this.tag).substring(this.tag.lastIndexOf('/') + 1);
  }
 
  get cols() {
    return parseInt(this.route.routeSnapshot?.queryParams['cols'] || this.viewExt?.config?.defaultCols || 0);
  }
 
  get viewExtSort() {
    Eif (!['tag', 'home'].includes(this.current!)) return undefined;
    return this.viewExt?.config?.defaultSort;
  }
 
  get viewExtFilter() {
    Eif (!['tag', 'home'].includes(this.current!)) return undefined;
    return this.viewExt?.config?.defaultFilter;
  }
 
  get sort() {
    const sort = this.route.routeSnapshot?.queryParams['sort'];
    Eif (!sort) {
      Iif (this.search && this.defaultSearchSort) return this.defaultSearchSort;
      return this.viewExtSort || this.defaultSort || [];
    }
    if (!Array.isArray(sort)) return [sort]
    return sort;
  }
 
  get isSorted() {
    if (this.sort.length > 1) return true;
    if (this.search && this.defaultSearchSort) return !isEqual(this.sort, this.defaultSearchSort);
    return !isEqual(this.sort, this.defaultSort);
  }
 
  get isVoteSorted() {
    return this.sort[0]?.startsWith('plugins->plugin/user/vote');
  }
 
  get filter(): UrlFilter[] {
    const filter = this.route.routeSnapshot?.queryParams['filter'];
    if (!filter) return [];
    Iif (!Array.isArray(filter)) return [filter]
    return filter;
  }
 
  get queryFilters(): string[] {
    return this.filter
      .filter(f => f.startsWith('query/'))
      .map(f => f.substring('query/'.length));
  }
 
  get search() {
    return this.route.routeSnapshot?.queryParams['search'];
  }
 
  get isSearch() {
    return !!this.search;
  }
 
  get pageNumber() {
    return this.route.routeSnapshot?.queryParams['pageNumber'] || this.defaultPageNumber;
  }
 
  get pageSize() {
    Iif (this.route.routeSnapshot?.queryParams['pageSize']) {
      return parseInt(this.route.routeSnapshot?.queryParams['pageSize']);
    }
    Iif (this.isTemplate('kanban')) return this.account.config.kanbanLoadSize || this.defaultKanbanLoadSize;
    return parseInt(this.route.routeSnapshot?.queryParams['pageSize'] ?? (this.isTemplate('blog') ? this.defaultBlogPageSize : this.defaultPageSize));
  }
 
  get published() {
    return this.route.routeSnapshot?.queryParams['published'];
  }
 
  get view(): string {
    return this.route.routeSnapshot?.queryParams['view'];
  }
 
  get noView() {
    return !this.view;
  }
 
  get list() {
    return this.view === 'list';
  }
 
  get graph() {
    return this.view === 'graph';
  }
 
  get showRemotes() {
    Eif (!this.showRemotesCheckbox) return true;
    return this.route.routeSnapshot?.queryParams['showRemotes'] !== undefined && this.route.routeSnapshot?.queryParams['showRemotes'] !== 'false';
  }
 
  get repost() {
    return this.ref?.sources?.[0] && hasTag('plugin/repost', this.ref);
  }
}