-
Notifications
You must be signed in to change notification settings - Fork 293
Expand file tree
/
Copy pathBriefContext.tsx
More file actions
42 lines (36 loc) · 1.12 KB
/
BriefContext.tsx
File metadata and controls
42 lines (36 loc) · 1.12 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
import type { Dispatch, FC, SetStateAction } from 'react';
import React from 'react';
import { createContextProvider } from '@kickass-coderz/react';
import type { Post } from '../../../graphql/posts';
import { usePersistentState } from '../../../hooks';
import { useAuthContext } from '../../../contexts/AuthContext';
type BriefContext = {
brief?: Pick<Post, 'id'> & {
createdAt: Date;
};
setBrief: Dispatch<SetStateAction<BriefContext['brief']>>;
};
const [BriefContextProvider, useBriefContext] = createContextProvider(
(): BriefContext => {
const { user } = useAuthContext();
const persistentBriefKey = `brief_card_${user?.id ?? 'anonymous'}_v3`;
const [brief, setBrief] = usePersistentState<BriefContext['brief']>(
persistentBriefKey,
undefined,
);
return {
brief,
setBrief,
};
},
);
function withBriefContext<T>(Component: FC<T>) {
return function WithBriefContextComponent(props: T) {
return (
<BriefContextProvider>
<Component {...props} />
</BriefContextProvider>
);
};
}
export { BriefContextProvider, useBriefContext, withBriefContext };