-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: better soc & feat: keyboard shortcuts
- Loading branch information
1 parent
c423b88
commit 756d2d4
Showing
14 changed files
with
354 additions
and
259 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
frontend/src/components/features/connections/filter/AttributeFilterForm.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { Button } from "@/components/ui/shadcn/button"; | ||
import { Input } from "@/components/ui/shadcn/input"; | ||
import { useKeyboardShortcut } from "@/hooks/ui/useKeyboardShortcuts"; | ||
|
||
import { Attribute } from "@/types/Attribute"; | ||
import { X } from "lucide-react"; | ||
import { useEffect, useRef, useState } from "react"; | ||
|
||
interface AttributeFilterFormProps { | ||
addAttribute: (attribute: Attribute) => boolean; | ||
closeForm: () => void; | ||
containerRef: React.RefObject<HTMLDivElement>; | ||
} | ||
|
||
export default function AttributesFilterForm({ | ||
addAttribute, | ||
closeForm, | ||
containerRef, | ||
}: AttributeFilterFormProps) { | ||
const [key, setKey] = useState(""); | ||
const [value, setValue] = useState(""); | ||
const keyInputRef = useRef<HTMLInputElement>(null); | ||
|
||
const handleSubmitAttribute = () => { | ||
const attribute: Attribute = { key, value }; | ||
const added = addAttribute(attribute); | ||
if (added) { | ||
closeForm(); | ||
} | ||
}; | ||
|
||
useKeyboardShortcut( | ||
[ | ||
{ key: "Enter", callback: handleSubmitAttribute }, | ||
{ key: "Escape", callback: closeForm }, | ||
], | ||
containerRef.current | ||
); | ||
|
||
useEffect(() => { | ||
if (keyInputRef.current) { | ||
keyInputRef.current.focus(); | ||
} | ||
}, []); | ||
|
||
return ( | ||
<div className="flex items-center gap-4"> | ||
<Input | ||
className="w-20" | ||
ref={keyInputRef} | ||
onChange={(e) => setKey(e.target.value)} | ||
placeholder="key" | ||
/> | ||
<Input | ||
className="w-20" | ||
onChange={(e) => setValue(e.target.value)} | ||
placeholder="value" | ||
/> | ||
|
||
{/* <Switch id="regex-mode" /> */} | ||
{/* <Label className="font-thin" htmlFor="regex-mode"> | ||
Regex | ||
</Label> */} | ||
<div className="flex items-end gap-2"> | ||
<Button size={"sm"} onClick={handleSubmitAttribute}> | ||
Add | ||
</Button> | ||
<Button size={"sm"} onClick={closeForm}> | ||
<X /> | ||
</Button> | ||
</div> | ||
</div> | ||
); | ||
} |
33 changes: 33 additions & 0 deletions
33
frontend/src/components/features/connections/filter/AttributeFilterList.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { Button } from "@/components/ui/shadcn/button"; | ||
import { Attribute } from "@/types/Attribute"; | ||
import { X } from "lucide-react"; | ||
|
||
interface AttributeFilterListProps { | ||
attributes: Attribute[]; | ||
removeAttribute: (attribute: Attribute) => void; | ||
} | ||
|
||
export default function AttributeFilterList({ | ||
attributes, | ||
removeAttribute, | ||
}: Readonly<AttributeFilterListProps>) { | ||
return ( | ||
<> | ||
{attributes.map((attribute, index) => ( | ||
<div | ||
key={`${attribute.key}_${index.toString()}`} | ||
className="flex gap-2 mt-2" | ||
> | ||
<Button | ||
onClick={() => removeAttribute(attribute)} | ||
size="sm" | ||
variant="secondary" | ||
> | ||
{attribute.key}={attribute.value} | ||
<X /> | ||
</Button> | ||
</div> | ||
))} | ||
</> | ||
); | ||
} |
80 changes: 80 additions & 0 deletions
80
frontend/src/components/features/connections/filter/AttributesFilter.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { useReducer, useRef } from "react"; | ||
import { Attribute } from "@/types/Attribute"; | ||
import { Column } from "@tanstack/react-table"; | ||
import { Connection } from "@/types/Connection"; | ||
import { useAttributeFilter } from "@/hooks/features/connections/useAttributeFilter"; | ||
import AttributeFilterList from "./AttributeFilterList"; | ||
import AttributesFilterButton from "./AttributesFilterButton"; | ||
import AttributesFilterForm from "./AttributeFilterForm"; | ||
|
||
interface AttributesFilterProps { | ||
column: Column<Connection>; | ||
} | ||
|
||
interface FilterState { | ||
isOpen: boolean; | ||
currentAttribute: Attribute; | ||
} | ||
|
||
export type FilterAction = | ||
| { type: "OPEN_FILTER" } | ||
| { type: "CLOSE_FILTER" } | ||
| { type: "SET_ATTRIBUTE"; payload: Attribute } | ||
| { type: "CLEAR_ATTRIBUTE" }; | ||
|
||
const filterReducer = ( | ||
state: FilterState, | ||
action: FilterAction | ||
): FilterState => { | ||
switch (action.type) { | ||
case "OPEN_FILTER": | ||
return { ...state, isOpen: true }; | ||
case "CLOSE_FILTER": | ||
return { ...state, isOpen: false }; | ||
case "SET_ATTRIBUTE": | ||
return { ...state, currentAttribute: action.payload }; | ||
case "CLEAR_ATTRIBUTE": | ||
return { ...state, currentAttribute: { key: "", value: "" } }; | ||
default: | ||
return state; | ||
} | ||
}; | ||
|
||
export default function AttributesFilter({ | ||
column, | ||
}: Readonly<AttributesFilterProps>) { | ||
const [state, dispatch] = useReducer(filterReducer, { | ||
isOpen: false, | ||
currentAttribute: { key: "", value: "" }, | ||
}); | ||
|
||
const { attributes, addAttribute, removeAttribute } = | ||
useAttributeFilter(column); | ||
|
||
const containerRef = useRef<HTMLDivElement>(null); | ||
|
||
return ( | ||
<div className="flex gap-2 items-end" tabIndex={-1} ref={containerRef}> | ||
<div className="flex gap-2"> | ||
{!state.isOpen && ( | ||
<AttributesFilterButton | ||
onClick={() => dispatch({ type: "OPEN_FILTER" })} | ||
/> | ||
)} | ||
|
||
{state.isOpen && ( | ||
<AttributesFilterForm | ||
addAttribute={addAttribute} | ||
closeForm={() => dispatch({ type: "CLOSE_FILTER" })} | ||
containerRef={containerRef} | ||
/> | ||
)} | ||
</div> | ||
|
||
<AttributeFilterList | ||
attributes={attributes} | ||
removeAttribute={removeAttribute} | ||
/> | ||
</div> | ||
); | ||
} |
15 changes: 15 additions & 0 deletions
15
frontend/src/components/features/connections/filter/AttributesFilterButton.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { Button } from "@/components/ui/shadcn/button"; | ||
|
||
interface ConnectionAttributesButtonProps { | ||
onClick: () => void; | ||
} | ||
|
||
export default function ConnectionAttributesButton({ | ||
onClick, | ||
}: ConnectionAttributesButtonProps) { | ||
return ( | ||
<Button size={"sm"} onClick={onClick}> | ||
New Attribute Filter | ||
</Button> | ||
); | ||
} |
72 changes: 0 additions & 72 deletions
72
frontend/src/components/features/connections/filter/ConnectionAttributeFilterForm.tsx
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.