Files
triggerdotdev--trigger.dev/apps/webapp/app/hooks/useRevalidateOnParam.ts
Matt Aitken bc0d1ff59a Metrics dashboards (#3019)
Summary
- Implemented metrics dashboards with a built-in dashboard and custom
dashboards
- Added a "Big number” display type

What changed
- New data format for metric layouts and saving/editing layouts
(editing, saving, cancel revert)
  - QueryWidget usable on Query page and Metrics dashboards
  - Time filtering, auto-reloading and timeBucket() auto-bin support
- Filters added to metrics; widget popover/improved history and blank
states
- Side menu:
- Metrics/Insights section with icons, colors, padding, collapsible
behavior and reordering of custom dashboards
- Move action logic into service for reuse and API querying; refactor
reordering for reuse
<!-- devin-review-badge-begin -->

---

<a
href="https://app.devin.ai/review/triggerdotdev/trigger.dev/pull/3019"
target="_blank">
  <picture>
<source media="(prefers-color-scheme: dark)"
srcset="https://static.devin.ai/assets/gh-open-in-devin-review-dark.svg?v=1">
<img
src="https://static.devin.ai/assets/gh-open-in-devin-review-light.svg?v=1"
alt="Open with Devin">
  </picture>
</a>
<!-- devin-review-badge-end -->

---------

Co-authored-by: James Ritchie <james@trigger.dev>
2026-02-12 17:48:02 +00:00

58 lines
1.7 KiB
TypeScript

import { useEffect } from "react";
import { useRevalidator, useSearchParams } from "@remix-run/react";
type UseRevalidateOnParamOptions = {
/** The query param(s) that trigger revalidation */
param: string | string[];
/** Callback fired when revalidation is triggered */
onRevalidate?: () => void;
};
/**
* Hook that triggers revalidation when specific query params are present,
* then removes those params from the URL.
*
* Usage:
* ```ts
* // Revalidate when ?_revalidate is present
* useRevalidateOnParam({ param: "_revalidate" });
*
* // With callback to close a modal
* useRevalidateOnParam({
* param: "_revalidate",
* onRevalidate: () => setEditorMode(null),
* });
* ```
*
* The redirect should include the param:
* ```ts
* return redirect(`${dashboardPath}?_revalidate=${Date.now()}`);
* ```
*/
export function useRevalidateOnParam({ param, onRevalidate }: UseRevalidateOnParamOptions) {
const [searchParams, setSearchParams] = useSearchParams();
const revalidator = useRevalidator();
const paramArray = Array.isArray(param) ? param : [param];
useEffect(() => {
// Check if any of the trigger params are present
const hasParam = paramArray.some((p) => searchParams.has(p));
if (hasParam) {
// Trigger revalidation
revalidator.revalidate();
// Call the callback if provided
onRevalidate?.();
// Remove the trigger params from the URL
const newParams = new URLSearchParams(searchParams);
paramArray.forEach((p) => newParams.delete(p));
// Update URL without the params (replace to avoid adding to history)
setSearchParams(newParams, { replace: true });
}
}, [searchParams, setSearchParams, revalidator, paramArray, onRevalidate]);
}