AttachmentList.jsx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. // eslint-disable-next-line import/no-unresolved
  4. import axios from 'axios'; // import axios from growi dependencies
  5. import { Attachment } from '@growi/ui';
  6. import RefsContext from '../util/RefsContext';
  7. import TagCacheManagerFactory from '../util/TagCacheManagerFactory';
  8. // eslint-disable-next-line no-unused-vars
  9. import styles from '../../css/index.css';
  10. import ExtractedAttachments from './ExtractedAttachments';
  11. const AttachmentLink = Attachment;
  12. export default class AttachmentList extends React.Component {
  13. constructor(props) {
  14. super(props);
  15. this.state = {
  16. isLoading: false,
  17. isLoaded: false,
  18. isError: false,
  19. errorMessage: null,
  20. attachments: [],
  21. };
  22. this.tagCacheManager = TagCacheManagerFactory.getInstance();
  23. }
  24. async componentWillMount() {
  25. const { refsContext } = this.props;
  26. // get state object cache
  27. const stateCache = this.tagCacheManager.getStateCache(refsContext);
  28. // check cache exists
  29. if (stateCache != null) {
  30. // restore state
  31. this.setState({
  32. ...stateCache,
  33. isLoading: false,
  34. });
  35. // parse with no effect
  36. try {
  37. refsContext.parse();
  38. }
  39. catch (err) {
  40. // do nothing
  41. }
  42. return; // go to render()
  43. }
  44. // parse
  45. try {
  46. refsContext.parse();
  47. }
  48. catch (err) {
  49. this.setState({
  50. isError: true,
  51. errorMessage: err.toString(),
  52. });
  53. // store to sessionStorage
  54. this.tagCacheManager.cacheState(refsContext, this.state);
  55. return;
  56. }
  57. this.loadContents();
  58. }
  59. async loadContents() {
  60. const { refsContext } = this.props;
  61. let res;
  62. try {
  63. this.setState({ isLoading: true });
  64. if (refsContext.isSingle) {
  65. res = await axios.get('/_api/plugin/ref', {
  66. params: {
  67. pagePath: refsContext.pagePath,
  68. fileNameOrId: refsContext.fileNameOrId,
  69. options: refsContext.options,
  70. },
  71. });
  72. this.setState({
  73. attachments: [res.data.attachment],
  74. });
  75. }
  76. else {
  77. res = await axios.get('/_api/plugin/refs', {
  78. params: {
  79. prefix: refsContext.prefix,
  80. pagePath: refsContext.pagePath,
  81. options: refsContext.options,
  82. },
  83. });
  84. this.setState({
  85. attachments: res.data.attachments,
  86. });
  87. }
  88. this.setState({
  89. isLoaded: true,
  90. });
  91. }
  92. catch (err) {
  93. this.setState({
  94. isError: true,
  95. errorMessage: err.response.data,
  96. });
  97. return;
  98. }
  99. finally {
  100. this.setState({ isLoading: false });
  101. // store to sessionStorage
  102. this.tagCacheManager.cacheState(refsContext, this.state);
  103. }
  104. }
  105. renderNoAttachmentsMessage() {
  106. const { refsContext } = this.props;
  107. let message;
  108. if (refsContext.prefix != null) {
  109. message = `${refsContext.prefix} and descendant pages have no attachments`;
  110. }
  111. else {
  112. message = `${refsContext.pagePath} has no attachments`;
  113. }
  114. return (
  115. <div className="text-muted">
  116. <small>
  117. <i className="fa fa-fw fa-info-circle" aria-hidden="true"></i>
  118. {message}
  119. </small>
  120. </div>
  121. );
  122. }
  123. renderContents() {
  124. const { refsContext } = this.props;
  125. if (this.state.isLoading) {
  126. return (
  127. <div className="text-muted">
  128. <i className="fa fa-spinner fa-pulse mr-1"></i>
  129. <span className="attachment-refs-blink">{refsContext.tagExpression}</span>
  130. </div>
  131. );
  132. }
  133. if (this.state.errorMessage != null) {
  134. return (
  135. <div className="text-warning">
  136. <i className="fa fa-exclamation-triangle fa-fw"></i>
  137. {refsContext.tagExpression} (-&gt; <small>{this.state.errorMessage}</small>)
  138. </div>
  139. );
  140. }
  141. if (this.state.isLoaded) {
  142. const { attachments } = this.state;
  143. // no attachments
  144. if (attachments.length === 0) {
  145. return this.renderNoAttachmentsMessage();
  146. }
  147. return (refsContext.isExtractImg)
  148. ? <ExtractedAttachments attachments={attachments} refsContext={refsContext} />
  149. : attachments.map((attachment) => {
  150. return <AttachmentLink key={attachment._id} attachment={attachment} />;
  151. });
  152. }
  153. }
  154. render() {
  155. return <div className="attachment-refs">{this.renderContents()}</div>;
  156. }
  157. }
  158. AttachmentList.propTypes = {
  159. appContainer: PropTypes.object.isRequired,
  160. refsContext: PropTypes.instanceOf(RefsContext).isRequired,
  161. };