frontend client for gemstone. decentralised workplace app

refactor: rename provider and better error messages

serenity 5b66a1f4 c9fa122d

+1 -1
src/providers/authed/ChannelsProvider.tsx
···
const channelsValue = useContext(ChannelsContext);
if (!channelsValue)
throw new Error(
-
"Channels context was null. Did you try to access this outside of the authed providers? ChannelsProvider must be below OAuth provider.",
+
"Channels context was null. Did you try to access this outside of the authed providers? ChannelsProvider must be below MembershipsProvider.",
);
return channelsValue;
};
+29 -39
src/providers/authed/LatticeSessionsProvider.tsx src/providers/authed/HandshakesProvider.tsx
···
import type { ReactNode } from "react";
import { createContext, useContext, useMemo } from "react";
-
type LatticeSessionsMap = Map<Did, LatticeSessionInfo>;
+
type HandshakesMap = Map<Did, LatticeSessionInfo>;
-
interface LatticeSessionContextValue {
-
sessions: LatticeSessionsMap;
+
interface HandshakeContextValue {
+
handshakesMap: HandshakesMap;
isInitialising: boolean;
error: Error | null;
-
getSession: (latticeDid: Did) => LatticeSessionInfo | undefined;
+
getHandshake: (latticeDid: Did) => LatticeSessionInfo | undefined;
}
-
const LatticeSessionsContext = createContext<LatticeSessionContextValue | null>(
-
null,
-
);
+
const HandshakesContext = createContext<HandshakeContextValue | null>(null);
-
export const useLatticeSession = () => {
-
const value = useContext(LatticeSessionsContext);
-
if (value === null)
+
export const useHandshakes = () => {
+
const handshakesValue = useContext(HandshakesContext);
+
if (!handshakesValue)
throw new Error(
-
"Lattice session context not inited. Or ordering of providers was wrong.",
+
"Handshakes context was null. Did you try to access this outside of the authed providers? HandshakesProvider(Inner) must be below ChannelsProvider.",
);
-
return value.sessions;
+
return handshakesValue;
};
-
const LatticeSessionsProviderInner = ({
-
children,
-
}: {
-
children: ReactNode;
-
}) => {
+
const HandshakesProviderInner = ({ children }: { children: ReactNode }) => {
const {
memberships,
isInitialising: membershipsInitialising,
···
"LatticeSessionsProvider must be used within an OAuth provider.",
);
-
const { session, isLoading, agent, client } = oauth;
-
const isOAuthReady = !isLoading && !!agent && !!client && !!session;
+
const {
+
session: oAuthSession,
+
isLoading: isOauthLoading,
+
agent: oAuthAgent,
+
client: oAuthClient,
+
} = oauth;
+
const isOAuthReady =
+
!isOauthLoading && !!oAuthAgent && !!oAuthClient && !!oAuthSession;
const handshakeQueries = useQueries({
queries: channels.map((channelObj) => ({
···
handshakeQueries.find((q) => q.error)?.error ??
null;
-
const sessions = useMemo(() => {
-
const sessionsMap = new Map<Did, LatticeSessionInfo>();
+
const handshakes = useMemo(() => {
+
const handshakesMap = new Map<Did, LatticeSessionInfo>();
handshakeQueries.forEach((queryResult) => {
if (queryResult.data) {
const { did, sessionInfo } = queryResult.data;
-
sessionsMap.set(did, sessionInfo);
+
handshakesMap.set(did, sessionInfo);
}
});
-
return sessionsMap;
+
return handshakesMap;
}, [handshakeQueries]);
-
const value: LatticeSessionContextValue = {
+
const value: HandshakeContextValue = {
isInitialising,
error,
-
sessions,
-
getSession: (latticeDid) => sessions.get(latticeDid),
+
handshakesMap: handshakes,
+
getHandshake: (latticeDid) => handshakes.get(latticeDid),
};
-
return (
-
<LatticeSessionsContext value={value}>
-
{children}
-
</LatticeSessionsContext>
-
);
+
return <HandshakesContext value={value}>{children}</HandshakesContext>;
};
const handshakesQueryFn = async ({
···
};
};
-
export const LatticeSessionsProvider = ({
-
children,
-
}: {
-
children: ReactNode;
-
}) => {
+
export const HandshakesProvider = ({ children }: { children: ReactNode }) => {
// Memberships must be above channels
// channels must be above lattice sessions inner
// do it this way to preserve the order if we need to move them around some day
return (
<MembershipsProvider>
<ChannelsProvider>
-
<LatticeSessionsProviderInner>
-
{children}
-
</LatticeSessionsProviderInner>
+
<HandshakesProviderInner>{children}</HandshakesProviderInner>
</ChannelsProvider>
</MembershipsProvider>
);
+1 -1
src/providers/authed/MembershipsProvider.tsx
···
const membershipsValue = useContext(MembershipsContext);
if (!membershipsValue)
throw new Error(
-
"Memberships context was null. Did you try to access this outside of the authed providers? MembershipsProvider must be below OAuth provider.",
+
"Memberships context was null. Did you try to access this outside of the authed providers? MembershipsProvider must be below OAuthProvider.",
);
return membershipsValue;
};
+2 -2
src/providers/authed/index.tsx
···
-
import { LatticeSessionsProvider } from "@/providers/authed/LatticeSessionsProvider";
+
import { HandshakesProvider } from "@/providers/authed/HandshakesProvider";
import type { ReactNode } from "react";
export const AuthedProviders = ({ children }: { children: ReactNode }) => {
-
return <LatticeSessionsProvider>{children}</LatticeSessionsProvider>;
+
return <HandshakesProvider>{children}</HandshakesProvider>;
};