({
pluginId,
slug,
starCount,
}: {
pluginId: string;
slug: string;
starCount: number;
})
| 10 | import { Button } from "../ui/button"; |
| 11 | |
| 12 | export function StarButton({ |
| 13 | pluginId, |
| 14 | slug, |
| 15 | starCount, |
| 16 | }: { |
| 17 | pluginId: string; |
| 18 | slug: string; |
| 19 | starCount: number; |
| 20 | }) { |
| 21 | const [isAuthenticated, setIsAuthenticated] = useState(false); |
| 22 | const [starred, setStarred] = useState(false); |
| 23 | const [count, setCount] = useState(starCount); |
| 24 | const [loaded, setLoaded] = useState(false); |
| 25 | const [isSignInModalOpen, setIsSignInModalOpen] = useState(false); |
| 26 | const prevRef = useRef({ starred: false, count: starCount }); |
| 27 | |
| 28 | useEffect(() => { |
| 29 | const supabase = createClient(); |
| 30 | supabase.auth.getSession().then(({ data: { session } }) => { |
| 31 | if (!session) { |
| 32 | setLoaded(true); |
| 33 | return; |
| 34 | } |
| 35 | setIsAuthenticated(true); |
| 36 | supabase |
| 37 | .from("plugin_stars") |
| 38 | .select("plugin_id") |
| 39 | .eq("plugin_id", pluginId) |
| 40 | .eq("user_id", session.user.id) |
| 41 | .maybeSingle() |
| 42 | .then(({ data }) => { |
| 43 | const isStarred = !!data; |
| 44 | setStarred(isStarred); |
| 45 | prevRef.current = { starred: isStarred, count: starCount }; |
| 46 | setLoaded(true); |
| 47 | }); |
| 48 | }); |
| 49 | }, [pluginId, starCount]); |
| 50 | |
| 51 | const { execute } = useAction(starPluginAction, { |
| 52 | onError: () => { |
| 53 | setStarred(prevRef.current.starred); |
| 54 | setCount(prevRef.current.count); |
| 55 | }, |
| 56 | }); |
| 57 | |
| 58 | const handleClick = () => { |
| 59 | if (!isAuthenticated) { |
| 60 | setIsSignInModalOpen(true); |
| 61 | return; |
| 62 | } |
| 63 | |
| 64 | prevRef.current = { starred, count }; |
| 65 | setStarred(!starred); |
| 66 | setCount(starred ? count - 1 : count + 1); |
| 67 | execute({ pluginId, slug }); |
| 68 | }; |
| 69 |
nothing calls this directly
no test coverage detected