From 3ab9f0423e97af63da4bf6a13ffd786955bd5b3b Mon Sep 17 00:00:00 2001
From: Armand Philippot <git@armandphilippot.com>
Date: Thu, 26 Oct 2023 21:55:55 +0200
Subject: refactor(hooks,providers): rewrite useAckee hook and AckeeProvider

---
 .../providers/ackee-provider/ackee-provider.tsx    | 95 ++++++++++++++++++++++
 1 file changed, 95 insertions(+)
 create mode 100644 src/utils/providers/ackee-provider/ackee-provider.tsx

(limited to 'src/utils/providers/ackee-provider/ackee-provider.tsx')

diff --git a/src/utils/providers/ackee-provider/ackee-provider.tsx b/src/utils/providers/ackee-provider/ackee-provider.tsx
new file mode 100644
index 0000000..8cd29cd
--- /dev/null
+++ b/src/utils/providers/ackee-provider/ackee-provider.tsx
@@ -0,0 +1,95 @@
+import { useRouter } from 'next/router';
+import {
+  type FC,
+  type ReactNode,
+  createContext,
+  type Dispatch,
+  type SetStateAction,
+  useMemo,
+} from 'react';
+import { useAckee } from 'use-ackee';
+import type { AckeeTrackerValue } from '../../../types';
+import { useLocalStorage } from '../../hooks';
+
+type AckeeContextProps = {
+  tracking: AckeeTrackerValue;
+  setTracking: Dispatch<SetStateAction<AckeeTrackerValue>>;
+};
+
+export const AckeeContext = createContext<AckeeContextProps>({
+  setTracking: (value) => value,
+  tracking: 'full',
+});
+
+const validator = (value: unknown): value is AckeeTrackerValue =>
+  value === 'full' || value === 'partial';
+
+export type AckeeProviderProps = {
+  /**
+   * The provider children.
+   */
+  children?: ReactNode;
+  /**
+   *  The id given by Ackee for this domain.
+   */
+  domainId: string;
+  /**
+   * Should we track visits from localhost?
+   *
+   * @default false
+   */
+  isLocalhostTracked?: boolean;
+  /**
+   * Should we track our own visits?
+   *
+   * @default false
+   */
+  isOwnVisitsTracked?: boolean;
+  /**
+   * An URL that points to your Ackee installation (without trailing slash).
+   */
+  server: string;
+  /**
+   * The key to use in local storage.
+   */
+  storageKey: string;
+  /**
+   * Should the tracking be detailed (full) or not (partial)?
+   */
+  tracking: AckeeTrackerValue;
+};
+
+export const AckeeProvider: FC<AckeeProviderProps> = ({
+  children,
+  domainId,
+  isLocalhostTracked = false,
+  isOwnVisitsTracked = false,
+  server,
+  storageKey,
+  tracking: tracker,
+}) => {
+  const [tracking, setTracking] = useLocalStorage<AckeeTrackerValue>(
+    storageKey,
+    tracker,
+    validator
+  );
+  const { asPath } = useRouter();
+
+  useAckee(
+    asPath,
+    { domainId, server },
+    {
+      detailed: tracking === 'full',
+      ignoreLocalhost: !isLocalhostTracked,
+      ignoreOwnVisits: !isOwnVisitsTracked,
+    }
+  );
+
+  const value = useMemo(() => {
+    return { setTracking, tracking };
+  }, [setTracking, tracking]);
+
+  return (
+    <AckeeContext.Provider value={value}>{children}</AckeeContext.Provider>
+  );
+};
-- 
cgit v1.2.3

