|
| 1 | +import styles from './Donations.module.scss' |
| 2 | +import { useEffect, useRef } from 'react' |
| 3 | +import cn from 'classnames' |
| 4 | +import { Scrollable } from '../Scrollable' |
| 5 | + |
| 6 | +const get_href_from_url_like_string = (text: string): string | null => { |
| 7 | + if (/\s/.test(text)) { |
| 8 | + return null |
| 9 | + } |
| 10 | + if (text.startsWith('http://') || text.startsWith('https://')) { |
| 11 | + try { |
| 12 | + return new URL(text).href |
| 13 | + } catch { |
| 14 | + return null |
| 15 | + } |
| 16 | + } |
| 17 | + if (text.includes('.') && !text.includes('@')) { |
| 18 | + try { |
| 19 | + return new URL(`https://${text}`).href |
| 20 | + } catch { |
| 21 | + return null |
| 22 | + } |
| 23 | + } |
| 24 | + return null |
| 25 | +} |
| 26 | + |
| 27 | +const parse_support_message = ( |
| 28 | + message: string |
| 29 | +): { |
| 30 | + username: string |
| 31 | + href: string | null |
| 32 | + after_text: string |
| 33 | +} => { |
| 34 | + const span_match = message.match( |
| 35 | + /<span class="suppUsername"[^>]*>([\s\S]*?)<\/span>/i |
| 36 | + ) |
| 37 | + |
| 38 | + if (span_match) { |
| 39 | + const span_content = span_match[1].trim() |
| 40 | + const after_text = (message.split('</span>')[1] ?? '').trim() |
| 41 | + const a_match = span_content.match( |
| 42 | + /<a[^>]*href="([^"]+)"[^>]*>([^<]+)<\/a>/i |
| 43 | + ) |
| 44 | + |
| 45 | + if (a_match) { |
| 46 | + const href = a_match[1] |
| 47 | + const username = a_match[2] |
| 48 | + return { username, href, after_text } |
| 49 | + } else { |
| 50 | + const username = span_content |
| 51 | + const href = get_href_from_url_like_string(username) |
| 52 | + return { username, href, after_text } |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + const bold_match = message.match(/<strong[^>]*>([^<]+)<\/strong>/i) |
| 57 | + if (bold_match) { |
| 58 | + const username = bold_match[1].trim() |
| 59 | + const after_text = (message.split('</strong>')[1] ?? '').trim() |
| 60 | + const href = get_href_from_url_like_string(username) |
| 61 | + return { username, href, after_text } |
| 62 | + } |
| 63 | + |
| 64 | + // Fallback for when no tags are found |
| 65 | + const username = message.trim() |
| 66 | + const href = get_href_from_url_like_string(username) |
| 67 | + return { username: href ? username : message, href, after_text: '' } |
| 68 | +} |
| 69 | + |
| 70 | +type Donation = { |
| 71 | + support_message: string |
| 72 | + support_note: string |
| 73 | +} |
| 74 | + |
| 75 | +export type DonationsProps = { |
| 76 | + donations?: Donation[] |
| 77 | + is_fetching?: boolean |
| 78 | + is_revalidating?: boolean |
| 79 | + on_fetch_next_page: () => void |
| 80 | + has_more?: boolean |
| 81 | +} |
| 82 | + |
| 83 | +export const Donations: React.FC<DonationsProps> = (props) => { |
| 84 | + const observer_target = useRef<HTMLDivElement>(null) |
| 85 | + |
| 86 | + useEffect(() => { |
| 87 | + if (!observer_target.current) return |
| 88 | + |
| 89 | + const observer = new IntersectionObserver( |
| 90 | + (entries) => { |
| 91 | + if (entries[0].isIntersecting) { |
| 92 | + props.on_fetch_next_page() |
| 93 | + } |
| 94 | + }, |
| 95 | + { threshold: 1.0 } |
| 96 | + ) |
| 97 | + |
| 98 | + const target = observer_target.current |
| 99 | + observer.observe(target) |
| 100 | + |
| 101 | + return () => observer.unobserve(target) |
| 102 | + }, [props.on_fetch_next_page]) |
| 103 | + |
| 104 | + return ( |
| 105 | + <Scrollable> |
| 106 | + <div className={styles.container}> |
| 107 | + <div> |
| 108 | + Hi there! If you enjoy using Code Web Chat, buying a $3 coffee is a |
| 109 | + great way to show your support for the project. |
| 110 | + </div> |
| 111 | + <div> |
| 112 | + {props.is_fetching ? ( |
| 113 | + <> |
| 114 | + <div className={styles.donations__donation}> |
| 115 | + <div |
| 116 | + className={styles.skeleton} |
| 117 | + style={{ width: '50%', height: '14px' }} |
| 118 | + /> |
| 119 | + <div className={styles.skeleton} style={{ height: '30px' }} /> |
| 120 | + </div> |
| 121 | + <div className={styles.donations__donation}> |
| 122 | + <div |
| 123 | + className={styles.skeleton} |
| 124 | + style={{ width: '70%', height: '14px' }} |
| 125 | + /> |
| 126 | + <div className={styles.skeleton} style={{ height: '30px' }} /> |
| 127 | + </div> |
| 128 | + <div className={styles.donations__donation}> |
| 129 | + <div |
| 130 | + className={styles.skeleton} |
| 131 | + style={{ width: '40%', height: '14px' }} |
| 132 | + /> |
| 133 | + <div className={styles.skeleton} style={{ height: '30px' }} /> |
| 134 | + </div> |
| 135 | + </> |
| 136 | + ) : ( |
| 137 | + <> |
| 138 | + <div |
| 139 | + className={cn(styles.donations, { |
| 140 | + [styles['donations--revalidating']]: props.is_revalidating |
| 141 | + })} |
| 142 | + > |
| 143 | + {props.donations?.map((donation, index) => ( |
| 144 | + <div className={styles.donations__donation} key={index}> |
| 145 | + {(() => { |
| 146 | + const { username, href, after_text } = |
| 147 | + parse_support_message(donation.support_message) |
| 148 | + const title = `${username} ${after_text}` |
| 149 | + return ( |
| 150 | + <div |
| 151 | + className={styles.donations__donation__header} |
| 152 | + title={title} |
| 153 | + > |
| 154 | + <strong> |
| 155 | + {href ? ( |
| 156 | + <a href={href} target="_blank" rel="nofollow"> |
| 157 | + {username} |
| 158 | + </a> |
| 159 | + ) : ( |
| 160 | + username |
| 161 | + )} |
| 162 | + </strong>{' '} |
| 163 | + {after_text} |
| 164 | + </div> |
| 165 | + ) |
| 166 | + })()} |
| 167 | + {donation.support_note && ( |
| 168 | + <div className={styles.donations__donation__note}> |
| 169 | + {donation.support_note} |
| 170 | + </div> |
| 171 | + )} |
| 172 | + </div> |
| 173 | + ))} |
| 174 | + </div> |
| 175 | + {props.has_more && <div ref={observer_target} />} |
| 176 | + </> |
| 177 | + )} |
| 178 | + </div> |
| 179 | + </div> |
| 180 | + </Scrollable> |
| 181 | + ) |
| 182 | +} |
0 commit comments