-
Notifications
You must be signed in to change notification settings - Fork 171
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature(editor) Snatching of control support. (#4711)
- Modified `UpdateProjectServerState` to take a `Partial<ProjectServerState>`. - Renamed `releaseControl` to `clearAllControlFromThisEditor`. - Created `CollaborationStateUpdater` component to manage the visibility changes of the editor. - Added `SnatchProjectControl` and `ReleaseProjectControl` to `CollaborationRequest`. - Renamed `ClaimProjectControlResult` to `RequestProjectControlResult`. - Renamed `ClearAllOfCollaboratorsControlResult` to `ReleaseControlResult`. - Added `snatchControlOverProject` and releaseControlOverProject`. - Renamed `releaseControl` to `clearAllControlFromThisEditor`. - Modified `editorDispatchClosingOut` to use `result.projectServerState` instead of `storedState.projectServerState`. - Modified `editorDispatchInner` to take account of `projectServerState` when determining if anything has changed. - Changed `getProjectServerState` to return a more useful value for test environments and to check if the user owns a project before attempting to claim ownership. - Added `owner_id` column to `project_collaboration`. - Updated some database logic to take account of `owner_id` in `project_collaboration`. - Added `forceClaimCollaborationControl` and `releaseCollaborationControl` to the database handling. - Added `SnatchCollaborationControl` and `ReleaseCollaborationControl` to `ServiceTypes`. - Modified `ClearCollaboratorOwnership` to take the user ID.
- Loading branch information
1 parent
0a25904
commit 6b2cb42
Showing
19 changed files
with
326 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
editor/src/components/editor/store/collaboration-state.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import React from 'react' | ||
import type { EditorAction, EditorDispatch } from '../action-types' | ||
import { | ||
displayControlErrorToast, | ||
releaseControlOverProject, | ||
snatchControlOverProject, | ||
} from './collaborative-editing' | ||
import { Substores, useEditorState } from './store-hook' | ||
import { switchEditorMode, updateProjectServerState } from '../actions/action-creators' | ||
import { EditorModes } from '../editor-modes' | ||
|
||
interface CollaborationStateUpdaterProps { | ||
projectId: string | null | ||
dispatch: EditorDispatch | ||
} | ||
|
||
export const CollaborationStateUpdater = React.memo( | ||
(props: React.PropsWithChildren<CollaborationStateUpdaterProps>) => { | ||
const { projectId, dispatch, children } = props | ||
const isMyProject = useEditorState( | ||
Substores.projectServerState, | ||
(store) => store.projectServerState.isMyProject, | ||
'CollaborationStateUpdater isMyProject', | ||
) | ||
React.useEffect(() => { | ||
// If the document is hidden, that means the editor is in the background | ||
// or minimised, so release control. Otherwise if it has become unhidden | ||
// then snatch control of the project. | ||
function didFocus(): void { | ||
if (projectId != null) { | ||
// Only attempt to do any kind of snatching of control if the project is "mine". | ||
if (isMyProject === 'yes') { | ||
void snatchControlOverProject(projectId) | ||
.then((controlResult) => { | ||
const newHolderOfTheBaton = controlResult ?? false | ||
let actions: Array<EditorAction> = [ | ||
updateProjectServerState({ currentlyHolderOfTheBaton: newHolderOfTheBaton }), | ||
] | ||
// Makes sense for the editing user to be in control and they probably want to be editing | ||
// when they regain control. | ||
if (newHolderOfTheBaton) { | ||
actions.push(switchEditorMode(EditorModes.selectMode(null, false, 'none'))) | ||
} | ||
dispatch(actions) | ||
}) | ||
.catch((error) => { | ||
displayControlErrorToast( | ||
dispatch, | ||
'Error while attempting to gain control over this project.', | ||
) | ||
}) | ||
} | ||
} | ||
} | ||
function didBlur(): void { | ||
if (projectId != null) { | ||
void releaseControlOverProject(projectId) | ||
.then(() => { | ||
dispatch([updateProjectServerState({ currentlyHolderOfTheBaton: false })]) | ||
}) | ||
.catch((error) => { | ||
displayControlErrorToast( | ||
dispatch, | ||
'Error while attempting to release control over this project.', | ||
) | ||
}) | ||
} | ||
} | ||
window.addEventListener('focus', didFocus) | ||
window.addEventListener('blur', didBlur) | ||
return () => { | ||
window.removeEventListener('focus', didFocus) | ||
window.removeEventListener('blur', didBlur) | ||
} | ||
}, [dispatch, projectId, isMyProject]) | ||
return <>{children}</> | ||
}, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
ALTER TABLE ONLY "public"."project_collaboration" | ||
ADD COLUMN "owner_id" character varying; |
Oops, something went wrong.