-
Notifications
You must be signed in to change notification settings - Fork 0
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 #14 from ajesuscode/10-add-tide-visual-graph-compo…
…nent feat: added tide visual display
- Loading branch information
Showing
9 changed files
with
730 additions
and
21 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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,27 @@ | ||
"use client"; | ||
import React, { useState } from "react"; | ||
import { RisingTideIcon, FallinTideIcon } from "../icons/icons"; | ||
|
||
interface CurrentTideProps { | ||
currentTide: { tide: string; time: string } | null; | ||
} | ||
|
||
const CurrentTide = ({ currentTide }: CurrentTideProps) => { | ||
return ( | ||
<div className="flex flex-row gap-0 justify-start items-center bg-dark/50 rounded-sm p-2"> | ||
{currentTide?.tide === "rising" ? ( | ||
<RisingTideIcon size={20} color="text-light/50" /> | ||
) : ( | ||
<FallinTideIcon size={20} color="text-light/50" /> | ||
)} | ||
<span className="text-light/50 font-body text-sm pr-2"> | ||
{currentTide?.tide || null} | ||
</span> | ||
<span className="text-light/50 font-body text-sm"> | ||
{currentTide?.time || null} | ||
</span> | ||
</div> | ||
); | ||
}; | ||
|
||
export default CurrentTide; |
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,30 @@ | ||
import React from "react"; | ||
import { DateTime } from "luxon"; | ||
import { getTidesData } from "@/app/utils/surfUtils"; | ||
import TideVisualGraph from "./TideVisualGraph"; | ||
|
||
type TidesType = { | ||
height: number; | ||
id: number; | ||
time: number; | ||
type: string; | ||
}; | ||
|
||
const TideContainer = async () => { | ||
const tidesData = await getTidesData(); | ||
let newTides: TidesType[] = []; | ||
if (tidesData) { | ||
newTides = tidesData.map((item) => ({ | ||
...item, | ||
time: DateTime.fromISO(item.time).toMillis(), | ||
})); | ||
} | ||
|
||
return ( | ||
<> | ||
<TideVisualGraph tides={newTides} /> | ||
</> | ||
); | ||
}; | ||
|
||
export default TideContainer; |
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,117 @@ | ||
"use client"; | ||
import React from "react"; | ||
import { DateTime } from "luxon"; | ||
import { getTidesData } from "@/app/utils/surfUtils"; | ||
import { | ||
LineChart, | ||
Line, | ||
XAxis, | ||
YAxis, | ||
CartesianGrid, | ||
Tooltip, | ||
ResponsiveContainer, | ||
Label, | ||
ReferenceLine, | ||
Area, | ||
AreaChart, | ||
} from "recharts"; | ||
|
||
interface TideVisualGraphProps { | ||
tides: { | ||
height: number; | ||
id: number; | ||
time: number; | ||
type: string; | ||
}[]; | ||
} | ||
|
||
const TideVisualGraph = ({ tides }: TideVisualGraphProps) => { | ||
let isMobile = false; | ||
if (typeof window !== "undefined") { | ||
const screenWidth = window.screen.width; | ||
if (screenWidth < 768) { | ||
isMobile = true; | ||
} | ||
} | ||
|
||
const firstDayTides = tides.slice(0, 5).map((tide) => { | ||
if (tide.height < 0) { | ||
return { ...tide, height: 0.2 }; | ||
} else if (tide.height > 0) { | ||
return { ...tide, height: 1 }; | ||
} else { | ||
return tide; | ||
} | ||
}); | ||
|
||
const startHour = DateTime.fromMillis(firstDayTides[0].time).startOf( | ||
"hour" | ||
); | ||
const endHour = DateTime.fromMillis( | ||
firstDayTides[firstDayTides.length - 1].time | ||
).endOf("hour"); | ||
|
||
const currentTime = DateTime.local().toMillis(); | ||
|
||
const hourlyTimestamps: number[] = []; | ||
|
||
for ( | ||
let hour = startHour; | ||
hour <= endHour; | ||
hour = hour.plus({ hours: 1 }) | ||
) { | ||
hourlyTimestamps.push(hour.toMillis()); | ||
} | ||
|
||
return ( | ||
<div className="p-4 bg-light/20 rounded-md"> | ||
<ResponsiveContainer | ||
width="100%" | ||
height={120} | ||
style={{ marginTop: 20 }} | ||
> | ||
<AreaChart | ||
data={firstDayTides} | ||
margin={{ top: 5, right: 10, left: 10, bottom: 0 }} | ||
> | ||
<XAxis | ||
dataKey="time" | ||
scale="time" | ||
type="number" | ||
domain={["auto", "auto"]} | ||
ticks={hourlyTimestamps} | ||
tick={{ | ||
stroke: "#c7d2fe", | ||
strokeWidth: 1, | ||
fontSize: 8, | ||
}} | ||
tickFormatter={ | ||
(time) => DateTime.fromMillis(time).toFormat("HH") // This will output just the hour part | ||
} | ||
interval={isMobile ? 2 : 1} | ||
tickLine={false} | ||
tickSize={5} | ||
style={{ stroke: "#84cc16" }} | ||
></XAxis> | ||
<YAxis type="number" hide={true} dataKey="height" /> | ||
|
||
{/* Add Area below Line */} | ||
<Area | ||
type="monotone" | ||
dataKey="height" | ||
fill="#1e1b4b" | ||
stroke="#1e1b4b" | ||
/> | ||
<ReferenceLine | ||
x={currentTime} | ||
stroke="#84cc16" | ||
strokeWidth={5} | ||
ifOverflow="extendDomain" | ||
/> | ||
</AreaChart> | ||
</ResponsiveContainer> | ||
</div> | ||
); | ||
}; | ||
|
||
export default TideVisualGraph; |
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
Oops, something went wrong.