Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

Add a shared useIsTouchDevice hook

Rafiaec4eecf87d3
diff --git a/apps/web/app/(ee)/partners.dub.co/(dashboard)/overview/earnings-chart.tsx b/apps/web/app/(ee)/partners.dub.co/(dashboard)/overview/earnings-chart.tsx
index 21ba9a4ee1..c7fcc465db 100644
--- a/apps/web/app/(ee)/partners.dub.co/(dashboard)/overview/earnings-chart.tsx
+++ b/apps/web/app/(ee)/partners.dub.co/(dashboard)/overview/earnings-chart.tsx
@@ -4,6 +4,7 @@ import { formatDateTooltip } from "@/lib/analytics/format-date-tooltip";
import { testIds } from "@/lib/e2e/test-ids";
import { usePartnerProfileEarningsTimeseries } from "@/lib/swr/use-partner-profile-earnings";
import useProgramEnrollments from "@/lib/swr/use-program-enrollments";
+import { useIsTouchDevice } from "@dub/ui";
import {
Areas,
ChartContext,
@@ -16,7 +17,7 @@ import { currencyFormatter, nFormatter } from "@dub/utils";
import NumberFlow from "@number-flow/react";
import { LinearGradient } from "@visx/gradient";
import { useRouter } from "next/navigation";
-import { useEffect, useId, useMemo, useState } from "react";
+import { useId, useMemo } from "react";
import { ProgramLogo } from "./overview-card";
import { useOverviewDateRange } from "./use-overview-date-range";
import { useTopProgramEarnings } from "./use-top-program-earnings";
@@ -30,11 +31,7 @@ export function EarningsChart() {
// on touch devices, the chart opens /programs at the end of each drag, which
// stops partners from reading the tooltip
- const [isTouchDevice, setIsTouchDevice] = useState(false);
-
- useEffect(() => {
- setIsTouchDevice(window.matchMedia("(pointer: coarse)").matches);
- }, []);
+ const isTouchDevice = useIsTouchDevice();
const { data: timeseries, error } = usePartnerProfileEarningsTimeseries({
groupBy: "programId",
diff --git a/apps/web/ui/partners/confirm-payouts-sheet.tsx b/apps/web/ui/partners/confirm-payouts-sheet.tsx
index 05599f3a18..bc59435828 100644
--- a/apps/web/ui/partners/confirm-payouts-sheet.tsx
+++ b/apps/web/ui/partners/confirm-payouts-sheet.tsx
@@ -40,6 +40,7 @@ import {
ShimmerDots,
Table,
TooltipContent,
+ useIsTouchDevice,
useRouterStuff,
useTable,
useTablePagination,
@@ -815,11 +816,7 @@ function ConfirmPayoutsSheetContent() {
useTrialLimitActivateModal();
const trialActive = isWorkspaceBillingTrialActive(trialEndsAt);
- const [isTouchDevice, setIsTouchDevice] = useState(false);
-
- useEffect(() => {
- setIsTouchDevice(window.matchMedia("(pointer: coarse)").matches);
- }, []);
+ const isTouchDevice = useIsTouchDevice();
return (
<>
diff --git a/packages/ui/src/hooks/index.ts b/packages/ui/src/hooks/index.ts
index 2e36db2cfa..6b32e5fdf6 100644
--- a/packages/ui/src/hooks/index.ts
+++ b/packages/ui/src/hooks/index.ts
@@ -9,6 +9,7 @@ export * from "./use-enter-submit";
export * from "./use-in-viewport";
export * from "./use-input-focused";
export * from "./use-intersection-observer";
+export * from "./use-is-touch-device";
export * from "./use-keyboard-shortcut";
export * from "./use-latest-callback";
export * from "./use-local-storage";
diff --git a/packages/ui/src/hooks/use-is-touch-device.ts b/packages/ui/src/hooks/use-is-touch-device.ts
new file mode 100644
index 0000000000..b9cbe14c0f
--- /dev/null
+++ b/packages/ui/src/hooks/use-is-touch-device.ts
@@ -0,0 +1,12 @@
+import { useEffect, useState } from "react";
+
+// true when the primary pointer is a finger, for example on a phone or tablet
+export function useIsTouchDevice() {
+ const [isTouchDevice, setIsTouchDevice] = useState(false);
+
+ useEffect(() => {
+ setIsTouchDevice(window.matchMedia("(pointer: coarse)").matches);
+ }, []);
+
+ return isTouchDevice;
+}