Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

In-plot bookmarking #416

Merged
merged 8 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion packages/upset/src/atoms/config/currentIntersectionAtom.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Bookmark, Row } from '@visdesignlab/upset2-core';
import { selector } from 'recoil';
import { selector, selectorFamily } from 'recoil';

import { queryColorPalette } from '../../utils/styles';
import { upsetConfigAtom } from './upsetConfigAtoms';
Expand Down Expand Up @@ -64,6 +64,41 @@ export const nextColorSelector = selector<string>({
},
});

/**
* Selector to determine if a row is bookmarked.
*
* This selector uses the `bookmarkSelector` to get the list of bookmarks and checks if the given row
* is present in that list by comparing the row's ID with the IDs of the bookmarks.
*
* @param row - The row to check for being bookmarked.
* @returns A boolean indicating whether the row is bookmarked.
*/
export const isRowBookmarkedSelector = selectorFamily<boolean, Row>({
key: 'is-row-bookmarked',
get: (row: Row) => ({ get }) => {
const bookmarks = get(bookmarkSelector);
return bookmarks.some(b => b.id === row.id);
},
});

/**
* Selector to get the color associated with a bookmarked row OR to get the next color if the row is not bookmarked.
*
* This selector uses the `bookmarkedColorPalette` to find the color for the given row.
* If the row does not have a color in the palette, it falls back to the `nextColorSelector`.
*
* @param row - The row for which the color is being selected.
* @returns The color associated with the given row.
*/
export const BookmarkedColorSelector = selectorFamily<string, Row>({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First char of name should be lowercase?

key: 'bookmarked-color-selector',
get: (row: Row) => ({ get }) => {
const palette = get(bookmarkedColorPalette);
const nextColor = get(nextColorSelector);
return palette[row.id] || nextColor;
},
});

/**
* The color to use for the current element selection stored in the config
*/
Expand Down
50 changes: 19 additions & 31 deletions packages/upset/src/components/Columns/BookmarkStar.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
import { Row } from '@visdesignlab/upset2-core';
import StarIcon from '@mui/icons-material/Star';
import {
FC,
MouseEvent,
useContext,
useMemo,
useState,
} from 'react';
import { FC, MouseEvent, useContext, useMemo, useState } from 'react';
import { useRecoilValue } from 'recoil';
import { ProvenanceContext } from '../Root';
import { dimensionsSelector } from '../../atoms/dimensionsAtom';
import translate from '../../utils/transform';
import {
bookmarkedColorPalette,
bookmarkSelector,
nextColorSelector,
BookmarkedColorSelector,
isRowBookmarkedSelector,
} from '../../atoms/config/currentIntersectionAtom';

type Props = {
Expand All @@ -39,23 +32,13 @@ const BOOKMARKED_OPACITY = 1.0;
*/
export const BookmarkStar: FC<Props> = ({ row }) => {
const dimensions = useRecoilValue(dimensionsSelector);
const colorPallete = useRecoilValue(bookmarkedColorPalette);
const nextColor = useRecoilValue(nextColorSelector);
const bookmarks = useRecoilValue(bookmarkSelector);
const bookmarked = useRecoilValue(isRowBookmarkedSelector(row));
const color = useRecoilValue(BookmarkedColorSelector(row));
const { actions } = useContext(ProvenanceContext);

const [hovered, setHovered] = useState(false);
const bookmarked = useMemo(() => bookmarks.find((b) => b.id === row.id), [
bookmarks,
row.id,
]);
const rowDisplayName = row.elementName.replaceAll('~&~', ' & ') || '';

const color = useMemo(() => (bookmarked ? colorPallete[row.id] : nextColor), [
colorPallete,
row.id,
bookmarked,
nextColor,
]);
const [hovered, setHovered] = useState(false);

/**
* Calculates the opacity value based on the bookmark and hover states.
Expand Down Expand Up @@ -98,14 +81,14 @@ export const BookmarkStar: FC<Props> = ({ row }) => {
event.stopPropagation();
if (bookmarked) {
actions.removeBookmark(bookmarked);
return;
} else {
actions.addBookmark({
id: row.id,
label: rowDisplayName,
size: row.size,
type: 'intersection',
});
}
actions.addBookmark({
id: row.id,
label: row.elementName,
size: row.size,
type: 'intersection',
});
};

return (
Expand All @@ -120,6 +103,11 @@ export const BookmarkStar: FC<Props> = ({ row }) => {
onMouseLeave={handleMouseLeave}
onClick={(e: any) => handleClick(e)}
>
<rect
height={dimensions.body.rowHeight}
width={dimensions.set.width}
fill="transparent"
/>
<StarIcon
height={dimensions.body.rowHeight}
width={dimensions.set.width}
Expand Down
Loading