|
| 1 | +import React, { useContext, useState } from "react"; |
| 2 | +import { Link } from "react-router-dom"; |
| 3 | +import styled from "styled-components"; |
| 4 | +import { DeclarationsContext } from "./DeclarationsContext"; |
| 5 | +import { KindIcon } from "./utils/components"; |
| 6 | + |
| 7 | +const COLLAPSE_THRESHOLD = 8; |
| 8 | + |
| 9 | +const Wrapper = styled.div` |
| 10 | + padding: 8px 12px; |
| 11 | + border-top: 1px solid ${(props) => props.theme.groupBorder}; |
| 12 | +`; |
| 13 | + |
| 14 | +const Title = styled.div` |
| 15 | + font-size: 12px; |
| 16 | + font-weight: 600; |
| 17 | + text-transform: uppercase; |
| 18 | + letter-spacing: 0.04em; |
| 19 | + color: ${(props) => props.theme.textDim}; |
| 20 | + margin-bottom: 4px; |
| 21 | +`; |
| 22 | + |
| 23 | +const RefList = styled.div` |
| 24 | + display: flex; |
| 25 | + flex-wrap: wrap; |
| 26 | + gap: 4px; |
| 27 | +`; |
| 28 | + |
| 29 | +const RefLink = styled(Link)` |
| 30 | + display: inline-flex; |
| 31 | + align-items: center; |
| 32 | + gap: 3px; |
| 33 | + padding: 2px 8px; |
| 34 | + border-radius: 4px; |
| 35 | + font-size: 14px; |
| 36 | + text-decoration: none; |
| 37 | + color: ${(props) => props.theme.text}; |
| 38 | + background: ${(props) => props.theme.groupMembers}; |
| 39 | + border: 1px solid ${(props) => props.theme.groupBorder}; |
| 40 | + transition: border-color 0.1s; |
| 41 | +
|
| 42 | + &:hover { |
| 43 | + border-color: ${(props) => props.theme.highlight}; |
| 44 | + } |
| 45 | +`; |
| 46 | + |
| 47 | +const RefField = styled.span` |
| 48 | + color: ${(props) => props.theme.textDim}; |
| 49 | +
|
| 50 | + &::before { |
| 51 | + content: "."; |
| 52 | + } |
| 53 | +`; |
| 54 | + |
| 55 | +const ToggleButton = styled.button` |
| 56 | + background: none; |
| 57 | + border: none; |
| 58 | + color: ${(props) => props.theme.textDim}; |
| 59 | + font-size: 12px; |
| 60 | + cursor: pointer; |
| 61 | + padding: 2px 4px; |
| 62 | +
|
| 63 | + &:hover { |
| 64 | + color: ${(props) => props.theme.text}; |
| 65 | + } |
| 66 | +`; |
| 67 | + |
| 68 | +export function ReferencedBy({ name }: { name: string }) { |
| 69 | + const { root, references } = useContext(DeclarationsContext); |
| 70 | + const [expanded, setExpanded] = useState(false); |
| 71 | + |
| 72 | + const refs = references.get(name); |
| 73 | + if (!refs || refs.length === 0) return null; |
| 74 | + |
| 75 | + const collapsible = refs.length > COLLAPSE_THRESHOLD; |
| 76 | + const visible = collapsible && !expanded ? refs.slice(0, COLLAPSE_THRESHOLD) : refs; |
| 77 | + |
| 78 | + return ( |
| 79 | + <Wrapper> |
| 80 | + <Title>Referenced by ({refs.length})</Title> |
| 81 | + <RefList> |
| 82 | + {visible.map((ref, i) => ( |
| 83 | + <RefLink key={`${ref.declarationName}-${ref.fieldName ?? ""}-${i}`} to={`${root}/${ref.declarationName}`}> |
| 84 | + <KindIcon kind={ref.declarationKind} size="small" /> |
| 85 | + <span>{ref.declarationName}{ref.fieldName && <RefField>{ref.fieldName}</RefField>}</span> |
| 86 | + </RefLink> |
| 87 | + ))} |
| 88 | + {collapsible && ( |
| 89 | + <ToggleButton onClick={() => setExpanded(!expanded)}> |
| 90 | + {expanded ? "show less" : `+${refs.length - COLLAPSE_THRESHOLD} more…`} |
| 91 | + </ToggleButton> |
| 92 | + )} |
| 93 | + </RefList> |
| 94 | + </Wrapper> |
| 95 | + ); |
| 96 | +} |
0 commit comments