Skip to content

Commit

Permalink
chore: [IOPLT-778] Adds possible webUrl action on StatusMessage compo…
Browse files Browse the repository at this point in the history
…nent (#6453)

## Short description
This PR adds the possibility to specify a web_url to be opened by
tapping on StatusMessage displayed on top of the header

## List of changes proposed in this pull request
- `useStatusAlertProps` hook has been updated

## How to test
Edit the backend.json payload on the io-dev-api-server
  • Loading branch information
CrisTofani authored Dec 3, 2024
1 parent 6b11464 commit 0b3b8e3
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion ts/hooks/useStatusAlertProps.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,31 @@
import { AlertEdgeToEdgeProps } from "@pagopa/io-app-design-system";
import { useMemo } from "react";
import { pipe } from "fp-ts/lib/function";
import * as O from "fp-ts/lib/Option";
import { GestureResponderEvent } from "react-native";
import { useIOSelector } from "../store/hooks";
import { statusMessageByRouteSelector } from "../store/reducers/backendStatus/statusMessages";
import { getFullLocale } from "../utils/locale";
import { LevelEnum } from "../../definitions/content/StatusMessage";
import I18n from "../i18n";
import { openWebUrl } from "../utils/url";

const statusVariantMap: Record<LevelEnum, AlertEdgeToEdgeProps["variant"]> = {
[LevelEnum.normal]: "info",
[LevelEnum.critical]: "error",
[LevelEnum.warning]: "warning"
};

type AlertActionProps =
| {
action?: string;
onPress: (event: GestureResponderEvent) => void;
}
| {
action?: never;
onPress?: never;
};

export const useStatusAlertProps = (
routeName?: string
): AlertEdgeToEdgeProps | undefined => {
Expand All @@ -25,9 +40,23 @@ export const useStatusAlertProps = (
}
// If there is at least one status-message to display, extract its content and variant (using memoization to avoid re-renderings, since we are creating a new instance)
const firstAlert = currentStatusMessage[0];

const statusAction: AlertActionProps = pipe(
firstAlert.web_url,
O.fromNullable,
O.fold(
() => ({}),
url => ({
action: I18n.t("global.sectionStatus.moreInfo"),
onPress: () => openWebUrl(url[locale])
})
)
);

return {
content: firstAlert.message[locale],
variant: statusVariantMap[firstAlert.level]
variant: statusVariantMap[firstAlert.level],
...statusAction
};
}, [currentStatusMessage, locale]);
};

0 comments on commit 0b3b8e3

Please sign in to comment.