From 727cfe00831cc9babf34634bc3f5f1d3b8b4ab12 Mon Sep 17 00:00:00 2001 From: Timothy Cardoza Date: Thu, 19 Mar 2026 16:20:37 -0500 Subject: [PATCH] more stable but unfinished sharing refactor --- src/components/Navigation.tsx | 3 +- src/store/index.ts | 52 ++++++++++++++++++++++++++++------- 2 files changed, 44 insertions(+), 11 deletions(-) diff --git a/src/components/Navigation.tsx b/src/components/Navigation.tsx index 5d29f88..f17f876 100644 --- a/src/components/Navigation.tsx +++ b/src/components/Navigation.tsx @@ -3,7 +3,7 @@ import { LayoutDashboard, ListTodo, Settings, Clock, ArrowUpCircle, PanelLeftClo import { cn } from "@/lib/utils"; import { Button } from "./ui/button"; import { Popover, PopoverContent, PopoverTrigger } from "@/components/ui/popover"; -import { store, currentTaskContext, setCurrentTaskContext } from "@/store"; +import { store, currentTaskContext, setCurrentTaskContext, loadTasksForOwner } from "@/store"; import { pb } from "@/lib/pocketbase"; import { createSignal, createEffect } from "solid-js"; @@ -170,6 +170,7 @@ export const ContextSwitcher: Component<{ setCurrentTaskContext({ bucketId: id, name }); } else { setCurrentTaskContext({ userId: id, name }); + void loadTasksForOwner(id); } // Close popover diff --git a/src/store/index.ts b/src/store/index.ts index 39f1fac..ba19899 100644 --- a/src/store/index.ts +++ b/src/store/index.ts @@ -554,6 +554,11 @@ const getPersonalContext = (userId = pb.authStore.model?.id) => ) || null : null; +const getSubscribedBucketContexts = () => + store.subscribedBuckets + .map(id => store.contexts.find(context => context.id === id)) + .filter((context): context is ShareContext => !!context && !context.deletedAt && context.kind === "bucket"); + export const matchesFilter = (task: Task) => { const currentUserId = pb.authStore.model?.id; const ctx = currentTaskContext(); @@ -1532,9 +1537,8 @@ export const initStore = async () => { // STAGE 2 & 3: Background Sync const backgroundSync = async () => { - const subscribedBucketKeys = store.subscribedBuckets - .map(id => store.contexts.find(context => context.id === id)?.key) - .filter(Boolean); + const subscribedBucketTags = getSubscribedBucketContexts() + .map(context => buildShareTag(context.displayName)); const supervisedUserIds = store.supervisedContexts.map(context => context.ownerUserId); const collaborativeKeys = store.contexts .filter(context => @@ -1553,8 +1557,8 @@ export const initStore = async () => { fields: LIST_FIELDS, requestKey: null }), - (subscribedBucketKeys.length > 0) ? pb.collection(TASGRID_COLLECTION).getFullList({ - filter: `(${subscribedBucketKeys.map(key => `tags ~ "@${key}"`).join(' || ')}) && completed = false`, + (subscribedBucketTags.length > 0) ? pb.collection(TASGRID_COLLECTION).getFullList({ + filter: `(${subscribedBucketTags.map(tag => `tags ~ "${tag}"`).join(' || ')}) && completed = false`, sort: '-created', fields: LIST_FIELDS, requestKey: null @@ -2379,9 +2383,8 @@ export const loadAllHistory = async () => { toast.promise( (async () => { // Fetch ALL tasks for user (including completed) - const bucketKeys = store.subscribedBuckets - .map(id => store.contexts.find(context => context.id === id)?.key) - .filter(Boolean); + const bucketTags = getSubscribedBucketContexts() + .map(context => buildShareTag(context.displayName)); const supervisedUserIds = store.supervisedContexts.map(context => context.ownerUserId); const collaborativeTags = store.contexts .filter(context => @@ -2396,9 +2399,9 @@ export const loadAllHistory = async () => { pb.collection(TASGRID_COLLECTION).getFullList({ filter: relationFilter("user", userId), sort: '-created', fields: LIST_FIELDS, requestKey: null }) ]; - if (bucketKeys.length > 0) { + if (bucketTags.length > 0) { promises.push(pb.collection(TASGRID_COLLECTION).getFullList({ - filter: bucketKeys.map(key => `tags ~ "@${key}"`).join(' || '), + filter: bucketTags.map(tag => `tags ~ "${tag}"`).join(' || '), sort: '-created', fields: LIST_FIELDS, requestKey: null @@ -2602,6 +2605,35 @@ export const shareTask = async (_taskId: string, _userId: string, _access: 'view toast.info("Direct task sharing has been replaced by @user tags."); }; +export const loadTasksForOwner = async (userId: string) => { + if (!pb.authStore.isValid || !userId) return; + + try { + const records = await pb.collection(TASGRID_COLLECTION).getFullList({ + filter: relationFilter("user", userId), + sort: '-created', + fields: LIST_FIELDS, + requestKey: `owner-context-${userId}` + }); + + setStore("tasks", (currentTasks) => { + const taskMap = new Map(currentTasks.map(task => [task.id, task])); + records.forEach((record: any) => { + if (record.tags?.includes("__template__")) return; + const mapped = mapRecordToTask(record); + const existing = taskMap.get(mapped.id); + if (existing && existing.content) { + mapped.content = existing.content; + } + taskMap.set(mapped.id, mapped); + }); + return Array.from(taskMap.values()); + }); + } catch (err) { + console.error("Failed to load owner context tasks:", err); + } +}; + export const revokeShare = async (_taskId: string, _userId: string) => { if (!pb.authStore.isValid) return; toast.info("Remove the matching @user tag from the task instead.");