|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import {ComponentProps, Fragment, useContext, useState} from 'react'; |
| 4 | +import {createPortal} from 'react-dom'; |
| 5 | +import {usePopper} from 'react-popper'; |
| 6 | +import {AnimatePresence} from 'framer-motion'; |
| 7 | +import {useTheme} from 'next-themes'; |
| 8 | + |
| 9 | +import {useOnClickOutside} from 'sentry-docs/clientUtils'; |
| 10 | +import {useIsMounted} from 'sentry-docs/hooks/isMounted'; |
| 11 | + |
| 12 | +import {CodeContext} from '../codeContext'; |
| 13 | + |
| 14 | +import {AnimatedContainer} from './animatedContainer'; |
| 15 | +import {Keyword} from './keyword'; |
| 16 | +import { |
| 17 | + Arrow, |
| 18 | + Dropdown, |
| 19 | + ItemButton, |
| 20 | + KeywordDropdown, |
| 21 | + KeywordIndicator, |
| 22 | + KeywordSearchInput, |
| 23 | + PositionWrapper, |
| 24 | + Selections, |
| 25 | +} from './styles.css'; |
| 26 | +import {dropdownPopperOptions} from './utils'; |
| 27 | + |
| 28 | +type KeywordSelectorProps = { |
| 29 | + group: string; |
| 30 | + index: number; |
| 31 | + keyword: string; |
| 32 | +}; |
| 33 | + |
| 34 | +export function KeywordSelector({keyword, group, index}: KeywordSelectorProps) { |
| 35 | + const [isOpen, setIsOpen] = useState(false); |
| 36 | + const [referenceEl, setReferenceEl] = useState<HTMLSpanElement | null>(null); |
| 37 | + const [dropdownEl, setDropdownEl] = useState<HTMLElement | null>(null); |
| 38 | + const [isAnimating, setIsAnimating] = useState(false); |
| 39 | + const [orgFilter, setOrgFilter] = useState(''); |
| 40 | + const {theme} = useTheme(); |
| 41 | + const isDarkMode = theme === 'dark'; |
| 42 | + const {isMounted} = useIsMounted(); |
| 43 | + |
| 44 | + const {styles, state, attributes} = usePopper( |
| 45 | + referenceEl, |
| 46 | + dropdownEl, |
| 47 | + dropdownPopperOptions |
| 48 | + ); |
| 49 | + |
| 50 | + useOnClickOutside({ |
| 51 | + ref: {current: referenceEl}, |
| 52 | + enabled: isOpen, |
| 53 | + handler: () => setIsOpen(false), |
| 54 | + }); |
| 55 | + |
| 56 | + const codeContext = useContext(CodeContext); |
| 57 | + if (!codeContext) { |
| 58 | + return null; |
| 59 | + } |
| 60 | + |
| 61 | + const [sharedSelection, setSharedSelection] = codeContext.sharedKeywordSelection; |
| 62 | + |
| 63 | + const {codeKeywords} = codeContext; |
| 64 | + const choices = codeKeywords?.[group] ?? []; |
| 65 | + const currentSelectionIdx = sharedSelection[group] ?? 0; |
| 66 | + const currentSelection = choices[currentSelectionIdx]; |
| 67 | + |
| 68 | + if (!currentSelection) { |
| 69 | + return <Fragment>keyword</Fragment>; |
| 70 | + } |
| 71 | + |
| 72 | + const selector = isOpen && ( |
| 73 | + <PositionWrapper style={styles.popper} ref={setDropdownEl} {...attributes.popper}> |
| 74 | + <AnimatedContainer> |
| 75 | + <Dropdown dark={isDarkMode}> |
| 76 | + <Arrow |
| 77 | + style={styles.arrow} |
| 78 | + data-placement={state?.placement} |
| 79 | + data-popper-arrow |
| 80 | + /> |
| 81 | + {choices.length > 5 && ( |
| 82 | + <KeywordSearchInput |
| 83 | + placeholder="Search Project" |
| 84 | + onClick={e => e.stopPropagation()} |
| 85 | + value={orgFilter} |
| 86 | + onChange={e => setOrgFilter(e.target.value)} |
| 87 | + dark={isDarkMode} |
| 88 | + /> |
| 89 | + )} |
| 90 | + <Selections> |
| 91 | + {choices |
| 92 | + .filter(({title}) => { |
| 93 | + return title.includes(orgFilter); |
| 94 | + }) |
| 95 | + .map((item, idx) => { |
| 96 | + const isActive = idx === currentSelectionIdx; |
| 97 | + return ( |
| 98 | + <ItemButton |
| 99 | + data-sentry-mask |
| 100 | + key={idx} |
| 101 | + isActive={isActive} |
| 102 | + onClick={() => { |
| 103 | + const newSharedSelection = {...sharedSelection}; |
| 104 | + newSharedSelection[group] = idx; |
| 105 | + setSharedSelection(newSharedSelection); |
| 106 | + setIsOpen(false); |
| 107 | + }} |
| 108 | + dark={isDarkMode} |
| 109 | + > |
| 110 | + {item.title} |
| 111 | + </ItemButton> |
| 112 | + ); |
| 113 | + })} |
| 114 | + </Selections> |
| 115 | + </Dropdown> |
| 116 | + </AnimatedContainer> |
| 117 | + </PositionWrapper> |
| 118 | + ); |
| 119 | + |
| 120 | + return ( |
| 121 | + <Fragment> |
| 122 | + <KeywordDropdown |
| 123 | + key={index} |
| 124 | + ref={setReferenceEl} |
| 125 | + role="button" |
| 126 | + tabIndex={0} |
| 127 | + title={currentSelection?.title} |
| 128 | + onClick={() => setIsOpen(!isOpen)} |
| 129 | + onKeyDown={e => e.key === 'Enter' && setIsOpen(!isOpen)} |
| 130 | + > |
| 131 | + <KeywordIndicatorComponent isOpen={isOpen} /> |
| 132 | + <span |
| 133 | + style={{ |
| 134 | + // We set inline-grid only when animating the keyword so they |
| 135 | + // correctly overlap during animations, but this must be removed |
| 136 | + // after so copy-paste correctly works. |
| 137 | + display: isAnimating ? 'inline-grid' : undefined, |
| 138 | + }} |
| 139 | + > |
| 140 | + <AnimatePresence initial={false}> |
| 141 | + <Keyword |
| 142 | + onAnimationStart={() => setIsAnimating(true)} |
| 143 | + onAnimationComplete={() => setIsAnimating(false)} |
| 144 | + key={currentSelectionIdx} |
| 145 | + > |
| 146 | + {currentSelection[keyword]} |
| 147 | + </Keyword> |
| 148 | + </AnimatePresence> |
| 149 | + </span> |
| 150 | + </KeywordDropdown> |
| 151 | + {isMounted && |
| 152 | + createPortal(<AnimatePresence>{selector}</AnimatePresence>, document.body)} |
| 153 | + </Fragment> |
| 154 | + ); |
| 155 | +} |
| 156 | + |
| 157 | +function KeywordIndicatorComponent({ |
| 158 | + isOpen, |
| 159 | + size = '12px', |
| 160 | + ...props |
| 161 | +}: ComponentProps<typeof KeywordIndicator>) { |
| 162 | + return <KeywordIndicator isOpen={isOpen} size={size} {...props} />; |
| 163 | +} |
0 commit comments