|
| 1 | +import { Box, Flex, IconButton } from 'theme-ui' |
| 2 | +import { Row, Column, Input, Button } from '@carbonplan/components' |
| 3 | +import { RotatingArrow, Search, X } from '@carbonplan/icons' |
| 4 | +import { useEffect, useState } from 'react' |
| 5 | +import { format } from 'd3-format' |
| 6 | +import useSWR from 'swr' |
| 7 | + |
| 8 | +export const formatValue = (value) => { |
| 9 | + if (value < 1000) { |
| 10 | + return value |
| 11 | + } else { |
| 12 | + let result = format('.3s')(value) |
| 13 | + if (value >= 1e9) { |
| 14 | + return result.replace('G', 'B') |
| 15 | + } |
| 16 | + return result.toUpperCase(0) |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +const sx = { |
| 21 | + row: { |
| 22 | + borderStyle: 'solid', |
| 23 | + borderWidth: '0px', |
| 24 | + borderTopWidth: '1px', |
| 25 | + borderColor: 'muted', |
| 26 | + height: '100%', |
| 27 | + py: [3, 3, 3, 4], |
| 28 | + }, |
| 29 | + label: { |
| 30 | + fontFamily: 'heading', |
| 31 | + letterSpacing: 'smallcaps', |
| 32 | + textTransform: 'uppercase', |
| 33 | + fontSize: [2, 2, 2, 3], |
| 34 | + }, |
| 35 | + value: { |
| 36 | + fontFamily: 'faux', |
| 37 | + letterSpacing: 'faux', |
| 38 | + textTransform: 'uppercase', |
| 39 | + color: 'purple', |
| 40 | + fontSize: [3, 3, 3, 4], |
| 41 | + }, |
| 42 | +} |
| 43 | + |
| 44 | +async function getTransactions([, search]) { |
| 45 | + try { |
| 46 | + const reqUrl = new URL( |
| 47 | + `https://carbonplan.org/research/offsets-db/api/query` |
| 48 | + ) |
| 49 | + const params = new URLSearchParams() |
| 50 | + params.append('path', 'credits') |
| 51 | + params.append('per_page', 1) |
| 52 | + params.append('transaction_type', 'retirement') |
| 53 | + if (search) { |
| 54 | + params.append('beneficiary_search', search.trim()) |
| 55 | + params.append( |
| 56 | + 'beneficiary_search_fields', |
| 57 | + 'retirement_beneficiary_harmonized' |
| 58 | + ) |
| 59 | + params.append('beneficiary_search_fields', 'retirement_account') |
| 60 | + params.append('beneficiary_search_fields', 'retirement_beneficiary') |
| 61 | + params.append('beneficiary_search_fields', 'retirement_note') |
| 62 | + params.append('beneficiary_search_fields', 'retirement_reason') |
| 63 | + } |
| 64 | + reqUrl.search = params.toString() |
| 65 | + const serverRes = await fetch(reqUrl, { |
| 66 | + credentials: 'include', |
| 67 | + }) |
| 68 | + if (!serverRes.ok) { |
| 69 | + throw new Error( |
| 70 | + `API request failed: ${serverRes.status} ${serverRes.statusText}` |
| 71 | + ) |
| 72 | + } |
| 73 | + const result = await serverRes.json() |
| 74 | + return result.pagination.total_entries |
| 75 | + } catch (e) { |
| 76 | + throw new Error('Unexpected error') |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +export const useDebounce = (value, delay = 100) => { |
| 81 | + const [debounced, setDebounced] = useState(value) |
| 82 | + |
| 83 | + useEffect(() => { |
| 84 | + const timeoutID = setTimeout(() => { |
| 85 | + setDebounced(value) |
| 86 | + }, delay) |
| 87 | + |
| 88 | + return () => { |
| 89 | + clearTimeout(timeoutID) |
| 90 | + } |
| 91 | + }, [value, delay]) |
| 92 | + |
| 93 | + return debounced |
| 94 | +} |
| 95 | + |
| 96 | +const BeneficiarySearch = () => { |
| 97 | + const [search, setSearch] = useState('Delta Airlines') |
| 98 | + const { data: transactions, isLoading } = useSWR( |
| 99 | + ['/credits', useDebounce(search)], |
| 100 | + getTransactions, |
| 101 | + { |
| 102 | + revalidateOnFocus: false, |
| 103 | + revalidateIfStale: false, |
| 104 | + } |
| 105 | + ) |
| 106 | + return ( |
| 107 | + <> |
| 108 | + <Row columns={6} sx={sx.row}> |
| 109 | + <Column start={1} width={[3, 2, 2, 2]}> |
| 110 | + <Box sx={sx.label}> |
| 111 | + <Flex |
| 112 | + as='label' |
| 113 | + htmlFor='beneficiary_search' |
| 114 | + sx={{ gap: 2, alignItems: 'center' }} |
| 115 | + > |
| 116 | + User <Search sx={{ width: 14 }} /> |
| 117 | + </Flex> |
| 118 | + </Box> |
| 119 | + </Column> |
| 120 | + <Column start={[4, 3, 3, 3]} width={[3, 4, 4, 4]}> |
| 121 | + <Flex sx={{ justifyContent: 'space-between' }}> |
| 122 | + <Input |
| 123 | + id='beneficiary_search' |
| 124 | + placeholder='enter a search term' |
| 125 | + color='purple' |
| 126 | + value={search} |
| 127 | + onChange={(e) => setSearch(e.target.value)} |
| 128 | + sx={{ |
| 129 | + ...(search ? sx.value : { fontFamily: 'mono', fontSize: 2 }), |
| 130 | + height: '22px', |
| 131 | + textTransform: 'none', |
| 132 | + borderBottom: 0, |
| 133 | + borderColor: 'muted', |
| 134 | + flexGrow: 1, |
| 135 | + }} |
| 136 | + /> |
| 137 | + <IconButton |
| 138 | + sx={{ |
| 139 | + cursor: 'pointer', |
| 140 | + p: [0], |
| 141 | + mt: ['3px', '3px', '3px', '4px'], |
| 142 | + mx: [2], |
| 143 | + width: 18, |
| 144 | + height: 18, |
| 145 | + flexShrink: 0, |
| 146 | + }} |
| 147 | + onKeyDown={(e) => { |
| 148 | + if (e.key === 'Enter') { |
| 149 | + setSearch('') |
| 150 | + } |
| 151 | + }} |
| 152 | + onClick={() => { |
| 153 | + setSearch('') |
| 154 | + }} |
| 155 | + > |
| 156 | + <X |
| 157 | + sx={{ |
| 158 | + strokeWidth: 1.5, |
| 159 | + color: 'secondary', |
| 160 | + transition: 'stroke 0.15s', |
| 161 | + '@media (hover: hover) and (pointer: fine)': { |
| 162 | + '&:hover': { |
| 163 | + stroke: 'primary', |
| 164 | + }, |
| 165 | + }, |
| 166 | + }} |
| 167 | + /> |
| 168 | + </IconButton> |
| 169 | + </Flex> |
| 170 | + </Column> |
| 171 | + </Row> |
| 172 | + <Row columns={6} sx={sx.row}> |
| 173 | + <Column start={1} width={[3, 2, 2, 2]}> |
| 174 | + <Box sx={sx.label}>Retirements</Box> |
| 175 | + </Column> |
| 176 | + <Column start={[4, 3, 3, 3]} width={[3, 4, 4, 4]}> |
| 177 | + <Flex |
| 178 | + sx={{ |
| 179 | + height: '100%', |
| 180 | + alignItems: 'baseline', |
| 181 | + gap: 2, |
| 182 | + mt: [0, 0, 0, '-6px'], |
| 183 | + }} |
| 184 | + > |
| 185 | + <Box sx={sx.value}> |
| 186 | + {!isLoading && typeof transactions === 'number' |
| 187 | + ? formatValue(transactions) |
| 188 | + : '—'} |
| 189 | + </Box> |
| 190 | + <Box sx={{ fontFamily: 'mono' }}> |
| 191 | + {transactions === 1 ? 'transaction' : 'transactions'} |
| 192 | + </Box> |
| 193 | + </Flex> |
| 194 | + </Column> |
| 195 | + </Row> |
| 196 | + <Row columns={6} sx={{ ...sx.row, pb: 0 }}> |
| 197 | + <Column start={[4, 3, 3, 3]} width={[3, 4, 4, 4]}> |
| 198 | + <Button |
| 199 | + suffix={<RotatingArrow />} |
| 200 | + size='xs' |
| 201 | + inverted |
| 202 | + href={`https://carbonplan.org/research/offsets-db/transactions${ |
| 203 | + search ? `?beneficiary=${search}` : '' |
| 204 | + }`} |
| 205 | + > |
| 206 | + View in OffsetsDB |
| 207 | + </Button> |
| 208 | + </Column> |
| 209 | + </Row> |
| 210 | + </> |
| 211 | + ) |
| 212 | +} |
| 213 | + |
| 214 | +export default BeneficiarySearch |
0 commit comments