-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
130 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
"use client"; | ||
|
||
import { createContext, type PropsWithChildren, useContext } from "react"; | ||
|
||
export const AptPriceContext = createContext<number | undefined>(undefined); | ||
|
||
export function AptPriceContextProvider({ | ||
aptPrice, | ||
children, | ||
}: PropsWithChildren<{ aptPrice: number }>) { | ||
return <AptPriceContext.Provider value={aptPrice}>{children}</AptPriceContext.Provider>; | ||
} | ||
|
||
export const useAptPrice = (): number | undefined => { | ||
const context = useContext(AptPriceContext); | ||
if (context === null) { | ||
throw new Error("useAptPrice must be used within a AptPriceContext."); | ||
} | ||
return context; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
"use server"; | ||
|
||
import { COINMARKETCAP_API_KEY } from "lib/server-env"; | ||
import { unstable_cache } from "next/cache"; | ||
|
||
const COINMARKETCAP_APT_ID = "21794"; | ||
const COINMARKETCAP_ROOT_URL = "https://pro-api.coinmarketcap.com"; | ||
const COINMARKETCAP_ENDPOINT = "v2/cryptocurrency/quotes/latest"; | ||
|
||
export const getAptPrice = unstable_cache( | ||
async () => | ||
fetch(`${COINMARKETCAP_ROOT_URL}/${COINMARKETCAP_ENDPOINT}?id=${COINMARKETCAP_APT_ID}`, { | ||
headers: { | ||
Accept: "application/json", | ||
"X-CMC_PRO_API_KEY": COINMARKETCAP_API_KEY, | ||
}, | ||
}) | ||
.then((r) => r.json()) | ||
.then((r) => r.data[COINMARKETCAP_APT_ID].quote.USD.price as number), | ||
["apt-price"], | ||
{ revalidate: 60 * 10 } // Ten minutes | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters