-
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.
feat: add mechanism to favorite Config Types in config summary
Fixes #2068 chore: reduce pin icon size Update src/components/Configs/ConfigSummary/ConfigSummaryTypeFavorite.tsx
- Loading branch information
1 parent
faf394d
commit 04720ee
Showing
3 changed files
with
107 additions
and
10 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
71 changes: 71 additions & 0 deletions
71
src/components/Configs/ConfigSummary/ConfigSummaryTypeFavorite.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,71 @@ | ||
import { ConfigSummary } from "@flanksource-ui/api/types/configs"; | ||
import clsx from "clsx"; | ||
import { useAtom } from "jotai"; | ||
import { atomWithStorage } from "jotai/utils"; | ||
import { useCallback } from "react"; | ||
import { PiPushPinFill, PiPushPinThin } from "react-icons/pi"; | ||
|
||
export const configTypesFavorites = atomWithStorage<Record<string, boolean>>( | ||
"configTypesFavorites", | ||
{}, | ||
undefined, | ||
{ | ||
getOnInit: true | ||
} | ||
); | ||
|
||
type ConfigSummaryFavoriteButtonProps = { | ||
configSummary: ConfigSummary; | ||
children?: React.ReactNode; | ||
}; | ||
|
||
export default function ConfigSummaryFavoriteButton({ | ||
configSummary, | ||
children | ||
}: ConfigSummaryFavoriteButtonProps) { | ||
const [favorites, setFavorites] = useAtom(configTypesFavorites); | ||
|
||
const isFavorite = favorites[configSummary.type]; | ||
|
||
const toggleFavorite = useCallback( | ||
(e: React.MouseEvent<HTMLElement>) => { | ||
e.preventDefault(); | ||
e.stopPropagation(); | ||
setFavorites((prev) => { | ||
// We want to remove the favorite | ||
if (prev[configSummary.type]) { | ||
// Avoid mutating the state, use object destructuring to remove the | ||
// key from the object | ||
const { [configSummary.type]: _, ...rest } = prev; | ||
return rest; | ||
} | ||
return { | ||
...prev, | ||
[configSummary.type]: true | ||
}; | ||
}); | ||
}, | ||
[configSummary.type, setFavorites] | ||
); | ||
|
||
return ( | ||
<div className="flex flex-row w-full gap-1 group"> | ||
<div className="flex-1 overflow-x-hidden text-ellipsis">{children}</div> | ||
<div | ||
role="button" | ||
onClick={toggleFavorite} | ||
title={isFavorite ? "Unpin" : "Pin"} | ||
className={clsx( | ||
"flex-row gap-1 px-1", | ||
isFavorite ? "flex" : "hidden group-hover:flex" | ||
)} | ||
> | ||
{isFavorite ? ( | ||
<PiPushPinFill className="h-5 w-5 text-amber-300" /> | ||
) : ( | ||
<PiPushPinThin className="h-5 w-5" /> | ||
)} | ||
</div> | ||
</div> | ||
); | ||
} |