22import { useState , useRef , useEffect } from 'react' ;
33import { toPng , toCanvas } from 'html-to-image' ;
44import type { DashboardExportData } from '@/types/dashboard' ;
5+ import { getDashboardUrl , getOrigin } from '@/utils/urls' ;
56
67type OptionState = 'idle' | 'loading' | 'success' | 'error' ;
78
8- const BASE_ORIGIN =
9- ( typeof window !== 'undefined' ? window . location . origin : null ) ??
10- process . env . NEXT_PUBLIC_SITE_URL ??
11- 'https://commitpulse.vercel.app' ;
12- const PROFILE_URL = ( username : string ) => `${ BASE_ORIGIN } /dashboard/${ username } ` ;
13-
149const SVG_NAMESPACE = 'http://www.w3.org/2000/svg' ;
1510const XLINK_NAMESPACE = 'http://www.w3.org/1999/xlink' ;
1611const UNSAFE_SVG_ELEMENTS = new Set ( [
@@ -48,7 +43,7 @@ function sanitizeFilenameSegment(value: string): string {
4843}
4944
5045function buildStreakSvgUrl ( username : string ) : string {
51- const url = new URL ( '/api/streak' , BASE_ORIGIN ) ;
46+ const url = new URL ( '/api/streak' , getOrigin ( ) ) ;
5247 url . searchParams . set ( 'user' , sanitizeUsernameForUrl ( username ) ) ;
5348 return url . toString ( ) ;
5449}
@@ -147,7 +142,7 @@ export function useShareActions(
147142 const handleCopyLink = async ( ) : Promise < boolean > => {
148143 setOptionState ( 'copy' , 'loading' ) ;
149144 try {
150- await navigator . clipboard . writeText ( PROFILE_URL ( username ) ) ;
145+ await navigator . clipboard . writeText ( getDashboardUrl ( username ) ) ;
151146 setOptionState ( 'copy' , 'success' ) ;
152147 setTimeout ( ( ) => onClose ( ) , 800 ) ;
153148 return true ;
@@ -158,20 +153,20 @@ export function useShareActions(
158153 } ;
159154
160155 const handleTwitter = ( ) => {
161- const url = PROFILE_URL ( username ) ;
156+ const url = getDashboardUrl ( username ) ;
162157 const text = encodeURIComponent ( `Check out my GitHub commit pulse on CommitPulse 🚀\n${ url } ` ) ;
163158 window . open ( `https://twitter.com/intent/tweet?text=${ text } ` , '_blank' , 'noopener' ) ;
164159 onClose ( ) ;
165160 } ;
166161
167162 const handleLinkedIn = ( ) => {
168- const url = encodeURIComponent ( PROFILE_URL ( username ) ) ;
163+ const url = encodeURIComponent ( getDashboardUrl ( username ) ) ;
169164 window . open ( `https://www.linkedin.com/sharing/share-offsite/?url=${ url } ` , '_blank' , 'noopener' ) ;
170165 onClose ( ) ;
171166 } ;
172167
173168 const handleReddit = ( ) => {
174- const url = encodeURIComponent ( PROFILE_URL ( username ) ) ;
169+ const url = encodeURIComponent ( getDashboardUrl ( username ) ) ;
175170 const title = encodeURIComponent ( 'Check out my CommitPulse dashboard 🚀' ) ;
176171 window . open (
177172 `https://www.reddit.com/submit?url=${ url } &title=${ title } ` ,
@@ -356,7 +351,7 @@ export function useShareActions(
356351 const rows : Array < Array < string | number > > = [
357352 [ 'field' , 'value' ] ,
358353 [ 'username' , username ] ,
359- [ 'profileUrl' , PROFILE_URL ( username ) ] ,
354+ [ 'profileUrl' , getDashboardUrl ( username ) ] ,
360355 [ 'exportedAt' , exportedAt ] ,
361356 [ 'totalContributions' , exportData . stats . totalContributions ] ,
362357 [ 'currentStreak' , exportData . stats . currentStreak ] ,
@@ -388,7 +383,7 @@ export function useShareActions(
388383
389384 const payload = {
390385 username,
391- profileUrl : PROFILE_URL ( username ) ,
386+ profileUrl : getDashboardUrl ( username ) ,
392387 exportedAt : new Date ( ) . toISOString ( ) ,
393388 totalContributions : exportData . stats . totalContributions ,
394389 currentStreak : exportData . stats . currentStreak ,
@@ -422,7 +417,7 @@ export function useShareActions(
422417 await navigator . share ( {
423418 title : `${ username } 's Commit Pulse` ,
424419 text : `Check out my GitHub commit pulse on CommitPulse 🚀` ,
425- url : PROFILE_URL ( username ) ,
420+ url : getDashboardUrl ( username ) ,
426421 } ) ;
427422 setOptionState ( 'native' , 'success' ) ;
428423 setTimeout ( ( ) => onClose ( ) , 800 ) ;
0 commit comments