@coko/client
Version:
Client side common code for coko apps
94 lines (93 loc) • 1.89 kB
JavaScript
import { createContext as e, useContext as t } from "react";
import { useApolloClient as n, useQuery as r, useSubscription as i } from "@apollo/client/react";
import { gql as a } from "@apollo/client";
//#region src/helpers/useCurrentUser.tsx
var o = a`
query CurrentUser {
currentUser {
id
displayName
username
teams {
id
role
objectId
global
members(currentUserOnly: true) {
id
user {
id
}
status
}
}
isActive
defaultIdentity {
id
isVerified
}
identities {
id
provider
hasValidRefreshToken
}
}
}
`, s = a`
subscription OnUserUpdated($userId: ID!) {
userUpdated(userId: $userId) {
id
displayName
username
teams {
id
role
objectId
global
members(currentUserOnly: true) {
id
user {
id
}
status
}
}
isActive
defaultIdentity {
id
isVerified
}
identities {
id
provider
hasValidRefreshToken
}
}
}
`, c = e({
currentUserQuery: void 0,
onLogout: () => {}
}), l = () => {
let { currentUserQuery: e, onLogout: a } = t(c), l = e || o, u = n(), { data: d, loading: f, error: p, refetch: m } = r(l), h = d?.currentUser;
return i(s, {
skip: !h,
variables: { userId: h?.id },
onData: ({ data: e }) => {
let t = e.data?.userUpdated;
t && u.cache.writeQuery({
query: l,
data: { currentUser: t }
});
}
}), p && console.error(p), {
currentUser: h,
error: p,
loading: f,
refetch: m,
logout: () => {
u.cache.reset(), localStorage.removeItem("token"), a();
}
};
};
//#endregion
export { c as CurrentUserQueryContext, l as useCurrentUser };