Skip to content

Expire spending limit notifications #12467

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

Merged
merged 1 commit into from
Aug 29, 2022
Merged
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
22 changes: 16 additions & 6 deletions components/dashboard/src/AppNotifications.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { useEffect, useState } from "react";
import Alert from "./components/Alert";
import { getGitpodService } from "./service/service";

const KEY_APP_NOTIFICATIONS = "KEY_APP_NOTIFICATIONS";
const KEY_APP_NOTIFICATIONS = "gitpod-app-notifications";

export function AppNotifications() {
const [notifications, setNotifications] = useState<string[]>([]);
Expand All @@ -22,7 +22,9 @@ export function AppNotifications() {
(async () => {
const serverState = await getGitpodService().server.getNotifications();
setNotifications(serverState);
setLocalStorageObject(KEY_APP_NOTIFICATIONS, serverState);
if (serverState.length > 0) {
setLocalStorageObject(KEY_APP_NOTIFICATIONS, serverState, /* expires in */ 60 /* seconds */);
}
})();
}, []);

Expand Down Expand Up @@ -57,19 +59,27 @@ function getLocalStorageObject(key: string): any {
if (!string) {
return;
}
return JSON.parse(string);
const stored = JSON.parse(string);
if (Date.now() > stored.expirationTime) {
window.localStorage.removeItem(key);
return undefined;
}
return stored.value;
} catch (error) {
return;
window.localStorage.removeItem(key);
}
}

function removeLocalStorageObject(key: string): void {
window.localStorage.removeItem(key);
}

function setLocalStorageObject(key: string, object: Object): void {
function setLocalStorageObject(key: string, object: Object, expiresInSeconds: number): void {
try {
window.localStorage.setItem(key, JSON.stringify(object));
window.localStorage.setItem(
key,
JSON.stringify({ expirationTime: Date.now() + expiresInSeconds * (1000 * 60), value: object }),
);
} catch (error) {
console.error("Setting localstorage item failed", key, object, error);
}
Expand Down