|
1 | | -import React, { Suspense, useCallback, useEffect, useMemo } from "react" |
2 | | -import { |
3 | | - RoutedDrawer, |
4 | | - LearningResourceExpanded, |
5 | | - imgConfigs, |
6 | | -} from "ol-components" |
7 | | -import type { |
8 | | - LearningResourceCardProps, |
9 | | - RoutedDrawerProps, |
10 | | -} from "ol-components" |
11 | | -import { useLearningResourcesDetail } from "api/hooks/learningResources" |
12 | | -import { useSearchParams, ReadonlyURLSearchParams } from "next/navigation" |
13 | | - |
| 1 | +import React, { useCallback } from "react" |
14 | 2 | import { RESOURCE_DRAWER_QUERY_PARAM } from "@/common/urls" |
15 | | -import { useUserMe } from "api/hooks/user" |
16 | | -import NiceModal from "@ebay/nice-modal-react" |
17 | | -import { |
18 | | - AddToLearningPathDialog, |
19 | | - AddToUserListDialog, |
20 | | -} from "../Dialogs/AddToListDialog" |
21 | | -import { SignupPopover } from "../SignupPopover/SignupPopover" |
22 | | -import { usePostHog } from "posthog-js/react" |
23 | | - |
24 | | -const RESOURCE_DRAWER_PARAMS = [RESOURCE_DRAWER_QUERY_PARAM] as const |
25 | | - |
26 | | -const useCapturePageView = (resourceId: number) => { |
27 | | - const { data, isSuccess } = useLearningResourcesDetail(Number(resourceId)) |
28 | | - const posthog = usePostHog() |
29 | | - const apiKey = process.env.NEXT_PUBLIC_POSTHOG_API_KEY |
30 | | - |
31 | | - useEffect(() => { |
32 | | - if (!apiKey || apiKey.length < 1) return |
33 | | - if (!isSuccess) return |
34 | | - posthog.capture("lrd_view", { |
35 | | - resourceId: data?.id, |
36 | | - readableId: data?.readable_id, |
37 | | - platformCode: data?.platform?.code, |
38 | | - resourceType: data?.resource_type, |
39 | | - }) |
40 | | - }, [ |
41 | | - isSuccess, |
42 | | - posthog, |
43 | | - data?.id, |
44 | | - data?.readable_id, |
45 | | - data?.platform?.code, |
46 | | - data?.resource_type, |
47 | | - apiKey, |
48 | | - ]) |
49 | | -} |
50 | | - |
51 | | -/** |
52 | | - * Convert HTML to plaintext, removing any HTML tags. |
53 | | - * This conversion method has some issues: |
54 | | - * 1. It is unsafe for untrusted HTML |
55 | | - * 2. It must be run in a browser, not on a server. |
56 | | - */ |
57 | | -// eslint-disable-next-line camelcase |
58 | | -// const unsafe_html2plaintext = (text: string) => { |
59 | | -// const div = document.createElement("div") |
60 | | -// div.innerHTML = text |
61 | | -// return div.textContent || div.innerText || "" |
62 | | -// } |
63 | | - |
64 | | -const DrawerContent: React.FC<{ |
65 | | - resourceId: number |
66 | | - closeDrawer: () => void |
67 | | -}> = ({ resourceId, closeDrawer }) => { |
68 | | - const resource = useLearningResourcesDetail(Number(resourceId)) |
69 | | - const [signupEl, setSignupEl] = React.useState<HTMLElement | null>(null) |
70 | | - const { data: user } = useUserMe() |
71 | | - const handleAddToLearningPathClick: LearningResourceCardProps["onAddToLearningPathClick"] = |
72 | | - useMemo(() => { |
73 | | - if (user?.is_learning_path_editor) { |
74 | | - return (event, resourceId: number) => { |
75 | | - NiceModal.show(AddToLearningPathDialog, { resourceId }) |
76 | | - } |
77 | | - } |
78 | | - return null |
79 | | - }, [user]) |
80 | | - const handleAddToUserListClick: LearningResourceCardProps["onAddToUserListClick"] = |
81 | | - useMemo(() => { |
82 | | - return (event, resourceId: number) => { |
83 | | - if (!user?.is_authenticated) { |
84 | | - setSignupEl(event.currentTarget) |
85 | | - return |
86 | | - } |
87 | | - NiceModal.show(AddToUserListDialog, { resourceId }) |
88 | | - } |
89 | | - }, [user]) |
90 | | - useCapturePageView(Number(resourceId)) |
91 | | - |
92 | | - return ( |
93 | | - <> |
94 | | - <LearningResourceExpanded |
95 | | - imgConfig={imgConfigs.large} |
96 | | - resource={resource.data} |
97 | | - user={user} |
98 | | - onAddToLearningPathClick={handleAddToLearningPathClick} |
99 | | - onAddToUserListClick={handleAddToUserListClick} |
100 | | - closeDrawer={closeDrawer} |
101 | | - /> |
102 | | - <SignupPopover anchorEl={signupEl} onClose={() => setSignupEl(null)} /> |
103 | | - </> |
104 | | - ) |
105 | | -} |
106 | | - |
107 | | -const PAPER_PROPS: RoutedDrawerProps["PaperProps"] = { |
108 | | - sx: { |
109 | | - maxWidth: (theme) => ({ |
110 | | - [theme.breakpoints.up("md")]: { |
111 | | - maxWidth: theme.breakpoints.values.md, |
112 | | - }, |
113 | | - [theme.breakpoints.down("sm")]: { |
114 | | - maxWidth: "100%", |
115 | | - }, |
116 | | - }), |
117 | | - minWidth: (theme) => ({ |
118 | | - [theme.breakpoints.down("md")]: { |
119 | | - minWidth: "100%", |
120 | | - }, |
121 | | - }), |
122 | | - }, |
123 | | -} |
| 3 | +import { ReadonlyURLSearchParams, useSearchParams } from "next/navigation" |
| 4 | +import { useFeatureFlagEnabled } from "posthog-js/react" |
| 5 | +import LearningResourceDrawerV2 from "./LearningResourceDrawerV2" |
| 6 | +import LearningResourceDrawerV1 from "./LearningResourceDrawerV1" |
| 7 | +import { FeatureFlags } from "@/common/feature_flags" |
124 | 8 |
|
125 | 9 | const LearningResourceDrawer = () => { |
126 | | - return ( |
127 | | - <Suspense> |
128 | | - <RoutedDrawer |
129 | | - anchor="right" |
130 | | - requiredParams={RESOURCE_DRAWER_PARAMS} |
131 | | - PaperProps={PAPER_PROPS} |
132 | | - hideCloseButton={true} |
133 | | - > |
134 | | - {({ params, closeDrawer }) => { |
135 | | - return ( |
136 | | - <DrawerContent |
137 | | - resourceId={Number(params.resource)} |
138 | | - closeDrawer={closeDrawer} |
139 | | - /> |
140 | | - ) |
141 | | - }} |
142 | | - </RoutedDrawer> |
143 | | - </Suspense> |
144 | | - ) |
| 10 | + const drawerV2 = useFeatureFlagEnabled(FeatureFlags.DrawerV2Enabled) |
| 11 | + // console.log(`LearningResourceDrawer: drawerV2=${drawerV2}`) |
| 12 | + if (drawerV2 === undefined) { |
| 13 | + return null |
| 14 | + } |
| 15 | + return drawerV2 ? <LearningResourceDrawerV2 /> : <LearningResourceDrawerV1 /> |
145 | 16 | } |
146 | 17 |
|
147 | 18 | const getOpenDrawerSearchParams = ( |
|
0 commit comments