Skip to content
This repository has been archived by the owner on Sep 26, 2024. It is now read-only.

Fixes to message routing in the extension #158

Merged
merged 3 commits into from
Aug 23, 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
69 changes: 38 additions & 31 deletions apps/mocksi-lite-next/src/pages/background/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,44 +74,51 @@ chrome.runtime.onMessage.addListener(
);

chrome.runtime.onMessageExternal.addListener(
async (request, _sender, sendResponse) => {
(request, _sender, sendResponse) => {
console.log("Received message from external:", request);

if (request.message === "UNAUTHORIZED") {
const auth = await getAuth();
if (auth) {
const { accessToken, email } = auth;
sendResponse({
message: { accessToken, email },
status: "ok",
});
} else {
// execute in async block so that we return true
// synchronously, telling chrome to wait for the response
(async () => {
if (request.message === "UNAUTHORIZED") {
const auth = await getAuth();
if (auth) {
const { accessToken, email } = auth;
sendResponse({
message: { accessToken, email },
status: "ok",
});
} else {
await showAuthTab();
sendResponse({
message: "authenticating",
status: "ok",
});
}
} else if (request.message === "AUTH_ERROR") {
await clearAuth();
await showAuthTab();
sendResponse({
message: "authenticating",
status: "ok",
});
} else {
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
if (tabs[0].id) {
chrome.tabs.sendMessage(tabs[0].id, {
data: request.data,
message: request.message,
}, (response) => {
sendResponse(response);
});
} else {
sendResponse({ message: request.message, status: "no-tab" });
console.log("No active tab found, could not send message");
}
});
}
} else if (request.message === "AUTH_ERROR") {
await clearAuth();
await showAuthTab();
sendResponse({
message: "authenticating",
status: "ok",
});
} else {
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
if (tabs[0].id) {
chrome.tabs.sendMessage(tabs[0].id, {
data: request.data,
message: request.message,
});
} else {
console.log("No active tab found, could not send message");
}
});
sendResponse({ message: request.message, status: "ok" });
return true;
}
})();

return true;
},
);
110 changes: 59 additions & 51 deletions apps/mocksi-lite-next/src/pages/content/mocksi-extension.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { ModificationRequest } from "@repo/reactor";
import { Reactor } from "@repo/reactor";
import { Reactor, AppliedModifications } from "@repo/reactor";
import React from "react";
import ReactDOM from "react-dom";
import { createRoot } from "react-dom/client";
Expand Down Expand Up @@ -62,60 +62,68 @@ chrome.runtime.onMessage.addListener((request) => {

React.useEffect(() => {
chrome.runtime.onMessage.addListener(
async (request, _sender, sendResponse) => {
// reactor
if (request.message === "EDITING") {
reactor.attach(document, getHighlighter());
}
if (request.message === "NEW_EDIT") {
if (request.data) {
const { find, highlightEdits, replace } = request.data;
const modifications = await findReplaceAll(
find,
replace,
highlightEdits,
);
(request, _sender, sendResponse) => {

// execute in async block so that we return true
// synchronously, telling chrome to wait for the response
(async () => {
let data = null;

// reactor
if (request.message === "EDITING") {
reactor.attach(document, getHighlighter());
}
}
if (request.message === "STOP_EDITING") {
reactor.detach();
}
// resize iframe
if (iframeRef.current) {
let styles = {};
switch (request.message) {
case "ANALYZING":
case "PLAY":
case "RECORDING":
styles = {
bottom: "auto",
height: "150px",
top: "0px",
width: "400px",
};
break;
case "MINIMIZED":
styles = {
bottom: "10px",
height: "100px",
top: "auto",
width: "100px",
};
break;
default:
styles = {
bottom: "10px",
height: "600px",
top: "auto",
width: "500px",
};
if (request.message === "NEW_EDIT") {
if (request.data) {
const { find, highlightEdits, replace } = request.data;
await findReplaceAll(
find,
replace,
highlightEdits,
);
data = Array.from(reactor.getAppliedModifications()).map((mod) => mod.modificationRequest);
}
}
if (request.message === "STOP_EDITING") {
reactor.detach();
}
// resize iframe
if (iframeRef.current) {
let styles = {};
switch (request.message) {
case "ANALYZING":
case "PLAY":
case "RECORDING":
styles = {
bottom: "auto",
height: "150px",
top: "0px",
width: "400px",
};
break;
case "MINIMIZED":
styles = {
bottom: "10px",
height: "100px",
top: "auto",
width: "100px",
};
break;
default:
styles = {
bottom: "10px",
height: "600px",
top: "auto",
width: "500px",
};
}

// set inline styles for iframe
Object.assign(iframeRef.current.style, styles);
}
// set inline styles for iframe
Object.assign(iframeRef.current.style, styles);
}

sendResponse({ message: request.message, status: "ok" });
sendResponse({ data: data, message: request.message, status: "ok" });
})();
return true;
},
);
Expand Down
Loading