aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/frontend/src/theme/ThemeStore.ts
blob: 12449b9428951de41c030ac717dc6eec9ea94e90 (plain) (blame)
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
/*
 * SPDX-FileCopyrightText: 2021-2023 The Refinery Authors <https://refinery.tools/>
 *
 * SPDX-License-Identifier: EPL-2.0
 */

import { makeAutoObservable } from 'mobx';

export enum ThemePreference {
  System,
  PreferLight,
  PreferDark,
}

export type SelectedPane = 'code' | 'graph' | 'table';

export default class ThemeStore {
  preference = ThemePreference.System;

  systemDarkMode: boolean;

  showCode = true;

  showGraph = true;

  showTable = false;

  constructor() {
    const mediaQuery = window.matchMedia('(prefers-color-scheme: dark)');
    this.systemDarkMode = mediaQuery.matches;
    mediaQuery.addEventListener('change', (event) => {
      this.systemDarkMode = event.matches;
    });
    makeAutoObservable(this);
  }

  get darkMode(): boolean {
    switch (this.preference) {
      case ThemePreference.PreferLight:
        return false;
      case ThemePreference.PreferDark:
        return true;
      default:
        return this.systemDarkMode;
    }
  }

  toggleDarkMode(): void {
    if (this.darkMode) {
      this.preference = this.systemDarkMode
        ? ThemePreference.PreferLight
        : ThemePreference.System;
    } else {
      this.preference = this.systemDarkMode
        ? ThemePreference.System
        : ThemePreference.PreferDark;
    }
  }

  toggleCode(): void {
    if (!this.showGraph && !this.showTable) {
      return;
    }
    this.showCode = !this.showCode;
  }

  toggleGraph(): void {
    if (!this.showCode && !this.showTable) {
      return;
    }
    this.showGraph = !this.showGraph;
  }

  toggleTable(): void {
    if (!this.showCode && !this.showGraph) {
      return;
    }
    this.showTable = !this.showTable;
  }

  get selectedPane(): SelectedPane {
    if (this.showCode) {
      return 'code';
    }
    if (this.showGraph) {
      return 'graph';
    }
    if (this.showTable) {
      return 'table';
    }
    return 'code';
  }

  setSelectedPane(pane: SelectedPane, keepCode = true): void {
    this.showCode = pane === 'code' || (keepCode && this.showCode);
    this.showGraph = pane === 'graph';
    this.showTable = pane === 'table';
  }
}