@@ -2,6 +2,7 @@ import { json } from "@remix-run/node";
22import type { LoaderFunctionArgs } from "@remix-run/node" ;
33import { useFetcher , type ShouldRevalidateFunction } from "@remix-run/react" ;
44import { useEffect , useRef } from "react" ;
5+ import { useLatest } from "react-use" ;
56import { logger } from "~/services/logger.server" ;
67import { requireUserId } from "~/services/session.server" ;
78import { getRecentChangelogs , verifyOrgMembership } from "~/services/platformNotifications.server" ;
@@ -43,28 +44,30 @@ const POLL_INTERVAL_MS = 60_000;
4344export function useRecentChangelogs ( organizationId ?: string , projectId ?: string ) {
4445 const fetcher = useFetcher < typeof loader > ( ) ;
4546 const { load, state } = fetcher ;
47+ const stateRef = useLatest ( state ) ;
4648 const lastLoadedUrl = useRef < string | null > ( null ) ;
49+ const params = new URLSearchParams ( ) ;
50+ if ( organizationId ) params . set ( "organizationId" , organizationId ) ;
51+ if ( projectId ) params . set ( "projectId" , projectId ) ;
52+ const qs = params . toString ( ) ;
53+ const url = `/resources/platform-changelogs${ qs ? `?${ qs } ` : "" } ` ;
4754
4855 useEffect ( ( ) => {
49- const params = new URLSearchParams ( ) ;
50- if ( organizationId ) params . set ( "organizationId" , organizationId ) ;
51- if ( projectId ) params . set ( "projectId" , projectId ) ;
52- const qs = params . toString ( ) ;
53- const url = `/resources/platform-changelogs${ qs ? `?${ qs } ` : "" } ` ;
54-
5556 if ( lastLoadedUrl . current !== url && state === "idle" ) {
5657 lastLoadedUrl . current = url ;
5758 load ( url ) ;
5859 }
60+ } , [ load , state , url ] ) ;
5961
62+ useEffect ( ( ) => {
6063 const interval = setInterval ( ( ) => {
61- if ( state === "idle" ) {
64+ if ( stateRef . current === "idle" ) {
6265 load ( url ) ;
6366 }
6467 } , POLL_INTERVAL_MS ) ;
6568
6669 return ( ) => clearInterval ( interval ) ;
67- } , [ organizationId , projectId , load , state ] ) ;
70+ } , [ load , stateRef , url ] ) ;
6871
6972 return {
7073 changelogs : fetcher . data ?. changelogs ?? [ ] ,
0 commit comments