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

Second fix for back button #746

Open
wants to merge 2 commits 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
22 changes: 22 additions & 0 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 13 additions & 27 deletions src/client/useActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,6 @@ export const useActions = (
[dispatch],
);

const setActiveFileLeft = useCallback(() => {
dispatch({ type: 'set_active_file_left' });
}, [dispatch]);

const setActiveFileRight = useCallback(() => {
dispatch({ type: 'set_active_file_right' });
}, [dispatch]);

const openTab = useCallback(
(tabState: TabState): void => {
dispatch({
Expand Down Expand Up @@ -57,7 +49,6 @@ export const useActions = (
[dispatch],
);

// True to show the settings modal.
const setIsSettingsOpen = useCallback(
(value: boolean) => {
dispatch({
Expand All @@ -68,24 +59,10 @@ export const useActions = (
[dispatch],
);

const setIsDocOpen = useCallback(
(value: boolean) => {
dispatch({
type: 'set_is_doc_open',
value: value,
});
},
[dispatch],
);

const closeSettings = useCallback(() => {
setIsSettingsOpen(false);
}, [setIsSettingsOpen]);

const closeDoc = useCallback(() => {
setIsDocOpen(false);
}, [setIsDocOpen]);

const editorNoLongerWantsFocus = useCallback(() => {
dispatch({
type: 'editor_no_longer_wants_focus',
Expand All @@ -102,18 +79,27 @@ export const useActions = (
[dispatch],
);

// New action function for setting the tab list
const setTabList = useCallback(
(tabList: Array<TabState>) => {
dispatch({
type: 'set_tab_list',
tabList,
});
},
[dispatch],
);

return {
setActiveFileId,
setActiveFileLeft,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are used in the keyboard shortcuts. Deleting them will break things.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before deleting things, it's a good idea to search for them in the codebase to see if they are used anywhere.

setActiveFileRight,
openTab,
closeTabs,
setTheme,
setIsSettingsOpen,
setIsDocOpen,
closeSettings,
closeDoc,
editorNoLongerWantsFocus,
setUsername,
setTabList, // Export the new action function
};
};

27 changes: 13 additions & 14 deletions src/client/useURLSync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,20 @@ import {
encodeTabs,
} from './tabsSearchParameters';

// Synchronizes the tab state with the URL parameters.
export const useURLSync = ({
content,
openTab,
setActiveFileId,
tabList,
activeFileId,
}: {
content,
setTabList,
setActiveFileId,
tabList,
activeFileId,
}: {
content: VZCodeContent | null;
openTab: (tabState: TabState) => void;
setTabList: (tabList: Array<TabState>) => void;
setActiveFileId: (activeFileId: FileId) => void;
tabList: Array<TabState>;
activeFileId: FileId | null;
}) => {
// Use React router to get and set the search parameters.
// Use React router to get and set the search parameters.
const [searchParams, setSearchParams] = useSearchParams();

// Extract the tab state parameters from the search parameters.
Expand Down Expand Up @@ -51,7 +50,7 @@ export const useURLSync = ({
}

// Decode the search parameters.
const { tabList, activeFileId } = decodeTabs({
const { tabList: decodedTabList, activeFileId: decodedActiveFileId } = decodeTabs({
tabStateParams,
content,
});
Expand All @@ -60,13 +59,13 @@ export const useURLSync = ({
isInitialized.current = true;

// Update the state.
tabList.forEach(openTab);
setActiveFileId(activeFileId);
setTabList(decodedTabList);
setActiveFileId(decodedActiveFileId);
}, [
content,
searchParams,
tabStateParams,
setTabList,
setActiveFileId,
openTab,
isInitialized,
]);

Expand Down
18 changes: 10 additions & 8 deletions src/client/vzReducer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { setIsDocOpenReducer } from './setIsDocOpenReducer';
import { setThemeReducer } from './setThemeReducer';
import { editorNoLongerWantsFocusReducer } from './editorNoLongerWantsFocusReducer';
import { setUsernameReducer } from './setUsernameReducer';
import { setTabListReducer } from './setTabListReducer'; // Imported a new reducer
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
import { setTabListReducer } from './setTabListReducer'; // Imported a new reducer
import { setTabListReducer } from './setTabListReducer';


export { createInitialState } from './createInitialState';

Expand Down Expand Up @@ -41,8 +42,8 @@ export type VZState = {

// The shape of the actions that can be dispatched to the reducer.
export type VZAction =
// `set_active_file_id`
// * Sets the active file ID.
// `set_active_file_id`
// * Sets the active file ID.
| { type: 'set_active_file_id'; activeFileId: FileId }

// `set_active_file_left' 'set_active_file_right`
Expand All @@ -54,16 +55,16 @@ export type VZAction =
// `open_tab`
// * Opens a tab.
// * Also serves to change an already open transient tab to persistent.
| {
type: 'open_tab';
fileId: FileId;
isTransient?: boolean;
}
| { type: 'open_tab'; fileId: FileId; isTransient?: boolean; }

// `close_tabs`
// * Closes a set of tabs.
| { type: 'close_tabs'; fileIdsToClose: Array<FileId> }

// `set_tab_list`
// * Sets the tab list.
| { type: 'set_tab_list'; tabList: Array<TabState> }

// `set_theme`
// * Sets the theme.
| { type: 'set_theme'; themeLabel: ThemeLabel }
Expand Down Expand Up @@ -111,6 +112,7 @@ const reducers = [
setIsDocOpenReducer,
editorNoLongerWantsFocusReducer,
setUsernameReducer,
setTabListReducer,
];

export const vzReducer = (
Expand All @@ -121,4 +123,4 @@ export const vzReducer = (
(currentState, currentReducer) =>
currentReducer(currentState, action),
state,
);
);
13 changes: 13 additions & 0 deletions src/client/vzReducer/setTabListReducer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// setTabListReducer.ts
import { VZState, VZAction } from './index';

export const setTabListReducer = (state: VZState, action: VZAction): VZState => {
if (action.type !== 'set_tab_list') {
return state;
}

return {
...state,
tabList: action.tabList,
};
};