Skip to content

Commit

Permalink
Merge pull request #865 from 3DStreet/autosave
Browse files Browse the repository at this point in the history
autosave
  • Loading branch information
kfarr committed Sep 27, 2024
2 parents 093ca70 + e7de9a7 commit b8859f1
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 18 deletions.
11 changes: 7 additions & 4 deletions src/components/notify.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,22 @@ AFRAME.registerComponent('notify', {
if (STREET) {
STREET.notify = {};
STREET.notify.successMessage = (messageText) => {
this.message(messageText, 'success');
return this.message(messageText, 'success');
};
STREET.notify.errorMessage = (messageText) => {
this.message(messageText, 'error');
return this.message(messageText, 'error');
};
STREET.notify.warningMessage = (messageText) => {
this.message(messageText, 'warning');
return this.message(messageText, 'warning');
};
STREET.notify.dismissNotification = (notification) => {
return this.notify.dismiss(notification);
};
}
},
message: function (messageText, messageType = 'info') {
if (messageText && this.types.includes(messageType)) {
this.notify.open({
return this.notify.open({
type: messageType,
message: messageText
});
Expand Down
2 changes: 1 addition & 1 deletion src/editor/api/scene.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ const isSceneAuthor = async ({ sceneId, authorId }) => {
}
} catch (error) {
console.error('Error fetching scene while running isSceneAuthor:', error);
return false;
throw new Error('Error checking scene authorship');
}
};

Expand Down
56 changes: 45 additions & 11 deletions src/editor/components/scenegraph/Toolbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ import {
import Events from '../../lib/Events';
import { saveBlob } from '../../lib/utils';
import { Button, ProfileButton } from '../components';
import { SavingModal } from '../modals/SavingModal';
import { uploadThumbnailImage } from '../modals/ScreenshotModal/ScreenshotModal.component.jsx';
import { sendMetric } from '../../services/ga.js';
import posthog from 'posthog-js';
import { UndoRedo } from '../components/UndoRedo';
import debounce from 'lodash-es/debounce';
// const LOCALSTORAGE_MOCAP_UI = "aframeinspectormocapuienabled";

function filterHelpers(scene, visible) {
Expand Down Expand Up @@ -65,23 +65,29 @@ export default class Toolbar extends Component {
isSavingScene: false,
pendingSceneSave: false,
signInSuccess: false,
isAuthor: props.isAuthor
isAuthor: props.isAuthor,
notification: null
};
this.saveButtonRef = React.createRef();
}

componentDidMount() {
document.addEventListener('click', this.handleClickOutsideSave);
this.checkSignInStatus();
Events.on('historychanged', (cmd) => {
if (cmd) {
console.log('historychanged', cmd);
// Debounce the cloudSaveHandler call
this.debouncedCloudSaveHandler();
}
});
}

componentDidUpdate(prevProps) {
if (prevProps.isAuthor !== this.props.isAuthor) {
this.setState({ isAuthor: this.props.isAuthor });
}
if (this.props.currentUser !== prevProps.currentUser) {
console.log('component updated');
console.log(this.props);
this.setState({ currentUser: this.props.currentUser });

if (this.state.pendingSceneSave && this.props.currentUser) {
Expand Down Expand Up @@ -172,6 +178,9 @@ export default class Toolbar extends Component {

cloudSaveHandler = async ({ doSaveAs = false }) => {
try {
if (this.state.notification) {
STREET.notify.dismissNotification(this.state.notification);
}
// if there is no current user, show sign in modal
let currentSceneId = STREET.utils.getCurrentSceneId();
let currentSceneTitle = STREET.utils.getCurrentSceneTitle();
Expand All @@ -184,6 +193,7 @@ export default class Toolbar extends Component {
});

if (!this.props.currentUser) {
console.log('no user');
Events.emit('opensigninmodal');
return;
}
Expand All @@ -201,12 +211,16 @@ export default class Toolbar extends Component {
Events.emit('openpaymentmodal');
return;
}

let isCurrentUserTheSceneAuthor;
// if owner != doc.id then doSaveAs = true;
const isCurrentUserTheSceneAuthor = await isSceneAuthor({
sceneId: currentSceneId,
authorId: this.props.currentUser.uid
});
try {
isCurrentUserTheSceneAuthor = await isSceneAuthor({
sceneId: currentSceneId,
authorId: this.props.currentUser.uid
});
} catch (error) {
return;
}

if (!isCurrentUserTheSceneAuthor) {
doSaveAs = true;
Expand Down Expand Up @@ -269,9 +283,11 @@ export default class Toolbar extends Component {
);
this.setState({ savedNewDocument: false }); // go back to default assumption of save overwrite
} else {
STREET.notify.successMessage(
const notification = STREET.notify.successMessage(
'Scene saved to 3DStreet Cloud in existing file.'
);
this.setState({ notification });
console.log('message', notification);
}
this.setState({ isAuthor: true });
sendMetric('SaveSceneAction', doSaveAs ? 'saveAs' : 'save');
Expand All @@ -285,6 +301,24 @@ export default class Toolbar extends Component {
}
};

debouncedCloudSaveHandler = debounce(() => {
if (this.state.currentUser) {
const streetGeo = document
.getElementById('reference-layers')
?.getAttribute('street-geo');
if (
!this.props.currentUser.isPro &&
streetGeo &&
streetGeo['latitude'] &&
streetGeo['longitude']
) {
Events.emit('openpaymentmodal');
return;
}
this.cloudSaveHandler({ doSaveAs: false });
}
}, 500);

handleRemixClick = () => {
posthog.capture('remix_scene_clicked');
if (!this.props.currentUser) {
Expand Down Expand Up @@ -397,10 +431,10 @@ export default class Toolbar extends Component {
<Button
leadingIcon={<Save24Icon />}
onClick={this.toggleSaveActionState.bind(this)}
disabled={this.state.isSavingScene}
>
<div className="hideInLowResolution">Save</div>
</Button>
{this.state.isSavingScene && <SavingModal />}
{this.state.isSaveActionActive && (
<div className="dropdownedButtons">
<Button
Expand Down
4 changes: 2 additions & 2 deletions src/editor/lib/history.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export class History {
this.undos = [];
this.redos = [];
this.idCounter = 0;

Events.emit('historychanged');
console.log('history cleared');
Events.emit('historychanged', null);
}
}

0 comments on commit b8859f1

Please sign in to comment.