feat: update ai spline

This commit is contained in:
DIYgod
2025-10-22 16:18:17 +08:00
parent a598f4275a
commit 496ce345e9
6 changed files with 45 additions and 54 deletions
@@ -1,10 +1,11 @@
import { cn } from "@follow/utils"
import { ErrorBoundary } from "@sentry/react"
import { createElement, lazy, Suspense } from "react"
const AISplineLoader = lazy(() =>
import("./AISplineLoader").then((res) => ({ default: res.AISplineLoader })),
)
export const AISpline = () => {
export const AISpline = ({ className }: { className?: string }) => {
return createElement(
ErrorBoundary,
{
@@ -13,9 +14,9 @@ export const AISpline = () => {
createElement(
Suspense,
{
fallback: createElement("div", { className: "size-16 mx-auto" }),
fallback: createElement("div", { className: cn("size-20 mx-auto", className) }),
},
createElement(AISplineLoader),
createElement(AISplineLoader, { className }),
),
)
}
@@ -1,12 +1,13 @@
import { clamp, cn } from "@follow/utils"
import Spline from "@splinetool/react-spline"
import { useCallback, useRef } from "react"
const resolvedAIIconUrl = "https://cdn.follow.is/ai.splinecode"
// TODO: use folo cdn
const resolvedAIIconUrl = "https://prod.spline.design/n2hjp93nWReC-512/scene.splinecode"
export const AISplineLoader = () => {
export const AISplineLoader = ({ className }: { className?: string }) => {
const containerRef = useRef<HTMLDivElement>(null)
const headRef = useRef<any>(null)
const bodyRef = useRef<any>(null)
// Angle conversion function: degrees to radians
const degToRad = (degrees: number) => degrees * (Math.PI / 180)
@@ -27,10 +28,10 @@ export const AISplineLoader = () => {
// Calculate head rotation angle based on relative position
// Y-axis rotation (left-right): -70 to 70 degrees
const headRotationY = clampedX * 70
const headRotationY = clampedX * 20
// X-axis rotation (up-down): -60 to 60 degrees
const headRotationX = clampedY * 60
const headRotationX = clampedY * 20
return {
x: degToRad(headRotationX),
@@ -40,101 +41,76 @@ export const AISplineLoader = () => {
[],
)
// Calculate body rotation angle (horizontal rotation only)
const calculateBodyRotation = useCallback((mouseX: number, containerRect: DOMRect) => {
const containerCenterX = containerRect.left + containerRect.width / 2
// Calculate mouse X position relative to container center (-1 to 1)
const relativeX = (mouseX - containerCenterX) / (window.innerWidth / 2)
const clampedX = Math.max(-1, Math.min(1, relativeX))
// Body Y-axis rotation: -30 to 30 degrees (smaller range than head rotation)
const bodyRotationY = clampedX * 30
return {
x: 0, // Body doesn't rotate up-down
y: degToRad(bodyRotationY),
}
}, [])
const handleLoad = useCallback(
(app: any) => {
const head = app.findObjectByName("Head")
const body = app.findObjectByName("Body")
const head = app.findObjectByName("Folo Character_V3")
if (!head || !body) {
if (!head) {
console.warn("Cannot find Head or Body object")
return
}
headRef.current = head
bodyRef.current = body
const onMove = (e: MouseEvent) => {
if (!containerRef.current || !headRef.current || !bodyRef.current) return
if (!containerRef.current || !headRef.current) return
const containerRect = containerRef.current.getBoundingClientRect()
// Calculate head rotation
const headRotation = calculateHeadRotation(e.clientX, e.clientY, containerRect)
headRef.current.rotation.x = headRotation.x
headRef.current.rotation.y = headRotation.y
// Calculate body rotation
const bodyRotation = calculateBodyRotation(e.clientX, containerRect)
bodyRef.current.rotation.x = bodyRotation.x
bodyRef.current.rotation.y = bodyRotation.y
headRef.current.rotation.x = clamp(headRotation.x, -0.5, 0.5)
headRef.current.rotation.y = clamp(headRotation.y, -0.5, 0.5)
}
// Reset to default position when mouse leaves
const onMouseLeave = () => {
if (!headRef.current || !bodyRef.current) return
if (!headRef.current) return
// Smooth transition back to default position
const resetAnimation = () => {
if (!headRef.current || !bodyRef.current) return
if (!headRef.current) return
const currentHeadX = headRef.current.rotation.x
const currentHeadY = headRef.current.rotation.y
const currentBodyY = bodyRef.current.rotation.y
// Simple linear interpolation to smoothly return rotation to 0
headRef.current.rotation.x = currentHeadX * 0.9
headRef.current.rotation.y = currentHeadY * 0.9
bodyRef.current.rotation.y = currentBodyY * 0.9
// Continue animation if not fully returned to 0
if (
Math.abs(currentHeadX) > 0.01 ||
Math.abs(currentHeadY) > 0.01 ||
Math.abs(currentBodyY) > 0.01
) {
if (Math.abs(currentHeadX) > 0.01 || Math.abs(currentHeadY) > 0.01) {
requestAnimationFrame(resetAnimation)
} else {
// Complete reset to 0
headRef.current.rotation.x = 0
headRef.current.rotation.y = 0
bodyRef.current.rotation.x = 0
bodyRef.current.rotation.y = 0
}
}
resetAnimation()
}
const onClick = () => {
app.emitEvent("mouseDown", "Folo Character_V3")
}
onClick()
window.addEventListener("pointermove", onMove)
document.addEventListener("mouseleave", onMouseLeave)
window.addEventListener("click", onClick)
return () => {
window.removeEventListener("pointermove", onMove)
document.removeEventListener("mouseleave", onMouseLeave)
window.removeEventListener("click", onClick)
}
},
[calculateHeadRotation, calculateBodyRotation],
[calculateHeadRotation],
)
return (
<div ref={containerRef} className={"!size-16"}>
<div ref={containerRef} className={cn("size-20", className)}>
<Spline scene={resolvedAIIconUrl} onLoad={handleLoad} className="size-full" />
</div>
)
@@ -7,9 +7,16 @@ import { useTranslation } from "react-i18next"
import { AIChatPanelStyle, setAIPanelVisibility, useAIChatPanelStyle } from "~/atoms/settings/ai"
import { useTimelineSummaryAutoContext } from "~/modules/ai-chat/hooks/useTimelineSummaryAutoContext"
import { useBlockActions, useChatActions, useCurrentTitle } from "~/modules/ai-chat/store/hooks"
import {
useBlockActions,
useChatActions,
useCurrentTitle,
useHasMessages,
} from "~/modules/ai-chat/store/hooks"
import { useSettingModal } from "~/modules/settings/modal/use-setting-modal-hack"
import { useAIRootState } from "../../store/AIChatContext"
import { AISpline } from "../3d-models/AISpline"
import { ChatHistoryDropdown } from "./ChatHistoryDropdown"
import { ChatMoreDropdown } from "./ChatMoreDropdown"
import { AIHeaderTitle } from "./ChatTitle"
@@ -25,11 +32,13 @@ const ChatHeaderLayout = ({
displayTitle: string | undefined
}) => ReactNode
}) => {
const hasMessages = useHasMessages()
const currentTitle = useCurrentTitle()
const chatActions = useChatActions()
const blockActions = useBlockActions()
const { t } = useTranslation("ai")
const shouldDisableTimelineSummary = useTimelineSummaryAutoContext()
const settingModalPresent = useSettingModal()
const displayTitle = currentTitle
@@ -72,6 +81,11 @@ const ChatHeaderLayout = ({
<div className="relative z-10 flex h-full items-center justify-between px-4">
<div className="mr-2 flex min-w-0 items-center">
{hasMessages && (
<div onClick={() => settingModalPresent("ai")}>
<AISpline className="-mx-1 -mb-1 mr-1 size-9" />
</div>
)}
<ChatHistoryDropdown
triggerElement={
<AIHeaderTitle title={displayTitle} placeholder={t("common.new_chat")} />
@@ -62,7 +62,7 @@ const DefaultWelcomeHeader = ({ description }: { description: string }) => (
animate={{ opacity: 1, y: 0 }}
className="space-y-6 text-center"
>
<div className="mx-auto size-16">
<div className="center">
<AISpline />
</div>
<div className="flex flex-col gap-2">
@@ -35,7 +35,7 @@ export const AIIndicator: FC = () => {
onClick={handleClick}
className={clsx(
"fixed bottom-8 right-8 z-40",
"size-16 rounded-2xl",
"rounded-2xl",
"hover:scale-105",
"active:scale-95",
"flex items-center justify-center",
@@ -198,7 +198,7 @@ function FeedSelectionFirstScreen() {
transition={{ delay: 0.1, duration: 0.6, type: "spring" }}
className="mb-6"
>
<div className="shadow-perfect mx-auto mb-4 flex size-16 items-center justify-center rounded-2xl">
<div className="mx-auto mb-4 flex items-center justify-center">
<AISplineLoader />
</div>
</m.div>