2022-10-03 21:35:26 +05:00
|
|
|
|
import { Button, Drawer, Skeleton, Tree, TreeDataNode, TreeProps, Typography } from 'antd'
|
|
|
|
|
import { useState, useEffect, 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
|
|
|
|
|
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]]
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-03 21:35:26 +05:00
|
|
|
|
const getLabel = (wellsTree: TreeDataNode[], value?: string): string | undefined => {
|
2022-08-01 17:34:44 +05:00
|
|
|
|
const [url, type] = getKeyByUrl(value)
|
|
|
|
|
if (!url) return
|
2022-10-03 21:35:26 +05:00
|
|
|
|
let deposit: TreeDataNode | undefined
|
|
|
|
|
let cluster: TreeDataNode | undefined
|
|
|
|
|
let well: TreeDataNode | 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-10-03 21:35:26 +05:00
|
|
|
|
cluster = deposit.children?.find((cluster: TreeDataNode) => 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) => (
|
2022-10-03 21:35:26 +05:00
|
|
|
|
cluster = deposit.children?.find((cluster: TreeDataNode) => (
|
|
|
|
|
well = cluster.children?.find((well: TreeDataNode) => 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 || '')
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-03 21:35:26 +05:00
|
|
|
|
export type WellTreeSelectorProps = TreeProps<TreeDataNode> & {
|
|
|
|
|
show?: boolean
|
|
|
|
|
expand?: boolean | Key[]
|
|
|
|
|
current?: Key
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const getExpandKeys = (treeData: TreeDataNode[], depositKeys?: Key[] | boolean): Key[] => {
|
|
|
|
|
const out: Key[] = []
|
|
|
|
|
treeData.forEach((deposit) => {
|
|
|
|
|
if (Array.isArray(depositKeys) && !depositKeys.includes(deposit.key)) return
|
|
|
|
|
if (deposit.key) out.push(deposit.key)
|
|
|
|
|
deposit.children?.forEach((cluster) => {
|
|
|
|
|
if (cluster.key) out.push(cluster.key)
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
return out
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const WellTreeSelector = memo<WellTreeSelectorProps>(({ show, expand, current, ...other }) => {
|
|
|
|
|
const [wellsTree, setWellsTree] = useState<TreeDataNode[]>([])
|
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(() => {
|
2022-10-03 21:35:26 +05:00
|
|
|
|
if (current) setSelected([current])
|
|
|
|
|
}, [current])
|
|
|
|
|
|
|
|
|
|
useEffect(() => setVisible((prev) => show ?? prev), [show])
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
setExpanded((prev) => expand ? getExpandKeys(wellsTree, expand) : prev)
|
|
|
|
|
}, [wellsTree, expand])
|
2022-08-01 17:34:44 +05:00
|
|
|
|
|
2021-11-19 11:46:11 +05:00
|
|
|
|
useEffect(() => {
|
|
|
|
|
invokeWebApiWrapperAsync(
|
|
|
|
|
async () => {
|
|
|
|
|
const deposits: Array<DepositDto> = await DepositService.getDeposits()
|
2022-10-03 21:35:26 +05:00
|
|
|
|
const wellsTree: TreeDataNode[] = deposits.map(deposit =>({
|
2021-11-19 11:46:11 +05:00
|
|
|
|
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
|
|
|
|
`Не удалось загрузить список скважин`,
|
2022-08-18 11:49:10 +05:00
|
|
|
|
{ actionName: 'Получить список скважин' }
|
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
|