11import { useCallback , useEffect , useState , useRef } from 'react'
22import axios from 'axios'
33
4- type Donation = {
4+ type Coffee = {
55 support_message : string
66 support_note : string
77}
88
9- export const use_latest_donations = ( ) => {
10- const [ viewing_donations , set_viewing_donations ] = useState ( false )
11- const [ donations , set_donations ] = useState < Donation [ ] > ( [ ] )
9+ export const use_recent_coffees = ( ) => {
10+ const [ viewing_coffees , set_viewing_coffees ] = useState ( false )
11+ const [ coffees , set_coffees ] = useState < Coffee [ ] > ( [ ] )
1212 const [ is_fetching , set_is_fetching ] = useState ( false )
1313 const [ is_revalidating , set_is_revalidating ] = useState ( false )
1414 const [ fetched_once , set_fetched_once ] = useState ( false )
1515 const [ page , set_page ] = useState ( 1 )
1616 const [ is_fetching_next , set_is_fetching_next ] = useState ( false )
1717 const [ has_more , set_has_more ] = useState ( true )
18- const cached_donations = useRef < Donation [ ] > ( [ ] )
18+ const cached_coffees = useRef < Coffee [ ] > ( [ ] )
1919
20- const fetch_donations = useCallback ( async ( is_revalidation = false ) => {
20+ const fetch_coffees = useCallback ( async ( is_revalidation = false ) => {
2121 try {
2222 if ( is_revalidation ) {
2323 set_is_revalidating ( true )
@@ -30,23 +30,23 @@ export const use_latest_donations = () => {
3030 )
3131
3232 if ( response . data ?. data ) {
33- const new_donations = response . data . data . map ( ( raw : any ) => ( {
33+ const new_coffees = response . data . data . map ( ( raw : any ) => ( {
3434 support_message : raw . support_message ,
3535 support_note : raw . support_note ?? ''
3636 } ) )
3737
38- set_donations ( new_donations )
39- cached_donations . current = new_donations
38+ set_coffees ( new_coffees )
39+ cached_coffees . current = new_coffees
4040 set_page ( 2 )
4141
42- if ( new_donations . length < 10 ) {
42+ if ( new_coffees . length < 10 ) {
4343 set_has_more ( false )
4444 } else {
4545 set_has_more ( true )
4646 }
4747 }
4848 } catch ( error ) {
49- console . error ( 'Failed to fetch donations ' , error )
49+ console . error ( 'Failed to fetch coffees ' , error )
5050 } finally {
5151 if ( is_revalidation ) {
5252 set_is_revalidating ( false )
@@ -58,23 +58,23 @@ export const use_latest_donations = () => {
5858 } , [ ] )
5959
6060 useEffect ( ( ) => {
61- if ( ! viewing_donations ) {
61+ if ( ! viewing_coffees ) {
6262 set_page ( 1 )
6363 set_has_more ( true )
6464 return
6565 }
6666
6767 // Stale-while-revalidate strategy
68- if ( fetched_once && cached_donations . current . length > 0 ) {
68+ if ( fetched_once && cached_coffees . current . length > 0 ) {
6969 // Show stale data immediately
70- set_donations ( cached_donations . current )
70+ set_coffees ( cached_coffees . current )
7171 // Then revalidate in background
72- fetch_donations ( true )
72+ fetch_coffees ( true )
7373 } else {
7474 // First time fetch
75- fetch_donations ( false )
75+ fetch_coffees ( false )
7676 }
77- } , [ viewing_donations , fetch_donations ] )
77+ } , [ viewing_coffees , fetch_coffees ] )
7878
7979 const on_fetch_next_page = useCallback ( async ( ) => {
8080 if (
@@ -92,26 +92,26 @@ export const use_latest_donations = () => {
9292 `https://app.buymeacoffee.com/api/creators/slug/robertpiosik/coffees?web=1&page=${ page } &per_page=10`
9393 )
9494 if ( response . data ?. data ) {
95- const new_donations = response . data . data . map ( ( raw : any ) => ( {
95+ const new_coffees = response . data . data . map ( ( raw : any ) => ( {
9696 support_message : raw . support_message ,
9797 support_note : raw . support_note ?? ''
9898 } ) )
99- if ( new_donations . length > 0 ) {
100- set_donations ( ( prev ) => {
101- const updated = [ ...prev , ...new_donations ]
102- cached_donations . current = updated
99+ if ( new_coffees . length > 0 ) {
100+ set_coffees ( ( prev ) => {
101+ const updated = [ ...prev , ...new_coffees ]
102+ cached_coffees . current = updated
103103 return updated
104104 } )
105105 set_page ( ( prev ) => prev + 1 )
106106 }
107- if ( new_donations . length < 10 ) {
107+ if ( new_coffees . length < 10 ) {
108108 set_has_more ( false )
109109 }
110110 } else {
111111 set_has_more ( false )
112112 }
113113 } catch ( error ) {
114- console . error ( 'Failed to fetch donations ' , error )
114+ console . error ( 'Failed to fetch coffees ' , error )
115115 set_has_more ( false )
116116 } finally {
117117 set_is_fetching_next ( false )
@@ -126,12 +126,12 @@ export const use_latest_donations = () => {
126126 ] )
127127
128128 return {
129- viewing_donations ,
130- set_viewing_donations ,
131- donations ,
129+ viewing_coffees ,
130+ set_viewing_coffees ,
131+ coffees ,
132132 is_fetching,
133133 is_revalidating,
134- donations_fetched_once : fetched_once ,
134+ coffees_fetched_once : fetched_once ,
135135 on_fetch_next_page,
136136 has_more
137137 }
0 commit comments