summaryrefslogtreecommitdiffstats
path: root/comm/mail/components/accountcreation/views/start.mjs
blob: 66487cc92870b8c56ec796a8779a309e236d50dd (plain)
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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/* global gSync */

const { AppConstants } = ChromeUtils.importESModule(
  "resource://gre/modules/AppConstants.sys.mjs"
);
const { MailServices } = ChromeUtils.import(
  "resource:///modules/MailServices.jsm"
);
const { UIState } = ChromeUtils.importESModule(
  "resource://services-sync/UIState.sys.mjs"
);

class AccountHubStart extends HTMLElement {
  #accounts = [
    {
      id: "email",
      l10n: "account-hub-email-setup-button",
      type: "MAIL",
    },
    {
      id: "calendar",
      l10n: "account-hub-calendar-setup-button",
      type: "CALENDAR",
    },
    {
      id: "addressBook",
      l10n: "account-hub-address-book-setup-button",
      type: "ADDRESS_BOOK",
    },
    {
      id: "chat",
      l10n: "account-hub-chat-setup-button",
      type: "CHAT",
    },
    {
      id: "feed",
      l10n: "account-hub-feed-setup-button",
      type: "FEED",
    },
    {
      id: "newsgroup",
      l10n: "account-hub-newsgroup-setup-button",
      type: "NNTP",
    },
    // TODO: Import/Export of profiles is kinda broken so we don't want to
    // expose it so much for now.
    // {
    //   id: "import",
    //   l10n: "account-hub-import-setup-button",
    //   type: "IMPORT",
    // },
  ];

  connectedCallback() {
    if (this.hasConnected) {
      return;
    }
    this.hasConnected = true;

    this.classList.add("account-hub-view");

    let template = document.getElementById("accountHubStart");
    this.appendChild(template.content.cloneNode(true));

    this.initUI();

    this.setupAccountFlows();
  }

  /**
   * Update the UI to reflect reality whenever this view is triggered.
   */
  initUI() {
    const hasAccounts = MailServices.accounts.accounts.length;
    this.querySelector("#welcomeHeader").hidden = hasAccounts;
    this.querySelector("#defaultHeader").hidden = !hasAccounts;

    if (AppConstants.NIGHTLY_BUILD) {
      this.updateFxAButton();
    }

    // Hide the release notes link for nightly builds since we don't have any.
    if (AppConstants.NIGHTLY_BUILD) {
      this.querySelector("#hubReleaseNotes").closest("li").hidden = true;
      return;
    }

    if (
      Services.prefs.getPrefType("app.releaseNotesURL") !=
      Services.prefs.PREF_INVALID
    ) {
      let relNotesURL = Services.urlFormatter.formatURLPref(
        "app.releaseNotesURL"
      );
      if (relNotesURL != "about:blank") {
        this.querySelector("#hubReleaseNotes").href = relNotesURL;
        return;
      }
      // Hide the release notes link if we don't have a URL to add.
      this.querySelector("#hubReleaseNotes").closest("li").hidden = true;
    }
  }

  /**
   * Populate the main container fo the start view with all the available
   * account creation flows.
   */
  setupAccountFlows() {
    const fragment = new DocumentFragment();
    for (const account of this.#accounts) {
      const button = document.createElement("button");
      button.id = `${account.id}Button`;
      button.classList.add("button", "button-account");
      document.l10n.setAttributes(button, account.l10n);
      button.addEventListener("click", () => {
        this.dispatchEvent(
          new CustomEvent("open-view", {
            bubbles: true,
            composed: true,
            detail: {
              type: account.type,
            },
          })
        );
      });
      fragment.append(button);
    }
    this.querySelector(".hub-body-grid").replaceChildren(fragment);

    if (AppConstants.NIGHTLY_BUILD) {
      this.querySelector("#hubSyncButton").addEventListener("click", () => {
        // FIXME: Open this in a dialog or browser inside the modal, or find a
        // way to close the account hub without an account and open it again in
        // case the FxA login fails to set up accounts.
        gSync.initFxA();
      });
    }
  }

  /**
   * Set up the Firefox Sync button.
   */
  updateFxAButton() {
    const state = UIState.get();
    this.querySelector("#hubSyncButton").hidden =
      state.status == UIState.STATUS_SIGNED_IN;
  }

  /**
   * The start view doesn't have any abortable operation that needs to be
   * checked, so we always return true.
   *
   * @returns {boolean} - Always true.
   */
  reset() {
    return true;
  }
}
customElements.define("account-hub-start", AccountHubStart);