-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Varun Raj <varun@skcript.com>
- Loading branch information
Showing
11 changed files
with
145 additions
and
2 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
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,33 @@ | ||
import { NextApiResponse } from "next"; | ||
|
||
import { db } from "@/config/db"; | ||
import { getCurrentUser } from "@/handlers/serverUtils/user.utils"; | ||
import { NextApiRequest } from "next"; | ||
import { assets, exif } from "@/schema"; | ||
import { and, eq, isNotNull } from "drizzle-orm"; | ||
|
||
export default async function handler(req: NextApiRequest, res: NextApiResponse) { | ||
const currentUser = await getCurrentUser(req); | ||
if (!currentUser) { | ||
return res.status(401).json({ message: 'Unauthorized' }); | ||
} | ||
|
||
const dbAssets = await db.select({ | ||
assetId: assets.id, | ||
latitude: exif.latitude, | ||
longitude: exif.longitude, | ||
}).from(assets) | ||
.innerJoin(exif, eq(assets.id, exif.assetId)) | ||
.where( | ||
and( | ||
eq(assets.ownerId, currentUser.id), | ||
isNotNull(exif.latitude), | ||
isNotNull(exif.longitude) | ||
) | ||
); | ||
const heatmapData = dbAssets.map((asset) => [ | ||
asset.longitude, | ||
asset.latitude, | ||
]); | ||
res.status(200).json(heatmapData); | ||
} |
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,96 @@ | ||
import Header from '@/components/shared/Header' | ||
import { useConfig } from '@/contexts/ConfigContext'; | ||
import React, { useEffect, useMemo, useState } from 'react' | ||
import { useMap, useMapsLibrary } from '@vis.gl/react-google-maps'; | ||
import { APIProvider, Map } from '@vis.gl/react-google-maps'; | ||
import PageLayout from '@/components/layouts/PageLayout'; | ||
import { getAssetGeoHeatmap } from '@/handlers/api/asset.handler'; | ||
import { useTheme } from 'next-themes'; | ||
import { MapPinX } from 'lucide-react'; | ||
|
||
|
||
const HeatMap = () => { | ||
|
||
const map = useMap(); | ||
const visualization = useMapsLibrary('visualization'); | ||
|
||
const heatmap = useMemo(() => { | ||
if (!visualization) return null; | ||
|
||
return new google.maps.visualization.HeatmapLayer({ | ||
radius: 20, | ||
opacity: 0.5 | ||
}); | ||
|
||
}, [visualization]); | ||
|
||
useEffect(() => { | ||
if (!heatmap) return; | ||
|
||
getAssetGeoHeatmap().then((data) => { | ||
console.log(data); | ||
heatmap.setData(data.map(([lng, lat]: [number, number]) => ({ | ||
location: new google.maps.LatLng(lat, lng), | ||
weight: 10 | ||
}))); | ||
}); | ||
|
||
}, [heatmap]); | ||
|
||
useEffect(() => { | ||
if (!heatmap) return; | ||
|
||
heatmap.setMap(map); | ||
|
||
return () => { | ||
heatmap.setMap(null); | ||
}; | ||
}, [heatmap, map]); | ||
|
||
return null; | ||
} | ||
|
||
export default function GeoHeatmap() { | ||
const { googleMapsApiKey } = useConfig(); | ||
const { theme } = useTheme(); | ||
const [mapId, setMapId] = useState('7a9e2ebecd32a903'); | ||
|
||
useEffect(() => { | ||
if (theme === 'light') { | ||
setMapId(""); | ||
} else { | ||
setMapId('7a9e2ebecd32a903'); | ||
} | ||
}, [theme]); | ||
|
||
|
||
return ( | ||
<PageLayout className="!p-0 !mb-0"> | ||
<Header leftComponent="Geo Heatmap" /> | ||
<div className='h-full w-full'> | ||
{googleMapsApiKey ? ( | ||
<APIProvider apiKey={googleMapsApiKey}> | ||
<Map | ||
defaultCenter={{ lat: 0, lng: 0 }} | ||
mapId={mapId} | ||
defaultZoom={2} | ||
gestureHandling={'greedy'} | ||
disableDefaultUI={true} | ||
className='h-full w-full' | ||
/> | ||
|
||
<HeatMap /> | ||
</APIProvider> | ||
) : ( | ||
<div className='h-full w-full flex items-center justify-center flex-col gap-2'> | ||
<MapPinX className='w-10 h-10 text-muted-foreground' /> | ||
<p className='text-muted-foreground'>No Google Maps API key found</p> | ||
<p className='text-muted-foreground'> | ||
Please create and add your Google Maps API key in the env file under <kbd className='bg-zinc-200 text-black dark:text-white px-1 py-0.5 rounded-md dark:bg-zinc-500'>GOOGLE_MAPS_API_KEY</kbd>. | ||
</p> | ||
</div> | ||
)} | ||
</div> | ||
</PageLayout> | ||
) | ||
} |