@melony/react 0.1.41 → 0.1.43

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import * as React11 from 'react';
2
- import React11__default, { createContext, useState, useContext, useCallback, useEffect, useMemo, useRef } from 'react';
2
+ import React11__default, { createContext, useState, useContext, useCallback, useRef, useEffect, useMemo } from 'react';
3
3
  import { convertEventsToMessages } from 'melony';
4
4
  import { NuqsAdapter } from 'nuqs/adapters/react';
5
5
  import { QueryClient, QueryClientProvider, useQueryClient, useQuery, useMutation } from '@tanstack/react-query';
@@ -756,10 +756,15 @@ var ThreadProvider = ({
756
756
  initialThreadId: providedInitialThreadId
757
757
  }) => {
758
758
  const queryClient = useQueryClient();
759
+ const melonyContext = useContext(MelonyContext);
759
760
  const [activeThreadId, setActiveThreadId] = useQueryState(
760
761
  "threadId",
761
762
  parseAsString
762
763
  );
764
+ const prevActiveThreadIdRef = useRef(activeThreadId);
765
+ useEffect(() => {
766
+ prevActiveThreadIdRef.current = activeThreadId;
767
+ }, [activeThreadId]);
763
768
  useEffect(() => {
764
769
  if (!activeThreadId && providedInitialThreadId) {
765
770
  setActiveThreadId(providedInitialThreadId);
@@ -768,16 +773,24 @@ var ThreadProvider = ({
768
773
  const {
769
774
  data: threads = [],
770
775
  isLoading,
776
+ isFetched: isFetchedThreads,
771
777
  error: threadsError,
772
778
  refetch: refreshThreads
773
779
  } = useQuery({
774
780
  queryKey: ["threads"],
775
- queryFn: () => service.getThreads()
781
+ queryFn: () => service.getThreads(),
782
+ staleTime: prevActiveThreadIdRef.current === null && activeThreadId !== null ? Infinity : 0
776
783
  });
784
+ const isNewThread = useMemo(() => {
785
+ if (!activeThreadId || !isFetchedThreads) return false;
786
+ return !threads.some((t) => t.id === activeThreadId);
787
+ }, [activeThreadId, threads, isFetchedThreads]);
777
788
  const { data: threadEvents = [], isLoading: isLoadingEvents } = useQuery({
778
789
  queryKey: ["threads", activeThreadId, "events"],
779
790
  queryFn: () => service.getEvents(activeThreadId),
780
- enabled: !!activeThreadId
791
+ enabled: !!activeThreadId,
792
+ initialData: isNewThread ? melonyContext?.events : void 0,
793
+ staleTime: isNewThread ? Infinity : 0
781
794
  });
782
795
  const createMutation = useMutation({
783
796
  mutationFn: async () => {