a tool for shared writing and social publishing
1"use client";
2import { getIdentityData } from "actions/getIdentityData";
3import { createContext, useContext, useEffect } from "react";
4import useSWR, { KeyedMutator, mutate } from "swr";
5import { DashboardState } from "./PageLayouts/DashboardLayout";
6import { supabaseBrowserClient } from "supabase/browserClient";
7
8export type InterfaceState = {
9 dashboards: { [id: string]: DashboardState | undefined };
10};
11type Identity = Awaited<ReturnType<typeof getIdentityData>>;
12let IdentityContext = createContext({
13 identity: null as Identity,
14 mutate: (() => {}) as KeyedMutator<Identity>,
15});
16export const useIdentityData = () => useContext(IdentityContext);
17export function IdentityContextProvider(props: {
18 children: React.ReactNode;
19 initialValue: Identity;
20}) {
21 let { data: identity, mutate } = useSWR("identity", () => getIdentityData(), {
22 fallbackData: props.initialValue,
23 });
24 useEffect(() => {
25 if (!identity?.atp_did) return;
26 let supabase = supabaseBrowserClient();
27 let channel = supabase.channel(`identity.atp_did:${identity.atp_did}`);
28 channel.on("broadcast", { event: "notification" }, () => {
29 mutate();
30 });
31 channel.subscribe();
32 return () => {
33 channel.unsubscribe();
34 };
35 }, [identity?.atp_did]);
36 return (
37 <IdentityContext.Provider value={{ identity, mutate }}>
38 {props.children}
39 </IdentityContext.Provider>
40 );
41}