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

feat: add badge when permissions permissions #134

Merged
merged 1 commit into from
Mar 14, 2024
Merged
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
30 changes: 27 additions & 3 deletions src/pages/background/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,24 @@ chrome.runtime.onMessage.addListener(function (
});
}

if (
message.type === "action-icon" &&
message.subtype === "set-tab-action-icon"
) {
chrome.action.setBadgeBackgroundColor({ color: "#008000" }, () => {
chrome.action.setBadgeText({ tabId: sender.tab?.id, text: "1" });
sendResponse({ data: { success: true } });
});
}

if (
message.type === "action-icon" &&
message.subtype === "unset-tab-action-icon"
) {
chrome.action.setBadgeText({ tabId: sender.tab?.id, text: "" });
sendResponse({ data: { success: true } });
}

if (
message.type === "action-icon" &&
message.subtype === "unset-action-icon"
Expand All @@ -85,12 +103,10 @@ chrome.runtime.onMessage.addListener(function (
message.type === "vendor-info" &&
message.subtype === "attempt-set-vendor-url"
) {
const currentUrl = await getCurrentUrl();
const { vendorUrl } = message?.data ?? {};

await configService.setWebRequestedPermission(
WEB_APP_PERMS.SET_VENDOR_URL,
{ origin: currentUrl?.origin, vendorUrl }
{ origin: sender.tab.url, vendorUrl }
);
sendResponse({ data: { success: true } });
}
Expand Down Expand Up @@ -177,6 +193,14 @@ chrome.runtime.onMessage.addListener(function (
message.subtype
);

if (
message.type === "action-icon" &&
message.subtype === "unset-action-icon"
) {
chrome.action.setBadgeText({ text: "" });
sendResponse({ data: { success: true } });
}

if (
message.type === "authentication" &&
message.subtype === "check-agent-connection"
Expand Down
8 changes: 6 additions & 2 deletions src/pages/content/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ window.addEventListener(
case TAB_STATE.SELECT_AUTO_SIGNIN:
await chrome.runtime.sendMessage<IMessage<void>>({
type: "action-icon",
subtype: "set-action-icon",
subtype: "set-tab-action-icon",
});
const respVendorData = await chrome.runtime.sendMessage<
IMessage<void>
Expand Down Expand Up @@ -71,6 +71,10 @@ window.addEventListener(
break;
case "vendor-info":
if (event.data.subtype === "attempt-set-vendor-url") {
await chrome.runtime.sendMessage<IMessage<void>>({
type: "action-icon",
subtype: "set-action-icon",
});
await chrome.runtime.sendMessage(chrome.runtime.id, {
type: "vendor-info",
subtype: "attempt-set-vendor-url",
Expand Down Expand Up @@ -208,7 +212,7 @@ function removeDialog() {

chrome.runtime.sendMessage<IMessage<void>>({
type: "action-icon",
subtype: "unset-action-icon",
subtype: "unset-tab-action-icon",
});
}

Expand Down
31 changes: 15 additions & 16 deletions src/screens/permission/permission.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
} from "@pages/background/services/config";
import { isValidUrl, setActionIcon } from "@pages/background/utils";
import { Card, Button, Text } from "@components/ui";
import { IMessage } from "@config/types";

interface IPermissions {
permissionData: any;
Expand Down Expand Up @@ -55,6 +56,16 @@ export function Permission({
}
};

const removePostPermissionFlags = async () => {
await configService.setWebRequestedPermission(
WEB_APP_PERMS.SET_VENDOR_URL,
"delete"
);
await chrome.runtime.sendMessage<IMessage<void>>({
type: "action-icon",
subtype: "unset-action-icon",
});
};
const handleSetAgentUrl = async (_url: string) => {
const hasError = await checkErrorAgentUrl(_url);
if (hasError) return;
Expand Down Expand Up @@ -91,19 +102,13 @@ export function Permission({
}
if (!hasError) {
await configService.setUrl(permissionData?.vendorUrl);
await configService.setWebRequestedPermission(
WEB_APP_PERMS.SET_VENDOR_URL,
"delete"
);
await removePostPermissionFlags();
afterCallback();
}
};

const handleCancel = async () => {
await configService.setWebRequestedPermission(
WEB_APP_PERMS.SET_VENDOR_URL,
"delete"
);
await removePostPermissionFlags();
afterCallback();
};

Expand All @@ -113,10 +118,7 @@ export function Permission({
await setActionIcon(receivedVendorData?.icon);
}

await configService.setWebRequestedPermission(
WEB_APP_PERMS.SET_VENDOR_URL,
"delete"
);
await removePostPermissionFlags();
afterCallback();
};

Expand All @@ -130,10 +132,7 @@ export function Permission({
await setActionIcon(receivedVendorData?.icon);
}

await configService.setWebRequestedPermission(
WEB_APP_PERMS.SET_VENDOR_URL,
"delete"
);
await removePostPermissionFlags();
handleDisconnect();
};

Expand Down