customCommitsResolutionModal.tsx 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import {useState} from 'react';
  2. import type {ModalRenderProps} from 'sentry/actionCreators/modal';
  3. import Button from 'sentry/components/button';
  4. import SelectAsyncField from 'sentry/components/deprecatedforms/selectAsyncField';
  5. import TimeSince from 'sentry/components/timeSince';
  6. import Version from 'sentry/components/version';
  7. import {t} from 'sentry/locale';
  8. import space from 'sentry/styles/space';
  9. import type {Commit, ResolutionStatusDetails} from 'sentry/types';
  10. interface CustomCommitsResolutionModalProps extends ModalRenderProps {
  11. onSelected: (x: ResolutionStatusDetails) => void;
  12. orgSlug: string;
  13. projectSlug?: string;
  14. }
  15. function CustomCommitsResolutionModal({
  16. onSelected,
  17. orgSlug,
  18. projectSlug,
  19. closeModal,
  20. Header,
  21. Body,
  22. Footer,
  23. }: CustomCommitsResolutionModalProps) {
  24. const [commit, setCommit] = useState<Commit | undefined>();
  25. const [commits, setCommits] = useState<Commit[] | undefined>();
  26. const onChange = (value: string | number | boolean) => {
  27. setCommit(commits?.find(result => result.id === value));
  28. };
  29. const onAsyncFieldResults = (results: Commit[]) => {
  30. setCommits(results);
  31. return results.map(c => ({
  32. value: c.id,
  33. label: <Version version={c.id} anchor={false} />,
  34. details: (
  35. <span>
  36. {t('Created')} <TimeSince date={c.dateCreated} />
  37. </span>
  38. ),
  39. c,
  40. }));
  41. };
  42. const onSubmit = (e: React.FormEvent) => {
  43. e.preventDefault();
  44. onSelected({
  45. inCommit: {
  46. commit: commit?.id,
  47. repository: commit?.repository?.name,
  48. },
  49. });
  50. closeModal();
  51. };
  52. return (
  53. <form onSubmit={onSubmit}>
  54. <Header>
  55. <h4>{t('Resolved In')}</h4>
  56. </Header>
  57. <Body>
  58. <SelectAsyncField
  59. label={t('Commit')}
  60. id="commit"
  61. name="commit"
  62. onChange={onChange}
  63. placeholder={t('e.g. d86b832')}
  64. url={`/projects/${orgSlug}/${projectSlug}/commits/`}
  65. onResults={onAsyncFieldResults}
  66. onQuery={query => ({query})}
  67. />
  68. </Body>
  69. <Footer>
  70. <Button type="button" css={{marginRight: space(1.5)}} onClick={closeModal}>
  71. {t('Cancel')}
  72. </Button>
  73. <Button type="submit" priority="primary">
  74. {t('Save Changes')}
  75. </Button>
  76. </Footer>
  77. </form>
  78. );
  79. }
  80. export default CustomCommitsResolutionModal;