Avatar.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. * Copyright (C) 2016 - present Instructure, Inc.
  3. *
  4. * This file is part of Canvas.
  5. *
  6. * Canvas is free software: you can redistribute it and/or modify it under
  7. * the terms of the GNU Affero General Public License as published by the Free
  8. * Software Foundation, version 3 of the License.
  9. *
  10. * Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
  11. * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. * A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
  13. * details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License along
  16. * with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. import I18n from 'i18n!student_context_tray'
  19. import React from 'react'
  20. import PropTypes from 'prop-types'
  21. import InstUIAvatar from 'instructure-ui/lib/components/Avatar'
  22. import Typography from 'instructure-ui/lib/components/Typography'
  23. import Link from 'instructure-ui/lib/components/Link'
  24. class Avatar extends React.Component {
  25. static propTypes = {
  26. user: PropTypes.shape({
  27. name: PropTypes.string,
  28. avatar_url: PropTypes.string,
  29. short_name: PropTypes.string,
  30. _id: PropTypes.string
  31. }).isRequired,
  32. courseId: PropTypes.oneOfType([
  33. PropTypes.string,
  34. PropTypes.number
  35. ]).isRequired,
  36. canMasquerade: PropTypes.bool.isRequired,
  37. }
  38. render () {
  39. const {user, courseId, canMasquerade} = this.props
  40. if (Object.keys(user).includes('avatar_url')) {
  41. const name = user.short_name || user.name || 'user';
  42. return (
  43. <div className="StudentContextTray__Avatar">
  44. <Link href={`/courses/${this.props.courseId}/users/${user._id}`} aria-label={I18n.t('Go to %{name}\'s profile', {name})}>
  45. <InstUIAvatar
  46. size="x-large"
  47. name={name}
  48. src={user.avatar_url}
  49. />
  50. </Link>
  51. {
  52. canMasquerade && (
  53. <Typography size="x-small" weight="bold" as="div">
  54. <a
  55. href={`/courses/${courseId}?become_user_id=${user._id}`}
  56. aria-label={I18n.t('Act as %{name}', { name: user.short_name })}
  57. >
  58. {I18n.t('Act as User')}
  59. </a>
  60. </Typography>
  61. )
  62. }
  63. </div>
  64. )
  65. }
  66. return null
  67. }
  68. }
  69. export default Avatar