|
| 1 | +import PropTypes from "prop-types"; |
| 2 | +import React from "react"; |
| 3 | +import { Stack } from "@chakra-ui/react"; |
| 4 | +import { mdToReact } from "./mdToReact"; |
| 5 | +import { useApiDocContext } from "."; |
| 6 | + |
| 7 | +export function DocBlock({ |
| 8 | + canonicalReference, |
| 9 | + summary = true, |
| 10 | + remarks = false, |
| 11 | + example = false, |
| 12 | + remarkCollapsible = true, |
| 13 | + since = true, |
| 14 | + deprecated = true, |
| 15 | +}) { |
| 16 | + return ( |
| 17 | + <Stack spacing="4"> |
| 18 | + {/** TODO: @since, @deprecated etc. */} |
| 19 | + {deprecated && <Deprecated canonicalReference={canonicalReference} />} |
| 20 | + {since && <Since canonicalReference={canonicalReference} />} |
| 21 | + {summary && <Summary canonicalReference={canonicalReference} />} |
| 22 | + {remarks && ( |
| 23 | + <Remarks |
| 24 | + collapsible={remarkCollapsible} |
| 25 | + canonicalReference={canonicalReference} |
| 26 | + /> |
| 27 | + )} |
| 28 | + {example && <Example canonicalReference={canonicalReference} />} |
| 29 | + </Stack> |
| 30 | + ); |
| 31 | +} |
| 32 | + |
| 33 | +DocBlock.propTypes = { |
| 34 | + canonicalReference: PropTypes.string.isRequired, |
| 35 | + summary: PropTypes.bool, |
| 36 | + remarks: PropTypes.bool, |
| 37 | + example: PropTypes.bool, |
| 38 | + remarkCollapsible: PropTypes.bool, |
| 39 | + since: PropTypes.bool, |
| 40 | + deprecated: PropTypes.bool, |
| 41 | +}; |
| 42 | + |
| 43 | +function MaybeCollapsible({ collapsible, children }) { |
| 44 | + return ( |
| 45 | + collapsible ? |
| 46 | + children ? |
| 47 | + <details> |
| 48 | + <summary>Read more...</summary> |
| 49 | + {children} |
| 50 | + </details> |
| 51 | + : null |
| 52 | + : children |
| 53 | + ); |
| 54 | +} |
| 55 | +MaybeCollapsible.propTypes = { |
| 56 | + collapsible: PropTypes.bool, |
| 57 | + children: PropTypes.node, |
| 58 | +}; |
| 59 | + |
| 60 | +/** |
| 61 | + * Might still need more work on the Gatsby side to get this to work. |
| 62 | + */ |
| 63 | +export function Deprecated({ canonicalReference, collapsible = false }) { |
| 64 | + const getItem = useApiDocContext(); |
| 65 | + const item = getItem(canonicalReference); |
| 66 | + const value = item.comment?.deprecated; |
| 67 | + if (!value) return null; |
| 68 | + return ( |
| 69 | + <MaybeCollapsible collapsible={collapsible}> |
| 70 | + <b>{mdToReact(value)}</b> |
| 71 | + </MaybeCollapsible> |
| 72 | + ); |
| 73 | +} |
| 74 | +Deprecated.propTypes = { |
| 75 | + canonicalReference: PropTypes.string.isRequired, |
| 76 | + collapsible: PropTypes.bool, |
| 77 | +}; |
| 78 | + |
| 79 | +/** |
| 80 | + * Might still need more work on the Gatsby side to get this to work. |
| 81 | + */ |
| 82 | +export function Since({ canonicalReference, collapsible = false }) { |
| 83 | + const getItem = useApiDocContext(); |
| 84 | + const item = getItem(canonicalReference); |
| 85 | + const value = item.comment?.since; |
| 86 | + if (!value) return null; |
| 87 | + return ( |
| 88 | + <MaybeCollapsible collapsible={collapsible}> |
| 89 | + <i>Added to Apollo Client in version {value}</i> |
| 90 | + </MaybeCollapsible> |
| 91 | + ); |
| 92 | +} |
| 93 | +Since.propTypes = { |
| 94 | + canonicalReference: PropTypes.string.isRequired, |
| 95 | + collapsible: PropTypes.bool, |
| 96 | +}; |
| 97 | + |
| 98 | +export function Summary({ canonicalReference, collapsible = false }) { |
| 99 | + const getItem = useApiDocContext(); |
| 100 | + const item = getItem(canonicalReference); |
| 101 | + const value = item.comment?.summary; |
| 102 | + if (!value) return null; |
| 103 | + return ( |
| 104 | + <MaybeCollapsible collapsible={collapsible}> |
| 105 | + {mdToReact(value)} |
| 106 | + </MaybeCollapsible> |
| 107 | + ); |
| 108 | +} |
| 109 | +Summary.propTypes = { |
| 110 | + canonicalReference: PropTypes.string.isRequired, |
| 111 | + collapsible: PropTypes.bool, |
| 112 | +}; |
| 113 | + |
| 114 | +export function Remarks({ canonicalReference, collapsible = false }) { |
| 115 | + const getItem = useApiDocContext(); |
| 116 | + const item = getItem(canonicalReference); |
| 117 | + const value = item.comment?.remarks?.replace(/^@remarks/g, ""); |
| 118 | + if (!value) return null; |
| 119 | + return ( |
| 120 | + <MaybeCollapsible collapsible={collapsible}> |
| 121 | + {mdToReact(value)} |
| 122 | + </MaybeCollapsible> |
| 123 | + ); |
| 124 | +} |
| 125 | +Remarks.propTypes = { |
| 126 | + canonicalReference: PropTypes.string.isRequired, |
| 127 | + collapsible: PropTypes.bool, |
| 128 | +}; |
| 129 | + |
| 130 | +export function Example({ |
| 131 | + canonicalReference, |
| 132 | + collapsible = false, |
| 133 | + index = 0, |
| 134 | +}) { |
| 135 | + const getItem = useApiDocContext(); |
| 136 | + const item = getItem(canonicalReference); |
| 137 | + const value = item.comment?.examples[index]; |
| 138 | + if (!value) return null; |
| 139 | + return ( |
| 140 | + <MaybeCollapsible collapsible={collapsible}> |
| 141 | + <b>{mdToReact(value)}</b> |
| 142 | + </MaybeCollapsible> |
| 143 | + ); |
| 144 | +} |
| 145 | +Example.propTypes = { |
| 146 | + canonicalReference: PropTypes.string.isRequired, |
| 147 | + collapsible: PropTypes.bool, |
| 148 | + index: PropTypes.number, |
| 149 | +}; |
0 commit comments