a tool for shared writing and social publishing
at feature/reader 28 lines 949 B view raw
1"use client"; 2import { getIdentityData } from "actions/getIdentityData"; 3import { createContext, useContext } from "react"; 4import useSWR, { KeyedMutator, mutate } from "swr"; 5import { DashboardState } from "./PageLayouts/DashboardLayout"; 6 7export type InterfaceState = { 8 dashboards: { [id: string]: DashboardState | undefined }; 9}; 10type Identity = Awaited<ReturnType<typeof getIdentityData>>; 11let IdentityContext = createContext({ 12 identity: null as Identity, 13 mutate: (() => {}) as KeyedMutator<Identity>, 14}); 15export const useIdentityData = () => useContext(IdentityContext); 16export function IdentityContextProvider(props: { 17 children: React.ReactNode; 18 initialValue: Identity; 19}) { 20 let { data: identity, mutate } = useSWR("identity", () => getIdentityData(), { 21 fallbackData: props.initialValue, 22 }); 23 return ( 24 <IdentityContext.Provider value={{ identity, mutate }}> 25 {props.children} 26 </IdentityContext.Provider> 27 ); 28}