Raw
1 import { motion } from "motion/react"
2 import React, { useEffect, useState } from "react"
3
4 interface AiAssistantLoadingProps {
5 className?: string
6 size?: number
7 state?: string
8 }
9
10 const patterns = [
11 {
12 squares: [
13 { row: 1, col: 2, color: "bg-zinc-200" },
14 { row: 1, col: 3, color: "bg-zinc-400" },
15 { row: 2, col: 1, color: "bg-zinc-200" },
16 { row: 2, col: 2, color: "bg-zinc-600" },
17 { row: 2, col: 3, color: "bg-zinc-400" },
18 { row: 2, col: 4, color: "bg-zinc-200" },
19 { row: 3, col: 1, color: "bg-zinc-200" },
20 { row: 3, col: 2, color: "bg-zinc-400" },
21 { row: 3, col: 3, color: "bg-zinc-600" },
22 { row: 3, col: 4, color: "bg-zinc-200" },
23 { row: 4, col: 2, color: "bg-zinc-400" },
24 { row: 4, col: 3, color: "bg-zinc-200" },
25 ],
26 },
27 {
28 squares: [
29 { row: 1, col: 1, color: "bg-zinc-500" },
30 { row: 1, col: 4, color: "bg-zinc-500" },
31 { row: 2, col: 1, color: "bg-zinc-200" },
32 { row: 2, col: 2, color: "bg-zinc-400" },
33 { row: 2, col: 3, color: "bg-zinc-400" },
34 { row: 2, col: 4, color: "bg-zinc-200" },
35 { row: 3, col: 1, color: "bg-zinc-200" },
36 { row: 3, col: 2, color: "bg-zinc-400" },
37 { row: 3, col: 3, color: "bg-zinc-400" },
38 { row: 3, col: 4, color: "bg-zinc-200" },
39 { row: 4, col: 1, color: "bg-zinc-500" },
40 { row: 4, col: 4, color: "bg-zinc-500" },
41 ],
42 },
43 {
44 squares: [
45 { row: 1, col: 1, color: "bg-zinc-300" },
46 { row: 1, col: 2, color: "bg-zinc-300" },
47 { row: 1, col: 3, color: "bg-zinc-300" },
48 { row: 1, col: 4, color: "bg-zinc-300" },
49 { row: 2, col: 1, color: "bg-zinc-400" },
50 { row: 2, col: 2, color: "bg-zinc-400" },
51 { row: 2, col: 3, color: "bg-zinc-400" },
52 { row: 2, col: 4, color: "bg-zinc-400" },
53 { row: 3, col: 1, color: "bg-zinc-500" },
54 { row: 3, col: 2, color: "bg-zinc-500" },
55 { row: 3, col: 3, color: "bg-zinc-500" },
56 { row: 3, col: 4, color: "bg-zinc-500" },
57 { row: 4, col: 1, color: "bg-zinc-200" },
58 { row: 4, col: 2, color: "bg-zinc-200" },
59 { row: 4, col: 3, color: "bg-zinc-200" },
60 { row: 4, col: 4, color: "bg-zinc-200" },
61 ],
62 },
63 {
64 squares: [
65 { row: 1, col: 2, color: "bg-zinc-300" },
66 { row: 1, col: 3, color: "bg-zinc-200" },
67 { row: 2, col: 1, color: "bg-zinc-200" },
68 { row: 2, col: 2, color: "bg-zinc-500" },
69 { row: 2, col: 3, color: "bg-zinc-500" },
70 { row: 3, col: 2, color: "bg-zinc-300" },
71 { row: 3, col: 3, color: "bg-zinc-400" },
72 { row: 4, col: 1, color: "bg-zinc-400" },
73 { row: 4, col: 2, color: "bg-zinc-300" },
74 { row: 4, col: 4, color: "bg-zinc-200" },
75 ],
76 },
77 {
78 squares: [
79 { row: 1, col: 2, color: "bg-zinc-200" },
80 { row: 1, col: 3, color: "bg-zinc-300" },
81 { row: 1, col: 4, color: "bg-zinc-400" },
82 { row: 2, col: 2, color: "bg-zinc-400" },
83 { row: 2, col: 3, color: "bg-zinc-500" },
84 { row: 2, col: 4, color: "bg-zinc-500" },
85 { row: 3, col: 2, color: "bg-zinc-500" },
86 { row: 3, col: 3, color: "bg-zinc-400" },
87 { row: 3, col: 4, color: "bg-zinc-200" },
88 { row: 4, col: 1, color: "bg-zinc-300" },
89 ],
90 },
91 {
92 squares: [
93 { row: 1, col: 1, color: "bg-zinc-300" },
94 { row: 2, col: 1, color: "bg-zinc-400" },
95 { row: 2, col: 2, color: "bg-zinc-400" },
96 { row: 2, col: 3, color: "bg-zinc-400" },
97 { row: 3, col: 1, color: "bg-zinc-500" },
98 { row: 3, col: 3, color: "bg-zinc-500" },
99 { row: 3, col: 4, color: "bg-zinc-500" },
100 { row: 4, col: 2, color: "bg-zinc-200" },
101 { row: 4, col: 3, color: "bg-zinc-200" },
102 ],
103 },
104 ]
105
106 const colorMap = {
107 "bg-zinc-200": "#e4e4e7",
108 "bg-zinc-300": "#d4d4d8",
109 "bg-zinc-400": "#a1a1aa",
110 "bg-zinc-500": "#71717a",
111 "bg-zinc-600": "#52525b",
112 }
113
114 export function AiAssistantLoading() {
115 const [currentPattern, setCurrentPattern] = useState(0)
116
117 useEffect(() => {
118 const interval = setInterval(() => {
119 setCurrentPattern((prev) => (prev + 1) % patterns.length)
120 }, 800)
121
122 return () => clearInterval(interval)
123 }, [])
124
125 const allPositions = []
126 for (let row = 1; row <= 4; row++) {
127 for (let col = 1; col <= 4; col++) {
128 allPositions.push({ row, col })
129 }
130 }
131
132 const currentSquares = patterns[currentPattern].squares
133
134 return (
135 <div className="relative shrink-0 size-[15px]">
136 <div className="absolute gap-[1.5px] grid grid-cols-[fit-content(100%)_fit-content(100%)_fit-content(100%)_fit-content(100%)] grid-rows-[fit-content(100%)_fit-content(100%)_fit-content(100%)_fit-content(100%)] left-1/2 size-[12.5px] top-1/2 translate-x-[-50%] translate-y-[-50%]">
137 {allPositions.map(({ row, col }) => {
138 const square = currentSquares.find(
139 (s) => s.row === row && s.col === col
140 )
141
142 return (
143 <motion.div
144 key={`${row}-${col}`}
145 className={`[grid-area:${row}_/_${col}] shrink-0 size-[2px]`}
146 animate={{
147 backgroundColor: square
148 ? colorMap[square.color as keyof typeof colorMap]
149 : "rgba(0, 0, 0, 0)",
150 opacity: square ? 1 : 0,
151 }}
152 transition={{
153 duration: 0.4,
154 ease: "easeInOut",
155 }}
156 />
157 )
158 })}
159 </div>
160 </div>
161 )
162 }
163
164 export const BuildingLoader: React.FC<AiAssistantLoadingProps> = ({
165 className = "",
166 state = "Loading",
167 }) => {
168 return (
169 <div
170 className={`inline-flex items-center gap-docs_0.5 mt-docs_1 ${className}`}
171 >
172 <AiAssistantLoading />
173
174 <div className="relative overflow-hidden">
175 <span className="txt-small-plus text-ui-fg-subtle">
176 {state ? `${state}...` : ""}
177 </span>
178 <div className="absolute inset-0 -translate-x-full animate-[shimmer_1.5s_linear_infinite] bg-gradient-to-r from-transparent via-white/60 to-transparent" />
179 </div>
180
181 <style>{`
182 @keyframes shimmer {
183 0% { transform: translateX(-100%); }
184 100% { transform: translateX(200%); }
185 }
186 `}</style>
187 </div>
188 )
189 }