WellTreeSelector доработан для работы с нецифровыми значениями в местах смены переменных

This commit is contained in:
Александр Сироткин 2022-12-05 21:07:17 +05:00
parent dc0f80fee5
commit ebe3a50fbe

View File

@ -12,12 +12,19 @@ import { ReactComponent as ClusterIcon } from '@images/ClusterIcon.svg'
import '@styles/wellTreeSelect.css' import '@styles/wellTreeSelect.css'
/**
* Для поиска в URL текущего раздела по шаблону `/{type}/{id}`
*
* Если найдено совпадение может вернуть 1 или 2 группы соответственно
*/
const URL_REGEX = /^\/([^\/?#]+)(?:\/([^\/?#]+))?/
export const getWellState = (idState?: number): WellIconState => idState === 1 ? 'active' : 'unknown' export const getWellState = (idState?: number): WellIconState => idState === 1 ? 'active' : 'unknown'
export const checkIsWellOnline = (lastTelemetryDate: unknown): boolean => export const checkIsWellOnline = (lastTelemetryDate: unknown): boolean =>
isRawDate(lastTelemetryDate) && (Date.now() - +new Date(lastTelemetryDate) < 600_000) isRawDate(lastTelemetryDate) && (Date.now() - +new Date(lastTelemetryDate) < 600_000)
const getKeyByUrl = (url?: string): [Key | null, string | null] => { const getKeyByUrl = (url?: string): [Key | null, string | null] => {
const result = url?.match(/^\/([^\/]+)\/([^\/?]+)/) // pattern "/:type/:id" const result = url?.match(URL_REGEX) // pattern "/:type/:id"
if (!result) return [null, null] if (!result) return [null, null]
return [result[0], result[1]] return [result[0], result[1]]
} }
@ -137,8 +144,8 @@ export const WellTreeSelector = memo<WellTreeSelectorProps>(({ expand, current,
}, [wellsTree]) }, [wellsTree])
const onSelect = useCallback((value: Key[]): void => { const onSelect = useCallback((value: Key[]): void => {
const newRoot = /\/(\w+)\/\d+/.exec(String(value)) const newRoot = URL_REGEX.exec(String(value))
const oldRoot = /\/(\w+)(?:\/\d+)?/.exec(location.pathname) const oldRoot = URL_REGEX.exec(location.pathname)
if (!newRoot || !oldRoot) return if (!newRoot || !oldRoot) return
let newPath = newRoot[0] let newPath = newRoot[0]