@@ -225,7 +225,7 @@ export const Tree = React.memo<{
225
225
} , [ data , rootIds , orphanIds ] ) ;
226
226
227
227
// Create a list of component IDs which have exactly one story, which name exactly matches the component name.
228
- const singleStoryComponents = useMemo ( ( ) => {
228
+ const singleStoryComponentIds = useMemo ( ( ) => {
229
229
return orphansFirst . filter ( ( nodeId ) => {
230
230
const { children = [ ] , isComponent, isLeaf, name } = data [ nodeId ] ;
231
231
return (
@@ -239,20 +239,21 @@ export const Tree = React.memo<{
239
239
} , [ data , orphansFirst ] ) ;
240
240
241
241
// Omit single-story components from the list of nodes.
242
- const collapsedItems = useMemo (
243
- ( ) => orphansFirst . filter ( ( id ) => ! singleStoryComponents . includes ( id ) ) ,
244
- [ orphansFirst , singleStoryComponents ]
245
- ) ;
242
+ const collapsedItems = useMemo ( ( ) => {
243
+ return orphansFirst . filter ( ( id ) => ! singleStoryComponentIds . includes ( id ) ) ;
244
+ } , [ orphanIds , orphansFirst , singleStoryComponentIds ] ) ;
246
245
247
246
// Rewrite the dataset to place the child story in place of the component.
248
247
const collapsedData = useMemo ( ( ) => {
249
- return singleStoryComponents . reduce (
248
+ return singleStoryComponentIds . reduce (
250
249
( acc , id ) => {
251
250
const { children, parent } = data [ id ] as Group ;
252
251
const [ childId ] = children ;
253
- const siblings = [ ...data [ parent ] . children ] ;
254
- siblings [ siblings . indexOf ( id ) ] = childId ;
255
- acc [ parent ] = { ...data [ parent ] , children : siblings } ;
252
+ if ( parent ) {
253
+ const siblings = [ ...data [ parent ] . children ] ;
254
+ siblings [ siblings . indexOf ( id ) ] = childId ;
255
+ acc [ parent ] = { ...data [ parent ] , children : siblings } ;
256
+ }
256
257
acc [ childId ] = { ...data [ childId ] , parent, depth : data [ childId ] . depth - 1 } as Story ;
257
258
return acc ;
258
259
} ,
0 commit comments