bookmarkStar.tsx 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import styled from '@emotion/styled';
  2. import {addErrorMessage} from 'sentry/actionCreators/indicator';
  3. import {update} from 'sentry/actionCreators/projects';
  4. import Button from 'sentry/components/button';
  5. import {IconStar} from 'sentry/icons';
  6. import {t} from 'sentry/locale';
  7. import {Organization, Project} from 'sentry/types';
  8. import {defined} from 'sentry/utils';
  9. import useApi from 'sentry/utils/useApi';
  10. type Props = {
  11. organization: Organization;
  12. project: Project;
  13. className?: string;
  14. /**
  15. * Allows the bookmarked state of the project to be overridden. Useful for
  16. * optimistic updates.
  17. */
  18. isBookmarked?: boolean;
  19. onToggle?: (isBookmarked: boolean) => void;
  20. };
  21. const BookmarkStar = styled(
  22. ({
  23. isBookmarked: isBookmarkedProp,
  24. className,
  25. organization,
  26. project,
  27. onToggle,
  28. }: Props) => {
  29. const api = useApi();
  30. const isBookmarked = defined(isBookmarkedProp)
  31. ? isBookmarkedProp
  32. : project.isBookmarked;
  33. const toggleProjectBookmark = (event: React.MouseEvent) => {
  34. update(api, {
  35. orgId: organization.slug,
  36. projectId: project.slug,
  37. data: {isBookmarked: !isBookmarked},
  38. }).catch(() =>
  39. addErrorMessage(t('Unable to toggle bookmark for %s', project.slug))
  40. );
  41. // prevent dropdowns from closing
  42. event.stopPropagation();
  43. onToggle?.(!isBookmarked);
  44. };
  45. return (
  46. <Button
  47. aria-label="Bookmark Project"
  48. aria-pressed={isBookmarked}
  49. onClick={toggleProjectBookmark}
  50. size="zero"
  51. priority="link"
  52. className={className}
  53. icon={
  54. <IconStar
  55. color={isBookmarked ? 'yellow300' : 'subText'}
  56. isSolid={isBookmarked}
  57. />
  58. }
  59. />
  60. );
  61. }
  62. )`
  63. &:hover svg {
  64. fill: ${p =>
  65. p.project.isBookmarked || p.isBookmarked ? p.theme.yellow400 : p.theme.textColor};
  66. }
  67. `;
  68. export default BookmarkStar;