generated from Blazity/next-enterprise
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from Blazity/stock-remote-source
Stock remote source
- Loading branch information
Showing
8 changed files
with
151 additions
and
9 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
8 changes: 8 additions & 0 deletions
8
src/components/RecentArticles/RecentArticlesInfiniteDynamic.tsx
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,8 @@ | ||
"use client" | ||
|
||
import dynamic from "next/dynamic" | ||
import type { RecentArticlesInfiniteProps } from "./RecentArticlesInfinite" | ||
|
||
export const RecentArticlesInfiniteDynamic = dynamic<RecentArticlesInfiniteProps>(() => | ||
import("./RecentArticlesInfinite").then((mod) => mod.default) | ||
) |
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,51 @@ | ||
import { useMemo } from "react" | ||
import { z } from "zod" | ||
import { HygraphClient } from "hygraphClient" | ||
import { StockDisplayRenderer } from "./StockDisplayRenderer" | ||
|
||
type StockQuoteBase = { | ||
id: string | ||
name: string | ||
} | ||
type ValidStockQuote = StockQuoteBase & { quote: AlphaVantageQuote } | ||
|
||
type AlphaVantageQuote = { | ||
"Global Quote": { | ||
"10. change percent": number | ||
} | ||
} | ||
|
||
const SYMBOLS_TO_FETCH = ["SPY", "AAPL", "AMZN", "GOOGL", "MSFT"] | ||
|
||
export async function StockDisplay() { | ||
const { getStockDailyQuotes } = HygraphClient() | ||
const { stockDailyQuotes: quotes } = await getStockDailyQuotes( | ||
{ symbols: SYMBOLS_TO_FETCH }, | ||
{ next: { revalidate: 60 * 60 } } | ||
) | ||
|
||
const validStockQuotes = useMemo( | ||
() => | ||
quotes | ||
.map((stockQuote) => ({ ...stockQuote, quote: validateQuote(stockQuote.quote) })) | ||
.filter((stockQuote): stockQuote is ValidStockQuote => stockQuote.quote !== null) | ||
.map(({ quote, ...stockQuoteProps }) => ({ | ||
id: stockQuoteProps.id, | ||
name: stockQuoteProps.name, | ||
changePercent: quote["Global Quote"]["10. change percent"], | ||
})), | ||
[quotes] | ||
) | ||
|
||
return <StockDisplayRenderer quotes={validStockQuotes} /> | ||
} | ||
|
||
function validateQuote(stockQuote: unknown) { | ||
const result = quoteSchema.safeParse(stockQuote) | ||
if (!result.success) return null | ||
return result.data | ||
} | ||
|
||
const quoteSchema = z.object({ | ||
"Global Quote": z.object({ "10. change percent": z.string().transform((val) => parseFloat(val.slice(0, -1))) }), | ||
}) |
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,53 @@ | ||
"use client" | ||
|
||
import { MoveDown, MoveUp } from "lucide-react" | ||
import { useEffect, useState } from "react" | ||
import { cn } from "utils/cn" | ||
|
||
type StockDisplayRendererProps = { | ||
quotes: { name: string; id: string; changePercent: number }[] | ||
} | ||
|
||
export function StockDisplayRenderer({ quotes }: StockDisplayRendererProps) { | ||
const [currentIndex, setCurrentIndex] = useState(0) | ||
const [visible, setVisible] = useState<boolean>(true) | ||
|
||
useEffect(() => { | ||
const interval = setInterval(() => { | ||
setVisible(false) | ||
}, 4000) | ||
|
||
return () => { | ||
clearInterval(interval) | ||
} | ||
}, []) | ||
|
||
useEffect(() => { | ||
if (visible) return | ||
const timeoutId = setTimeout(() => { | ||
setCurrentIndex((prevIndex) => (prevIndex + 1) % quotes.length) | ||
setVisible(true) | ||
}, 1000) | ||
|
||
return () => { | ||
clearTimeout(timeoutId) | ||
} | ||
}, [visible, quotes.length]) | ||
|
||
const currentQuote = quotes[currentIndex] | ||
|
||
if (!currentQuote) return | ||
|
||
const isNegative = currentQuote.changePercent < 0 | ||
|
||
return ( | ||
<div className={cn("flex gap-4 text-sm transition-opacity duration-1000", visible ? "opacity-100" : "opacity-0")}> | ||
{currentQuote.name} | ||
<span className={cn("flex items-center gap-1", isNegative ? "text-red-800" : "text-green-800")}> | ||
{!isNegative && "+"} | ||
{currentQuote.changePercent.toFixed(2)}% | ||
{isNegative ? <MoveDown className="mt-0.5 h-3 w-3" /> : <MoveUp className="mt-0.5 h-3 w-3" />} | ||
</span> | ||
</div> | ||
) | ||
} |
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