[Glitch] fix: Keep user on Compose page when changing screen size, #34937

Port 6166e61638 to glitch-soc

Signed-off-by: Claire <claire.github-309c@sitedethib.com>
This commit is contained in:
diondiondion
2025-06-23 11:53:21 +02:00
committed by Claire
parent 9fccf0a8c6
commit 7fff0d24c8
3 changed files with 39 additions and 4 deletions

View File

@@ -23,7 +23,7 @@ import { useColumnsContext } from '../util/columns_context';
import BundleColumnError from './bundle_column_error';
import { ColumnLoading } from './column_loading';
import { ComposePanel } from './compose_panel';
import { ComposePanel, RedirectToMobileComposeIfNeeded } from './compose_panel';
import DrawerLoading from './drawer_loading';
import { NavigationPanel } from 'flavours/glitch/features/navigation_panel';
@@ -124,6 +124,7 @@ export default class ColumnsArea extends ImmutablePureComponent {
<div className='columns-area__panels__pane columns-area__panels__pane--compositional'>
<div className='columns-area__panels__pane__inner'>
{renderComposePanel && <ComposePanel />}
<RedirectToMobileComposeIfNeeded />
</div>
</div>

View File

@@ -1,4 +1,4 @@
import { useCallback, useEffect } from 'react';
import { useCallback, useEffect, useLayoutEffect } from 'react';
import { useLayout } from '@/flavours/glitch/hooks/useLayout';
import { useAppDispatch, useAppSelector } from '@/flavours/glitch/store';
@@ -7,6 +7,7 @@ import {
mountCompose,
unmountCompose,
} from 'flavours/glitch/actions/compose';
import { useAppHistory } from 'flavours/glitch/components/router';
import ServerBanner from 'flavours/glitch/components/server_banner';
import { Search } from 'flavours/glitch/features/compose/components/search';
import ComposeFormContainer from 'flavours/glitch/features/compose/containers/compose_form_container';
@@ -54,3 +55,25 @@ export const ComposePanel: React.FC = () => {
</div>
);
};
/**
* Redirect the user to the standalone compose page when the
* sidebar composer is hidden due to a change in viewport size
* while a post is being written.
*/
export const RedirectToMobileComposeIfNeeded: React.FC = () => {
const history = useAppHistory();
const shouldRedirect = useAppSelector((state) =>
state.compose.get('should_redirect_to_compose_page'),
);
useLayoutEffect(() => {
if (shouldRedirect) {
history.push('/publish');
}
}, [history, shouldRedirect]);
return null;
};

View File

@@ -79,6 +79,7 @@ const initialState = ImmutableMap({
is_submitting: false,
is_changing_upload: false,
is_uploading: false,
should_redirect_to_compose_page: false,
progress: 0,
isUploadingThumbnail: false,
thumbnailProgress: 0,
@@ -398,11 +399,21 @@ export const composeReducer = (state = initialState, action) => {
case STORE_HYDRATE:
return hydrate(state, action.state.get('compose'));
case COMPOSE_MOUNT:
return state.set('mounted', state.get('mounted') + 1);
return state
.set('mounted', state.get('mounted') + 1)
.set('should_redirect_to_compose_page', false);
case COMPOSE_UNMOUNT:
return state
.set('mounted', Math.max(state.get('mounted') - 1, 0))
.set('is_composing', false);
.set('is_composing', false)
.set(
'should_redirect_to_compose_page',
(state.get('mounted') === 1 &&
state.get('is_composing') === true &&
(state.get('text').trim() !== '' ||
state.get('media_attachments').size > 0)
)
);
case COMPOSE_ADVANCED_OPTIONS_CHANGE:
return state
.set('advanced_options', state.get('advanced_options').set(action.option, !!overwrite(!state.getIn(['advanced_options', action.option]), action.value)))