Skip to content

Commit

Permalink
chore: fix lint warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
kswenson committed Oct 30, 2024
1 parent fa44946 commit 2b7911f
Showing 1 changed file with 18 additions and 17 deletions.
35 changes: 18 additions & 17 deletions v3/src/components/common/formula-insert-values-menu.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { TriangleDownIcon, TriangleUpIcon } from "@chakra-ui/icons"
import {Divider, Flex, List, ListItem,} from "@chakra-ui/react"
import React, { useEffect, useRef, useState } from "react"
import React, { useCallback, useEffect, useRef, useState } from "react"
import { useMemo } from "use-memo-one"
import { useDataSetContext } from "../../hooks/use-data-set-context"
import { getGlobalValueManager, getSharedModelManager } from "../../models/tiles/tile-environment"
import { useFormulaEditorContext } from "./formula-editor-context"
Expand All @@ -25,46 +26,46 @@ export const InsertValuesMenu = ({setShowValuesMenu}: IProps) => {
)
const attributeNames = dataSet?.attributes.map(attr => attr.name)
const globalManager = dataSet && getGlobalValueManager(getSharedModelManager(dataSet))
const globals = globalManager
? Array.from(globalManager.globals.values()).map(global => ({ label: global.name }))
: []
const globals = useMemo(() => globalManager
? Array.from(globalManager.globals.values()).map(global => ({ label: global.name }))
: [], [globalManager])

Check warning on line 31 in v3/src/components/common/formula-insert-values-menu.tsx

View check run for this annotation

Codecov / codecov/patch

v3/src/components/common/formula-insert-values-menu.tsx#L31

Added line #L31 was not covered by tests
// TODO_Boundaries
const remoteBoundaryData = [ "CR_Cantones", "CR_Provincias", "DE_state_boundaries",
const remoteBoundaryData = useMemo(() => [ "CR_Cantones", "CR_Provincias", "DE_state_boundaries",
"IT_region_boundaries", "JP_Prefectures", "US_congressional_boundaries", "US_county_boundaries",
"US_puma_boundaries", "US_state_boundaries", "country_boundaries" ]
"US_puma_boundaries", "US_state_boundaries", "country_boundaries" ], [])
const constants = ["e", "false", "true", "π"]
const scrollableContainerRef = useRef<HTMLUListElement>(null)
const [listContainerStyle, setListContainerStyle] = useState({})
const [, setScrollPosition] = useState(0)

let maxItemLength = 0
const maxItemLength = useRef(0)

const insertValueToFormula = (value: string) => {
editorApi?.insertVariableString(value)
setShowValuesMenu(false)
}

const getListContainerStyle = () => {
const getListContainerStyle = useCallback(() => {
// calculate the top of the list container based on the height of the list. The list should be
// nearly centered on the button that opens it.
// The list should not extend beyond the top or bottom of the window.
const listEl = document.querySelector(".formula-operand-list-container") as HTMLElement
const button = document.querySelector(".formula-editor-button.insert-value")

attributeNames?.forEach((attrName) => {
if (attrName.length > maxItemLength) {
maxItemLength = attrName.length
if (attrName.length > maxItemLength.current) {
maxItemLength.current = attrName.length
}
})
remoteBoundaryData.forEach((boundary) => {
if (boundary.length > maxItemLength) {
maxItemLength = boundary.length
if (boundary.length > maxItemLength.current) {
maxItemLength.current = boundary.length
}
})

globals.forEach((global) => {
if (global.label.length > maxItemLength) {
maxItemLength = global.label.length
if (global.label.length > maxItemLength.current) {
maxItemLength.current = global.label.length

Check warning on line 68 in v3/src/components/common/formula-insert-values-menu.tsx

View check run for this annotation

Codecov / codecov/patch

v3/src/components/common/formula-insert-values-menu.tsx#L68

Added line #L68 was not covered by tests
}
})

Expand All @@ -80,10 +81,10 @@ export const InsertValuesMenu = ({setShowValuesMenu}: IProps) => {
} else {
top = spaceBelow - listHeight
}
return { top, height: kMaxHeight, width: 40 + 10 * maxItemLength }
return { top, height: kMaxHeight, width: 40 + 10 * maxItemLength.current }
}
return {}
}
}, [attributeNames, globals, remoteBoundaryData])

const handleScroll = (direction: "up" | "down") => {
const container = scrollableContainerRef.current
Expand Down Expand Up @@ -111,7 +112,7 @@ export const InsertValuesMenu = ({setShowValuesMenu}: IProps) => {
return () => {
container?.removeEventListener("scroll", handleScrollPosition)
}
}, [])
}, [getListContainerStyle])

const isScrollable = scrollableContainerRef.current && scrollableContainerRef.current.scrollHeight > kMaxHeight
const canScrollUp = scrollableContainerRef.current && scrollableContainerRef.current.scrollTop > 0
Expand Down

0 comments on commit 2b7911f

Please sign in to comment.