Skip to content

Commit

Permalink
Merge pull request #856 from commercelayer/use-navigation
Browse files Browse the repository at this point in the history
Replace cookie usage with session storage for app cross-linking
  • Loading branch information
gciotola authored Dec 17, 2024
2 parents e9ec172 + 492e058 commit f622295
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 deletions packages/app-elements/src/helpers/useAppLinking.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { useTokenProvider } from '#providers/TokenProvider'
import { type TokenProviderAllowedApp } from '#providers/TokenProvider/types'
import Cookies from 'js-cookie'
import isEmpty from 'lodash/isEmpty'
import { useCallback } from 'react'
import { useLocation, useRouter, useSearch } from 'wouter'
Expand All @@ -24,7 +23,7 @@ const config: { apps: AppsConfig } = {
interface UseAppLinkingHook {
/**
* Navigate to internal app path, to different app (outside router base), or to an external URL.
* Current path is saved in a cookie to allow going back to it (when using `goBack`).
* Current path is saved in session storage to allow going back to it (when using `goBack`).
*/
navigateTo: (param: { app: AppsWithConfig; resourceId?: string }) => {
href: string
Expand All @@ -38,7 +37,7 @@ interface UseAppLinkingHook {

/**
* Go back to the last visited location when `navigateTo` has been used.
* If no cookie is found, it will navigate to the default relative
* If no saved item is found, it will navigate to the default relative
*/
goBack: (param: {
currentResourceId?: string
Expand Down Expand Up @@ -79,7 +78,7 @@ export function useAppLinking(): UseAppLinkingHook {
e.preventDefault()

if (isExternalUrl(to)) {
// TODO: we can't set a cookie for the go back from a different domain
// TODO: we can't set persistent entry for the go back from a different domain
// probably in a future we can use a query string param
window.location.assign(to)
} else {
Expand Down Expand Up @@ -206,9 +205,12 @@ function saveGoBackItem({
returnToApp: AppsWithConfig
location: string
}): void {
const cookieName = makeCookieName({ destinationApp, resourceId })
Cookies.set(
cookieName,
if (typeof window === 'undefined') {
return
}
const itemKey = makePersistentKey({ destinationApp, resourceId })
sessionStorage.setItem(
itemKey,
JSON.stringify({
version: currentVersion,
returnToApp,
Expand All @@ -224,9 +226,12 @@ function getGoBackItem({
destinationApp: AppsWithConfig
resourceId?: string
}): GoBackItem | null {
const cookieName = makeCookieName({ destinationApp, resourceId })
const value = Cookies.get(cookieName)
Cookies.remove(cookieName)
if (typeof window === 'undefined') {
return null
}
const itemKey = makePersistentKey({ destinationApp, resourceId })
const value = sessionStorage.getItem(itemKey)
sessionStorage.removeItem(itemKey)

try {
const item = JSON.parse(value ?? '{}') as GoBackItem
Expand All @@ -240,12 +245,12 @@ function getGoBackItem({
}
}

function makeCookieName({
function makePersistentKey({
destinationApp,
resourceId
}: {
destinationApp: AppsWithConfig
resourceId?: string
}): string {
return `cl.apps.${destinationApp}_${resourceId ?? 'list'}`
return `cl.apps.nav.${destinationApp}_${resourceId ?? 'list'}`
}

0 comments on commit f622295

Please sign in to comment.