Elffuss BraidHub

kikocis/continue public

main · 2.6 KiBRawPermalink
1import { ModelConfig } from "@continuedev/config-yaml";
2import {
3 DefaultApiInterface,
4 GetFreeTrialStatus200Response,
5} from "@continuedev/sdk/dist/api/dist/index.js";
6import { Text } from "ink";
7import React, { useEffect, useState } from "react";
8
9export function isModelUsingFreeTrial(model: ModelConfig): boolean {
10 return (
11 model.provider === "continue-proxy" &&
12 (model as any).apiKeyLocation.startsWith("free_trial:")
13 );
14}
15
16interface FreeTrialStatusProps {
17 apiClient?: DefaultApiInterface;
18 onTransitionStateChange?: (isShowingTransition: boolean) => void;
19 model: ModelConfig;
20}
21
22const FreeTrialStatus: React.FC<FreeTrialStatusProps> = ({
23 apiClient,
24 onTransitionStateChange,
25 model,
26}) => {
27 const [status, setStatus] = useState<GetFreeTrialStatus200Response | null>(
28 null,
29 );
30 const [loading, setLoading] = useState(true);
31
32 const fetchStatus = async () => {
33 try {
34 if (!apiClient) {
35 setStatus(null);
36 setLoading(false);
37 return;
38 }
39
40 const response = await apiClient.getFreeTrialStatus();
41 setStatus(response);
42 setLoading(false);
43 } catch {
44 // Silently handle errors - component returns null if no status
45 setStatus(null);
46 setLoading(false);
47 }
48 };
49
50 useEffect(() => {
51 // Initial fetch
52 fetchStatus();
53
54 // Don't poll in test environment
55 if (process.env.NODE_ENV === "test") {
56 return;
57 }
58
59 // Poll every 5 seconds
60 const interval = setInterval(fetchStatus, 5000);
61
62 return () => clearInterval(interval);
63 }, []);
64
65 // Check if user has maxed out their free trial and notify parent
66 useEffect(() => {
67 if (!status || !status.optedInToFreeTrial || loading) {
68 // No transition needed
69 if (onTransitionStateChange) {
70 onTransitionStateChange(false);
71 }
72 return;
73 }
74
75 const chatUsed = status.chatCount ?? 0;
76 const chatLimit = status.chatLimit;
77
78 // Check if user has maxed out their free trial
79 const shouldShowTransition =
80 chatUsed >= chatLimit && isModelUsingFreeTrial(model);
81
82 if (onTransitionStateChange) {
83 onTransitionStateChange(shouldShowTransition);
84 }
85 }, [status, loading, onTransitionStateChange, model]);
86
87 // Don't render anything while loading or if no status
88 if (
89 loading ||
90 !status ||
91 !status.optedInToFreeTrial ||
92 !isModelUsingFreeTrial(model)
93 ) {
94 return null;
95 }
96
97 const chatUsed = status.chatCount ?? 0;
98 const chatLimit = status.chatLimit;
99
100 // Only show the normal status text - transition UI is handled by parent
101 return (
102 <Text color="dim">
103 {chatUsed}/{chatLimit} free trial
104 </Text>
105 );
106};
107
108export { FreeTrialStatus };