import type { InfiniteQueryObserverOptions, MutationKey, InfiniteData } from "@tanstack/query-core"; import { type WeavyType } from "../client/weavy"; import { MessageType, MutateMessageProps, type MessagesResultType } from "../types/messages.types"; import { PollOptionType } from "../types/polls.types"; import { addCacheItem, getCacheItem, getPendingCacheItem, updateCacheItem, updateCacheItems, } from "../utils/query-cache"; import { MemberDetailType } from "../types/members.types"; import { QueryPagingAroundType } from "../types/query.types"; import { firstPageParam, initialPageParam, lastPageParam, queryPageParams } from "./api"; export type MessagesQueryKeyType = ["messages", number | null]; export function getMessagesOptions( weavy: WeavyType, appId: number | null, around?: number, options: object = {} ): InfiniteQueryObserverOptions> { // TODO: `around` must be in query key for messages to update on parameter change const queryKey: MessagesQueryKeyType = ["messages", appId] return { ...options, initialPageParam: initialPageParam(around), queryKey, queryFn: async (opt) => { const queryParams = queryPageParams(opt.pageParam as QueryPagingAroundType) const url = `/api/apps/${appId}/messages?order_by=id+desc&${queryParams.toString()}`; const response = await weavy.fetch(url); const result = (await response.json()) as MessagesResultType; result.data = result.data?.reverse() || []; return result; }, getPreviousPageParam: lastPageParam(), getNextPageParam: firstPageParam(), }; } export function getAddMessageMutationOptions(weavy: WeavyType, mutationKey: MutationKey) { const options = { mutationFn: async (variables: MutateMessageProps) => { const response = await weavy.fetch("/api/apps/" + variables.app_id + "/messages", { method: "POST", body: JSON.stringify({ text: variables.text, blobs: variables.blobs, embed_id: variables.embed_id || null, meeting_id: variables.meeting_id, options: (variables.poll_options || []) .filter((o: PollOptionType) => o.text.trim() !== "") .map((o: PollOptionType) => { return { text: o.text }; }), metadata: variables.metadata || null, context: variables.context, }), }); return (await response.json()) as MessageType; }, mutationKey: mutationKey, onMutate: (variables: MutateMessageProps) => { const queryKey = ["messages", variables.app_id]; // TODO: wait for any queries to finish instead of cancelling //await weavy.queryClient.cancelQueries({ queryKey: queryKey }); const newest = getPendingCacheItem(weavy.queryClient, queryKey, false); const pending: MessageType = { id: newest ? newest.id - 1 : -1, app: { id: variables.app_id }, text: variables.text, html: variables.text, plain: variables.text, created_by: variables.user, created_at: new Date().toUTCString(), attachments: { count: 0 }, reactions: { count: 0 }, is_starred: false, is_subscribed: true, is_trashed: false, }; addCacheItem(weavy.queryClient, queryKey, pending); }, onSuccess: (data: MessageType) => { // update members and set marked_id for current user updateCacheItems( weavy.queryClient, { queryKey: ["members", data.app.id] }, data.created_by.id, (item) => ({ ...item, marked_id: data.id, marked_at: data.created_at, }) ); const queryKey = ["messages", data.app.id]; const { queryClient } = weavy; // try to find existing or pending message const existing = getCacheItem(queryClient, queryKey, data.id); const pending = existing ? null : getPendingCacheItem(queryClient, queryKey, true); // update existing or pending message if found if (existing) { updateCacheItem(queryClient, queryKey, existing.id, () => data); } else if (pending) { updateCacheItem(queryClient, queryKey, pending.id, () => data); } else { // add message if not found addCacheItem(queryClient, queryKey, data); } }, }; return options; }