AuthView.tsx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import { h } from 'preact'
  2. import { useCallback } from 'preact/hooks'
  3. import type { Body, Meta } from '@uppy/utils/lib/UppyFile'
  4. import type Translator from '@uppy/utils/lib/Translator'
  5. import type { Opts } from './ProviderView.js'
  6. import type ProviderViews from './ProviderView.js'
  7. type AuthViewProps<M extends Meta, B extends Body> = {
  8. loading: boolean | string
  9. pluginName: string
  10. pluginIcon: () => h.JSX.Element
  11. i18n: Translator['translateArray']
  12. handleAuth: ProviderViews<M, B>['handleAuth']
  13. renderForm?: Opts<M, B>['renderAuthForm']
  14. }
  15. function GoogleIcon() {
  16. return (
  17. <svg
  18. width="26"
  19. height="26"
  20. viewBox="0 0 26 26"
  21. xmlns="http://www.w3.org/2000/svg"
  22. >
  23. <g fill="none" fill-rule="evenodd">
  24. <circle fill="#FFF" cx="13" cy="13" r="13" />
  25. <path
  26. d="M21.64 13.205c0-.639-.057-1.252-.164-1.841H13v3.481h4.844a4.14 4.14 0 01-1.796 2.716v2.259h2.908c1.702-1.567 2.684-3.875 2.684-6.615z"
  27. fill="#4285F4"
  28. fill-rule="nonzero"
  29. />
  30. <path
  31. d="M13 22c2.43 0 4.467-.806 5.956-2.18l-2.908-2.259c-.806.54-1.837.86-3.048.86-2.344 0-4.328-1.584-5.036-3.711H4.957v2.332A8.997 8.997 0 0013 22z"
  32. fill="#34A853"
  33. fill-rule="nonzero"
  34. />
  35. <path
  36. d="M7.964 14.71A5.41 5.41 0 017.682 13c0-.593.102-1.17.282-1.71V8.958H4.957A8.996 8.996 0 004 13c0 1.452.348 2.827.957 4.042l3.007-2.332z"
  37. fill="#FBBC05"
  38. fill-rule="nonzero"
  39. />
  40. <path
  41. d="M13 7.58c1.321 0 2.508.454 3.44 1.345l2.582-2.58C17.463 4.891 15.426 4 13 4a8.997 8.997 0 00-8.043 4.958l3.007 2.332C8.672 9.163 10.656 7.58 13 7.58z"
  42. fill="#EA4335"
  43. fill-rule="nonzero"
  44. />
  45. <path d="M4 4h18v18H4z" />
  46. </g>
  47. </svg>
  48. )
  49. }
  50. function DefaultForm<M extends Meta, B extends Body>({
  51. pluginName,
  52. i18n,
  53. onAuth,
  54. }: {
  55. pluginName: string
  56. i18n: Translator['translateArray']
  57. onAuth: AuthViewProps<M, B>['handleAuth']
  58. }) {
  59. // In order to comply with Google's brand we need to create a different button
  60. // for the Google Drive plugin
  61. const isGoogleDrive = pluginName === 'Google Drive'
  62. const onSubmit = useCallback(
  63. (e: Event) => {
  64. e.preventDefault()
  65. onAuth()
  66. },
  67. [onAuth],
  68. )
  69. return (
  70. <form onSubmit={onSubmit}>
  71. {isGoogleDrive ?
  72. <button
  73. type="submit"
  74. className="uppy-u-reset uppy-c-btn uppy-c-btn-primary uppy-Provider-authBtn uppy-Provider-btn-google"
  75. data-uppy-super-focusable
  76. >
  77. <GoogleIcon />
  78. {i18n('signInWithGoogle')}
  79. </button>
  80. : <button
  81. type="submit"
  82. className="uppy-u-reset uppy-c-btn uppy-c-btn-primary uppy-Provider-authBtn"
  83. data-uppy-super-focusable
  84. >
  85. {i18n('authenticateWith', { pluginName })}
  86. </button>
  87. }
  88. </form>
  89. )
  90. }
  91. const defaultRenderForm = ({
  92. pluginName,
  93. i18n,
  94. onAuth,
  95. }: {
  96. pluginName: string
  97. i18n: Translator['translateArray']
  98. onAuth: AuthViewProps<Meta, Body>['handleAuth']
  99. }) => <DefaultForm pluginName={pluginName} i18n={i18n} onAuth={onAuth} />
  100. export default function AuthView<M extends Meta, B extends Body>({
  101. loading,
  102. pluginName,
  103. pluginIcon,
  104. i18n,
  105. handleAuth,
  106. renderForm = defaultRenderForm,
  107. }: AuthViewProps<M, B>) {
  108. return (
  109. <div className="uppy-Provider-auth">
  110. <div className="uppy-Provider-authIcon">{pluginIcon()}</div>
  111. <div className="uppy-Provider-authTitle">
  112. {i18n('authenticateWithTitle', {
  113. pluginName,
  114. })}
  115. </div>
  116. <div className="uppy-Provider-authForm">
  117. {renderForm({ pluginName, i18n, loading, onAuth: handleAuth })}
  118. </div>
  119. </div>
  120. )
  121. }