Refactor
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
import React, { useEffect } from 'react'
|
||||
|
||||
export function useAnimationToIndicateTopicUpdate(
|
||||
lastUpdate: number,
|
||||
selected: boolean,
|
||||
setShowUpdateAnimation: React.Dispatch<React.SetStateAction<boolean>>,
|
||||
showUpdateAnimation: boolean
|
||||
) {
|
||||
useEffect(() => {
|
||||
if (Date.now() - lastUpdate < 3000 && !selected) {
|
||||
setShowUpdateAnimation(true)
|
||||
}
|
||||
}, [lastUpdate, selected])
|
||||
useEffect(() => {
|
||||
if (showUpdateAnimation) {
|
||||
const timeout = setTimeout(() => setShowUpdateAnimation(false), 500)
|
||||
return function cleanup() {
|
||||
clearTimeout(timeout)
|
||||
}
|
||||
}
|
||||
}, [showUpdateAnimation])
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
import * as q from '../../../../../../backend/src/Model'
|
||||
import React, { useCallback } from 'react'
|
||||
import { KeyCodes } from '../../../../utils/KeyCodes'
|
||||
import { treeActions } from '../../../../actions'
|
||||
|
||||
export function useDeleteKeyCallback(topic: q.TreeNode<any>, actions: typeof treeActions) {
|
||||
return useCallback(
|
||||
(event: React.KeyboardEvent) => {
|
||||
if (event.keyCode === KeyCodes.delete || event.keyCode === KeyCodes.backspace) {
|
||||
event.stopPropagation()
|
||||
event.preventDefault()
|
||||
actions.clearTopic(topic, true, 50)
|
||||
}
|
||||
},
|
||||
[topic]
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
import { Props } from '..'
|
||||
import { useEffect, useState } from 'react'
|
||||
|
||||
export function useIsAllowedToAutoExpandState(props: Props): boolean {
|
||||
const { settings, treeNode, isRoot } = props
|
||||
const [isAllowedToAutoExpand, setAllowAutoExpand] = useState(false)
|
||||
useEffect(() => {
|
||||
const newIsAllowedToAutoExpand = isRoot || treeNode.edgeCount() <= settings.get('autoExpandLimit')
|
||||
if (newIsAllowedToAutoExpand !== isAllowedToAutoExpand) {
|
||||
setAllowAutoExpand(newIsAllowedToAutoExpand)
|
||||
}
|
||||
}, [treeNode.edgeCount(), settings.get('autoExpandLimit')])
|
||||
return isAllowedToAutoExpand
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
import * as q from '../../../../../../backend/src/Model'
|
||||
import React, { useEffect } from 'react'
|
||||
import { TopicViewModel } from '../../../../model/TopicViewModel'
|
||||
|
||||
export function useViewModelSubscriptions(
|
||||
treeNode: q.TreeNode<TopicViewModel>,
|
||||
nodeRef: React.MutableRefObject<HTMLDivElement | undefined>,
|
||||
setSelected: (value: boolean) => void,
|
||||
setCollapsedOverride: (value: boolean) => void
|
||||
) {
|
||||
const selectionDidChange = () => {
|
||||
const selected = treeNode.viewModel && treeNode.viewModel.isSelected()
|
||||
treeNode.viewModel && setSelected(Boolean(selected))
|
||||
|
||||
if (selected && nodeRef && nodeRef.current) {
|
||||
nodeRef.current.focus({ preventScroll: false })
|
||||
}
|
||||
}
|
||||
|
||||
const expandedDidChange = () => {
|
||||
treeNode.viewModel && setCollapsedOverride(!treeNode.viewModel.isExpanded())
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
addSubscriber()
|
||||
return function cleanup() {
|
||||
removeSubscriber()
|
||||
}
|
||||
}, [treeNode])
|
||||
|
||||
function addSubscriber() {
|
||||
treeNode.viewModel = new TopicViewModel()
|
||||
treeNode.viewModel.selectionChange.subscribe(selectionDidChange)
|
||||
treeNode.viewModel.expandedChange.subscribe(expandedDidChange)
|
||||
}
|
||||
|
||||
function removeSubscriber() {
|
||||
if (treeNode.viewModel) {
|
||||
treeNode.viewModel.selectionChange.unsubscribe(selectionDidChange)
|
||||
treeNode.viewModel.expandedChange.unsubscribe(expandedDidChange)
|
||||
treeNode.viewModel = undefined
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user