Skip to content
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

chore: Add basic feature flags support #422

Merged
merged 3 commits into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
chore: Add basic feature flags support
  • Loading branch information
going-confetti committed Jan 17, 2025
commit 380ab3d522c0521f5ea1ba13b156d1e78a673fd0
21 changes: 21 additions & 0 deletions src/components/Feature.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { PropsWithChildren } from 'react'

import { useFeature } from '@/store/features'
import type { Feature as FeatureType } from '@/types/features'

interface FeatureProps {
feature: FeatureType
}

export function Feature({
children,
feature,
}: PropsWithChildren<FeatureProps>) {
const [isEnabled] = useFeature(feature)

if (!isEnabled) {
return null
}

return <>{children}</>
}
10 changes: 9 additions & 1 deletion src/components/Layout/Sidebar/Sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { getRoutePath } from '@/routeMap'
import { useCreateGenerator } from '@/hooks/useCreateGenerator'
import { SearchField } from '@/components/SearchField'
import { useState } from 'react'
import { Feature } from '@/components/Feature'

interface SidebarProps {
isExpanded?: boolean
Expand All @@ -17,7 +18,7 @@ interface SidebarProps {

export function Sidebar({ isExpanded, onCollapseSidebar }: SidebarProps) {
const [searchTerm, setSearchTerm] = useState('')
const { recordings, generators, scripts } = useFiles(searchTerm)
const { recordings, generators, scripts, dataFiles } = useFiles(searchTerm)

const createNewGenerator = useCreateGenerator()

Expand Down Expand Up @@ -100,6 +101,13 @@ export function Sidebar({ isExpanded, onCollapseSidebar }: SidebarProps) {
files={scripts}
noFilesMessage="No scripts found"
/>
<Feature feature="data-files">
<FileTree
label="Data files"
files={dataFiles}
noFilesMessage="No data files found"
/>
</Feature>
</Flex>
</ScrollArea>
</Flex>
Expand Down
1 change: 1 addition & 0 deletions src/store/features/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './useFeaturesStore'
36 changes: 36 additions & 0 deletions src/store/features/useFeaturesStore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { Feature } from '@/types/features'
import { create } from 'zustand'
import { immer } from 'zustand/middleware/immer'
import { persist } from 'zustand/middleware'

interface FeaturesStore {
features: Record<Feature, boolean>
toggleFeature: (feature: Feature) => void
}

const defaultFeatures: Record<Feature, boolean> = {
'data-files': false,
}

export const useFeaturesStore = create<FeaturesStore>()(
persist(
immer((set) => ({
features: {
...defaultFeatures,
},
toggleFeature: (feature) =>
set((state) => {
state.features[feature] = !state.features[feature]
}),
})),
{ name: 'features-storage' }
)
)

export function useFeature(feature: Feature) {
const toggleFeature = useFeaturesStore((state) => state.toggleFeature)
const isEnabled = useFeaturesStore((state) => state.features[feature])
const toggleEnabled = () => toggleFeature(feature)

return [isEnabled, toggleEnabled] as const
}
1 change: 1 addition & 0 deletions src/types/features.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type Feature = 'data-files'
Loading