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

Prevent same content across pages #5

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
5 changes: 2 additions & 3 deletions static/spa/src/App.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import React from 'react';
import {invoke} from "@forge/bridge";
import {invoke, view} from "@forge/bridge";
import AppFactory from "./AppFactory";

const App = AppFactory(invoke);
const App = AppFactory(invoke, view);
export default App;
50 changes: 36 additions & 14 deletions static/spa/src/AppFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,31 @@ function compressDoc(doc) {
return {compressedJson: compressedJson};
}

export default function (invoke) {
export default function (invoke, view) {
return function App() {
const [isFetched, setIsFetched] = React.useState(false);
const [height, setHeight] = React.useState(400);
const [currentDocument, setCurrentDocument] = React.useState(defaultDocument);
const [allDoc, setAllDoc] = React.useState(defaultDocument);
let context;
const getContext = async () => context || (context = await view.getContext());

if (!isFetched) {
invoke('get-all').then((doc) => {
console.debug('[App] get-all', doc);
doc = decompressIfNecessary(doc);
if (doc && doc.id) {
console.debug('[App] get-all', doc);
Promise.all([invoke('get-all'), getContext()]).then(([doc, context]) => {
console.debug('[App] get-all - doc:', doc, ', context:', context);
const decompressed = decompressIfNecessary(doc);
setAllDoc(decompressed);
console.debug('[App] get-all - decompressed doc:', decompressed);

//decompressed.spaces is a new storage format introduced in Jan 2024
const singleDoc = decompressed && decompressed.spaces && decompressed.spaces[context.extension.space.id]?.contents[context.extension.content.id] || decompressed;

if (singleDoc && singleDoc.id) {
console.debug('[App] get-all - resolved doc:', singleDoc);
// TODO: allow assets when we have a way to upload them
// This also fix the issue that data cannot be correctly migrated
const fixedDoc = Object.assign({}, doc, {assets: {}});
console.debug('[App] get-all - fixed doc:', doc);
const fixedDoc = Object.assign({}, singleDoc, {assets: {}});
console.debug('[App] get-all - fixed doc:', singleDoc);

setCurrentDocument(fixedDoc);
setIsFetched(true);
Expand All @@ -57,7 +66,7 @@ export default function (invoke) {
});
}

function onPersist(app) {
async function onPersist(app) {
if (JSON.stringify(app.document) === JSON.stringify(currentDocument)) {
console.debug('[App] onPersist skipped');
return;
Expand All @@ -67,26 +76,39 @@ export default function (invoke) {

const merged = Object.assign(app.document, {viewport: {height}});
console.log('persist document - merged', merged);
saveToBackend(merged);
await saveToBackend(merged);
setCurrentDocument(merged);
mixpanel.track('Document Persisted', {
'Viewport Height': height
});
}

function onPersistViewport(h) {
async function onPersistViewport(h) {
console.log('persist viewport with height', h);
const merged = Object.assign(currentDocument, {viewport: {height: h}});
console.log('persist document', merged);
saveToBackend(merged);
await saveToBackend(merged);
setCurrentDocument(merged);
mixpanel.track('Viewport resize', {
'Viewport Height': height
});
}

function saveToBackend(doc) {
invoke('update', compressDoc(doc));
async function saveToBackend(doc) {
const {extension: {content: content, space: space}} = await getContext();
let newDoc = allDoc;
if(!newDoc.spaces) {
newDoc = {spaces: {[space.id]: {contents: {[content.id]: doc}}}};
} else {
if(!newDoc.spaces[space.id]) {
newDoc.spaces[space.id] = {contents: {}};
}
newDoc.spaces[space.id].contents[content.id] = doc;
setAllDoc(newDoc);
}

console.log('saveToBackend:', newDoc);
invoke('update', compressDoc(newDoc));
}

return (
Expand Down