Something went wrong. Try again.
a tool for shared writing and social publishing
Something went wrong. Try again.
949 B · 28 lines
TSX
1234567891011121314151617181920212223242526272829"use client";import { getIdentityData } from "actions/getIdentityData";import { createContext, useContext } from "react";import useSWR, { KeyedMutator, mutate } from "swr";import { DashboardState } from "./PageLayouts/DashboardLayout";
export type InterfaceState = { dashboards: { [id: string]: DashboardState | undefined };};type Identity = Awaited<ReturnType<typeof getIdentityData>>;let IdentityContext = createContext({ identity: null as Identity, mutate: (() => {}) as KeyedMutator<Identity>,});export const useIdentityData = () => useContext(IdentityContext);export function IdentityContextProvider(props: { children: React.ReactNode; initialValue: Identity;}) { let { data: identity, mutate } = useSWR("identity", () => getIdentityData(), { fallbackData: props.initialValue, }); return ( <IdentityContext.Provider value={{ identity, mutate }}> {props.children} </IdentityContext.Provider> );}