Skip to content

Commit

Permalink
Core: Preserve location hash in URL Core API
Browse files Browse the repository at this point in the history
  • Loading branch information
Sidnioulz committed Dec 24, 2024
1 parent 2e5f92c commit 2146472
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
24 changes: 16 additions & 8 deletions code/core/src/manager-api/modules/url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ export interface SubAPI {
getUrlState: () => {
queryParams: QueryParams;
path: string;
hash: string;
viewMode?: string;
storyId?: string;
url: string;
Expand Down Expand Up @@ -187,8 +188,15 @@ export const init: ModuleFn<SubAPI, SubState> = (moduleArgs) => {
return customQueryParams ? customQueryParams[key] : undefined;
},
getUrlState() {
const { path, customQueryParams, storyId, url, viewMode } = store.getState();
return { path, queryParams: customQueryParams, storyId, url, viewMode };
const { location, path, customQueryParams, storyId, url, viewMode } = store.getState();
return {
path,
hash: location.hash ?? '',
queryParams: customQueryParams,
storyId,
url,
viewMode,
};
},
setQueryParams(input) {
const { customQueryParams } = store.getState();
Expand All @@ -208,9 +216,9 @@ export const init: ModuleFn<SubAPI, SubState> = (moduleArgs) => {
}
},
applyQueryParams(input, options) {
const { path, queryParams } = api.getUrlState();
const { path, hash = '', queryParams } = api.getUrlState();

navigateTo(path, { ...queryParams, ...input } as any, options);
navigateTo(`${path}${hash}`, { ...queryParams, ...input } as any, options);
api.setQueryParams(input);
},
navigateUrl(url, options) {
Expand All @@ -223,7 +231,7 @@ export const init: ModuleFn<SubAPI, SubState> = (moduleArgs) => {
* unserialized safely.
*/
const updateArgsParam = () => {
const { path, queryParams, viewMode } = api.getUrlState();
const { path, hash = '', queryParams, viewMode } = api.getUrlState();

if (viewMode !== 'story') {
return;
Expand All @@ -237,7 +245,7 @@ export const init: ModuleFn<SubAPI, SubState> = (moduleArgs) => {

const { args, initialArgs } = currentStory;
const argsString = buildArgsParam(initialArgs, args as Args);
navigateTo(path, { ...queryParams, args: argsString }, { replace: true });
navigateTo(`${path}${hash}`, { ...queryParams, args: argsString }, { replace: true });
api.setQueryParams({ args: argsString });
};

Expand All @@ -259,9 +267,9 @@ export const init: ModuleFn<SubAPI, SubState> = (moduleArgs) => {
});

provider.channel?.on(GLOBALS_UPDATED, ({ userGlobals, initialGlobals }: any) => {
const { path, queryParams } = api.getUrlState();
const { path, hash = '', queryParams } = api.getUrlState();
const globalsString = buildArgsParam(initialGlobals, userGlobals);
navigateTo(path, { ...queryParams, globals: globalsString }, { replace: true });
navigateTo(`${path}${hash}`, { ...queryParams, globals: globalsString }, { replace: true });
api.setQueryParams({ globals: globalsString });
});

Expand Down
2 changes: 1 addition & 1 deletion code/core/src/router/router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ Link.displayName = 'QueryLink';

/**
* A render-prop component where children is called with a location and will be called whenever it
* changes when it changes
* changes
*/
export const Location = ({ children }: LocationProps) => {
const location = R.useLocation();
Expand Down

0 comments on commit 2146472

Please sign in to comment.