Commit c68e9de9 authored by Kevin Wu's avatar Kevin Wu

Implement loading workspace

parent 82fa7252
......@@ -31,8 +31,8 @@ mutation addBranchQuery(
}`;
const [result, err] = await call(
request(ENDPOINT, query, {
url: url,
title: title,
url,
title,
workspaceId: this.selectedWorkspace.id,
}),
);
......@@ -46,12 +46,6 @@ mutation addBranchQuery(
}
async createChildBranch(parentBranchId, { id, url, title, openerTabId }) {
if (!parentBranchId) {
console.log("Ignoring new child on untracked branch: ");
console.log(url, title, openerTabId);
return;
}
const query = `
mutation addBranchQuery(
$url: String!,
......@@ -74,8 +68,8 @@ mutation addBranchQuery(
const [result, err] = await call(
request(ENDPOINT, query, {
url: url,
title: title,
url,
title,
parent: parentBranchId,
workspaceId: this.selectedWorkspace.id,
}),
......@@ -92,10 +86,10 @@ mutation addBranchQuery(
return result.addBranch.id;
}
async addStep(branchId, { id, url, title, favIconUrl = null }) {
async addStep(branchId, { id: tabId, url, title, favIconUrl = null }) {
if (!branchId) {
console.log("Ignoring step on untracked branch:");
console.log(id, url, title);
console.log(tabId, url, title);
return;
}
......@@ -118,9 +112,9 @@ mutation addStepQuery(
const [result, err] = await call(
request(ENDPOINT, query, {
url: url,
title: title,
branchId: branchId,
url,
title,
branchId,
workspaceId: this.selectedWorkspace.id,
}),
);
......@@ -134,12 +128,50 @@ mutation addStepQuery(
console.log(result);
}
selectWorkspace({ id, name }) {
console.log("Selected workspace:", id, name);
async selectWorkspace({ id, name }) {
this.selectedWorkspace = { id, name };
chrome.storage.local.set({
workspace: { id, name },
});
// load branch heads for workspace
const query = `
query workspaceTailQuery($id: ID) {
viewer {
workspace(workspaceId: $id) {
branches {
tail {
url
branch {
id
}
}
}
}
}
}
`;
const [result, err] = await call(request(ENDPOINT, query, { id }));
if (err) {
console.log("Error:");
console.log(err);
return;
}
const {
viewer: {
workspace: { branches },
},
} = result;
console.log(branches);
return branches.map(({ tail: { url, branch: { id: branchId } } }) => ({
url,
branchId,
}));
}
}
......
......@@ -5,6 +5,7 @@ const ext = new Ext();
const DOMAIN = require("../../../secrets.json")["domain"];
//idMap[chrome tab id] = continuum branch id
const idMap = [];
chrome.tabs.onCreated.addListener(async tab => {
......@@ -12,11 +13,16 @@ chrome.tabs.onCreated.addListener(async tab => {
return;
}
if (tab.url === "chrome://newtab/") {
if (tab.url === "chrome://newtab/" && !idMap.includes(tab.id)) {
idMap[tab.id] = await ext.createNewBranch(tab);
} else if (tab.url.startsWith("chrome://") || tab.url.startsWith(DOMAIN)) {
return;
} else {
if (!idMap[tab.openerTabId]) {
console.log("Ignoring new child on untracked branch: ");
console.log(tab.url, tab.title, tab.openerTabId);
return;
}
idMap[tab.id] = await ext.createChildBranch(idMap[tab.openerTabId], tab);
}
});
......@@ -38,8 +44,26 @@ chrome.tabs.onUpdated.addListener(async (tabId, changeInfo, tab) => {
// background serves as central dispatch for the extension.
// content and popup scripts initiate actions by sending messages here.
chrome.runtime.onMessage.addListener(({ message, ...payload }) => {
if (message === MESSAGES.SELECT_WORKSPACE) {
ext.selectWorkspace(payload);
chrome.runtime.onMessage.addListener(async ({ message, ...payload }) => {
switch (message) {
case MESSAGES.SELECT_WORKSPACE:
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);
},
);
});
break;
default:
throw new Error("Invalid message: " + message);
}
});
......@@ -62,12 +62,12 @@ class Timeline extends Component {
}
render() {
if(!this.state.ready) {
return <p>Loading...</p>
if (!this.state.ready) {
return <p>Loading...</p>;
}
if(this.state.error) {
return <p>Error</p>
if (this.state.error) {
return <p>Error</p>;
}
const stepArr = this.state.steps;
......
......@@ -38,7 +38,7 @@ const Mutation = new GraphQLObjectType({
throw new Error("Internal server error.");
}
if (result.length !== 1) {
throw new Error("Invalid branch.");
throw new Error("Invalid branch: " + args.initialStep.parent);
}
if (result[0].successor) {
console.log("Latest step already had a successor:");
......@@ -104,7 +104,7 @@ const Mutation = new GraphQLObjectType({
throw new Error("Internal server error.");
}
if (result.length !== 1) {
throw new Error("Invalid branch.");
throw new Error("Invalid branch: " + args.step.branch);
}
if (result[0].successor) {
console.log("Latest step already had a successor:");
......
......@@ -178,15 +178,6 @@ const Step = new GraphQLObjectType({
throw new Error("Internal server error.");
}
if (result.length !== 1) {
console.log(
"Expected exactly one result for workspace but got " +
result.length,
);
console.log(result);
throw new Error("Internal server error.");
}
return result[0];
},
},
......
......@@ -87,30 +87,40 @@ const User = new GraphQLObjectType({
workspace: {
type: Workspace,
async resolve(parent, args, request) {
const [result, err] = await db(
request
.knex("workspaces")
.select("id", "name")
.where({
owner: request.session.userid,
id: args.workspaceId,
}),
);
if (
args.workspaceId === null ||
typeof args.workspaceId === "undefined"
) {
return {
id: null,
name: "None",
};
} else {
const [result, err] = await db(
request
.knex("workspaces")
.select("id", "name")
.where({
owner: request.session.userid,
id: args.workspaceId,
}),
);
if (result.length !== 1) {
throw new Error("Invalid workspace.");
}
if (err) {
console.log(err);
throw new Error("Internal server error.");
}
if (err) {
console.log(err);
throw new Error("Internal server error.");
}
if (result.length !== 1) {
throw new Error("Invalid workspace.");
}
return result[0];
return result[0];
}
},
args: {
workspaceId: {
type: new GraphQLNonNull(GraphQLID),
type: GraphQLID,
},
},
},
......
......@@ -3,6 +3,7 @@ const PQueue = require("p-queue");
const pq = new PQueue({ concurrency: 1 });
module.exports = {
// TODO replace all usages of this with "call"
// wrapper function to make queries Go-like
db: async query => {
try {
......
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