Skip to content
Draft
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
51 changes: 49 additions & 2 deletions packages/suite-base/src/Workspace.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Copy link
Member

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 response and make it const.

  if (!layoutUrl) {
      return;
  }

  try {
    const response = await fetch(layoutUrl);
    if (!response.ok) {
      log.debug(`Failed to fetch layout: ${layoutUrl} (status ${response.status})`);
      return;
    }

    const parsed: unknown = JSON.parse(await response.text());

    if (!parsed || typeof parsed !== "object") {
      log.debug(`${layoutUrl} does not contain valid layout JSON`);
      return;
    }

    const layoutData = parsed as LayoutData;
    setCurrentLayout({ data: layoutData });

  } catch (error) {
    log.debug(`Could not load the layout from ${layoutUrl}`, error);
  }

try {
res = await fetch(layoutUrl);
} catch {
log.debug(`Could not load the layout from ${layoutUrl}`);
return;
}
const parsedState: unknown = JSON.parse(await res.text());
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd change this to type LayoutData instead and put it inside a try and catch in case something goes wrong with the parsing.


if (typeof parsedState !== "object" || !parsedState) {
Copy link
Contributor

Choose a reason for hiding this comment

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

With parsedState being type of LayoutData the second condition being verified isn't necessary here

log.debug(`${layoutUrl} does not contain valid layout JSON`);
return;
}

const layoutData = parsedState as LayoutData;
Copy link
Contributor

Choose a reason for hiding this comment

The 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;
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd initialize this variable as false to avoid eslint[@typescript-eslint/strict-boolean-expressions](https://typescript-eslint.io/rules/strict-boolean-expressions) on line 638


// Apply any available data source args
if (unappliedSourceArgs.ds) {
log.debug("Initialising source from url", unappliedSourceArgs);
Expand All @@ -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]);

Expand Down
11 changes: 11 additions & 0 deletions packages/suite-base/src/util/appURLState.ts
Copy link
Member

@luluiz luluiz Jun 17, 2025

Choose a reason for hiding this comment

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

Please, implement some unit tests in appURLState.test.ts to cover your changes

Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if ("layoutUrl" in urlState) {
if (urlState.layoutUrl) {
newURL.searchParams.set("layoutUrl", urlState.layoutUrl);
} else {
newURL.searchParams.delete("layoutUrl");
}
}
if ("layoutUrl" in urlState) {
urlState.layoutUrl
? newURL.searchParams.set("layoutUrl", layoutUrl)
: newURL.searchParams.delete("layoutUrl");
}


if ("dsParams" in urlState) {
[...newURL.searchParams].forEach(([k]) => {
if (k.startsWith("ds.")) {
Expand All @@ -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> = {};
Expand All @@ -83,6 +93,7 @@ export function parseAppURLState(url: URL): AppURLState | undefined {
{
time,
ds,
layoutUrl,
dsParams: _.isEmpty(dsParams) ? undefined : dsParams,
},
_.isEmpty,
Expand Down