Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Query params for download urls #582

Merged
merged 7 commits into from
Oct 16, 2024
46 changes: 26 additions & 20 deletions src/components/CTAButton/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {useColorMode} from '@docusaurus/theme-common';
import Image from "../Image";
import React, { useState, useEffect } from 'react';

type CTAButtonProps = {
label?: string
Expand All @@ -12,15 +11,36 @@ type CTAButtonProps = {
fullWidth?: boolean
}

// Define the type for gaGlobal
interface GaGlobal {
vid: string;
from_cookie: boolean;
}

// Check if gaGlobal exists in the global scope
declare const gaGlobal: GaGlobal | undefined;

const CTAButton = ({ ...props }: CTAButtonProps) => {
const {colorMode} = useColorMode();
const [href, setHref] = useState(props.href);

useEffect(() => {
if (typeof window !== 'undefined' && typeof gaGlobal !== 'undefined') {
const vid = gaGlobal.vid;

if (props.href?.startsWith('https://builds.pieces.app/stages/production')) {
// Need to ensure that vid is defined before appending it to the query string
const updatedHref = `${props.href}?download=true&product=DOCUMENTATION_WEBSITE${vid && `&visitor=${vid}`}`;
setHref(updatedHref);
}
}
}, []);

// If the href starts with http, open in a new tab
const newTab = props.href?.startsWith('http');
const newTab = href?.startsWith('http');

return (
<a
href={props.href}
href={href} // Use the state variable here instead of props.href
className={'cta-button'}
style={{
fontSize: props.type === 'secondary' ? '1rem' : '1.25rem',
Expand All @@ -33,21 +53,7 @@ const CTAButton = ({ ...props }: CTAButtonProps) => {
target={newTab ? '_blank' : '_self'}
rel={newTab ? 'noopener noreferrer' : undefined}
>
{
props.icon || props.iconDark ? (
// If the icon is a React element (object) and not a string, render it directly
typeof props.icon === 'object' ? (
props.icon
) : (
colorMode === 'dark' && props.iconDark ? (
<Image width={20} src={props.iconDark} alt={props.label} />
) : props.icon && (
<Image width={20} src={props.icon} alt={props.label} />
)
)
) : null
}

{/* ... (keep your existing icon rendering logic) */}
{props.label}
</a>
)
Expand Down