-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: improve URL and title management in ActionView
gisce/webclient#1612 - Added `useAutoUpdateUrlAndTitle` hook to automatically update the document title and URL based on the current tab and context. - Introduced `useUrlFromCurrentTab` hook to generate shareable URLs based on the current tab's action and parameters. - Updated `ConfigContext` to include a `title` property for better context management. - Refactored `shareUrlHelper` to simplify URL creation logic. - Improved `ActionView` component structure by extracting content rendering into a separate `ActionViewContent` component for better readability and maintainability.
- Loading branch information
1 parent
7286618
commit 644e14e
Showing
5 changed files
with
231 additions
and
99 deletions.
There are no files selected for viewing
This file contains 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 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 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,30 @@ | ||
import { useEffect } from "react"; | ||
import { useTabs } from "@/context/TabManagerContext"; | ||
import { useConfigContext } from "@/context/ConfigContext"; | ||
import { useUrlFromCurrentTab } from "./useUrlFromCurrentTab"; | ||
|
||
export const useAutoUpdateUrlAndTitle = () => { | ||
const { currentTab } = useTabs(); | ||
const { title } = useConfigContext(); | ||
|
||
const { shareUrl } = useUrlFromCurrentTab({ currentTab }); | ||
|
||
useEffect(() => { | ||
if (shareUrl) { | ||
const url = new URL(shareUrl, window.location.origin); | ||
if ( | ||
window.location.pathname + window.location.search !== | ||
url.pathname + url.search | ||
) { | ||
window.history.replaceState({}, "", url.pathname + url.search); | ||
} | ||
} | ||
}, [currentTab, shareUrl]); | ||
|
||
useEffect(() => { | ||
document.title = title; | ||
if (currentTab?.title && currentTab.title.length > 0) { | ||
document.title = currentTab?.title + " - " + title; | ||
} | ||
}, [currentTab, title]); | ||
}; |
This file contains 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,47 @@ | ||
import { useTabs } from "@/context/TabManagerContext"; | ||
import { useActionViewContext } from "@/context/ActionViewContext"; | ||
import { createShareOpenUrl } from "@/helpers/shareUrlHelper"; | ||
import { ActionInfo, Tab } from "@/types"; | ||
|
||
interface UseUrlFromCurrentTabResult { | ||
shareUrl: string | null; | ||
canShare: boolean; | ||
} | ||
|
||
export function useUrlFromCurrentTab({ | ||
currentTab: currentTabProps, | ||
}: { | ||
currentTab?: Tab; | ||
}): UseUrlFromCurrentTabResult { | ||
const { currentView, searchParams, currentId } = useActionViewContext(); | ||
const { currentTab: currentTabContext } = useTabs(); | ||
|
||
const currentTab = currentTabProps || currentTabContext; | ||
|
||
if (!currentTab?.action) { | ||
return { shareUrl: null, canShare: false }; | ||
} | ||
|
||
const initialView = { | ||
id: currentView.view_id, | ||
type: currentView.type, | ||
}; | ||
|
||
const { action_id } = currentTab.action; | ||
const finalActionData: ActionInfo = { | ||
...currentTab.action, | ||
...(initialView && { initialView }), | ||
...(searchParams && { searchParams }), | ||
...(currentId && { res_id: currentId }), | ||
}; | ||
|
||
const shareUrl = createShareOpenUrl(finalActionData); | ||
const { type } = initialView; | ||
|
||
let canShare = !!action_id; | ||
if (type === "form") { | ||
canShare = !!action_id && !!currentId; | ||
} | ||
|
||
return { shareUrl, canShare }; | ||
} |
This file contains 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