1- import { useHostTRPC , useHostTRPCClient } from "@posthog/host-router/react" ;
2- import { useMutation , useQuery , useQueryClient } from "@tanstack/react-query" ;
1+ import { useMutation , useQueryClient } from "@tanstack/react-query" ;
32import { useCallback , useMemo , useRef } from "react" ;
3+ import { useAuthenticatedQuery } from "../../hooks/useAuthenticatedQuery" ;
4+ import { pinnedTasksApi } from "./taskMetaApi" ;
5+
6+ const PINNED_TASKS_QUERY_KEY = [ "task-pins" ] as const ;
47
58export function usePinnedTasks ( ) {
6- const trpc = useHostTRPC ( ) ;
7- const hostClient = useHostTRPCClient ( ) ;
89 const queryClient = useQueryClient ( ) ;
9- const pinnedQueryKey = trpc . workspace . getPinnedTaskIds . queryKey ( ) ;
10+ const pinnedQueryKey = PINNED_TASKS_QUERY_KEY ;
1011
11- const { data : pinnedTaskIds = [ ] , isLoading } = useQuery (
12- trpc . workspace . getPinnedTaskIds . queryOptions ( undefined , {
13- staleTime : 30_000 ,
14- } ) ,
12+ const { data : pinnedTaskIds = [ ] , isLoading } = useAuthenticatedQuery (
13+ pinnedQueryKey ,
14+ ( client ) => client . getPinnedTaskIds ( ) ,
15+ { staleTime : 30_000 } ,
1516 ) ;
1617
1718 const pinnedSet = useMemo ( ( ) => new Set ( pinnedTaskIds ) , [ pinnedTaskIds ] ) ;
1819
1920 const togglePinMutation = useMutation ( {
20- mutationFn : ( { taskId } : { taskId : string } ) =>
21- hostClient . workspace . togglePin . mutate ( { taskId } ) ,
22- onMutate : async ( { taskId } ) => {
21+ scope : { id : "task-pins" } ,
22+ mutationFn : ( { taskId, pinned } : { taskId : string ; pinned : boolean } ) =>
23+ pinnedTasksApi . setPinned ( taskId , pinned ) ,
24+ onMutate : async ( { taskId, pinned } ) => {
2325 await queryClient . cancelQueries ( { queryKey : pinnedQueryKey } ) ;
2426 const previous = queryClient . getQueryData < string [ ] > ( pinnedQueryKey ) ;
2527 const wasPinned = previous ?. includes ( taskId ) ;
2628 queryClient . setQueryData < string [ ] > ( pinnedQueryKey , ( old ) => {
27- if ( ! old ) return wasPinned ? [ ] : [ taskId ] ;
28- return wasPinned ? old . filter ( ( id ) => id !== taskId ) : [ ...old , taskId ] ;
29+ const filtered = old ?. filter ( ( id ) => id !== taskId ) ?? [ ] ;
30+ return pinned ? [ ...filtered , taskId ] : filtered ;
2931 } ) ;
3032 return { previous, wasPinned, taskId } ;
3133 } ,
@@ -52,16 +54,27 @@ export function usePinnedTasks() {
5254 pinnedSetRef . current = pinnedSet ;
5355
5456 const togglePin = useCallback ( async ( taskId : string ) => {
55- await togglePinMutationRef . current . mutateAsync ( { taskId } ) ;
57+ const pinned = ! pinnedSetRef . current . has ( taskId ) ;
58+ const nextPinnedSet = new Set ( pinnedSetRef . current ) ;
59+ if ( pinned ) nextPinnedSet . add ( taskId ) ;
60+ else nextPinnedSet . delete ( taskId ) ;
61+ pinnedSetRef . current = nextPinnedSet ;
62+ await togglePinMutationRef . current . mutateAsync ( {
63+ taskId,
64+ pinned,
65+ } ) ;
5666 } , [ ] ) ;
5767
58- const unpin = useCallback ( async ( taskId : string ) => {
59- if ( ! pinnedSetRef . current . has ( taskId ) ) return ;
60- const result = await togglePinMutationRef . current . mutateAsync ( { taskId } ) ;
61- if ( result . isPinned ) {
62- await togglePinMutationRef . current . mutateAsync ( { taskId } ) ;
63- }
64- } , [ ] ) ;
68+ const unpin = useCallback (
69+ async ( taskId : string ) => {
70+ if ( ! pinnedSetRef . current . has ( taskId ) ) return ;
71+ await pinnedTasksApi . unpin ( taskId ) ;
72+ queryClient . setQueryData < string [ ] > ( pinnedQueryKey , ( old ) =>
73+ old ?. filter ( ( id ) => id !== taskId ) ,
74+ ) ;
75+ } ,
76+ [ queryClient , pinnedQueryKey ] ,
77+ ) ;
6578
6679 const isPinned = useCallback (
6780 ( taskId : string ) => pinnedSet . has ( taskId ) ,
0 commit comments