-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: use MRT for config relationships
Fixes #2285 fix: typescript error
- Loading branch information
1 parent
b86e615
commit 50f8d87
Showing
6 changed files
with
476 additions
and
117 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
117 changes: 117 additions & 0 deletions
117
src/components/Configs/ConfigList/Cells/MRTConfigListTagsCell.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,117 @@ | ||
import { MRTCellProps } from "@flanksource-ui/ui/MRTDataTable/MRTCellProps"; | ||
import { Tag } from "@flanksource-ui/ui/Tags/Tag"; | ||
import { useCallback } from "react"; | ||
import { useSearchParams } from "react-router-dom"; | ||
import { ConfigItem } from "../../../../api/types/configs"; | ||
|
||
type MRTConfigListTagsCellProps< | ||
T extends { | ||
tags?: Record<string, any>; | ||
id: string; | ||
} | ||
> = MRTCellProps<T> & { | ||
hideGroupByView?: boolean; | ||
enableFilterByTag?: boolean; | ||
}; | ||
|
||
export default function MRTConfigListTagsCell< | ||
T extends { tags?: Record<string, any>; id: string } | ||
>({ | ||
row, | ||
cell, | ||
hideGroupByView = false, | ||
enableFilterByTag = false | ||
}: MRTConfigListTagsCellProps<T>): JSX.Element | null { | ||
const [params, setParams] = useSearchParams(); | ||
|
||
const tagMap = cell.getValue<ConfigItem["tags"]>() || {}; | ||
const tagKeys = Object.keys(tagMap) | ||
.sort() | ||
.filter((key) => key !== "toString"); | ||
|
||
const onFilterByTag = useCallback( | ||
( | ||
e: React.MouseEvent<HTMLButtonElement>, | ||
tag: { | ||
key: string; | ||
value: string; | ||
}, | ||
action: "include" | "exclude" | ||
) => { | ||
if (!enableFilterByTag) { | ||
return; | ||
} | ||
|
||
e.preventDefault(); | ||
e.stopPropagation(); | ||
|
||
// Get the current tags from the URL | ||
const currentTags = params.get("tags"); | ||
const currentTagsArray = ( | ||
currentTags ? currentTags.split(",") : [] | ||
).filter((value) => { | ||
const tagKey = value.split("____")[0]; | ||
const tagAction = value.split(":")[1] === "1" ? "include" : "exclude"; | ||
|
||
if (tagKey === tag.key && tagAction !== action) { | ||
return false; | ||
} | ||
return true; | ||
}); | ||
|
||
// Append the new value, but for same tags, don't allow including and excluding at the same time | ||
const updatedValue = currentTagsArray | ||
.concat(`${tag.key}____${tag.value}:${action === "include" ? 1 : -1}`) | ||
.filter((value, index, self) => self.indexOf(value) === index) | ||
.join(","); | ||
|
||
// Update the URL | ||
params.set("tags", updatedValue); | ||
setParams(params); | ||
}, | ||
[enableFilterByTag, params, setParams] | ||
); | ||
|
||
const groupByProp = decodeURIComponent(params.get("groupByProp") ?? ""); | ||
|
||
if (tagKeys.length === 0) { | ||
return null; | ||
} | ||
|
||
if (!hideGroupByView && groupByProp) { | ||
if (!tagMap[groupByProp]) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<div className="flex w-full max-w-full flex-wrap space-y-1 pl-1 font-mono"> | ||
<div | ||
className="mr-1 max-w-full overflow-hidden text-ellipsis rounded-md border border-gray-300 bg-gray-200 px-1 py-0.75 text-xs font-semibold text-gray-600" | ||
key={groupByProp} | ||
> | ||
{groupByProp}:{" "} | ||
<span className="font-light">{tagMap[groupByProp]}</span> | ||
</div> | ||
</div> | ||
); | ||
} | ||
|
||
return ( | ||
<div className="flex flex-wrap gap-1"> | ||
{Object.entries(tagMap).map(([key, value]) => ( | ||
<Tag | ||
tag={{ | ||
key, | ||
value | ||
}} | ||
id={row.original.id} | ||
key={value} | ||
variant="gray" | ||
onFilterByTag={enableFilterByTag ? onFilterByTag : undefined} | ||
> | ||
{value} | ||
</Tag> | ||
))} | ||
</div> | ||
); | ||
} |
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
Oops, something went wrong.