customCommitsResolutionModal.tsx 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import React, {useState} from 'react';
  2. import {ModalRenderProps} from 'sentry/actionCreators/modal';
  3. import Button from 'sentry/components/button';
  4. import {SelectAsyncField} from 'sentry/components/deprecatedforms';
  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 {Commit} from 'sentry/types';
  10. type Props = ModalRenderProps & {
  11. onSelected: ({inCommit: string}) => 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. }: Props) {
  24. const [commit, setCommit] = useState<{commit: Commit | undefined}>({commit: undefined});
  25. const [commits, setCommits] = useState<{commits: Commit[] | undefined}>({
  26. commits: undefined,
  27. });
  28. const onChange = (value: string | number | boolean) => {
  29. if (commits === undefined) {
  30. return;
  31. }
  32. setCommit({
  33. commit: commits.commits?.find(result => result.id === value),
  34. });
  35. };
  36. const onAsyncFieldResults = (results: Commit[]) => {
  37. setCommits({
  38. commits: results,
  39. });
  40. return results.map(c => ({
  41. value: c.id,
  42. label: <Version version={c.id} anchor={false} />,
  43. details: (
  44. <span>
  45. {t('Created')} <TimeSince date={c.dateCreated} />
  46. </span>
  47. ),
  48. c,
  49. }));
  50. };
  51. const url = `/projects/${orgSlug}/${projectSlug}/commits/`;
  52. const onSubmit = (e: React.FormEvent) => {
  53. e.preventDefault();
  54. onSelected({
  55. inCommit: {
  56. commit: commit.commit?.id,
  57. repository: commit.commit?.repository?.name,
  58. },
  59. });
  60. closeModal();
  61. };
  62. return (
  63. <form onSubmit={onSubmit}>
  64. <Header>{t('Resolved In')}</Header>
  65. <Body>
  66. <SelectAsyncField
  67. label={t('Commit')}
  68. id="commit"
  69. name="commit"
  70. onChange={onChange}
  71. placeholder={t('e.g. 1.0.4')}
  72. url={url}
  73. onResults={onAsyncFieldResults}
  74. onQuery={query => ({query})}
  75. />
  76. </Body>
  77. <Footer>
  78. <Button type="button" css={{marginRight: space(1.5)}} onClick={closeModal}>
  79. {t('Cancel')}
  80. </Button>
  81. <Button type="submit" priority="primary">
  82. {t('Save Changes')}
  83. </Button>
  84. </Footer>
  85. </form>
  86. );
  87. }
  88. export default CustomCommitsResolutionModal;