Skip to content

Commit 29c4bff

Browse files
Merge pull request #2 from DeveloperMK07/Homepage
Implement Home Page With TradingView Widgets
2 parents 7274433 + 7702d28 commit 29c4bff

4 files changed

Lines changed: 449 additions & 6 deletions

File tree

app/(root)/page.tsx

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,55 @@
1+
import TradingViewWidget from "@/components/TradingViewWidget";
12
import {Button} from "@/components/ui/button";
3+
import { HEATMAP_WIDGET_CONFIG, MARKET_DATA_WIDGET_CONFIG, MARKET_OVERVIEW_WIDGET_CONFIG, TOP_STORIES_WIDGET_CONFIG } from "@/lib/constants";
24

35
const Home = () => {
6+
7+
const scriptUrl =`https://s3.tradingview.com/external-embedding/embed-widget-`
48
return (
59
<div className="flex min-h-screen home-wrapper">
6-
Home
10+
<section className="grid w-full gap-8 home-section">
11+
<div className="md:col-span-1 xl:col-span-1">
12+
<TradingViewWidget
13+
title="Market Overview"
14+
scriptUrl={`${scriptUrl}market-overview.js`}
15+
config={MARKET_OVERVIEW_WIDGET_CONFIG}
16+
className="custom-chart"
17+
height={600}
18+
/>
19+
</div>
20+
21+
<div className="md-col-span xl:col-span-2">
22+
<TradingViewWidget
23+
title="Stock HeatMap"
24+
scriptUrl={`${scriptUrl}stock-heatmap.js`}
25+
config={HEATMAP_WIDGET_CONFIG}
26+
// className="custom-chart"
27+
height={600}
28+
/>
29+
</div>
30+
</section>
31+
32+
<section className="grid w-full gap-8 home-section">
33+
<div className="h-full md:col-span-1 xl:col-span-1">
34+
<TradingViewWidget
35+
// title="Market Overview"
36+
scriptUrl={`${scriptUrl}timeline.js`}
37+
config={TOP_STORIES_WIDGET_CONFIG}
38+
className="custom-chart"
39+
height={600}
40+
/>
41+
</div>
42+
43+
<div className="h-full md:col-span-1 xl:col-span-2">
44+
<TradingViewWidget
45+
// title="Stock HeatMap"
46+
scriptUrl={`${scriptUrl}market-quotes.js`}
47+
config={MARKET_DATA_WIDGET_CONFIG}
48+
// className="custom-chart"
49+
height={600}
50+
/>
51+
</div>
52+
</section>
753
</div>
854
)
955
}

components/TradingViewWidget.tsx

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// TradingViewWidget.jsx
2+
'use client';
3+
import useTradingViewWidget from '@/hooks/useTradingViewWidget';
4+
import { cn } from '@/lib/utils';
5+
6+
import React, { useEffect, useRef, memo } from 'react';
7+
8+
interface TradingViewWidgetProps{
9+
title?: string;
10+
scriptUrl: string;
11+
config: Record<string,unknown>;
12+
height?: number;
13+
className?: string;
14+
}
15+
16+
const TradingViewWidget = ({ title, scriptUrl, config, height = 600, className }: TradingViewWidgetProps) => {
17+
const containerRef = useTradingViewWidget(scriptUrl, config, height);
18+
19+
20+
21+
return (
22+
<div className="w-full">
23+
{title && <h3 className='font-semibold text-2xl text-gray-100 mb-5'>{title}</h3>}
24+
<div className={cn("tradingview-widget-container", className)} ref={containerRef} style={{ height: "100%", width: "100%" }}>
25+
<div className="tradingview-widget-container__widget" style={{ height, width: "100%" }}/>
26+
</div>
27+
</div>
28+
29+
);
30+
}
31+
32+
export default memo(TradingViewWidget);

hooks/useTradingViewWidget.tsx

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'use client';
2+
import { useEffect,useRef } from "react";
3+
4+
const useTradingViewWidget = (scriptUrl:string, config:Record<string,unknown>,height=600) => {
5+
const containerRef = useRef<HTMLDivElement | null>(null);
6+
7+
useEffect( () => {
8+
if(!containerRef.current) return;
9+
if(containerRef.current.dataset.loaded) return;
10+
containerRef.current.innerHTML = `<div class="trading-widget-container__widget style="width:100%; height:${height}px;"></div>`;
11+
12+
const script = document.createElement("script");
13+
script.src = scriptUrl;
14+
script.async = true;
15+
script.innerHTML = JSON.stringify(config);
16+
17+
containerRef.current.appendChild(script);
18+
containerRef.current.dataset.loaded='true';
19+
20+
return () => {
21+
if(containerRef.current){
22+
containerRef.current.innerHTML ='';
23+
delete containerRef.current.dataset.loaded;
24+
}
25+
}
26+
},[scriptUrl,config,height])
27+
28+
return containerRef;
29+
}
30+
31+
export default useTradingViewWidget

0 commit comments

Comments
 (0)