diff --git a/app/(root)/stocks/[symbol]/page.tsx b/app/(root)/stocks/[symbol]/page.tsx index de8d835..11874fd 100644 --- a/app/(root)/stocks/[symbol]/page.tsx +++ b/app/(root)/stocks/[symbol]/page.tsx @@ -12,9 +12,11 @@ import { import { auth } from '@/lib/better-auth/auth'; import { headers } from 'next/headers'; import { isStockInWatchlist } from '@/lib/actions/watchlist.actions'; +import { formatSymbolForTradingView } from '@/lib/utils'; export default async function StockDetails({ params }: StockDetailsPageProps) { const { symbol } = await params; + const tvSymbol = formatSymbolForTradingView(symbol); const scriptUrl = `https://s3.tradingview.com/external-embedding/embed-widget-`; const session = await auth.api.getSession({ @@ -30,13 +32,13 @@ export default async function StockDetails({ params }: StockDetailsPageProps) {
diff --git a/components/watchlist/TradingViewWatchlist.tsx b/components/watchlist/TradingViewWatchlist.tsx index 1e25426..2427cc7 100644 --- a/components/watchlist/TradingViewWatchlist.tsx +++ b/components/watchlist/TradingViewWatchlist.tsx @@ -1,7 +1,7 @@ "use client"; import React, { useEffect, useRef, memo } from 'react'; -import { useTheme } from "next-themes"; +import { formatSymbolForTradingView } from '@/lib/utils'; interface TradingViewWatchlistProps { symbols: string[]; @@ -26,7 +26,7 @@ function TradingViewWatchlist({ symbols }: TradingViewWatchlistProps) { // Since we don't have exchange data easily, we'll try raw symbol. // Ideally we'd prefix "NASDAQ:" or "NYSE:" but let's test without first. const symbolList = symbols.map(s => ({ - name: s, + name: formatSymbolForTradingView(s), displayName: s })); diff --git a/lib/utils.ts b/lib/utils.ts index 6b0b702..183dab7 100644 --- a/lib/utils.ts +++ b/lib/utils.ts @@ -152,4 +152,26 @@ export const getFormattedTodayDate = () => new Date().toLocaleDateString('en-US' month: 'long', day: 'numeric', timeZone: 'UTC', -}); \ No newline at end of file +}); + +export function formatSymbolForTradingView(symbol: string): string { + if (!symbol) return ''; + const upperSymbol = symbol.toUpperCase(); + + // Shanghai + if (upperSymbol.endsWith('.SS')) { + return `SSE:${upperSymbol.slice(0, -3)}`; + } + + // Shenzhen + if (upperSymbol.endsWith('.SZ')) { + return `SZSE:${upperSymbol.slice(0, -3)}`; + } + + // Hong Kong + if (upperSymbol.endsWith('.HK')) { + return `HKEX:${upperSymbol.slice(0, -3)}`; + } + + return upperSymbol; +} \ No newline at end of file