Skip to content

Commit

Permalink
support disabled tables
Browse files Browse the repository at this point in the history
  • Loading branch information
insmac committed Nov 19, 2024
1 parent 7f63f1c commit 0c475ae
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 16 deletions.
15 changes: 7 additions & 8 deletions packages/web-console/src/scenes/Editor/Metrics/metric.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -173,14 +173,13 @@ export const Metric = ({
beforeLabel={
<TableSelector
loading={loading}
options={tables
.filter((t) => t.walEnabled)
.map((t) => {
return {
label: t.table_name,
value: t.id.toString(),
}
})}
options={tables.map((t) => {
return {
label: t.table_name,
value: t.id.toString(),
disabled: !t.walEnabled,
}
})}
placeholder="Select table"
onSelect={(value) => onTableChange(metric, parseInt(value as string))}
defaultValue={metric.tableId && tableName ? tableName : ""}
Expand Down
25 changes: 17 additions & 8 deletions packages/web-console/src/scenes/Editor/Metrics/table-selector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { useKeyPress } from "../../../components"
type Option = {
label: string
value: string
disabled: boolean
}
type Props = {
defaultValue: string
Expand Down Expand Up @@ -55,17 +56,21 @@ const Options = styled.ul`
border-radius: 0.4rem;
`

const Item = styled.li<{ active: boolean }>`
const Item = styled.li<{ active: boolean; disabled: boolean }>`
display: flex;
align-items: center;
height: 3rem;
cursor: pointer;
padding: 0 1rem;
${({ active, theme }) => `
background: ${active ? theme.color.selection : "transparent"};
`}
${({ disabled, theme }) => `
color: ${disabled ? theme.color.gray1 : theme.color.foreground};
cursor: ${disabled ? "not-allowed" : "pointer"};
`}
.highlight {
background-color: #7c804f;
color: ${({ theme }) => theme.color.foreground};
Expand Down Expand Up @@ -131,6 +136,7 @@ export const TableSelector = ({
useEffect(() => {
if (enterPress && filteredOptions.length > 0) {
if (keyIndex !== -1) {
if (filteredOptions[keyIndex].disabled) return
onSelect(filteredOptions[keyIndex].value)
setHasFocus(false)
setQuery(filteredOptions[keyIndex].label)
Expand Down Expand Up @@ -183,14 +189,17 @@ export const TableSelector = ({
<Item
active={keyIndex === filteredOptions.indexOf(option)}
key={option.value}
onClick={() => {
inputRef.current!.value = option.label
onSelect(option.value)
setQuery(option.label)
setHasFocus(false)
}}
onMouseEnter={() => setKeyIndex(index)}
onMouseLeave={() => setKeyIndex(-1)}
disabled={option.disabled}
{...(!option.disabled && {
onClick: () => {
inputRef.current!.value = option.label
onSelect(option.value)
setQuery(option.label)
setHasFocus(false)
},
})}
>
<Highlighter
highlightClassName="highlight"
Expand Down

0 comments on commit 0c475ae

Please sign in to comment.