Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added credit usage warning on 80% #84

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions gui/src/components/Layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import ProgressBar from "./loaders/ProgressBar";
import PostHogPageView from "./PosthogPageView";
import ProfileSwitcher from "./ProfileSwitcher";
import ShortcutContainer from "./ShortcutContainer";
import UsageWarning from "./usagesWarning";

// check mac or window
const platform = navigator.userAgent.toLowerCase();
Expand Down Expand Up @@ -291,6 +292,7 @@ const Layout = () => {
<Outlet />
<ModelDropdownPortalDiv id="model-select-top-div"></ModelDropdownPortalDiv>
<ProfileDropdownPortalDiv id="profile-select-top-div"></ProfileDropdownPortalDiv>
<UsageWarning />
{HIDE_FOOTER_ON_PAGES.includes(location.pathname) || (
<Footer>
<div className="mr-auto flex flex-grow gap-2 items-center overflow-hidden">
Expand Down
133 changes: 133 additions & 0 deletions gui/src/components/usagesWarning.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
import { useState, useEffect } from 'react';
import { AlertTriangle, X } from 'lucide-react';
import { SERVER_URL } from "../../../core/util/parameters";
import * as vscode from 'vscode';

export default function UsageWarning() {
const [isVisible, setIsVisible] = useState(false);
const [message, setMessage] = useState("");

const token = localStorage.getItem('userToken') || "";
const url = `${SERVER_URL}/get-usage`;

const getMessage = async () => {
try {
const res = await fetch(url, {
method: 'GET',
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${token}`,
}
});

if (!res.ok) {
console.log('Error fetching usage data');
return;
}

const { warning_message } = await res.json();
if (warning_message) {
setMessage(warning_message);
setIsVisible(true);
vscode.window.showWarningMessage(warning_message);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does vscode.window work this way in react 👀 isnt working for me

Copy link
Author

@itzamanjain itzamanjain Nov 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

based on prev patterns used in code base i used this so we can native notification warning

} else {
setMessage("");
setIsVisible(false);
}
} catch (error) {
console.log('Error fetching usage data:', error);
}
};

useEffect(() => {
getMessage();
}, []);
Comment on lines +42 to +44

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this will only fetch when component gets mounted, so if users is overusing credits it in a single go in one session, he wouldn't get notified right.

Copy link
Author

@itzamanjain itzamanjain Nov 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thinking of using setInterval to poll every 5 minutes for credit usage—open to better ideas if anyone has them!


const handleClose = () => {
setIsVisible(false);
};

if (!isVisible) return null;

return (
<div
style={{
position: 'fixed',
bottom: '20px',
right: '20px',
backgroundColor: 'rgba(255, 250, 220, 0.97)',
borderRadius: '8px',
boxShadow: '0 4px 6px rgba(0, 0, 0, 0.1), 0 1px 3px rgba(0, 0, 0, 0.08)',
padding: '12px 16px',
maxWidth: '300px',
zIndex: 1000,
animation: 'slideIn 0.3s ease-out',
border: '1px solid rgba(251, 191, 36, 0.3)',
}}
>
<div style={{ position: 'relative', paddingRight: '24px' }}>
<button
onClick={handleClose}
style={{
position: 'absolute',
top: '-4px',
right: '-8px',
background: 'none',
border: 'none',
cursor: 'pointer',
padding: '4px',
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
borderRadius: '50%',
transition: 'background-color 0.2s',
}}
aria-label="Close warning"
>
<X
size={18}
style={{
color: '#92400e',
}}
/>
</button>
<div style={{ display: 'flex', alignItems: 'flex-start' }}>
<AlertTriangle
size={20}
style={{
color: '#d97706',
marginRight: '12px',
flexShrink: 0,
marginTop: '2px',
}}
/>
<div
style={{
flex: 1,
fontSize: '12px',
fontWeight: 500,
color: '#92400e',
}}
>
{message}
</div>
</div>
</div>
<style>{`
@keyframes slideIn {
from {
transform: translateY(100%);
opacity: 0;
}
to {
transform: translateY(0);
opacity: 1;
}
}
button:hover {
background-color: rgba(251, 191, 36, 0.2);
}
`}</style>
</div>
);
}