Skip to content

Commit

Permalink
add useFeature hook, only show workflows when enabled (#12)
Browse files Browse the repository at this point in the history
* add useFeature hook

* only show workflows tab when workflow feature is enabled
  • Loading branch information
bgentry authored Jun 9, 2024
1 parent 6a05cf0 commit a2defb1
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 21 deletions.
45 changes: 24 additions & 21 deletions ui/src/components/Layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
}
```
*/
import { Fragment, PropsWithChildren } from "react";
import { Fragment, PropsWithChildren, useMemo } from "react";

import {
Dialog,
Expand All @@ -32,33 +32,37 @@ import { classNames } from "@utils/style";
import { Link } from "@tanstack/react-router";
import { JobState } from "@services/types";
import { useSidebarSetting } from "@contexts/SidebarSetting.hook";
import useFeature from "@hooks/use-feature";

type LayoutProps = PropsWithChildren<object>;

const navigation = [
{
name: "Jobs",
href: "/jobs",
icon: QueueListIcon,
search: { state: JobState.Running },
},
{ name: "Queues", href: "/queues", icon: InboxStackIcon },
{ name: "Workflows", href: "#", icon: RectangleGroupIcon },
];

const Layout = ({ children }: LayoutProps) => {
const { open: sidebarOpen, setOpen: setSidebarOpen } = useSidebarSetting();

const featureEnabledWorkflows = useFeature("ENABLE_WORKFLOWS", false);

const navigation = useMemo(
() =>
[
{
name: "Jobs",
href: "/jobs",
icon: QueueListIcon,
search: { state: JobState.Running },
},
{ name: "Queues", href: "/queues", icon: InboxStackIcon },
{
name: "Workflows",
href: "#",
icon: RectangleGroupIcon,
hidden: !featureEnabledWorkflows,
},
].filter((item) => item.hidden === undefined || item.hidden === false),
[featureEnabledWorkflows]
);

return (
<>
{/*
This example requires updating your template:
```
<html class="h-full bg-white">
<body class="h-full">
```
*/}
<div className="h-full">
<Transition show={sidebarOpen} as={Fragment}>
<Dialog
Expand Down Expand Up @@ -162,7 +166,6 @@ const Layout = ({ children }: LayoutProps) => {
</nav>
</div>
</div>
{/* <JobFilters statesAndCounts={statesAndCounts} /> */}
</DialogPanel>
</TransitionChild>
</div>
Expand Down
28 changes: 28 additions & 0 deletions ui/src/hooks/use-feature.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { useMemo } from "react";

const isNil = (val: string | null | undefined): boolean =>
val === undefined || val === null;

const getBoolVal = (val?: string | boolean | null): boolean => {
if (typeof val === "string") {
return val === "true";
}

return !!val;
};

const useFeature = (name: string, enabled = false): boolean => {
const featureEnabled = useMemo((): boolean => {
const localStorageValue = window?.localStorage?.getItem(`FEATURE_${name}`);
const envVar: string | undefined = import.meta.env[`VITE_FEATURE_${name}`];

if (!isNil(localStorageValue)) return getBoolVal(localStorageValue);
if (!isNil(envVar)) return getBoolVal(envVar);

return !!enabled;
}, [name, enabled]);

return featureEnabled;
};

export default useFeature;
1 change: 1 addition & 0 deletions ui/src/vite-env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
/// <reference types="vite/client" />

interface ImportMetaEnv {
readonly VITE_FEATURE_ENABLE_WORKFLOWS: string;
readonly VITE_RIVER_API_BASE_URL: string;
}

Expand Down

0 comments on commit a2defb1

Please sign in to comment.