11import { useCollectionContext } from "@/components/collection/context"
2- import { useHabitContext } from "@/components/habit/context"
32import { Button } from "@/components/ui/button"
43import {
54 Dialog ,
@@ -8,23 +7,24 @@ import {
87 DialogFooter ,
98 DialogTitle ,
109} from "@/components/ui/dialog"
11- import { HabitSchema } from "@/proto/models/v1/models_pb"
12- import { clone } from "@bufbuild/protobuf"
1310import { DialogTrigger } from "@radix-ui/react-dialog"
1411import { Link } from "@tanstack/react-router"
15- import { Check , Ellipsis , Loader2 , Plus , Undo2 } from "lucide-react"
12+ import { Check , Ellipsis , Plus , Undo2 } from "lucide-react"
1613import React from "react"
17- import { toast } from "sonner"
1814
1915import { TagContext } from "./context"
2016import { TagList } from "./list"
2117
22- export const TagPicker : React . FC = ( ) => {
18+ interface P {
19+ value : string [ ]
20+ onChange : ( tags : string [ ] ) => void
21+ }
22+
23+ export const TagPicker : React . FC < P > = ( { value, onChange } ) => {
2324 const { tags } = useCollectionContext ( )
24- const { habit, update } = useHabitContext ( )
2525
2626 const [ open , setOpen ] = React . useState ( false )
27- const [ selected , setSelected ] = React . useState < Set < string > > ( new Set ( habit . tagNames ) )
27+ const [ selected , setSelected ] = React . useState < Set < string > > ( new Set ( value ) )
2828
2929 const toggleTag = ( name : string ) => {
3030 const next = new Set ( selected )
@@ -38,24 +38,19 @@ export const TagPicker: React.FC = () => {
3838
3939 const handleCancel = ( ) => {
4040 setOpen ( false )
41- setSelected ( new Set ( habit . tagNames ) )
41+ setSelected ( new Set ( value ) )
4242 }
4343
4444 const handleSubmit = ( ) => {
45- const copy = clone ( HabitSchema , habit )
46- copy . tagNames = [ ...selected ] . sort ( )
47-
48- toast . promise (
49- update . mutateAsync ( copy ) . then ( ( ) => setOpen ( false ) ) ,
50- {
51- error : ( e : Error ) => ( { message : "Habit update failed" , description : e . message } ) ,
52- } ,
53- )
45+ const tags = [ ...selected ]
46+ tags . sort ( )
47+ onChange ( tags )
48+ setOpen ( false )
5449 }
5550
5651 return (
5752 < div data-testid = "tag-picker" className = "flex items-center gap-1 flex-wrap" >
58- < TagList tags = { habit . tagNames } >
53+ < TagList tags = { value } >
5954 < TagContext value = { { active : selected , onClick : toggleTag } } >
6055 < Dialog open = { open } onOpenChange = { setOpen } >
6156 < DialogTrigger asChild >
@@ -86,24 +81,12 @@ export const TagPicker: React.FC = () => {
8681 < TagList tags = { tags } />
8782 </ div >
8883 < DialogFooter >
89- < Button
90- variant = "outline"
91- onClick = { handleCancel }
92- disabled = { update . isPending }
93- >
84+ < Button variant = "outline" onClick = { handleCancel } >
9485 < Undo2 />
9586 Cancel
9687 </ Button >
97- < Button
98- variant = "outline"
99- disabled = { update . isPending }
100- onClick = { handleSubmit }
101- >
102- { update . isPending ? (
103- < Loader2 className = "animate-spin" />
104- ) : (
105- < Check className = "text-emerald-600" />
106- ) }
88+ < Button variant = "outline" onClick = { handleSubmit } >
89+ < Check className = "text-emerald-600" />
10790 Save
10891 </ Button >
10992 </ DialogFooter >
0 commit comments