Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

replace rive with dot animation

Prateek Sunalba6185236aa1
diff --git a/web/apps/ensu/src/components/chat/GeneratingRiveIndicator.tsx b/web/apps/ensu/src/components/chat/GeneratingRiveIndicator.tsx
index b40d3113aa..2e8d197392 100644
--- a/web/apps/ensu/src/components/chat/GeneratingRiveIndicator.tsx
+++ b/web/apps/ensu/src/components/chat/GeneratingRiveIndicator.tsx
@@ -1,6 +1,5 @@
import { Box, Typography } from "@mui/material";
-import { useRouter } from "next/router";
-import { memo, useEffect, useRef, useState } from "react";
+import { memo, useEffect, useState } from "react";
type GeneratingRiveIndicatorProps = {
size?: number;
@@ -9,22 +8,7 @@ type GeneratingRiveIndicatorProps = {
isOutroPhase?: boolean;
};
-type RivePlaybackTarget = string | string[];
-
-type RiveInput = { name?: string; value?: boolean | number; fire?: () => void };
-
-type RiveInstance = {
- cleanup?: () => void;
- resizeDrawingSurfaceToCanvas?: () => void;
- play?: (animation?: RivePlaybackTarget) => void;
- pause?: () => void;
- stop?: () => void;
- animationNames?: string[];
- stateMachineNames?: string[];
- stateMachineInputs?: (stateMachineName: string) => RiveInput[];
-};
-
-const RIVE_SRC = "/animations/ensu.riv";
+const DOT_STEP_MS = 420;
const GeneratingRiveIndicator = memo(
({
@@ -33,230 +17,43 @@ const GeneratingRiveIndicator = memo(
isGenerating = true,
isOutroPhase = false,
}: GeneratingRiveIndicatorProps) => {
- const canvasRef = useRef<HTMLCanvasElement | null>(null);
- const instanceRef = useRef<RiveInstance | null>(null);
- const playbackTargetRef = useRef<RivePlaybackTarget | undefined>(
- undefined,
- );
- const outroInputRef = useRef<RiveInput | null>(null);
- const outroNamedTargetRef = useRef<string | undefined>(undefined);
- const wasGeneratingRef = useRef(isGenerating);
- const wasOutroPhaseRef = useRef(isOutroPhase);
- const generatingLiveRef = useRef(isGenerating);
- const outroPhaseLiveRef = useRef(isOutroPhase);
- const [failedToLoad, setFailedToLoad] = useState(false);
- const { basePath } = useRouter();
- const riveSrc = `${basePath ?? ""}${RIVE_SRC}`;
-
- const playMain = () => {
- const instance = instanceRef.current;
- if (!instance) return;
- const target = playbackTargetRef.current;
- if (target) {
- instance.play?.(target);
- } else {
- instance.play?.();
- }
- };
-
- const triggerOutro = () => {
- const instance = instanceRef.current;
- if (!instance) return;
-
- const outroInput = outroInputRef.current;
- if (outroInput) {
- if (typeof outroInput.fire === "function") {
- outroInput.fire();
- return;
- }
-
- if (typeof outroInput.value === "boolean") {
- outroInput.value = true;
- return;
- }
-
- if (typeof outroInput.value === "number") {
- outroInput.value = 1;
- return;
- }
- }
-
- const outroNamedTarget = outroNamedTargetRef.current;
- if (outroNamedTarget) {
- instance.pause?.();
- instance.stop?.();
- instance.play?.(outroNamedTarget);
- }
- };
+ const [dotCount, setDotCount] = useState(1);
+ const shouldAnimate = isGenerating || isOutroPhase;
useEffect(() => {
- if (typeof window === "undefined") return;
-
- let canceled = false;
-
- const initialize = async () => {
- try {
- const rive = await import("@rive-app/canvas");
- if (canceled || !canvasRef.current) {
- return;
- }
-
- const { Alignment, Fit, Layout, Rive } = rive;
- instanceRef.current = new Rive({
- src: riveSrc,
- canvas: canvasRef.current,
- autoplay: false,
- layout: new Layout({
- fit: Fit.Contain,
- alignment: Alignment.Center,
- }),
- onLoad: () => {
- try {
- const instance = instanceRef.current;
- instance?.resizeDrawingSurfaceToCanvas?.();
-
- const stateMachineNames =
- instance?.stateMachineNames ?? [];
- const animationNames =
- instance?.animationNames ?? [];
-
- const stateMachineName = stateMachineNames[0];
- if (stateMachineName) {
- playbackTargetRef.current =
- stateMachineName;
- const inputs =
- instance?.stateMachineInputs?.(
- stateMachineName,
- ) ?? [];
- outroInputRef.current =
- inputs.find((input) =>
- input.name
- ?.toLowerCase()
- .includes("outro"),
- ) ?? null;
-
- if (
- outroInputRef.current &&
- typeof outroInputRef.current.value ===
- "boolean"
- ) {
- outroInputRef.current.value = false;
- }
- if (
- outroInputRef.current &&
- typeof outroInputRef.current.value ===
- "number"
- ) {
- outroInputRef.current.value = 0;
- }
- } else if (animationNames.length > 1) {
- playbackTargetRef.current = animationNames;
- } else if (animationNames.length === 1) {
- playbackTargetRef.current =
- animationNames[0];
- }
-
- outroNamedTargetRef.current = [
- ...stateMachineNames,
- ...animationNames,
- ].find((name) =>
- name.toLowerCase().includes("outro"),
- );
-
- playMain();
- } catch {
- // noop
- }
- },
- onStop: () => {
- try {
- if (
- generatingLiveRef.current &&
- !outroPhaseLiveRef.current
- ) {
- playMain();
- }
- } catch {
- // noop
- }
- },
- onLoadError: () => {
- if (!canceled) {
- setFailedToLoad(true);
- }
- },
- });
- } catch {
- if (!canceled) {
- setFailedToLoad(true);
- }
- }
- };
+ if (!shouldAnimate) {
+ setDotCount(1);
+ return;
+ }
- void initialize();
+ const timer = window.setInterval(() => {
+ setDotCount((prev) => (prev === 3 ? 1 : prev + 1));
+ }, DOT_STEP_MS);
return () => {
- canceled = true;
- try {
- instanceRef.current?.cleanup?.();
- } catch {
- // noop
- }
- instanceRef.current = null;
- playbackTargetRef.current = undefined;
- outroInputRef.current = null;
- outroNamedTargetRef.current = undefined;
+ window.clearInterval(timer);
};
- }, [riveSrc]);
+ }, [shouldAnimate]);
- useEffect(() => {
- const wasGenerating = wasGeneratingRef.current;
- const wasOutroPhase = wasOutroPhaseRef.current;
-
- generatingLiveRef.current = isGenerating;
- outroPhaseLiveRef.current = isOutroPhase;
+ if (!shouldAnimate) return null;
- if (!wasOutroPhase && isOutroPhase) {
- triggerOutro();
- }
-
- if (!isOutroPhase && isGenerating && !wasGenerating) {
- playMain();
- }
-
- wasGeneratingRef.current = isGenerating;
- wasOutroPhaseRef.current = isOutroPhase;
- }, [isGenerating, isOutroPhase]);
-
- if (failedToLoad) {
- return (
+ return (
+ <Box
+ sx={{ display: "flex", alignItems: "center", minHeight: size }}
+ >
<Typography
variant="message"
- sx={{ color: "text.muted", whiteSpace: "pre-wrap" }}
+ aria-label={fallbackText ?? "Generating response"}
+ sx={{
+ color: "text.muted",
+ lineHeight: 1,
+ fontFamily: "monospace",
+ minWidth: "3ch",
+ whiteSpace: "pre",
+ }}
>
- {fallbackText ?? "Generating your reply"}
+ {".".repeat(dotCount)}
</Typography>
- );
- }
-
- return (
- <Box
- sx={{
- display: "flex",
- alignItems: "center",
- overflow: "hidden",
- maxHeight: size,
- opacity: 1,
- }}
- >
- <Box
- component="canvas"
- ref={canvasRef}
- width={size}
- height={size}
- aria-label="Generating response animation"
- sx={{ width: size, height: size, display: "block" }}
- />
</Box>
);
},