-
Notifications
You must be signed in to change notification settings - Fork 146
feat: Add folder organization system for feature flags (#271) #323
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
Closed
MrLawrenceKwan
wants to merge
1
commit into
databuddy-analytics:main
from
MrLawrenceKwan:feature/flag-folders
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
172 changes: 172 additions & 0 deletions
172
apps/dashboard/app/(main)/websites/[id]/flags/_components/folder-selector.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,172 @@ | ||
| "use client"; | ||
|
|
||
| import { FolderIcon, FolderOpenIcon, PlusIcon } from "@phosphor-icons/react"; | ||
| import { useQuery } from "@tanstack/react-query"; | ||
| import { useState } from "react"; | ||
| import { Button } from "@/components/ui/button"; | ||
| import { | ||
| Command, | ||
| CommandEmpty, | ||
| CommandGroup, | ||
| CommandInput, | ||
| CommandItem, | ||
| CommandList, | ||
| CommandSeparator, | ||
| } from "@/components/ui/command"; | ||
| import { | ||
| Popover, | ||
| PopoverContent, | ||
| PopoverTrigger, | ||
| } from "@/components/ui/popover"; | ||
| import { orpc } from "@/lib/orpc"; | ||
| import { cn } from "@/lib/utils"; | ||
|
|
||
| interface FolderSelectorProps { | ||
| websiteId: string; | ||
| value: string | null | undefined; | ||
| onValueChange: (value: string | null) => void; | ||
| disabled?: boolean; | ||
| } | ||
|
|
||
| export function FolderSelector({ | ||
| websiteId, | ||
| value, | ||
| onValueChange, | ||
| disabled, | ||
| }: FolderSelectorProps) { | ||
| const [open, setOpen] = useState(false); | ||
| const [newFolderName, setNewFolderName] = useState(""); | ||
|
|
||
| const { data: flags } = useQuery({ | ||
| ...orpc.flags.list.queryOptions({ | ||
| input: { websiteId }, | ||
| }), | ||
| }); | ||
|
|
||
| // Extract unique folders from flags | ||
| const folders = Array.from( | ||
| new Set( | ||
| (flags ?? []) | ||
| .map((f: { folder?: string | null }) => f.folder) | ||
| .filter((f): f is string => Boolean(f)) | ||
| ) | ||
| ).sort(); | ||
|
|
||
| const handleCreateFolder = () => { | ||
| if (newFolderName.trim()) { | ||
| onValueChange(newFolderName.trim()); | ||
| setNewFolderName(""); | ||
| setOpen(false); | ||
| } | ||
| }; | ||
|
|
||
| const displayValue = value || "No folder"; | ||
| const IconComponent = value ? FolderOpenIcon : FolderIcon; | ||
|
|
||
| return ( | ||
| <Popover onOpenChange={setOpen} open={open}> | ||
| <PopoverTrigger asChild> | ||
| <Button | ||
| aria-expanded={open} | ||
| className="w-full justify-start gap-2 font-normal" | ||
| disabled={disabled} | ||
| role="combobox" | ||
| variant="outline" | ||
| > | ||
| <IconComponent className="size-4 text-muted-foreground" /> | ||
| <span className="truncate">{displayValue}</span> | ||
| </Button> | ||
| </PopoverTrigger> | ||
| <PopoverContent align="start" className="w-[300px] p-0"> | ||
| <Command> | ||
| <CommandInput placeholder="Search folders..." /> | ||
| <CommandList> | ||
| <CommandEmpty> | ||
| <div className="space-y-2 p-2"> | ||
| <p className="text-muted-foreground text-sm">No folders found</p> | ||
| {newFolderName && ( | ||
| <Button | ||
| className="w-full gap-2" | ||
| onClick={handleCreateFolder} | ||
| size="sm" | ||
| > | ||
| <PlusIcon className="size-4" /> | ||
| Create "{newFolderName}" | ||
| </Button> | ||
| )} | ||
| </div> | ||
| </CommandEmpty> | ||
| <CommandGroup> | ||
| <CommandItem | ||
| className="gap-2" | ||
| onSelect={() => { | ||
| onValueChange(null); | ||
| setOpen(false); | ||
| }} | ||
| value="" | ||
| > | ||
| <FolderIcon | ||
| className={cn("size-4", !value && "text-primary")} | ||
| /> | ||
| <span className={cn(!value && "font-medium")}>No folder</span> | ||
| </CommandItem> | ||
| </CommandGroup> | ||
| {folders.length > 0 && ( | ||
| <> | ||
| <CommandSeparator /> | ||
| <CommandGroup heading="Folders"> | ||
| {folders.map((folder) => ( | ||
| <CommandItem | ||
| className="gap-2" | ||
| key={folder} | ||
| onSelect={() => { | ||
| onValueChange(folder); | ||
| setOpen(false); | ||
| }} | ||
| value={folder} | ||
| > | ||
| <FolderOpenIcon | ||
| className={cn( | ||
| "size-4", | ||
| value === folder && "text-primary" | ||
| )} | ||
| /> | ||
| <span className={cn(value === folder && "font-medium")}> | ||
| {folder} | ||
| </span> | ||
| </CommandItem> | ||
| ))} | ||
| </CommandGroup> | ||
| </> | ||
| )} | ||
| <CommandSeparator /> | ||
| <div className="p-2"> | ||
| <div className="flex gap-2"> | ||
| <input | ||
| className="flex-1 rounded border bg-background px-3 py-1.5 text-sm outline-none focus:border-primary" | ||
| onChange={(e) => setNewFolderName(e.target.value)} | ||
| onKeyDown={(e) => { | ||
| if (e.key === "Enter") { | ||
| e.preventDefault(); | ||
| handleCreateFolder(); | ||
| } | ||
| }} | ||
| placeholder="New folder name..." | ||
| value={newFolderName} | ||
| /> | ||
|
Comment on lines
+145
to
+156
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing |
||
| <Button | ||
| disabled={!newFolderName.trim()} | ||
| onClick={handleCreateFolder} | ||
| size="sm" | ||
| type="button" | ||
| > | ||
| <PlusIcon className="size-4" /> | ||
| </Button> | ||
| </div> | ||
| </div> | ||
| </CommandList> | ||
| </Command> | ||
| </PopoverContent> | ||
| </Popover> | ||
| ); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CommandInputhas its own internal search state that filters the folder list, butnewFolderNamestate (line 38) is only set by the separate input field at the bottom (lines 143-156). When a user types "my-folder" in the CommandInput and no folders match, the CommandEmpty section shows but the create button won't appear becausenewFolderNameis empty. To fix this, either useuseCommandState()hook from cmdk to access the search value, or add anonValueChangehandler toCommandInputto sync withnewFolderName.