|
| 1 | +import React, { useState, useRef, useEffect } from "react"; |
| 2 | +import { GitHubItem } from "./Contributions"; |
| 3 | +import { GITHUB_PAGE_LIMIT } from "./Contributors"; |
| 4 | + |
| 5 | +interface AccordionProps { |
| 6 | + title: string; |
| 7 | + isOpen: boolean; |
| 8 | + onToggle: () => void; |
| 9 | + data: GitHubItem[]; |
| 10 | + loadMore?: () => void; |
| 11 | + total_count: number; |
| 12 | + primaryText?: string; |
| 13 | + secondaryText?: string; |
| 14 | + mainBackgroundColor?: string; |
| 15 | +} |
| 16 | + |
| 17 | +export const Accordion: React.FC<AccordionProps> = ({ |
| 18 | + title, |
| 19 | + isOpen, |
| 20 | + onToggle, |
| 21 | + data, |
| 22 | + loadMore, |
| 23 | + total_count, |
| 24 | + primaryText, |
| 25 | + secondaryText, |
| 26 | + mainBackgroundColor, |
| 27 | +}) => { |
| 28 | + const [hoveredIndex, setHoveredIndex] = useState<number | null>(null); |
| 29 | + const [hoverLoadMore, setHoverLoadMore] = useState<boolean>(false); |
| 30 | + const [maxHeight, setMaxHeight] = useState<string>( |
| 31 | + isOpen ? "1000px" : "0px", |
| 32 | + ); |
| 33 | + |
| 34 | + const contentRef = useRef<HTMLDivElement>(null); |
| 35 | + |
| 36 | + React.useEffect(() => { |
| 37 | + setMaxHeight(isOpen ? "1000px" : "0px"); |
| 38 | + }, [isOpen]); |
| 39 | + |
| 40 | + useEffect(() => { |
| 41 | + if (contentRef.current && data.length > GITHUB_PAGE_LIMIT) { |
| 42 | + contentRef.current.scrollTo({ |
| 43 | + top: contentRef.current.scrollHeight, |
| 44 | + behavior: "smooth", |
| 45 | + }); |
| 46 | + } |
| 47 | + }, [data]); |
| 48 | + |
| 49 | + return ( |
| 50 | + <div |
| 51 | + style={{ |
| 52 | + display: "flex", |
| 53 | + flexDirection: "column", |
| 54 | + justifyContent: "center", |
| 55 | + borderRadius: "0.5rem", |
| 56 | + padding: "1rem", |
| 57 | + color: primaryText ?? "black", |
| 58 | + background: mainBackgroundColor ?? "", |
| 59 | + }} |
| 60 | + > |
| 61 | + <div |
| 62 | + onClick={onToggle} |
| 63 | + style={{ |
| 64 | + cursor: "pointer", |
| 65 | + fontWeight: "bold", |
| 66 | + fontSize: "1rem", |
| 67 | + display: "flex", |
| 68 | + alignItems: "center", |
| 69 | + justifyContent: "space-between", |
| 70 | + width: "100%", |
| 71 | + }} |
| 72 | + > |
| 73 | + <div>{title}</div> |
| 74 | + <div |
| 75 | + style={{ |
| 76 | + transform: isOpen ? "rotate(90deg)" : "rotate(0deg)", |
| 77 | + transition: "transform 0.3s ease", |
| 78 | + }} |
| 79 | + > |
| 80 | + {"▶"} |
| 81 | + </div> |
| 82 | + </div> |
| 83 | + <div |
| 84 | + ref={contentRef} |
| 85 | + style={{ |
| 86 | + maxHeight, |
| 87 | + overflow: "scroll", |
| 88 | + transition: isOpen ? "max-height 1s ease" : "", |
| 89 | + scrollbarWidth: "none", |
| 90 | + msOverflowStyle: "none", |
| 91 | + }} |
| 92 | + > |
| 93 | + <div |
| 94 | + style={{ |
| 95 | + display: "flex", |
| 96 | + flexDirection: "column", |
| 97 | + gap: "1rem", |
| 98 | + margin: "2rem 0 1rem 1rem", |
| 99 | + }} |
| 100 | + > |
| 101 | + {data.map((entry, index) => ( |
| 102 | + <div |
| 103 | + key={index} |
| 104 | + style={{ |
| 105 | + opacity: hoveredIndex === index ? 0.8 : 1.0, |
| 106 | + }} |
| 107 | + > |
| 108 | + <div |
| 109 | + style={{ |
| 110 | + display: "flex", |
| 111 | + flexDirection: "column", |
| 112 | + marginBottom: "0.5rem", |
| 113 | + cursor: "pointer", |
| 114 | + transition: "color 0.2s ease", |
| 115 | + }} |
| 116 | + onMouseEnter={() => setHoveredIndex(index)} |
| 117 | + onMouseLeave={() => setHoveredIndex(null)} |
| 118 | + onClick={() => |
| 119 | + window.open( |
| 120 | + entry.html_url, |
| 121 | + "_blank", |
| 122 | + "noopener,noreferrer", |
| 123 | + ) |
| 124 | + } |
| 125 | + > |
| 126 | + <div |
| 127 | + style={{ |
| 128 | + display: "flex", |
| 129 | + alignItems: "center", |
| 130 | + gap: "0.5rem", |
| 131 | + }} |
| 132 | + > |
| 133 | + {entry.bullet && ( |
| 134 | + <div |
| 135 | + style={{ |
| 136 | + width: "0.5rem", |
| 137 | + height: "0.5rem", |
| 138 | + borderRadius: "50%", |
| 139 | + backgroundColor: entry.bullet, |
| 140 | + }} |
| 141 | + ></div> |
| 142 | + )} |
| 143 | + <div>{entry.title}</div> |
| 144 | + </div> |
| 145 | + <div |
| 146 | + style={{ |
| 147 | + fontSize: "0.8rem", |
| 148 | + color: secondaryText ?? "gray", |
| 149 | + }} |
| 150 | + > |
| 151 | + {entry.created_at.split("T")[0]} |
| 152 | + </div> |
| 153 | + </div> |
| 154 | + </div> |
| 155 | + ))} |
| 156 | + </div> |
| 157 | + </div> |
| 158 | + {isOpen && loadMore && data.length < total_count && ( |
| 159 | + <div |
| 160 | + style={{ |
| 161 | + width: "100%", |
| 162 | + display: "flex", |
| 163 | + justifyContent: "center", |
| 164 | + }} |
| 165 | + > |
| 166 | + <span |
| 167 | + style={{ |
| 168 | + color: hoverLoadMore |
| 169 | + ? (secondaryText ?? "#3b82f6") |
| 170 | + : (primaryText ?? "black"), |
| 171 | + cursor: "pointer", |
| 172 | + }} |
| 173 | + onMouseEnter={() => setHoverLoadMore(true)} |
| 174 | + onMouseLeave={() => setHoverLoadMore(false)} |
| 175 | + onClick={loadMore} |
| 176 | + > |
| 177 | + Load more |
| 178 | + </span> |
| 179 | + </div> |
| 180 | + )} |
| 181 | + </div> |
| 182 | + ); |
| 183 | +}; |
0 commit comments