AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.
"use client";
import type { ReactNode } from "react";
import React, {
createContext,
forwardRef,
useEffect,
useImperativeHandle,
useMemo,
useRef,
} from "react";
import type {
GlobalOptions as ConfettiGlobalOptions,
CreateTypes as ConfettiInstance,
Options as ConfettiOptions,
} from "canvas-confetti";
import confetti from "canvas-confetti";
import * as Sentry from "@sentry/nextjs";
import { cn } from "@/lib/utils";
/** AutoGPT design system purple palette for confetti */
const AGPT_CONFETTI_COLORS = [
"#7733f5", // purple-500
"#925cf7", // purple-400
"#a476f8", // purple-300
"#c0a1fa", // purple-200
"#6c2edf", // purple-600
"#5424ae", // purple-700
"#efe8fe", // purple-100
];
type Api = {
fire: (options?: ConfettiOptions) => void;
};
interface Props extends React.ComponentPropsWithRef<"canvas"> {
options?: ConfettiOptions;
globalOptions?: ConfettiGlobalOptions;
manualstart?: boolean;
children?: ReactNode;
}
export type ConfettiRef = Api | null;
export const ConfettiContext = createContext<Api>({} as Api);
const ConfettiComponent = forwardRef<ConfettiRef, Props>(
function Confetti(props, ref) {
const {
options,
globalOptions,
manualstart = false,
children,
className,
...rest
} = props;
const canvasElRef = useRef<HTMLCanvasElement | null>(null);
const instanceRef = useRef<ConfettiInstance | null>(null);
const fire = useMemo(
() =>
async (opts: ConfettiOptions = {}) => {
try {
await instanceRef.current?.({
colors: AGPT_CONFETTI_COLORS,
...options,
...opts,
});
} catch (error) {
Sentry.captureException(error);
}
},
[options],
);
const api = useMemo(() => ({ fire }), [fire]);
useImperativeHandle(ref, () => api, [api]);
// Mirror the latest props into refs so the mount-only effect below can read
// current values without listing them as dependencies. `fire`'s identity
// changes whenever `options` changes (a new object literal every render at
// most call sites), so depending on it would tear down and re-fire the
// instance on every re-render.
const manualstartRef = useRef(manualstart);
manualstartRef.current = manualstart;
const globalOptionsRef = useRef(globalOptions);
globalOptionsRef.current = globalOptions;
const fireRef = useRef(fire);
fireRef.current = fire;
// Create the confetti instance once after mount via useEffect,
// so React never re-fires a callback ref on re-renders. Auto-fire is
// tied to instance creation so StrictMode's remount (which resets the
// instance) still produces a visible burst.
useEffect(() => {
const node = canvasElRef.current;
if (!node || instanceRef.current) return;
// Cap DPR: full-resolution canvases on 2-3x displays multiply the
// pixels the main thread has to paint each frame for little visual gain.
// `resize: false` keeps this capped sizing — canvas-confetti's own resize
// handling ignores devicePixelRatio and would drop the cap on any resize.
const dpr = Math.min(window.devicePixelRatio || 1, 1.5);
node.width = node.offsetWidth * dpr;
node.height = node.offsetHeight * dpr;
instanceRef.current = confetti.create(node, {
resize: false,
useWorker: false,
...globalOptionsRef.current,
});
if (!manualstartRef.current) {
void fireRef.current();
}
return () => {
instanceRef.current?.reset();
instanceRef.current = null;
};
}, []);
return (
<ConfettiContext.Provider value={api}>
<canvas
ref={canvasElRef}
style={{ width: "100%", height: "100%" }}
className={cn(
"pointer-events-none fixed inset-0 z-50 h-full w-full",
className,
)}
{...rest}
/>
{children}
</ConfettiContext.Provider>
);
},
);
ConfettiComponent.displayName = "Confetti";
export { ConfettiComponent as Confetti, AGPT_CONFETTI_COLORS };
export type { Props as ConfettiProps, ConfettiOptions };