Skip to content

Commit

Permalink
Merge pull request #134 from HunnySajid/feat/permissions-badge
Browse files Browse the repository at this point in the history
feat: add badge when permissions  required
  • Loading branch information
HunnySajid committed Mar 14, 2024
2 parents 0de2fba + 80c2f8b commit ca69b89
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 21 deletions.
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

0 comments on commit ca69b89

Please sign in to comment.