|
| 1 | +import React from "react"; |
| 2 | +import { Spinner } from "@depmap/common-components"; |
| 3 | +import GeneTeaTerm from "@depmap/data-explorer-2/src/components/DataExplorerPage/components/plot/integrations/GeneTea/GeneTeaTerm"; |
| 4 | +import { Alert, Button } from "react-bootstrap"; |
| 5 | +import { useExcerptData } from "../../../../../hooks/useExcerptData"; |
| 6 | +import PaginationControls from "./PaginationControls"; |
| 7 | +import styles from "../../../../../styles/GeneTea.scss"; |
| 8 | + |
| 9 | +interface ExcerptTableProps { |
| 10 | + useTerms: boolean; |
| 11 | + term: string; |
| 12 | + termToMatchingGenesMap: Map<string, string[]>; |
| 13 | + useAllGenes: boolean; |
| 14 | +} |
| 15 | + |
| 16 | +const ExcerptTable: React.FC<ExcerptTableProps> = ({ |
| 17 | + useTerms, |
| 18 | + term, |
| 19 | + termToMatchingGenesMap, |
| 20 | + useAllGenes, |
| 21 | +}: ExcerptTableProps) => { |
| 22 | + const { |
| 23 | + isLoading, |
| 24 | + error, |
| 25 | + pageData, |
| 26 | + totalPages, |
| 27 | + currentPage, |
| 28 | + handleNextPage, |
| 29 | + handlePrevPage, |
| 30 | + handleClickCreateTermContext, |
| 31 | + totalMatchingGenes, |
| 32 | + pageSize, |
| 33 | + } = useExcerptData(term, termToMatchingGenesMap, useAllGenes); |
| 34 | + |
| 35 | + const isContentReady = pageData && !error && !isLoading; |
| 36 | + const isTableVisible = isContentReady && Object.keys(pageData!).length > 0; |
| 37 | + |
| 38 | + const renderTableBody = () => { |
| 39 | + if (!pageData) return null; |
| 40 | + |
| 41 | + return ( |
| 42 | + <tbody> |
| 43 | + {Object.entries(pageData).map(([gene, context]) => { |
| 44 | + const html = context |
| 45 | + .replace(/<a href/g, '<a target="_blank" href') |
| 46 | + .replace(/ \| /g, "<br/><br/>"); |
| 47 | + |
| 48 | + return ( |
| 49 | + <tr key={gene}> |
| 50 | + <td> |
| 51 | + <a href={`../gene/${gene}`} target="_blank" rel="noreferrer"> |
| 52 | + {gene} |
| 53 | + </a> |
| 54 | + </td> |
| 55 | + <td> |
| 56 | + {/* eslint-disable-next-line react/no-danger */} |
| 57 | + <div dangerouslySetInnerHTML={{ __html: html }} /> |
| 58 | + </td> |
| 59 | + </tr> |
| 60 | + ); |
| 61 | + })} |
| 62 | + </tbody> |
| 63 | + ); |
| 64 | + }; |
| 65 | + |
| 66 | + return ( |
| 67 | + <div className={styles.tableWrapper}> |
| 68 | + <p className={styles.tableParagraph}> |
| 69 | + The term “ |
| 70 | + <GeneTeaTerm term={term} synonyms={[]} coincident={[]} />” is associated |
| 71 | + with {totalMatchingGenes} of the selected genes. |
| 72 | + </p> |
| 73 | + |
| 74 | + {!useTerms && ( |
| 75 | + <div className={styles.saveAsContextButton}> |
| 76 | + <Button |
| 77 | + bsStyle="primary" |
| 78 | + bsSize="small" |
| 79 | + onClick={handleClickCreateTermContext} |
| 80 | + > |
| 81 | + Save Term as Gene Context |
| 82 | + </Button> |
| 83 | + </div> |
| 84 | + )} |
| 85 | + |
| 86 | + <table className="table"> |
| 87 | + <thead> |
| 88 | + <tr> |
| 89 | + <th>Gene</th> |
| 90 | + <th>Excerpt</th> |
| 91 | + </tr> |
| 92 | + </thead> |
| 93 | + {isTableVisible && renderTableBody()} |
| 94 | + </table> |
| 95 | + |
| 96 | + {isLoading && ( |
| 97 | + <div className={styles.geneTeaModalSpinner}> |
| 98 | + <Spinner left="0px" position="static" /> |
| 99 | + </div> |
| 100 | + )} |
| 101 | + |
| 102 | + {error && ( |
| 103 | + <Alert bsStyle="danger"> |
| 104 | + There was a problem retrieving the excerpt(s) for this term/page. |
| 105 | + Please try again! |
| 106 | + </Alert> |
| 107 | + )} |
| 108 | + |
| 109 | + {!isLoading && |
| 110 | + !error && |
| 111 | + totalMatchingGenes > 0 && |
| 112 | + pageData && |
| 113 | + Object.keys(pageData).length === 0 && ( |
| 114 | + <Alert bsStyle="danger"> |
| 115 | + Could not find an excerpt for any gene on this page. |
| 116 | + </Alert> |
| 117 | + )} |
| 118 | + |
| 119 | + {!isLoading && ( |
| 120 | + <PaginationControls |
| 121 | + currentPage={currentPage} |
| 122 | + totalPages={totalPages} |
| 123 | + totalMatchingGenes={totalMatchingGenes} |
| 124 | + pageSize={pageSize} |
| 125 | + isLoading={isLoading} |
| 126 | + handleNextPage={handleNextPage} |
| 127 | + handlePrevPage={handlePrevPage} |
| 128 | + /> |
| 129 | + )} |
| 130 | + </div> |
| 131 | + ); |
| 132 | +}; |
| 133 | + |
| 134 | +export default ExcerptTable; |
0 commit comments