Skip to content

Commit d075f42

Browse files
author
Linsen Wu
committed
Make notebook sections/sectiongroups undefined if not expanded
1 parent a813ece commit d075f42

File tree

5 files changed

+29
-10
lines changed

5 files changed

+29
-10
lines changed

Diff for: src/components/notebookRenderStrategy.tsx

+7-4
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export class NotebookRenderStrategy implements ExpandableNodeRenderStrategy {
5454
];
5555
}
5656

57-
if (this.notebook.needsToFetchChildren) {
57+
if (!this.notebook.sections && !this.notebook.sectionGroups) {
5858
return [
5959
<li className='progress-row'>
6060
<SpinnerIconSvg />
@@ -76,6 +76,10 @@ export class NotebookRenderStrategy implements ExpandableNodeRenderStrategy {
7676
</CreateNewSectionNode>] :
7777
[];
7878

79+
if (!this.notebook.sections || !this.notebook.sectionGroups) {
80+
return [...createNewSection]
81+
}
82+
7983
const setsize = this.notebook.sections.length + this.notebook.sectionGroups.length;
8084

8185
const sectionGroupRenderStrategies = this.notebook.sectionGroups.map(sectionGroup => new SectionGroupRenderStrategy(sectionGroup, this.globals));
@@ -85,7 +89,7 @@ export class NotebookRenderStrategy implements ExpandableNodeRenderStrategy {
8589
expanded={renderStrategy.isExpanded()} node={renderStrategy} globals={this.globals}
8690
treeViewId={Constants.TreeView.id} key={renderStrategy.getId()}
8791
id={renderStrategy.getId()} level={childrenLevel} ariaSelected={renderStrategy.isAriaSelected()} selected={renderStrategy.isSelected()}
88-
setsize={setsize} posinset={this.notebook.sections.length + i + 1} /> :
92+
setsize={setsize} posinset={this.notebook.sections ? this.notebook.sections.length + i + 1 : undefined} /> :
8993
<LeafNode node={renderStrategy} treeViewId={Constants.TreeView.id} key={renderStrategy.getId()} globals={this.globals}
9094
id={renderStrategy.getId()} level={childrenLevel} ariaSelected={renderStrategy.isAriaSelected()} />);
9195

@@ -143,7 +147,7 @@ export class NotebookRenderStrategy implements ExpandableNodeRenderStrategy {
143147
}
144148

145149
private onExpand() {
146-
if (this.notebook.needsToFetchChildren && this.notebook.apiUrl && this.globals.oneNoteDataProvider) {
150+
if (!this.notebook.sections && !this.notebook.sectionGroups && this.notebook.apiUrl && this.globals.oneNoteDataProvider) {
147151
this.globals.oneNoteDataProvider.getNotebookBySelfUrl(this.notebook.apiUrl, 5).then((notebook) => {
148152
this.notebook.sections = notebook.sections
149153
this.notebook.sectionGroups = notebook.sectionGroups
@@ -154,7 +158,6 @@ export class NotebookRenderStrategy implements ExpandableNodeRenderStrategy {
154158
this.notebook.apiHttpErrorMessage = Strings.getError(apiError.statusCode);
155159
}
156160
}).then(() => {
157-
this.notebook.needsToFetchChildren = false;
158161
if (this.globals.callbacks.onNotebookInfoReturned) {
159162
this.globals.callbacks.onNotebookInfoReturned(this.notebook);
160163
}

Diff for: src/oneNoteDataStructures/notebook.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@ export interface Notebook extends OneNoteItem {
77
apiHttpErrorMessage?: string;
88

99
expanded: boolean;
10-
sectionGroups: SectionGroup[];
11-
sections: Section[];
10+
sectionGroups?: SectionGroup[];
11+
sections?: Section[];
1212
webUrl: string;
1313
apiUrl?: string;
1414
lastModifiedTime: Date;
15-
needsToFetchChildren?: boolean
1615
}

Diff for: src/oneNoteDataStructures/notebookListUpdater.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export class NotebookListUpdater {
5858
addSection(newSection: Section) {
5959
const loneParent = newSection.parent!;
6060
const parentInHierarchy = OneNoteItemUtils.find(this.notebooks, item => item.id === loneParent.id) as SectionParent | undefined;
61-
if (parentInHierarchy) {
61+
if (parentInHierarchy && parentInHierarchy.sections) {
6262
// Establish two-way reference
6363
parentInHierarchy.sections = [newSection, ...parentInHierarchy.sections];
6464
newSection.parent = parentInHierarchy;
@@ -69,6 +69,11 @@ export class NotebookListUpdater {
6969
// Preserve properties we want to keep from the original object ...
7070
next.expanded = original.expanded;
7171

72+
if (!original.sections || !original.sectionGroups ||
73+
!next.sections || !next.sectionGroups) {
74+
return;
75+
}
76+
7277
// ... then recurse through the children
7378
for (let newSectionGroup of next.sectionGroups) {
7479
const originalSectionGroup = original.sectionGroups.find(sg => sg.id === newSectionGroup.id);
@@ -118,6 +123,10 @@ export class NotebookListUpdater {
118123
}
119124

120125
private getSectionRefFromSectionParent(sectionId: string, sectionParent: SectionParent): Section | undefined {
126+
if (!sectionParent.sections || !sectionParent.sectionGroups) {
127+
return undefined;
128+
}
129+
121130
// Search this parent's sections for the matching id ...
122131
for (let childSection of sectionParent.sections) {
123132
if (childSection.id === sectionId) {

Diff for: src/oneNoteDataStructures/oneNoteApiResponseTransformer.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ export class OneNoteApiResponseTransformer {
3030
lastModifiedTime: notebook.lastModifiedTime
3131
};
3232

33-
transformed.sectionGroups = notebook.sectionGroups ? notebook.sectionGroups.map(sg => this.transformSectionGroup(sg, transformed)) : [];
34-
transformed.sections = notebook.sections ? notebook.sections.map(section => this.transformSection(section, transformed)) : [];
33+
transformed.sectionGroups = notebook.sectionGroups ? notebook.sectionGroups.map(sg => this.transformSectionGroup(sg, transformed)) : undefined;
34+
transformed.sections = notebook.sections ? notebook.sections.map(section => this.transformSection(section, transformed)) : undefined;
3535

3636
return transformed;
3737
}

Diff for: src/oneNoteDataStructures/oneNoteItemUtils.ts

+8
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ export class OneNoteItemUtils {
2929
sectionGroups = sectionParentNotebook.apiProperties.spSectionGroups;
3030
sections = sectionParentNotebook.apiProperties.spSections;
3131
} else {
32+
if (!sectionParent.sections || !sectionParent.sectionGroups) {
33+
continue;
34+
}
35+
3236
sectionGroups = sectionParent.sectionGroups;
3337
sections = sectionParent.sections;
3438
}
@@ -93,6 +97,10 @@ export class OneNoteItemUtils {
9397
* one descendent section; false otherwise.
9498
*/
9599
static prune(root: Notebook | SectionGroup): boolean {
100+
if (!root.sections || !root.sectionGroups) {
101+
return false
102+
}
103+
96104
root.sectionGroups = root.sectionGroups.filter(OneNoteItemUtils.prune);
97105
return root.sectionGroups.length > 0 || root.sections.length > 0;
98106
}

0 commit comments

Comments
 (0)