bookmarkStar.tsx 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import {useState} from 'react';
  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 useApi from 'sentry/utils/useApi';
  9. type Props = {
  10. organization: Organization;
  11. project: Project;
  12. className?: string;
  13. onToggle?: (isBookmarked: boolean) => void;
  14. };
  15. function BookmarkStar({className, organization, project, onToggle}: Props) {
  16. const api = useApi();
  17. const [isBookmarked, setIsBookmarked] = useState(project.isBookmarked);
  18. const handleBookmarkToggle = (event: React.MouseEvent) => {
  19. // prevent dropdowns from closing
  20. event.stopPropagation();
  21. update(api, {
  22. orgId: organization.slug,
  23. projectId: project.slug,
  24. data: {isBookmarked: !isBookmarked},
  25. }).catch(() => addErrorMessage(t('Unable to toggle bookmark for %s', project.slug)));
  26. setIsBookmarked(current => !current);
  27. onToggle?.(!isBookmarked);
  28. };
  29. return (
  30. <Button
  31. aria-label={t('Bookmark Project')}
  32. aria-pressed={isBookmarked}
  33. onClick={handleBookmarkToggle}
  34. size="zero"
  35. priority="link"
  36. className={className}
  37. icon={
  38. <IconStar color={isBookmarked ? 'yellow300' : 'subText'} isSolid={isBookmarked} />
  39. }
  40. />
  41. );
  42. }
  43. export default BookmarkStar;