-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
fix: shared page mode syncing #14756
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
Open
ibex088
wants to merge
6
commits into
toeverything:canary
Choose a base branch
from
ibex088:fix/shared-mode-link-sync
base: canary
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
1bc2a0f
Fix shared page mode syncing
ibex088 ccfb096
Reset shared publish mode on doc change
ibex088 d3fd46d
Avoid implicit page mode on share errors
ibex088 7cbe17c
Preserve implicit share mode fallback
ibex088 48d61f6
fix: clean shared mode lint warnings
ibex088 406c7be
Merge branch 'canary' into fix/shared-mode-link-sync
darkskygit File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,42 @@ | ||
| import { PublicDocMode } from '@affine/graphql'; | ||
| import { describe, expect, test } from 'vitest'; | ||
|
|
||
| import { | ||
| getResolvedPublishMode, | ||
| getSearchWithMode, | ||
| } from '../desktop/pages/workspace/share/share-page.utils'; | ||
|
|
||
| describe('getResolvedPublishMode', () => { | ||
| test('prefers the query mode when it is present', () => { | ||
| expect(getResolvedPublishMode('edgeless', PublicDocMode.Page)).toBe( | ||
| 'edgeless' | ||
| ); | ||
| expect(getResolvedPublishMode('page', PublicDocMode.Edgeless)).toBe( | ||
| 'page' | ||
| ); | ||
| }); | ||
|
|
||
| test('falls back to the published public mode for shared docs', () => { | ||
| expect(getResolvedPublishMode(null, PublicDocMode.Edgeless)).toBe( | ||
| 'edgeless' | ||
| ); | ||
| expect(getResolvedPublishMode(null, PublicDocMode.Page)).toBe('page'); | ||
| }); | ||
|
|
||
| test('defaults to page when no mode is available', () => { | ||
| expect(getResolvedPublishMode(null, null)).toBe('page'); | ||
| expect(getResolvedPublishMode(null, undefined)).toBe('page'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('getSearchWithMode', () => { | ||
| test('adds mode to an empty search string', () => { | ||
| expect(getSearchWithMode('', 'edgeless')).toBe('?mode=edgeless'); | ||
| }); | ||
|
|
||
| test('replaces an existing mode and preserves other params', () => { | ||
| expect(getSearchWithMode('?foo=1&mode=page&bar=2', 'edgeless')).toBe( | ||
| '?foo=1&mode=edgeless&bar=2' | ||
| ); | ||
| }); | ||
| }); |
14 changes: 14 additions & 0 deletions
14
packages/frontend/core/src/__tests__/use-share-url.utils.spec.ts
This file contains hidden or 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,14 @@ | ||
| import { describe, expect, test } from 'vitest'; | ||
|
|
||
| import { getDefaultShareMode } from '../components/hooks/affine/use-share-url.utils'; | ||
|
|
||
| describe('getDefaultShareMode', () => { | ||
| test('returns edgeless when the current mode is edgeless', () => { | ||
| expect(getDefaultShareMode('edgeless')).toBe('edgeless'); | ||
| }); | ||
|
|
||
| test('returns undefined for page mode or an unset mode', () => { | ||
| expect(getDefaultShareMode('page')).toBeUndefined(); | ||
| expect(getDefaultShareMode(undefined)).toBeUndefined(); | ||
| }); | ||
| }); |
This file contains hidden or 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 hidden or 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
7 changes: 7 additions & 0 deletions
7
packages/frontend/core/src/components/hooks/affine/use-share-url.utils.ts
This file contains hidden or 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,7 @@ | ||
| import type { DocMode } from '@blocksuite/affine/model'; | ||
|
|
||
| export const getDefaultShareMode = ( | ||
| currentMode?: DocMode | ||
| ): DocMode | undefined => { | ||
| return currentMode === 'edgeless' ? 'edgeless' : undefined; | ||
| }; |
This file contains hidden or 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
21 changes: 21 additions & 0 deletions
21
packages/frontend/core/src/desktop/pages/workspace/share/share-page.utils.ts
This file contains hidden or 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,21 @@ | ||
| import { PublicDocMode } from '@affine/graphql'; | ||
| import { type DocMode, DocModes } from '@blocksuite/affine/model'; | ||
|
|
||
| export const getResolvedPublishMode = ( | ||
| queryMode: DocMode | null, | ||
| publicMode?: PublicDocMode | null | ||
| ): DocMode => { | ||
| if (queryMode && DocModes.includes(queryMode)) { | ||
| return queryMode; | ||
| } | ||
|
|
||
| return publicMode === PublicDocMode.Edgeless ? 'edgeless' : 'page'; | ||
| }; | ||
|
|
||
| export const getSearchWithMode = (search: string, mode: DocMode) => { | ||
| const searchParams = new URLSearchParams(search); | ||
| searchParams.set('mode', mode); | ||
|
|
||
| const nextSearch = searchParams.toString(); | ||
| return nextSearch ? `?${nextSearch}` : ''; | ||
| }; |
60 changes: 60 additions & 0 deletions
60
packages/frontend/core/src/desktop/pages/workspace/share/use-shared-mode-query-sync.ts
This file contains hidden or 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,60 @@ | ||
| import type { Editor } from '@affine/core/modules/editor'; | ||
| import type { DocMode } from '@blocksuite/affine/model'; | ||
| import { useLiveData } from '@toeverything/infra'; | ||
| import { useEffect, useRef } from 'react'; | ||
| import { useLocation, useNavigate } from 'react-router-dom'; | ||
|
|
||
| import { getSearchWithMode } from './share-page.utils'; | ||
|
|
||
| export const useSharedModeQuerySync = ({ | ||
| editor, | ||
| resolvedPublishMode, | ||
| }: { | ||
| editor: Editor | null; | ||
| resolvedPublishMode: DocMode | null; | ||
| }) => { | ||
| const location = useLocation(); | ||
| const navigate = useNavigate(); | ||
| const currentPublishMode = useLiveData(editor?.mode$) ?? resolvedPublishMode; | ||
| const previousPublishModeRef = useRef<DocMode | null>(null); | ||
|
|
||
| useEffect(() => { | ||
| if (!editor || !resolvedPublishMode) { | ||
| return; | ||
| } | ||
|
|
||
| if (editor.mode$.value !== resolvedPublishMode) { | ||
| editor.setMode(resolvedPublishMode); | ||
| } | ||
| }, [editor, resolvedPublishMode]); | ||
|
|
||
| useEffect(() => { | ||
| if (!currentPublishMode) { | ||
| return; | ||
| } | ||
|
|
||
| if (previousPublishModeRef.current === null) { | ||
| previousPublishModeRef.current = currentPublishMode; | ||
| return; | ||
| } | ||
|
|
||
| if (previousPublishModeRef.current === currentPublishMode) { | ||
| return; | ||
| } | ||
|
|
||
| previousPublishModeRef.current = currentPublishMode; | ||
|
|
||
| const nextSearch = getSearchWithMode(location.search, currentPublishMode); | ||
| if (nextSearch !== location.search) { | ||
| navigate( | ||
| { | ||
| pathname: location.pathname, | ||
| search: nextSearch, | ||
| }, | ||
| { replace: true } | ||
| ); | ||
| } | ||
| }, [currentPublishMode, location.pathname, location.search, navigate]); | ||
|
|
||
| return currentPublishMode; | ||
| }; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don’t turn publish-mode lookup failures into a blank page.
When
packages/frontend/core/src/desktop/pages/workspace/share/use-shared-publish-mode.tssetshasPublishModeErrorat Lines 54-60, this component still rendersnullhere, and the loading guard below does the same. A failed mode lookup now looks like an infinite blank screen instead of a recoverable error.🤖 Prompt for AI Agents