File size: 1,475 Bytes
c10f8f8
 
 
 
 
 
 
ddb7f1c
c10f8f8
 
 
 
56f47b1
 
c10f8f8
56f47b1
 
 
 
 
c10f8f8
56f47b1
c10f8f8
 
 
56f47b1
 
 
 
 
 
 
 
 
 
 
 
 
c10f8f8
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/* eslint-disable @typescript-eslint/no-explicit-any */
"use client";
import { useMount } from "react-use";
import { toast } from "sonner";
import { usePathname, useRouter } from "next/navigation";

import { useUser } from "@/hooks/useUser";
import { ProjectType, User } from "@/types";
import { useBroadcastChannel } from "@/lib/useBroadcastChannel";

export default function AppContext({
  children,
}: // me: initialData,
{
  children: React.ReactNode;
  // me?: {
  //   user: User | null;
  //   projects: ProjectType[];
  //   errCode: number | null;
  // };
}) {
  const { loginFromCode, user, logout, loading, errCode } = useUser();
  const pathname = usePathname();
  const router = useRouter();

  // useMount(() => {
  //   if (!initialData?.user && !user) {
  //     if ([401, 403].includes(errCode as number)) {
  //       logout();
  //     } else if (pathname.includes("/spaces")) {
  //       if (errCode) {
  //         toast.error("An error occured while trying to log in");
  //       }
  //       // If we did not manage to log in (probs because api is down), we simply redirect to the home page
  //       router.push("/");
  //     }
  //   }
  // });

  const events: any = {};

  useBroadcastChannel("auth", (message) => {
    if (pathname.includes("/auth/callback")) return;

    if (!message.code) return;
    if (message.type === "user-oauth" && message?.code && !events.code) {
      loginFromCode(message.code);
    }
  });

  return children;
}