2022-08-02 15:26:55 +05:00
|
|
|
|
import { Button, Drawer, Skeleton, Tree, TreeProps, Typography } from 'antd'
|
2021-12-21 13:03:15 +05:00
|
|
|
|
import { DefaultValueType } from 'rc-tree-select/lib/interface'
|
2022-08-01 17:34:44 +05:00
|
|
|
|
import { useState, useEffect, ReactNode, useCallback, memo, Key } from 'react'
|
2022-06-09 17:51:41 +05:00
|
|
|
|
import { useNavigate, useLocation } from 'react-router-dom'
|
2022-02-25 16:57:08 +05:00
|
|
|
|
|
2022-03-23 14:28:08 +05:00
|
|
|
|
import { WellIcon, WellIconState } from '@components/icons'
|
|
|
|
|
import { invokeWebApiWrapperAsync } from '@components/factory'
|
2022-08-01 17:34:44 +05:00
|
|
|
|
import { DepositService, DepositDto, WellDto } from '@api'
|
|
|
|
|
import { isRawDate } from '@utils'
|
2021-12-27 15:58:31 +05:00
|
|
|
|
|
2022-01-24 17:32:45 +05:00
|
|
|
|
import { ReactComponent as DepositIcon } from '@images/DepositIcon.svg'
|
|
|
|
|
import { ReactComponent as ClusterIcon } from '@images/ClusterIcon.svg'
|
2021-12-27 15:31:37 +05:00
|
|
|
|
|
2022-01-24 17:32:45 +05:00
|
|
|
|
import '@styles/wellTreeSelect.css'
|
2021-11-19 11:46:11 +05:00
|
|
|
|
|
|
|
|
|
export const getWellState = (idState?: number): WellIconState => idState === 1 ? 'active' : 'unknown'
|
2021-12-27 15:58:31 +05:00
|
|
|
|
export const checkIsWellOnline = (lastTelemetryDate: unknown): boolean =>
|
|
|
|
|
isRawDate(lastTelemetryDate) && (Date.now() - +new Date(lastTelemetryDate) < 600_000)
|
2021-11-19 11:46:11 +05:00
|
|
|
|
|
2021-12-21 13:03:15 +05:00
|
|
|
|
export type TreeNodeData = {
|
2021-11-19 11:46:11 +05:00
|
|
|
|
title?: string | null
|
|
|
|
|
key?: string
|
2021-12-21 13:03:15 +05:00
|
|
|
|
value?: DefaultValueType
|
|
|
|
|
icon?: ReactNode
|
2021-11-19 11:46:11 +05:00
|
|
|
|
children?: TreeNodeData[]
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-01 17:34:44 +05:00
|
|
|
|
const getKeyByUrl = (url?: string): [Key | null, string | null] => {
|
|
|
|
|
const result = url?.match(/^\/([^\/]+)\/([^\/?]+)/) // pattern "/:type/:id"
|
|
|
|
|
if (!result) return [null, null]
|
|
|
|
|
return [result[0], result[1]]
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-27 15:58:31 +05:00
|
|
|
|
const getLabel = (wellsTree: TreeNodeData[], value?: string): string | undefined => {
|
2022-08-01 17:34:44 +05:00
|
|
|
|
const [url, type] = getKeyByUrl(value)
|
|
|
|
|
if (!url) return
|
2021-12-27 15:58:31 +05:00
|
|
|
|
let deposit: TreeNodeData | undefined
|
|
|
|
|
let cluster: TreeNodeData | undefined
|
|
|
|
|
let well: TreeNodeData | undefined
|
2021-12-23 12:42:52 +05:00
|
|
|
|
switch (type) {
|
2022-06-09 17:51:41 +05:00
|
|
|
|
case 'deposit':
|
|
|
|
|
deposit = wellsTree.find((deposit) => deposit.key === url)
|
|
|
|
|
if (deposit)
|
|
|
|
|
return `${deposit.title}`
|
|
|
|
|
return 'Ошибка! Месторождение не найдено!'
|
|
|
|
|
|
2021-12-23 12:42:52 +05:00
|
|
|
|
case 'cluster':
|
|
|
|
|
deposit = wellsTree.find((deposit) => (
|
2022-06-09 17:51:41 +05:00
|
|
|
|
cluster = deposit.children?.find((cluster: TreeNodeData) => cluster.key === url)
|
2021-12-23 12:42:52 +05:00
|
|
|
|
))
|
|
|
|
|
if (deposit && cluster)
|
|
|
|
|
return `${deposit.title} / ${cluster.title}`
|
2022-06-09 17:51:41 +05:00
|
|
|
|
return 'Ошибка! Куст не найден!'
|
|
|
|
|
|
2021-12-23 12:42:52 +05:00
|
|
|
|
case 'well':
|
|
|
|
|
deposit = wellsTree.find((deposit) => (
|
2021-12-27 15:58:31 +05:00
|
|
|
|
cluster = deposit.children?.find((cluster: TreeNodeData) => (
|
2022-06-09 17:51:41 +05:00
|
|
|
|
well = cluster.children?.find((well: TreeNodeData) => well.key === url)
|
2021-12-23 12:42:52 +05:00
|
|
|
|
))
|
|
|
|
|
))
|
|
|
|
|
if (deposit && cluster && well)
|
|
|
|
|
return `${deposit.title} / ${cluster.title} / ${well.title}`
|
2022-06-09 17:51:41 +05:00
|
|
|
|
return 'Ошибка! Скважина не найдена!'
|
|
|
|
|
|
2021-12-23 12:42:52 +05:00
|
|
|
|
default: break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-01 17:34:44 +05:00
|
|
|
|
const getWellSortScore = (well: WellDto) => {
|
|
|
|
|
let out = [1, 2, 0][well.idState ?? 2]
|
|
|
|
|
const timeout = Date.now() - +new Date(well.lastTelemetryDate || 0)
|
|
|
|
|
if (timeout < 600_000) out += 600_000 - timeout
|
|
|
|
|
return out
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const sortWellsByActive = (a: WellDto, b: WellDto): number => {
|
|
|
|
|
const score = getWellSortScore(b) - getWellSortScore(a)
|
|
|
|
|
if (score !== 0) return score
|
|
|
|
|
return (a.caption || '')?.localeCompare(b.caption || '')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const WellTreeSelector = memo(({ show, ...other }: TreeProps<TreeNodeData> & { show?: boolean }) => {
|
2022-02-25 16:57:08 +05:00
|
|
|
|
const [wellsTree, setWellsTree] = useState<TreeNodeData[]>([])
|
2021-11-19 11:46:11 +05:00
|
|
|
|
const [showLoader, setShowLoader] = useState<boolean>(false)
|
2022-08-01 17:34:44 +05:00
|
|
|
|
const [visible, setVisible] = useState<boolean>(false)
|
|
|
|
|
const [expanded, setExpanded] = useState<Key[]>([])
|
|
|
|
|
const [selected, setSelected] = useState<Key[]>([])
|
2021-12-23 12:42:52 +05:00
|
|
|
|
const [value, setValue] = useState<string>()
|
2022-08-01 17:34:44 +05:00
|
|
|
|
|
2022-06-09 17:51:41 +05:00
|
|
|
|
const navigate = useNavigate()
|
2022-02-25 16:57:08 +05:00
|
|
|
|
const location = useLocation()
|
2021-11-19 11:46:11 +05:00
|
|
|
|
|
2022-08-01 17:34:44 +05:00
|
|
|
|
useEffect(() => {
|
|
|
|
|
setVisible((prev) => show ?? prev)
|
|
|
|
|
setExpanded((prev) => {
|
|
|
|
|
if (typeof show === 'undefined') return prev
|
|
|
|
|
if (!show) return []
|
|
|
|
|
const out: Key[] = []
|
|
|
|
|
wellsTree.forEach((deposit) => {
|
|
|
|
|
if (deposit.key) out.push(deposit.key)
|
|
|
|
|
deposit.children?.forEach((cluster) => {
|
|
|
|
|
if (cluster.key) out.push(cluster.key)
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
return out
|
|
|
|
|
})
|
|
|
|
|
}, [wellsTree, show])
|
|
|
|
|
|
2021-11-19 11:46:11 +05:00
|
|
|
|
useEffect(() => {
|
|
|
|
|
invokeWebApiWrapperAsync(
|
|
|
|
|
async () => {
|
|
|
|
|
const deposits: Array<DepositDto> = await DepositService.getDeposits()
|
|
|
|
|
const wellsTree: TreeNodeData[] = deposits.map(deposit =>({
|
|
|
|
|
title: deposit.caption,
|
|
|
|
|
key: `/deposit/${deposit.id}`,
|
2022-01-12 20:48:51 +05:00
|
|
|
|
value: `/deposit/${deposit.id}`,
|
2021-11-19 11:46:11 +05:00
|
|
|
|
icon: <DepositIcon width={24} height={24}/>,
|
2022-08-01 17:34:44 +05:00
|
|
|
|
children: deposit.clusters?.map(cluster => {
|
|
|
|
|
const wells = cluster.wells ? cluster.wells.slice() : []
|
|
|
|
|
wells.sort(sortWellsByActive)
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
title: cluster.caption,
|
|
|
|
|
key: `/cluster/${cluster.id}`,
|
|
|
|
|
value: `/cluster/${cluster.id}`,
|
|
|
|
|
icon: <ClusterIcon width={24} height={24}/>,
|
|
|
|
|
children: wells.map(well => ({
|
|
|
|
|
title: well.caption,
|
|
|
|
|
key: `/well/${well.id}`,
|
|
|
|
|
value: `/well/${well.id}`,
|
|
|
|
|
icon: <WellIcon
|
|
|
|
|
width={24}
|
|
|
|
|
height={24}
|
|
|
|
|
state={getWellState(well.idState)}
|
|
|
|
|
online={checkIsWellOnline(well.lastTelemetryDate)}
|
|
|
|
|
/>
|
|
|
|
|
})),
|
|
|
|
|
}
|
|
|
|
|
}),
|
2021-11-19 11:46:11 +05:00
|
|
|
|
}))
|
|
|
|
|
setWellsTree(wellsTree)
|
|
|
|
|
},
|
|
|
|
|
setShowLoader,
|
2022-02-07 17:44:46 +05:00
|
|
|
|
`Не удалось загрузить список скважин`,
|
|
|
|
|
'Получить список скважин'
|
2021-11-19 11:46:11 +05:00
|
|
|
|
)
|
|
|
|
|
}, [])
|
|
|
|
|
|
2022-08-01 17:34:44 +05:00
|
|
|
|
const onChange = useCallback((value?: string): void => {
|
|
|
|
|
const key = getKeyByUrl(value)[0]
|
|
|
|
|
setSelected(key ? [key] : [])
|
|
|
|
|
setValue(getLabel(wellsTree, value))
|
|
|
|
|
}, [wellsTree])
|
2021-12-23 12:42:52 +05:00
|
|
|
|
|
2022-08-01 17:34:44 +05:00
|
|
|
|
const onSelect = useCallback((value: Key[]): void => {
|
|
|
|
|
navigate(String(value), { state: { from: location.pathname }})
|
2022-06-09 17:51:41 +05:00
|
|
|
|
}, [navigate, location])
|
2021-11-19 11:46:11 +05:00
|
|
|
|
|
2022-08-01 17:34:44 +05:00
|
|
|
|
useEffect(() => onChange(location.pathname), [onChange, location])
|
|
|
|
|
|
2021-11-19 11:46:11 +05:00
|
|
|
|
return (
|
2022-08-01 17:34:44 +05:00
|
|
|
|
<>
|
|
|
|
|
<Button loading={showLoader} onClick={() => setVisible(true)}>{value ?? 'Выберите месторождение'}</Button>
|
|
|
|
|
<Drawer visible={visible} mask={false} onClose={() => setVisible(false)}>
|
|
|
|
|
<Typography.Title level={3}>Список скважин</Typography.Title>
|
2022-08-02 15:26:55 +05:00
|
|
|
|
<Skeleton active loading={showLoader}>
|
2022-08-02 14:29:59 +05:00
|
|
|
|
<Tree
|
|
|
|
|
{...other}
|
|
|
|
|
showIcon
|
|
|
|
|
selectedKeys={selected}
|
|
|
|
|
treeData={wellsTree}
|
|
|
|
|
onSelect={onSelect}
|
|
|
|
|
onExpand={setExpanded}
|
|
|
|
|
expandedKeys={expanded}
|
|
|
|
|
/>
|
2022-08-02 15:26:55 +05:00
|
|
|
|
</Skeleton>
|
2022-08-01 17:34:44 +05:00
|
|
|
|
</Drawer>
|
|
|
|
|
</>
|
2021-11-19 11:46:11 +05:00
|
|
|
|
)
|
2022-02-25 16:57:08 +05:00
|
|
|
|
})
|
2021-11-19 11:46:11 +05:00
|
|
|
|
|
|
|
|
|
export default WellTreeSelector
|