Skip to content

Open source CapCut alternative.

TypeScript

fix(site): re-read notes, projects and Library on @

David Le44ac74610ee0
diff --git a/site/src/cut/components/AssetRefs.tsx b/site/src/cut/components/AssetRefs.tsx
index aa836f52..3cd8d2eb 100644
--- a/site/src/cut/components/AssetRefs.tsx
+++ b/site/src/cut/components/AssetRefs.tsx
@@ -2,6 +2,7 @@
import { useEffect, useLayoutEffect, useMemo, useRef, useState, type ReactNode, type RefObject } from "react";
import Link from "next/link";
+import { useQueryClient } from "@tanstack/react-query";
import { useTranslations } from "next-intl";
import { CUT_APP_BASE, homeHref, projectHref } from "@/cut/lib/nav";
import { Check, Clock, Copy, FileText, X } from "lucide-react";
@@ -18,6 +19,7 @@ import {
} from "@/cut/lib/assetRef";
import { MEDIA_CORS } from "@/cut/lib/mediaCors";
import { isCollectionRef, useRefFor, useRefCandidates } from "@/cut/lib/assetRef";
+import { refreshMentionSources } from "@/cut/lib/queries";
import { useInView } from "@/cut/hooks/useInView";
import { revealRef } from "@/cut/lib/refReveal";
import { formatTime } from "@/cut/lib/time";
@@ -1418,6 +1420,17 @@ export function MentionTextarea({
}, [mirrorSegs, value, onChange]);
const mention = useMemo(() => mentionAtCaret(value, caret), [value, caret]);
+
+ // Each new mention re-reads the notes, projects and Library, so whatever
+ // the phone or another tab added is in the menu it opens.
+ const client = useQueryClient();
+ const mentionStart = mention?.start ?? null;
+ useEffect(() => {
+ if (mentionStart === null) {
+ return;
+ }
+ refreshMentionSources(client);
+ }, [client, mentionStart]);
// A caret strictly inside an intact, resolved token is a click on the pill —
// the mention is complete, and offering lookalike suggestions for its
// half-read query would misdirect it.
diff --git a/site/src/cut/lib/queries.ts b/site/src/cut/lib/queries.ts
index d265d9bf..5a3f78f7 100644
--- a/site/src/cut/lib/queries.ts
+++ b/site/src/cut/lib/queries.ts
@@ -380,6 +380,15 @@ export function useNotes(enabled = true) {
});
}
+/** Re-read the listings a mention reaches outside this tab. Notes, projects
+ * and the Library are also written from the phone and other tabs, and this
+ * tab's reads of them only run on mount. */
+export function refreshMentionSources(client: QueryClient) {
+ void client.invalidateQueries({ queryKey: notesKey });
+ void client.invalidateQueries({ queryKey: ["cut", "projects"] });
+ void client.invalidateQueries({ queryKey: ["cut", "library"] });
+}
+
/** Optimistic edit of the cached notes, snapshot moving with it. */
export function patchNotes(client: QueryClient, fn: (prev: NotesData) => NotesData) {
const next = client.setQueryData<NotesData>(notesKey, (prev) => (prev ? fn(prev) : prev));
diff --git a/site/src/cut/lib/refreshMentionSources.test.ts b/site/src/cut/lib/refreshMentionSources.test.ts
new file mode 100644
index 00000000..656b0fb0
--- /dev/null
+++ b/site/src/cut/lib/refreshMentionSources.test.ts
@@ -0,0 +1,32 @@
+import { expect, test } from "bun:test";
+import { QueryClient, QueryObserver } from "@tanstack/react-query";
+import { libraryKey, notesKey, projectsKey, refreshMentionSources } from "./queries";
+
+test("a new mention re-reads every listing written outside this tab", async () => {
+ const client = new QueryClient();
+ const reads = new Map<string, number>();
+ const keys = [notesKey, projectsKey("cloud"), projectsKey("local"), libraryKey("cloud+local")];
+ const observers = keys.map((queryKey) => {
+ const name = queryKey.join("/");
+ const observer = new QueryObserver(client, {
+ queryKey,
+ queryFn: async () => {
+ reads.set(name, (reads.get(name) ?? 0) + 1);
+ return [];
+ },
+ staleTime: Infinity,
+ });
+ observer.subscribe(() => {});
+ return observer;
+ });
+ await new Promise((r) => setTimeout(r, 10));
+
+ refreshMentionSources(client);
+ await new Promise((r) => setTimeout(r, 10));
+ for (const key of keys) {
+ expect(reads.get(key.join("/"))).toBe(2);
+ }
+ for (const observer of observers) {
+ observer.destroy();
+ }
+});