-
Notifications
You must be signed in to change notification settings - Fork 636
Add support for layoutUrl as a param #341
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
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -60,6 +60,10 @@ import { TopicList } from "@lichtblick/suite-base/components/TopicList"; | |
| import VariablesList from "@lichtblick/suite-base/components/VariablesList"; | ||
| import { WorkspaceDialogs } from "@lichtblick/suite-base/components/WorkspaceDialogs"; | ||
| import { useAppContext } from "@lichtblick/suite-base/context/AppContext"; | ||
| import { | ||
| LayoutData, | ||
| useCurrentLayoutActions, | ||
| } from "@lichtblick/suite-base/context/CurrentLayoutContext"; | ||
| import { | ||
| LayoutState, | ||
| useCurrentLayoutSelector, | ||
|
|
@@ -565,22 +569,57 @@ function WorkspaceContent(props: WorkspaceProps): React.JSX.Element { | |
| [getMessagePipeline], | ||
| ); | ||
|
|
||
| const { setCurrentLayout } = useCurrentLayoutActions(); | ||
|
|
||
| const targetUrlState = useMemo(() => { | ||
| const deepLinks = props.deepLinks ?? []; | ||
| return deepLinks[0] ? parseAppURLState(new URL(deepLinks[0])) : undefined; | ||
| }, [props.deepLinks]); | ||
|
|
||
| const [unappliedSourceArgs, setUnappliedSourceArgs] = useState( | ||
| targetUrlState ? { ds: targetUrlState.ds, dsParams: targetUrlState.dsParams } : undefined, | ||
| targetUrlState | ||
| ? { | ||
| ds: targetUrlState.ds, | ||
| dsParams: targetUrlState.dsParams, | ||
| layoutUrl: targetUrlState.layoutUrl, | ||
| } | ||
| : undefined, | ||
| ); | ||
|
|
||
| const selectEvent = useEvents(selectSelectEvent); | ||
|
|
||
| const fetchLayoutFromUrl = async (layoutUrl: string) => { | ||
| if (!layoutUrl) { | ||
| return; | ||
| } | ||
| let res; | ||
| try { | ||
| res = await fetch(layoutUrl); | ||
| } catch { | ||
| log.debug(`Could not load the layout from ${layoutUrl}`); | ||
| return; | ||
| } | ||
| const parsedState: unknown = JSON.parse(await res.text()); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd change this to type |
||
|
|
||
| if (typeof parsedState !== "object" || !parsedState) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. With |
||
| log.debug(`${layoutUrl} does not contain valid layout JSON`); | ||
| return; | ||
| } | ||
|
|
||
| const layoutData = parsedState as LayoutData; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Assertion wont be necessary here if suggestion above is implemented |
||
| setCurrentLayout({ | ||
| data: layoutData, | ||
| }); | ||
| }; | ||
|
|
||
| // Load data source from URL. | ||
| useEffect(() => { | ||
| if (!unappliedSourceArgs) { | ||
| return; | ||
| } | ||
|
|
||
| let shouldUpdate; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd initialize this variable as false to avoid |
||
|
|
||
| // Apply any available data source args | ||
| if (unappliedSourceArgs.ds) { | ||
| log.debug("Initialising source from url", unappliedSourceArgs); | ||
|
|
@@ -589,7 +628,15 @@ function WorkspaceContent(props: WorkspaceProps): React.JSX.Element { | |
| params: unappliedSourceArgs.dsParams, | ||
| }); | ||
| selectEvent(unappliedSourceArgs.dsParams?.eventId); | ||
| setUnappliedSourceArgs({ ds: undefined, dsParams: undefined }); | ||
| shouldUpdate = true; | ||
| } | ||
| // Apply any available datasource args | ||
| if (unappliedSourceArgs.layoutUrl) { | ||
| fetchLayoutFromUrl(unappliedSourceArgs.layoutUrl); | ||
| shouldUpdate = true; | ||
| } | ||
| if (shouldUpdate) { | ||
| setUnappliedSourceArgs({ ds: undefined, dsParams: undefined, layoutUrl: undefined }); | ||
| } | ||
| }, [selectEvent, selectSource, unappliedSourceArgs, setUnappliedSourceArgs]); | ||
|
|
||
|
|
||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please, implement some unit tests in |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -12,6 +12,7 @@ import { LayoutID } from "@lichtblick/suite-base/context/CurrentLayoutContext"; | |||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| export type AppURLState = { | ||||||||||||||||||||||||||
| ds?: string; | ||||||||||||||||||||||||||
| layoutUrl?: string; | ||||||||||||||||||||||||||
| dsParams?: Record<string, string>; | ||||||||||||||||||||||||||
| layoutId?: LayoutID; | ||||||||||||||||||||||||||
| time?: Time; | ||||||||||||||||||||||||||
|
|
@@ -43,6 +44,14 @@ export function updateAppURLState(url: URL, urlState: AppURLState): URL { | |||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| if ("layoutUrl" in urlState) { | ||||||||||||||||||||||||||
| if (urlState.layoutUrl) { | ||||||||||||||||||||||||||
| newURL.searchParams.set("layoutUrl", urlState.layoutUrl); | ||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||
| newURL.searchParams.delete("layoutUrl"); | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
Comment on lines
+47
to
+53
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| if ("dsParams" in urlState) { | ||||||||||||||||||||||||||
| [...newURL.searchParams].forEach(([k]) => { | ||||||||||||||||||||||||||
| if (k.startsWith("ds.")) { | ||||||||||||||||||||||||||
|
|
@@ -69,6 +78,7 @@ export function updateAppURLState(url: URL, urlState: AppURLState): URL { | |||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||
| export function parseAppURLState(url: URL): AppURLState | undefined { | ||||||||||||||||||||||||||
| const ds = url.searchParams.get("ds") ?? undefined; | ||||||||||||||||||||||||||
| const layoutUrl = url.searchParams.get("layoutUrl"); | ||||||||||||||||||||||||||
| const timeString = url.searchParams.get("time"); | ||||||||||||||||||||||||||
| const time = timeString == undefined ? undefined : fromRFC3339String(timeString); | ||||||||||||||||||||||||||
| const dsParams: Record<string, string> = {}; | ||||||||||||||||||||||||||
|
|
@@ -83,6 +93,7 @@ export function parseAppURLState(url: URL): AppURLState | undefined { | |||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||
| time, | ||||||||||||||||||||||||||
| ds, | ||||||||||||||||||||||||||
| layoutUrl, | ||||||||||||||||||||||||||
| dsParams: _.isEmpty(dsParams) ? undefined : dsParams, | ||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||
| _.isEmpty, | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd rename it to
responseand make itconst.