@@ -4,6 +4,8 @@ import { getOS } from "@/app/utils";
44import { GithubStars } from "@/components/GithubStars" ;
55import { useEffect , useState } from "react" ;
66import { useQuery } from "@tanstack/react-query" ;
7+ import { useSession } from "next-auth/react" ;
8+ import { useLogSnag } from "@logsnag/next" ;
79
810// Define the interface for a GitHub release asset
911interface ReleaseAsset {
@@ -47,12 +49,32 @@ const fetchReleases = async (): Promise<GitHubRelease[]> => {
4749
4850export const DownloadButton = ( ) => {
4951 const [ os , setOs ] = useState < string | null > ( null ) ;
52+ const { data : session } = useSession ( ) ;
53+ const { track } = useLogSnag ( ) ;
5054
5155 useEffect ( ( ) => {
5256 const updatedOs = getOS ( ) ;
5357 setOs ( updatedOs ) ;
5458 } , [ ] ) ;
5559
60+ const handleDownloadClick = ( os : string ) => {
61+ // Check if the user is logged in and has a valid session
62+ if ( session && session . user && session . user . email ) {
63+ // Track the download event with LogSnag
64+ track ( {
65+ channel : "downloads" ,
66+ event : "User Download" ,
67+ // @ts -ignore
68+ user_id : session . user . id ,
69+ tags : {
70+ os : os ?? "Unknown" ,
71+ } ,
72+ notify : true ,
73+ icon : "π" ,
74+ } ) ;
75+ }
76+ } ;
77+
5678 const {
5779 data : releases ,
5880 isLoading,
@@ -92,20 +114,33 @@ export const DownloadButton = () => {
92114 < span className = "animate-pulse inline-flex items-center rounded-md bg-blue-400/10 px-3 py-3 text-2xl font-medium text-blue-400 ring-1 ring-inset ring-blue-400/30 hover:bg-blue-400/20 dark:bg-blue-400/20 dark:text-blue-400 dark:ring-blue-400/20 w-44 h-16 cursor-pointer" > </ span >
93115 ) ;
94116
95- if ( os === "macOS" ) return < MacDownloadButton releases = { releases ?? [ ] } /> ;
117+ if ( os === "macOS" )
118+ return (
119+ < MacDownloadButton
120+ releases = { releases ?? [ ] }
121+ handleDownloadClick = { handleDownloadClick }
122+ />
123+ ) ;
96124
97125 return (
98126 < a
99127 href = { downloadUrl ?? "#" }
100128 className = "inline-flex items-center rounded-md bg-blue-400/10 px-3 py-3 text-2xl font-medium text-blue-400 ring-1 ring-inset ring-blue-400/30 hover:bg-blue-400/20 dark:bg-blue-400/20 dark:text-blue-400 dark:ring-blue-400/20"
101129 download
130+ onClick = { ( ) => handleDownloadClick ( os ) }
102131 >
103132 Download for { os }
104133 </ a >
105134 ) ;
106135} ;
107136
108- const MacDownloadButton = ( { releases } : { releases : GitHubRelease [ ] } ) => {
137+ const MacDownloadButton = ( {
138+ releases,
139+ handleDownloadClick,
140+ } : {
141+ releases : GitHubRelease [ ] ;
142+ handleDownloadClick : ( os : string ) => void ;
143+ } ) => {
109144 const intelMacPattern = / S c r e e n L i n k - \d + \. \d + \. \d + \. d m g / ;
110145 const armMacPattern = / S c r e e n L i n k - \d + \. \d + \. \d + - a r m 6 4 \. d m g / ;
111146
@@ -128,6 +163,7 @@ const MacDownloadButton = ({ releases }: { releases: GitHubRelease[] }) => {
128163 }
129164 className = "inline-flex items-center rounded-md bg-blue-400/10 px-3 py-3 text-2xl font-medium text-blue-400 ring-1 ring-inset ring-blue-400/30 hover:bg-blue-400/20 dark:bg-blue-400/20 dark:text-blue-400 dark:ring-blue-400/20"
130165 download
166+ onClick = { ( ) => handleDownloadClick ( "mac-intel" ) }
131167 >
132168 Download for Intel
133169 </ a >
@@ -141,6 +177,7 @@ const MacDownloadButton = ({ releases }: { releases: GitHubRelease[] }) => {
141177 }
142178 className = "inline-flex items-center rounded-md bg-blue-400/10 px-3 py-3 text-2xl font-medium text-blue-400 ring-1 ring-inset ring-blue-400/30 hover:bg-blue-400/20 dark:bg-blue-400/20 dark:text-blue-400 dark:ring-blue-400/20"
143179 download
180+ onClick = { ( ) => handleDownloadClick ( "mac-arm" ) }
144181 >
145182 Download for Apple Silicon
146183 </ a >
0 commit comments