forked from kriasoft/react-starter-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseCurrentUser.ts
More file actions
58 lines (49 loc) · 1.69 KB
/
useCurrentUser.ts
File metadata and controls
58 lines (49 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/* SPDX-FileCopyrightText: 2014-present Kriasoft <hello@kriasoft.com> */
/* SPDX-License-Identifier: MIT */
import * as React from "react";
import {
fetchQuery,
graphql,
useFragment,
useRelayEnvironment,
} from "react-relay";
import { createOperationDescriptor, getRequest, Snapshot } from "relay-runtime";
import {
CurrentUser_me,
CurrentUser_me$data,
CurrentUser_me$key,
} from "../fragments";
import type { useCurrentUserQuery as Query } from "./__generated__/useCurrentUserQuery.graphql";
const variables = {};
const query = graphql`
query useCurrentUserQuery {
me {
...CurrentUser_me
}
}
`;
const operation = createOperationDescriptor(getRequest(query), variables);
/**
* Returns the currently logged in user object (`me`), `null` for anonymous
* users, and `undefined` when the user status has not been resolved yet.
*/
export function useCurrentUser(forceFetch = false): User | null | undefined {
const relay = useRelayEnvironment();
// Attempt to read the current user record (me) from the local store.
const [snap, setSnap] = React.useState<Snapshot>(() =>
relay.lookup(operation.fragment)
);
// Subscribe to updates
React.useEffect(() => {
const subscription = relay.subscribe(snap, (x) => setSnap(x));
return () => subscription.dispose();
}, [relay]);
// Once the component is mounted, attempt to load user record from the API.
React.useEffect(() => {
fetchQuery<Query>(relay, query, variables, {
networkCacheConfig: { force: forceFetch || snap.isMissingData },
}).toPromise();
}, [relay, forceFetch]);
return useFragment(CurrentUser_me, snap.data.me as CurrentUser_me$key);
}
export type User = CurrentUser_me$data | null;