@salesforcedevs/docs-components 0.54.0 → 0.54.7-alpha

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. package/lwc.config.json +6 -2
  2. package/package.json +12 -4
  3. package/src/modules/README.md +41 -0
  4. package/src/modules/doc/amfReference/amfReference.css +5 -0
  5. package/src/modules/doc/amfReference/amfReference.html +39 -0
  6. package/src/modules/doc/amfReference/amfReference.ts +813 -0
  7. package/src/modules/doc/amfReference/route-meta.ts +22 -0
  8. package/src/modules/doc/amfReference/types.ts +93 -0
  9. package/src/modules/doc/amfReference/utils.ts +669 -0
  10. package/src/modules/doc/amfTopic/amfTopic.css +1 -0
  11. package/src/modules/doc/amfTopic/amfTopic.html +3 -0
  12. package/src/modules/doc/amfTopic/amfTopic.ts +94 -0
  13. package/src/modules/doc/amfTopic/types.ts +54 -0
  14. package/src/modules/doc/amfTopic/utils.ts +130 -0
  15. package/src/modules/doc/breadcrumbItem/breadcrumbItem.css +2 -2
  16. package/src/modules/doc/breadcrumbs/breadcrumbs.css +2 -2
  17. package/src/modules/doc/breadcrumbs/breadcrumbs.ts +3 -3
  18. package/src/modules/doc/content/content.css +4 -4
  19. package/src/modules/doc/content/content.ts +1 -1
  20. package/src/modules/doc/contentCallout/contentCallout.css +4 -4
  21. package/src/modules/doc/contentLayout/contentLayout.css +100 -0
  22. package/src/modules/doc/contentLayout/contentLayout.html +55 -0
  23. package/src/modules/doc/contentLayout/contentLayout.ts +242 -0
  24. package/src/modules/doc/header/header.css +1 -1
  25. package/src/modules/doc/header/header.ts +5 -5
  26. package/src/modules/doc/headingAnchor/headingAnchor.css +1 -1
  27. package/src/modules/doc/headingContent/headingContent.css +1 -1
  28. package/src/modules/doc/phase/phase.css +3 -3
  29. package/src/modules/doc/phase/phase.ts +1 -1
  30. package/src/modules/doc/xmlContent/types.ts +113 -0
  31. package/src/modules/doc/xmlContent/utils.ts +158 -0
  32. package/src/modules/doc/xmlContent/xmlContent.css +25 -0
  33. package/src/modules/doc/xmlContent/xmlContent.html +34 -0
  34. package/src/modules/doc/xmlContent/xmlContent.ts +568 -0
  35. package/src/modules/docBaseElements/lightningElementWithState/lightningElementWithState.ts +93 -0
  36. package/src/modules/docHelpers/amfStyle/amfStyle.css +390 -0
  37. package/src/modules/docHelpers/phaseContentLayout/phaseContentLayout.css +39 -0
  38. package/src/modules/{helpers → docHelpers}/status/status.css +0 -0
  39. package/src/modules/docUtils/SearchSyncer/SearchSyncer.ts +85 -0
  40. package/LICENSE +0 -12
@@ -0,0 +1,568 @@
1
+ import { api, track } from "lwc";
2
+ import { toJson } from "dxUtils/normalizers";
3
+ import { FetchContent } from "./utils";
4
+ import {
5
+ CoveoAdvancedQueryXMLConfig,
6
+ DocLanguage,
7
+ DocVersion,
8
+ TreeNode,
9
+ Header,
10
+ HistoryState,
11
+ PageReference
12
+ } from "./types";
13
+ import { SearchSyncer } from "docUtils/SearchSyncer";
14
+ import { LightningElementWithState } from "docBaseElements/lightningElementWithState";
15
+ import { Language } from "typings/custom";
16
+
17
+ // TODO: Imitating from actual implementation as doc-content use it like this. We should refactor it later.
18
+ const handleContentError = (error): void => console.log(error);
19
+
20
+ export default class DocXmlContent extends LightningElementWithState<{
21
+ isFetchingDocument: boolean;
22
+ isFetchingContent: boolean;
23
+ lastHighlightedSearch: string;
24
+ }> {
25
+ @api apiDomain = "https://developer.salesforce.com";
26
+ @api coveoOrganizationId!: string;
27
+ @api coveoPublicAccessToken!: string;
28
+
29
+ @api
30
+ get labels() {
31
+ return this._labels;
32
+ }
33
+
34
+ set labels(value) {
35
+ this._labels = toJson(value);
36
+ }
37
+
38
+ @api
39
+ get allLanguages(): Array<Language> {
40
+ return this._allLanguages;
41
+ }
42
+
43
+ set allLanguages(value: string) {
44
+ if (value) {
45
+ this._allLanguages = JSON.parse(value);
46
+ }
47
+ }
48
+
49
+ private _labels: Labels = null;
50
+ private availableLanguages: Array<DocLanguage> = [];
51
+ private availableVersions: Array<DocVersion> = [];
52
+ private contentProvider: FetchContent;
53
+ private docContent = "";
54
+ private language: DocLanguage = null;
55
+ private loaded = false;
56
+ private pdfUrl = "";
57
+ private tocMap = null;
58
+ private sidebarContent: Array<TreeNode> = null;
59
+ private version: DocVersion = null;
60
+ private docTitle = "";
61
+ private analyticsEvent = "custEv_ctaLinkClick";
62
+ private _pathName = "";
63
+ private _pageHeader?: Header;
64
+ private listenerAttached = false;
65
+ private searchSyncer = new SearchSyncer({
66
+ callbacks: {
67
+ onSearchChange: (nextSearchString: string): void => {
68
+ if (nextSearchString !== this.pageReference.search) {
69
+ this.updatePageReference({
70
+ ...this.pageReference,
71
+ search: nextSearchString
72
+ });
73
+ }
74
+ if (nextSearchString !== this.state.lastHighlightedSearch) {
75
+ this.updateHighlighting(
76
+ new URLSearchParams(nextSearchString).get("q") || ""
77
+ );
78
+ }
79
+ },
80
+ onUrlChange: (nextSearchString: string): void => {
81
+ if (nextSearchString !== this.pageReference.search) {
82
+ this.updatePageReference({
83
+ ...this.pageReference,
84
+ search: nextSearchString
85
+ });
86
+ }
87
+ const nextSearchParam =
88
+ new URLSearchParams(nextSearchString).get("q") || "";
89
+ this.updateSearchInput(nextSearchParam);
90
+ if (nextSearchString !== this.state.lastHighlightedSearch) {
91
+ this.updateHighlighting(nextSearchParam);
92
+ }
93
+ }
94
+ },
95
+ eventName: "sidebarsearchchange",
96
+ historyMethod: window.history.pushState,
97
+ searchParam: "q",
98
+ shouldStopPropagation: true,
99
+ target: window
100
+ });
101
+ private _allLanguages: Array<Language> = [];
102
+
103
+ @track private pageReference: PageReference = {};
104
+
105
+ constructor() {
106
+ super();
107
+ this.pageReference = this.getReferenceFromUrl();
108
+ // In order to prevent dispatching unnecessary highlight changes, we
109
+ // track these items and use their previous values for comparisons in
110
+ // `renderedCallback`:
111
+ this.state = {
112
+ isFetchingContent: false,
113
+ isFetchingDocument: false,
114
+ lastHighlightedSearch: "",
115
+ internalLinkClicked: false
116
+ };
117
+ }
118
+
119
+ connectedCallback(): void {
120
+ if (!this.pageReference?.deliverable) {
121
+ window.location.href = "/docs";
122
+ return;
123
+ }
124
+ this.contentProvider = new FetchContent(this.apiDomain, this.allLanguages);
125
+ this.fetchDocument().then(() => (this.loaded = true));
126
+ window.addEventListener("popstate", this.handlePopState);
127
+
128
+ this.searchSyncer.init();
129
+ }
130
+
131
+ renderedCallback(): void {
132
+ this.setState({ internalLinkClicked: true });
133
+ const urlSectionLink =
134
+ this.pageReference?.hash?.split("#").length > 1
135
+ ? this.pageReference.hash.split("#")[1]
136
+ : this.pageReference?.hash;
137
+
138
+ const contentEl = this.template.querySelector("doc-content");
139
+ const anchorEl =
140
+ urlSectionLink &&
141
+ (contentEl?.shadowRoot?.querySelector(`[id='${urlSectionLink}']`) ||
142
+ contentEl?.shadowRoot?.querySelector(
143
+ `[name='${urlSectionLink}']`
144
+ ));
145
+
146
+ if (anchorEl) {
147
+ anchorEl.scrollIntoView();
148
+ this.setState({ internalLinkClicked: false });
149
+ }
150
+
151
+ if (
152
+ (this.prevState.isFetchingContent &&
153
+ !this.state.isFetchingContent) ||
154
+ (this.prevState.isFetchingDocument &&
155
+ !this.state.isFetchingDocument)
156
+ ) {
157
+ const prevSearchParam =
158
+ new URLSearchParams(this.state.lastHighlightedSearch).get(
159
+ "q"
160
+ ) || "";
161
+ const nextSearchParam =
162
+ new URLSearchParams(this.pageReference.search).get("q") || "";
163
+ this.updateHighlighting(nextSearchParam);
164
+ if (prevSearchParam !== nextSearchParam) {
165
+ this.updateSearchInput(nextSearchParam);
166
+ }
167
+ }
168
+ }
169
+
170
+ disconnectedCallback(): void {
171
+ window.removeEventListener("popstate", this.handlePopState);
172
+ this.searchSyncer.dispose();
173
+ if (this.listenerAttached) {
174
+ this.pageHeader.removeEventListener(
175
+ "langchange",
176
+ this.handleLanguageChange
177
+ );
178
+ this.listenerAttached = false;
179
+ }
180
+ }
181
+
182
+ private get languageId(): string {
183
+ return this.language.id.replace("-", "_");
184
+ }
185
+
186
+ private get releaseVersionId(): string {
187
+ return this.version.id;
188
+ }
189
+
190
+ private get deliverable(): string {
191
+ return this.pageReference.deliverable;
192
+ }
193
+
194
+ private get coveoAdvancedQueryConfig(): CoveoAdvancedQueryXMLConfig {
195
+ return {
196
+ // Temporary fix for empty results on apexref page
197
+ ...(this.deliverable === "apexref"
198
+ ? { source: "Sitemap - Developer Docs Atlas - Apexref" }
199
+ : {
200
+ objecttype: "HTDeveloperDocumentsC",
201
+ sflocale__c: this.languageId,
202
+ sfrelease__c: this.releaseVersionId,
203
+ sfdelivarable__c: this.deliverable
204
+ })
205
+ };
206
+ }
207
+
208
+ private get pageHeader(): Header {
209
+ if (!this._pageHeader) {
210
+ this._pageHeader = document.querySelector("doc-header");
211
+ }
212
+
213
+ return this._pageHeader;
214
+ }
215
+
216
+ private get sidebarValue(): string {
217
+ if (this.pageReference?.contentDocumentId) {
218
+ const hashedUri = `${
219
+ this.pageReference.contentDocumentId
220
+ }${this.normalizeHash(this.pageReference.hash)}`;
221
+ if (hashedUri in this.tocMap) {
222
+ return hashedUri;
223
+ }
224
+
225
+ if (this.pageReference.contentDocumentId in this.tocMap) {
226
+ return this.pageReference.contentDocumentId;
227
+ }
228
+ }
229
+
230
+ return "";
231
+ }
232
+
233
+ private get disableVersion(): boolean {
234
+ return !this.availableVersions || this.availableVersions.length <= 1;
235
+ }
236
+
237
+ private get versionOptions(): Array<DocVersion> {
238
+ return this.disableVersion
239
+ ? this.availableVersions
240
+ : this.availableVersions.map((version) => ({
241
+ ...version,
242
+ link: {
243
+ href: this.pageReferenceToString({
244
+ ...this.pageReference,
245
+ docId: version.url
246
+ })
247
+ }
248
+ }));
249
+ }
250
+
251
+ private handlePopState = (): void =>
252
+ this.updatePageReference(this.getReferenceFromUrl());
253
+
254
+ handleSelect(event: CustomEvent<{ name: string }>): void {
255
+ event.stopPropagation();
256
+ const { name } = event.detail;
257
+
258
+ if (this.sidebarValue === name) {
259
+ return;
260
+ }
261
+
262
+ if (name) {
263
+ const hashIndex = name.indexOf("#");
264
+ this.pageReference.hash =
265
+ hashIndex > -1 ? name.slice(hashIndex) : "";
266
+
267
+ const contentId = hashIndex > -1 ? name.slice(0, hashIndex) : name;
268
+ if (this.pageReference.contentDocumentId !== contentId) {
269
+ this.pageReference.contentDocumentId = contentId;
270
+ this.fetchContent().catch(handleContentError);
271
+ }
272
+
273
+ this.updateUrl();
274
+ }
275
+ }
276
+
277
+ handleNavClick(event: CustomEvent<{ pageReference: PageReference }>): void {
278
+ event.stopPropagation();
279
+ const { pageReference } = event.detail;
280
+ this.updatePageReference(pageReference);
281
+ this.updateUrl();
282
+ }
283
+
284
+ handleLanguageChange = (event: CustomEvent<string>): Promise<void> => {
285
+ if (this.language && this.language.id === event.detail) {
286
+ return;
287
+ }
288
+
289
+ this.language = this.availableLanguages.find(
290
+ ({ id }) => id === event.detail
291
+ );
292
+ this.pageReference.docId = this.language.url;
293
+ this.updateUrl();
294
+ this.fetchDocument();
295
+ };
296
+
297
+ updatePageReference(newPageReference: PageReference): void {
298
+ this.pageReference.hash = newPageReference.hash;
299
+ this.pageReference.search = newPageReference.search;
300
+
301
+ if (this.isSamePage(newPageReference)) {
302
+ return;
303
+ }
304
+
305
+ const isSameDocId = this.pageReference.docId !== newPageReference.docId;
306
+ this.pageReference = newPageReference;
307
+
308
+ if (isSameDocId) {
309
+ this.fetchDocument();
310
+ return;
311
+ }
312
+
313
+ this.fetchContent().catch(handleContentError);
314
+ }
315
+
316
+ getReferenceFromUrl(): PageReference {
317
+ const [page, docId, deliverable, contentDocumentId] =
318
+ window.location.pathname.substr(1).split("/");
319
+
320
+ const { origin: domain, hash, search } = window.location;
321
+
322
+ return {
323
+ contentDocumentId,
324
+ deliverable,
325
+ docId,
326
+ domain,
327
+ hash,
328
+ page,
329
+ search
330
+ };
331
+ }
332
+
333
+ isSamePage(reference: PageReference): boolean {
334
+ return (
335
+ this.pageReference.contentDocumentId ===
336
+ reference.contentDocumentId &&
337
+ this.pageReference.docId === reference.docId &&
338
+ this.pageReference.page === reference.page &&
339
+ this.pageReference.deliverable === reference.deliverable
340
+ );
341
+ }
342
+
343
+ async fetchDocument(): Promise<void> {
344
+ this.setState({
345
+ isFetchingDocument: true
346
+ });
347
+ const data = await this.contentProvider.fetchDocumentData(
348
+ this.pageReference.docId
349
+ );
350
+
351
+ // This could be a 404 scenario.
352
+ if (!data) {
353
+ this.setState({
354
+ isFetchingDocument: false
355
+ });
356
+ return;
357
+ }
358
+
359
+ this.docTitle = data.docTitle;
360
+ this.tocMap = data.tocMap;
361
+ this.sidebarContent = data.toc;
362
+ this.version = data.version;
363
+ this.language = data.language;
364
+ this.availableLanguages = data.availableLanguages;
365
+ this.availableVersions = data.availableVersions;
366
+ this.pdfUrl = data.pdfUrl;
367
+
368
+ this.updateHeader();
369
+
370
+ if (this.pageReference.deliverable !== data.deliverable) {
371
+ this.pageReference.deliverable = data.deliverable;
372
+ this.updateUrl(HistoryState.REPLACE_STATE);
373
+ }
374
+
375
+ if (
376
+ this.pageReference?.contentDocumentId?.replace(/\.htm$/, "") !==
377
+ data.id
378
+ ) {
379
+ try {
380
+ await this.fetchContent();
381
+ this.setState({
382
+ isFetchingDocument: false
383
+ });
384
+ return;
385
+ } catch (error) {
386
+ this.pageReference.contentDocumentId = `${data.id}.htm`;
387
+ this.pageReference.hash = "";
388
+ this.pageReference.search = "";
389
+ this.updateUrl(HistoryState.REPLACE_STATE);
390
+ }
391
+ }
392
+
393
+ this.docContent = data.content;
394
+ this.addMetatags();
395
+ this.setState({
396
+ isFetchingDocument: false
397
+ });
398
+ }
399
+
400
+ async fetchContent(): Promise<void> {
401
+ this.setState({
402
+ isFetchingContent: true
403
+ });
404
+ const data = await this.contentProvider.fetchContent(
405
+ this.pageReference.deliverable,
406
+ this.pageReference.contentDocumentId,
407
+ {
408
+ language: this.language.id,
409
+ version: this.version.id
410
+ }
411
+ );
412
+
413
+ if (data) {
414
+ this.docContent = data.content;
415
+ this.addMetatags();
416
+
417
+ if (!this.pageReference.hash) {
418
+ document.querySelector("main")?.scrollIntoView({
419
+ behavior: "smooth",
420
+ block: "start",
421
+ inline: "nearest"
422
+ });
423
+ }
424
+ }
425
+ this.setState({
426
+ isFetchingContent: false
427
+ });
428
+ }
429
+
430
+ updateHeader(): void {
431
+ if (!this.pageHeader) {
432
+ return;
433
+ }
434
+
435
+ if (this.docTitle) {
436
+ this.pageHeader.subtitle = this.docTitle;
437
+ }
438
+
439
+ if (this.pdfUrl) {
440
+ this.pageHeader.bailHref = this.pdfUrl;
441
+ this.pageHeader.bailLabel = "PDF";
442
+ }
443
+
444
+ if (!this.listenerAttached) {
445
+ this.pageHeader.addEventListener(
446
+ "langchange",
447
+ this.handleLanguageChange
448
+ );
449
+ this.listenerAttached = true;
450
+ }
451
+
452
+ this.pageHeader.languages = this.availableLanguages;
453
+ this.pageHeader.language = this.language?.id;
454
+
455
+ if (this.pageReference) {
456
+ const { docId, deliverable, page } = this.pageReference;
457
+ this.pageHeader.headerHref = `/${page}/${docId}/${deliverable}/`;
458
+ }
459
+ }
460
+
461
+ updateUrl(method = HistoryState.PUSH_STATE): void {
462
+ window.history[method](
463
+ {},
464
+ "docs",
465
+ this.pageReferenceToString(this.pageReference)
466
+ );
467
+ }
468
+
469
+ private updateHighlighting(searchParam: string): void {
470
+ this.dispatchHighlightChange(searchParam);
471
+ this.setState({
472
+ lastHighlightedSearch: this.pageReference.search
473
+ });
474
+ }
475
+
476
+ private updateSearchInput(searchParam: string): void {
477
+ this.template
478
+ .querySelector("doc-content-layout")
479
+ ?.setSidebarInputValue(searchParam);
480
+ }
481
+
482
+ private pageReferenceToString(reference: PageReference): string {
483
+ const { page, docId, deliverable, contentDocumentId, hash, search } =
484
+ reference;
485
+ return `/${page}/${docId}/${deliverable}/${contentDocumentId}${this.normalizeSearch(
486
+ search
487
+ )}${this.normalizeHash(hash)}`;
488
+ }
489
+
490
+ private normalizeUrlPart(part: string, sentinel: string): string {
491
+ return (
492
+ (part &&
493
+ (part.startsWith(sentinel) ? part : `${sentinel}${part}`)) ||
494
+ ""
495
+ );
496
+ }
497
+
498
+ private normalizeSearch(search: string): string {
499
+ return this.normalizeUrlPart(search, "?");
500
+ }
501
+
502
+ private normalizeHash(hash: string): string {
503
+ return this.normalizeUrlPart(hash, "#");
504
+ }
505
+
506
+ private getComposedTitle(
507
+ topicTitle: string | undefined,
508
+ docTitle: string | undefined
509
+ ): string {
510
+ // map to avoid duplicates
511
+ const titleMap = {};
512
+ if (topicTitle) {
513
+ // sometimes the h1 tag text (which is docSubTitle) contains text with new line character. For e.g, "Bulk API 2.0 Older\n Documentation",
514
+ // here it contains \n in the text context which needs to be removed
515
+ // also, there are multiple spaces in between the text, which needs to be replaced with a single space
516
+ const docTopicTitle = topicTitle
517
+ .replace(/[\t\r\n]/g, " ")
518
+ .replace(/ +/g, " ")
519
+ .trim();
520
+ titleMap[docTopicTitle] = true;
521
+ }
522
+
523
+ if (docTitle) {
524
+ titleMap[docTitle] = true;
525
+ }
526
+
527
+ titleMap["Salesforce Developers"] = true;
528
+
529
+ return Object.keys(titleMap).join(" | ");
530
+ }
531
+
532
+ private dispatchHighlightChange(term: string): void {
533
+ this.dispatchEvent(
534
+ new CustomEvent("highlightedtermchange", {
535
+ detail: term,
536
+ bubbles: true,
537
+ composed: true
538
+ })
539
+ );
540
+ }
541
+
542
+ addMetatags(): void {
543
+ const div = document.createElement("div");
544
+ div.innerHTML = this.docContent;
545
+ const docDescription = div.querySelector(".shortdesc")?.textContent;
546
+ const topicTitle = div.querySelector("h1")?.textContent;
547
+
548
+ const title = document.querySelector("title");
549
+ const composedTitle = this.getComposedTitle(topicTitle, this.docTitle);
550
+
551
+ if (title) {
552
+ title.textContent = composedTitle;
553
+ }
554
+ const metatitle = document.querySelector('meta[name="title"]');
555
+ if (metatitle) {
556
+ metatitle.setAttribute("content", composedTitle);
557
+ }
558
+
559
+ if (docDescription) {
560
+ const metadescription = document.querySelector(
561
+ 'meta[name="description"]'
562
+ );
563
+ if (metadescription) {
564
+ metadescription.setAttribute("content", docDescription);
565
+ }
566
+ }
567
+ }
568
+ }
@@ -0,0 +1,93 @@
1
+ import { LightningElement, track } from "lwc";
2
+
3
+ /**
4
+ * This is a helper class for when you want your LWC component to have state
5
+ * that is automatically tracked _along with_ its previous state, in a React-
6
+ * like fashion, so that you can compare current state with previous state
7
+ * after a render (like React's `commponentDidUpdate`). One benefit of doing
8
+ * things this way is that you can put all of your reactions to state changes
9
+ * in one place, in `renderedCallback`, rather than having them in various
10
+ * places throughout the component.
11
+ *
12
+ * The API consists in `this.prevState`, `this.state`, and `this.setState`.
13
+ *
14
+ * Usage:
15
+ *
16
+ * ```
17
+ * type MyState = {
18
+ * isFetchingContent: boolean;
19
+ * };
20
+ *
21
+ * class MyFetchingComponent extends LightningElementWithState<MyState> {
22
+ * constructor() {
23
+ * // `this.state` can only be initialized once
24
+ * this.state = {
25
+ * isFetchingContent: false
26
+ * };
27
+ * }
28
+ *
29
+ * // Queued for execution whenever a `setState` call completes
30
+ * renderedCallback() {
31
+ * if (this.prevState.isFetchingContent && !this.state.isFetchingContent) {
32
+ * // Do something knowing that we just finished fetching.
33
+ * notifyFetchSuccessful();
34
+ * }
35
+ * }
36
+ *
37
+ * fetchSomething() {
38
+ * this.setState({
39
+ * isFetchingContent: true
40
+ * });
41
+ *
42
+ * fetch(whatever).then(() => {
43
+ * this.setState({
44
+ * isFetching: false
45
+ * }
46
+ * });
47
+ * }
48
+ * }
49
+ * ```
50
+ */
51
+ export abstract class LightningElementWithState<
52
+ T extends { [key: string]: unknown }
53
+ > extends LightningElement {
54
+ private _prevState = {} as T;
55
+ @track private _state = {} as T;
56
+
57
+ private _didInitializeState = false;
58
+
59
+ protected get prevState(): T {
60
+ return Object.freeze({
61
+ ...this._prevState
62
+ });
63
+ }
64
+
65
+ protected get state(): T {
66
+ return Object.freeze({
67
+ ...this._state
68
+ });
69
+ }
70
+
71
+ protected set state(initialState: T) {
72
+ if (!this._didInitializeState) {
73
+ this._state = {
74
+ ...initialState
75
+ };
76
+ this._didInitializeState = true;
77
+ } else {
78
+ throw new Error(
79
+ "To mutate state after initialization, use `this.setState`."
80
+ );
81
+ }
82
+ }
83
+
84
+ protected setState = (state: Partial<T>): void => {
85
+ this._prevState = {
86
+ ...this._state
87
+ };
88
+ this._state = {
89
+ ...this._state,
90
+ ...state
91
+ };
92
+ };
93
+ }