Changeset View
Changeset View
Standalone View
Standalone View
lib/shared/ancestor-threads.js
// @flow | // @flow | ||||
import * as React from 'react'; | |||||
import genesis from '../facts/genesis.js'; | import genesis from '../facts/genesis.js'; | ||||
import { | import { | ||||
threadInfoSelector, | threadInfoSelector, | ||||
ancestorThreadInfos, | ancestorThreadInfos, | ||||
} from '../selectors/thread-selectors.js'; | } from '../selectors/thread-selectors.js'; | ||||
import { threadIsPending } from '../shared/thread-utils.js'; | import { threadIsPending } from '../shared/thread-utils.js'; | ||||
import { type ThreadInfo } from '../types/thread-types.js'; | import { type ThreadInfo } from '../types/thread-types.js'; | ||||
import { useSelector } from '../utils/redux-utils.js'; | import { useSelector } from '../utils/redux-utils.js'; | ||||
function useAncestorThreads( | function useAncestorThreads( | ||||
threadInfo: ThreadInfo, | threadInfo: ThreadInfo, | ||||
): $ReadOnlyArray<ThreadInfo> { | ): $ReadOnlyArray<ThreadInfo> { | ||||
return useSelector(state => { | const ancestorThreads = useSelector(ancestorThreadInfos(threadInfo.id)); | ||||
if (!threadIsPending(threadInfo.id)) { | |||||
const ancestorThreads = ancestorThreadInfos(threadInfo.id)(state); | |||||
if (ancestorThreads.length > 1) { | |||||
return ancestorThreads.slice(0, -1); | |||||
} | |||||
return ancestorThreads; | const genesisThreadInfo = useSelector( | ||||
state => threadInfoSelector(state)[genesis.id], | |||||
); | |||||
return React.useMemo(() => { | |||||
if (!threadIsPending(threadInfo.id)) { | |||||
return ancestorThreads.length > 1 | |||||
? ancestorThreads.slice(0, -1) | |||||
: ancestorThreads; | |||||
} | } | ||||
const genesisThreadInfo = threadInfoSelector(state)[genesis.id]; | |||||
return genesisThreadInfo ? [genesisThreadInfo] : []; | return genesisThreadInfo ? [genesisThreadInfo] : []; | ||||
}); | }, [ancestorThreads, genesisThreadInfo, threadInfo.id]); | ||||
} | } | ||||
export { useAncestorThreads }; | export { useAncestorThreads }; |