-
-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added missing gdpr cookie consent popup
- Loading branch information
1 parent
ff2756b
commit e548052
Showing
9 changed files
with
458 additions
and
14 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
"use client"; | ||
|
||
import { useEffect, useState } from "react"; | ||
import { cn } from "@/lib/utils"; | ||
import { CheckCircle2Icon, CheckIcon, LoaderCircleIcon } from "lucide-react"; | ||
import { usePlausible } from "next-plausible"; | ||
import useLocalStorage from "use-local-storage"; | ||
|
||
import { NeonGradientCard } from "./magic-ui/neon-gradient-card"; | ||
import { Button } from "./ui/button"; | ||
|
||
export const AnalyticsPopup = () => { | ||
const plausible = usePlausible(); | ||
const [status, setStatus] = useState< | ||
"idle" | "loading" | "success-initial" | "success-final" | "hidden" | ||
>("idle"); | ||
const [analyticsAccepted, setAnalyticsAccepted] = useLocalStorage( | ||
"analytics_accepted", | ||
false, | ||
); | ||
|
||
const handleAccept = () => { | ||
plausible("analytics-accepted"); | ||
|
||
setStatus("loading"); | ||
}; | ||
|
||
useEffect(() => { | ||
if (status === "loading") { | ||
setTimeout(() => { | ||
setStatus("success-initial"); | ||
}, 500); | ||
} | ||
|
||
if (status === "success-initial") { | ||
setTimeout(() => { | ||
setStatus("success-final"); | ||
}, 10); | ||
} | ||
|
||
if (status === "success-final") { | ||
setTimeout(() => { | ||
setStatus("hidden"); | ||
}, 500); | ||
} | ||
|
||
if (status === "hidden") { | ||
setTimeout(() => { | ||
setAnalyticsAccepted(true); | ||
}, 250); | ||
} | ||
|
||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [status]); | ||
|
||
if (analyticsAccepted) return null; | ||
|
||
return ( | ||
<NeonGradientCard | ||
className={cn( | ||
"fixed bottom-4 left-4 h-fit w-[400px] p-0 opacity-100 transition-opacity", | ||
status === "hidden" && "opacity-0", | ||
)} | ||
> | ||
<div className="space-y-2"> | ||
<h2 className="text-lg font-semibold">Before you continue 🍪</h2> | ||
<p className="text-sm text-muted-foreground"> | ||
<b>We collect anonymous analytics</b>. You can see more information | ||
about what we collect in our{" "} | ||
<a | ||
href="/privacy-policy" | ||
className="text-blue-500 hover:text-blue-600" | ||
> | ||
privacy policy | ||
</a> | ||
. Your code never leaves your browser. | ||
</p> | ||
|
||
<div className="flex w-full items-center justify-between"> | ||
<div className="text-xs"> | ||
Before continuing, confirm that you're ok with that.{" "} | ||
</div> | ||
|
||
<div className="relative flex w-fit gap-2"> | ||
<Button onClick={handleAccept} className="gap-2"> | ||
<StatusIcon status={status} /> | ||
Accept | ||
</Button> | ||
</div> | ||
</div> | ||
</div> | ||
</NeonGradientCard> | ||
); | ||
}; | ||
|
||
const StatusIcon = ({ | ||
status, | ||
}: { | ||
status: "idle" | "loading" | "success-initial" | "success-final" | "hidden"; | ||
}) => { | ||
switch (status) { | ||
case "idle": | ||
return <CheckIcon size={20} className="-ml-1" />; | ||
case "loading": | ||
return <LoaderCircleIcon size={20} className="-ml-1 animate-spin" />; | ||
case "success-initial": | ||
case "success-final": | ||
case "hidden": | ||
return ( | ||
<CheckCircle2Icon | ||
size={20} | ||
className={cn( | ||
"-ml-1 scale-0 text-green-400 transition-transform", | ||
status === "success-final" && "scale-100", | ||
)} | ||
/> | ||
); | ||
} | ||
}; |
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,131 @@ | ||
/* eslint-disable @typescript-eslint/no-unsafe-return */ | ||
/* eslint-disable @typescript-eslint/no-unsafe-call */ | ||
/* eslint-disable @typescript-eslint/no-unsafe-member-access */ | ||
/* eslint-disable @typescript-eslint/no-unnecessary-condition */ | ||
/* eslint-disable @typescript-eslint/no-redundant-type-constituents */ | ||
/* eslint-disable @typescript-eslint/no-unsafe-assignment */ | ||
import type { ButtonProps } from "@/components/ui/button"; | ||
import type { | ||
GlobalOptions as ConfettiGlobalOptions, | ||
CreateTypes as ConfettiInstance, | ||
Options as ConfettiOptions, | ||
} from "canvas-confetti"; | ||
import type { ReactNode } from "react"; | ||
import React, { | ||
createContext, | ||
forwardRef, | ||
useCallback, | ||
useEffect, | ||
useImperativeHandle, | ||
useMemo, | ||
useRef, | ||
} from "react"; | ||
import { Button } from "@/components/ui/button"; | ||
import confetti from "canvas-confetti"; | ||
|
||
interface Api { | ||
fire: (options?: ConfettiOptions) => void; | ||
} | ||
|
||
type Props = React.ComponentPropsWithRef<"canvas"> & { | ||
options?: ConfettiOptions; | ||
globalOptions?: ConfettiGlobalOptions; | ||
manualstart?: boolean; | ||
children?: ReactNode; | ||
}; | ||
|
||
export type ConfettiRef = Api | null; | ||
|
||
const ConfettiContext = createContext<Api>({} as Api); | ||
|
||
const Confetti = forwardRef<ConfettiRef, Props>((props, ref) => { | ||
const { | ||
options, | ||
globalOptions = { resize: true, useWorker: true }, | ||
manualstart = false, | ||
children, | ||
...rest | ||
} = props; | ||
const instanceRef = useRef<ConfettiInstance | null>(null); // confetti instance | ||
|
||
const canvasRef = useCallback( | ||
// https://react.dev/reference/react-dom/components/common#ref-callback | ||
// https://reactjs.org/docs/refs-and-the-dom.html#callback-refs | ||
(node: HTMLCanvasElement) => { | ||
if (node !== null) { | ||
// <canvas> is mounted => create the confetti instance | ||
if (instanceRef.current) return; // if not already created | ||
instanceRef.current = confetti.create(node, { | ||
...globalOptions, | ||
resize: true, | ||
}); | ||
} else { | ||
// <canvas> is unmounted => reset and destroy instanceRef | ||
if (instanceRef.current) { | ||
instanceRef.current.reset(); | ||
instanceRef.current = null; | ||
} | ||
} | ||
}, | ||
[globalOptions], | ||
); | ||
|
||
// `fire` is a function that calls the instance() with `opts` merged with `options` | ||
const fire = useCallback( | ||
(opts = {}) => instanceRef.current?.({ ...options, ...opts }), | ||
[options], | ||
); | ||
|
||
const api = useMemo( | ||
() => ({ | ||
fire, | ||
}), | ||
[fire], | ||
); | ||
|
||
useImperativeHandle(ref, () => api, [api]); | ||
|
||
useEffect(() => { | ||
if (!manualstart) { | ||
fire(); | ||
} | ||
}, [manualstart, fire]); | ||
|
||
return ( | ||
<ConfettiContext.Provider value={api}> | ||
<canvas ref={canvasRef} {...rest} /> | ||
{children} | ||
</ConfettiContext.Provider> | ||
); | ||
}); | ||
|
||
interface ConfettiButtonProps extends ButtonProps { | ||
options?: ConfettiOptions & | ||
ConfettiGlobalOptions & { canvas?: HTMLCanvasElement }; | ||
children?: React.ReactNode; | ||
} | ||
|
||
function ConfettiButton({ options, children, ...props }: ConfettiButtonProps) { | ||
const handleClick = (event: React.MouseEvent<HTMLButtonElement>) => { | ||
const rect = event.currentTarget.getBoundingClientRect(); | ||
const x = rect.left + rect.width / 2; | ||
const y = rect.top + rect.height / 2; | ||
confetti({ | ||
...options, | ||
origin: { | ||
x: x / window.innerWidth, | ||
y: y / window.innerHeight, | ||
}, | ||
}); | ||
}; | ||
|
||
return ( | ||
<Button onClick={handleClick} {...props}> | ||
{children} | ||
</Button> | ||
); | ||
} | ||
|
||
Confetti.displayName = "Confetti"; | ||
|
||
export { Confetti, ConfettiButton }; |
Oops, something went wrong.