|
| 1 | +import { useAuthStore } from "@features/auth/stores/authStore"; |
| 2 | +import { useTaskViewedStore } from "@features/sidebar/stores/taskViewedStore"; |
| 3 | +import { get } from "@renderer/di/container"; |
| 4 | +import { RENDERER_TOKENS } from "@renderer/di/tokens"; |
| 5 | +import { logger } from "@renderer/lib/logger"; |
| 6 | +import type { TaskService } from "@renderer/services/task/service"; |
| 7 | +import { useNavigationStore } from "@stores/navigationStore"; |
| 8 | +import { trpcReact, trpcVanilla } from "@renderer/trpc"; |
| 9 | +import type { Task } from "@shared/types"; |
| 10 | +import { useQueryClient } from "@tanstack/react-query"; |
| 11 | +import { useCallback, useEffect, useRef } from "react"; |
| 12 | +import { toast } from "sonner"; |
| 13 | + |
| 14 | +const log = logger.scope("task-deep-link"); |
| 15 | + |
| 16 | +const taskKeys = { |
| 17 | + all: ["tasks"] as const, |
| 18 | + lists: () => [...taskKeys.all, "list"] as const, |
| 19 | + list: (filters?: { repository?: string }) => |
| 20 | + [...taskKeys.lists(), filters] as const, |
| 21 | +}; |
| 22 | + |
| 23 | +/** |
| 24 | + * Hook that subscribes to deep link events and handles opening tasks. |
| 25 | + * Uses TaskService to fetch task and set up workspace via the saga pattern. |
| 26 | + */ |
| 27 | +export function useTaskDeepLink() { |
| 28 | + const navigateToTask = useNavigationStore((state) => state.navigateToTask); |
| 29 | + const markAsViewed = useTaskViewedStore((state) => state.markAsViewed); |
| 30 | + const queryClient = useQueryClient(); |
| 31 | + const isAuthenticated = useAuthStore((state) => state.isAuthenticated); |
| 32 | + const hasFetchedPending = useRef(false); |
| 33 | + |
| 34 | + const handleOpenTask = useCallback( |
| 35 | + async (taskId: string) => { |
| 36 | + log.info(`Opening task from deep link: ${taskId}`); |
| 37 | + |
| 38 | + try { |
| 39 | + const taskService = get<TaskService>(RENDERER_TOKENS.TaskService); |
| 40 | + const result = await taskService.openTask(taskId); |
| 41 | + |
| 42 | + if (!result.success) { |
| 43 | + log.error("Failed to open task from deep link", { |
| 44 | + taskId, |
| 45 | + error: result.error, |
| 46 | + failedStep: result.failedStep, |
| 47 | + }); |
| 48 | + toast.error(`Failed to open task: ${result.error}`); |
| 49 | + return; |
| 50 | + } |
| 51 | + |
| 52 | + const { task } = result.data; |
| 53 | + |
| 54 | + // Add task to query cache so it shows in sidebar |
| 55 | + queryClient.setQueryData<Task[]>(taskKeys.list(), (old) => { |
| 56 | + if (!old) return [task]; |
| 57 | + const existingIndex = old.findIndex((t) => t.id === task.id); |
| 58 | + if (existingIndex >= 0) { |
| 59 | + const updated = [...old]; |
| 60 | + updated[existingIndex] = task; |
| 61 | + return updated; |
| 62 | + } |
| 63 | + return [task, ...old]; |
| 64 | + }); |
| 65 | + |
| 66 | + // Invalidate to ensure sync with server |
| 67 | + queryClient.invalidateQueries({ queryKey: taskKeys.lists() }); |
| 68 | + |
| 69 | + // Mark as viewed and navigate |
| 70 | + markAsViewed(taskId); |
| 71 | + navigateToTask(task); |
| 72 | + |
| 73 | + log.info(`Successfully opened task from deep link: ${taskId}`); |
| 74 | + } catch (error) { |
| 75 | + log.error("Unexpected error opening task from deep link:", error); |
| 76 | + toast.error("Failed to open task"); |
| 77 | + } |
| 78 | + }, |
| 79 | + [navigateToTask, markAsViewed, queryClient], |
| 80 | + ); |
| 81 | + |
| 82 | + // Check for pending deep link on mount (for cold start via deep link) |
| 83 | + useEffect(() => { |
| 84 | + if (!isAuthenticated || hasFetchedPending.current) return; |
| 85 | + |
| 86 | + const fetchPending = async () => { |
| 87 | + hasFetchedPending.current = true; |
| 88 | + try { |
| 89 | + const pendingTaskId = await trpcVanilla.deepLink.getPendingTaskId.query(); |
| 90 | + if (pendingTaskId) { |
| 91 | + log.info(`Found pending deep link task: ${pendingTaskId}`); |
| 92 | + handleOpenTask(pendingTaskId); |
| 93 | + } |
| 94 | + } catch (error) { |
| 95 | + log.error("Failed to check for pending deep link:", error); |
| 96 | + } |
| 97 | + }; |
| 98 | + |
| 99 | + fetchPending(); |
| 100 | + }, [isAuthenticated, handleOpenTask]); |
| 101 | + |
| 102 | + // Subscribe to deep link events (for warm start via deep link) |
| 103 | + trpcReact.deepLink.onOpenTask.useSubscription(undefined, { |
| 104 | + onData: (data) => { |
| 105 | + log.info(`Received deep link event for task: ${data.taskId}`); |
| 106 | + handleOpenTask(data.taskId); |
| 107 | + }, |
| 108 | + }); |
| 109 | +} |
0 commit comments