bookmarkStar.tsx 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import {useState} from 'react';
  2. import styled from '@emotion/styled';
  3. import {addErrorMessage} from 'sentry/actionCreators/indicator';
  4. import {update} from 'sentry/actionCreators/projects';
  5. import {Button} from 'sentry/components/button';
  6. import {IconStar} from 'sentry/icons';
  7. import {t} from 'sentry/locale';
  8. import {space} from 'sentry/styles/space';
  9. import {Organization, Project} from 'sentry/types';
  10. import useApi from 'sentry/utils/useApi';
  11. type Props = {
  12. organization: Organization;
  13. project: Project;
  14. className?: string;
  15. onToggle?: (isBookmarked: boolean) => void;
  16. };
  17. function BookmarkStar({className, organization, project, onToggle}: Props) {
  18. const api = useApi();
  19. const [isBookmarked, setIsBookmarked] = useState(project.isBookmarked);
  20. const handleBookmarkToggle = (event: React.MouseEvent) => {
  21. // prevent dropdowns from closing
  22. event.stopPropagation();
  23. update(api, {
  24. orgId: organization.slug,
  25. projectId: project.slug,
  26. data: {isBookmarked: !isBookmarked},
  27. }).catch(() => addErrorMessage(t('Unable to toggle bookmark for %s', project.slug)));
  28. setIsBookmarked(current => !current);
  29. onToggle?.(!isBookmarked);
  30. };
  31. return (
  32. <BookmarkStarButton
  33. aria-label={t('Bookmark Project')}
  34. aria-pressed={isBookmarked}
  35. onClick={handleBookmarkToggle}
  36. size="zero"
  37. borderless
  38. className={className}
  39. icon={
  40. <IconStar color={isBookmarked ? 'yellow400' : 'subText'} isSolid={isBookmarked} />
  41. }
  42. />
  43. );
  44. }
  45. const BookmarkStarButton = styled(Button)`
  46. border-radius: 50%;
  47. width: 24px;
  48. height: 24px;
  49. margin: -${space(0.5)};
  50. svg {
  51. /* Negative margin for visual centering within the button */
  52. margin-top: -1px;
  53. }
  54. `;
  55. export default BookmarkStar;