Commit f2f2753a authored by Kevin Wu's avatar Kevin Wu

Close other tabs and windows when loading workspace

parent c68e9de9
......@@ -134,7 +134,8 @@ mutation addStepQuery(
workspace: { id, name },
});
// load branch heads for workspace
// load branch tails for workspace
// remember, the ID of the step doesn't matter, we want the ID of the branch
const query = `
query workspaceTailQuery($id: ID) {
viewer {
......@@ -142,6 +143,7 @@ query workspaceTailQuery($id: ID) {
branches {
tail {
url
timestamp
branch {
id
}
......
const MESSAGES = Object.freeze(
["SELECT_WORKSPACE"]
// enum-ify
.reduce((acc, val, i) => {
acc[val] = i;
return acc;
}, {}),
);
export default MESSAGES;
......@@ -3,7 +3,7 @@ import React, { Component } from "react";
import { get } from "Common/js/call";
import WorkspaceSelector from "Common/js/components/WorkspaceSelector";
import MESSAGES from "./Messages";
import MESSAGES from "Common/js/Messages";
const DOMAIN = require("../../../secrets.json")["domain"];
......@@ -30,6 +30,13 @@ class Popup extends Component {
})();
}
handleSelectWorkspace(workspace) {
chrome.runtime.sendMessage({
message: MESSAGES.SELECT_WORKSPACE,
...workspace,
});
}
render() {
let bodyComponent;
switch (this.state.authed) {
......@@ -44,7 +51,9 @@ class Popup extends Component {
bodyComponent = <Login />;
break;
case true:
bodyComponent = <WorkspaceSelector />;
bodyComponent = (
<WorkspaceSelector onSelectWorkspace={this.handleSelectWorkspace} />
);
break;
}
......
import Ext from "./Ext";
import MESSAGES from "./Messages";
import MESSAGES from "Common/js/Messages";
const ext = new Ext();
......@@ -50,18 +50,45 @@ chrome.runtime.onMessage.addListener(async ({ message, ...payload }) => {
const tabs = await ext.selectWorkspace(payload);
idMap.splice(0, idMap.length); // empty idMap without reassignment
tabs.forEach(({ branchId, url }) => {
chrome.tabs.create(
{
url,
},
({ id: newTabId }) => {
idMap[newTabId] = branchId;
console.log(idMap);
},
);
await Promise.all(
tabs
.sort(({ timestamp: a, timestamp: b }) => b - a)
.map(
({ branchId, url }) =>
new Promise(resolve => {
chrome.tabs.create(
{
url,
index: 0,
},
({ id: newTabId }) => {
idMap[newTabId] = branchId;
console.log(idMap);
resolve();
},
);
}),
),
);
// close tabs not found in ID map
const openTabs = await new Promise(resolve => {
chrome.tabs.query({}, tabs => resolve(tabs));
});
openTabs.forEach(({ id }) => {
if (!idMap[id]) {
chrome.tabs.remove(id);
}
});
// load continuum page if workspace has no tabs
if (tabs.length === 0) {
chrome.windows.create({
url: DOMAIN,
});
}
break;
default:
throw new Error("Invalid message: " + message);
......
const MESSAGES = ["SELECT_WORKSPACE"];
export default MESSAGES;
......@@ -2,7 +2,7 @@ import React, { Component } from "react";
import { request } from "graphql-request";
import Button from "Common/js/components/Button";
import { Creatable as CreatableSelect } from "react-select";
import MESSAGES from "../../../chrome/src/js/Messages";
import MESSAGES from "Common/js/Messages";
import { call } from "Common/js/call";
const ENDPOINT = require("../../../secrets.json")["domain"] + "/graphql";
......@@ -122,17 +122,10 @@ class WorkspaceSelector extends Component {
}
selectWorkspace(workspace) {
console.log("workspace:");
console.log(workspace);
this.setState({
selectedWorkspace: workspace,
});
chrome.runtime.sendMessage({
message: MESSAGES.SELECT_WORKSPACE,
...workspace,
});
if (typeof this.props.onSelectWorkspace === "function") {
this.props.onSelectWorkspace(workspace);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment