Skip to content

Commit

Permalink
Refactor featureFlag to adopt object-based approach for improved ex…
Browse files Browse the repository at this point in the history
…tensibility

- Replaces single function (isVisible) with a featureFlag object
- Introduces a features list (name, active), allowing for future expansion
- Implements an isActive() method to handle feature toggles more cleanly
  • Loading branch information
kasperbirch1 committed Jan 7, 2025
1 parent c606de3 commit f7cd0ef
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 16 deletions.
12 changes: 8 additions & 4 deletions src/apps/loan-list/modal/material-details.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { RenewedLoanV2 } from "../../../core/fbs/model";
import RenewalModalMessage from "../../../components/renewal/RenewalModalMessage";
import { formatDate } from "../../../core/utils/helpers/date";
import useGetWorkUrlFromPublizonIdentifier from "../../../core/utils/useGetWorkUrlFromPublizonIdentifier";
import isVisible from "../../../core/utils/featureFlag";
import featureFlag from "../../../core/utils/featureFlag";

interface MaterialDetailsProps {
loan: LoanType | null;
Expand Down Expand Up @@ -121,7 +121,9 @@ const MaterialDetails: FC<MaterialDetailsProps & MaterialProps> = ({
renewalStatusList={renewalStatusList}
/>
)}
{isVisible("readerPlayer") && isDigital(loan) && workUrl ? (
{featureFlag.isActive("readerPlayer") &&
isDigital(loan) &&
workUrl ? (
<div className="modal-details__buttons modal-details__buttons--hide-on-mobile">
<Link
href={workUrl}
Expand Down Expand Up @@ -200,7 +202,9 @@ const MaterialDetails: FC<MaterialDetailsProps & MaterialProps> = ({
renewalStatusList={renewalStatusList}
/>
)}
{isVisible("readerPlayer") && isDigital(loan) && workUrl ? (
{featureFlag.isActive("readerPlayer") &&
isDigital(loan) &&
workUrl ? (
<div className="modal-details__buttons">
<Link
href={workUrl}
Expand All @@ -211,7 +215,7 @@ const MaterialDetails: FC<MaterialDetailsProps & MaterialProps> = ({
</div>
) : (
// Todo: Delete this else block after the readerPlayer feature flag is removed
!isVisible("readerPlayer") &&
!featureFlag.isActive("readerPlayer") &&
isDigital(loan) && (
<div className="modal-details__buttons">
<Link
Expand Down
4 changes: 2 additions & 2 deletions src/apps/material/material.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import MaterialDisclosure from "./MaterialDisclosure";
import ReservationFindOnShelfModals from "./ReservationFindOnShelfModals";
import PlayerModal from "../../components/material/player-modal/PlayerModal";
import useReaderPlayer from "../../core/utils/useReaderPlayer";
import isVisible from "../../core/utils/featureFlag";
import featureFlag from "../../core/utils/featureFlag";

export interface MaterialProps {
wid: WorkId;
Expand Down Expand Up @@ -200,7 +200,7 @@ const Material: React.FC<MaterialProps> = ({ wid }) => {
setSelectedPeriodical={setSelectedPeriodical}
/>
)}
{isVisible("readerPlayer") && readerPlayerType === "player" && (
{featureFlag.isActive("readerPlayer") && readerPlayerType === "player" && (
<>
{identifier && <PlayerModal identifier={identifier} />}
{orderId && <PlayerModal orderId={orderId} />}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import MaterialButtonOnlineExternal from "./MaterialButtonOnlineExternal";
import MaterialButtonOnlineInfomediaArticle from "./MaterialButtonOnlineInfomediaArticle";
import { ManifestationMaterialType } from "../../../../core/utils/types/material-type";
import MaterialButtonsOnlineInternal from "./MaterialButtonsOnlineInternal";
import isVisible from "../../../../core/utils/featureFlag";
import featureFlag from "../../../../core/utils/featureFlag";
import useReaderPlayer from "../../../../core/utils/useReaderPlayer";

export interface MaterialButtonsOnlineProps {
Expand Down Expand Up @@ -80,7 +80,7 @@ const MaterialButtonsOnline: FC<MaterialButtonsOnlineProps> = ({
ariaLabelledBy={ariaLabelledBy}
/>
)}
{isVisible("readerPlayer") && (
{featureFlag.isActive("readerPlayer") && (
<MaterialButtonsOnlineInternal
size={size}
manifestations={manifestations}
Expand Down
28 changes: 20 additions & 8 deletions src/core/utils/featureFlag.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
type FeatureFlagType = "readerPlayer";
const allowedUrls = ["dpl-", "dapple-cms"];

const isVisible = (name: FeatureFlagType): boolean => {
const allowedUrls = ["dpl-", "dapple-cms"];
const features = [
{ name: "exampleFeature", active: false },
{ name: "readerPlayer", active: true }
] as const;

if (name === "readerPlayer") {
return allowedUrls.some((url) => window.location.href.includes(url));
}
type FeatureNameType = typeof features[number]["name"];

const featureFlag = {
features,

isActive(name: FeatureNameType): boolean {
const feature = this.features.find((f) => f.name === name);
if (!feature) return false;

return false;
const isAllowedUrl = allowedUrls.some((url) =>
window.location.href.includes(url)
);

return feature.active && isAllowedUrl;
}
};

export default isVisible;
export default featureFlag;

0 comments on commit f7cd0ef

Please sign in to comment.