|
| 1 | +import { |
| 2 | + AddressType, |
| 3 | + useDialectDapp, |
| 4 | + useNotificationChannelDappSubscription, |
| 5 | + useNotificationSubscriptions, |
| 6 | + useThread, |
| 7 | +} from '@dialectlabs/react-sdk'; |
| 8 | +import clsx from 'clsx'; |
| 9 | +import { useCallback, useEffect, useState } from 'react'; |
| 10 | +import LoadingThread from '../../entities/LoadingThread'; |
| 11 | +import ConnectionWrapper from '../../entities/wrappers/ConnectionWrapper'; |
| 12 | +import ThreadEncyprionWrapper from '../../entities/wrappers/ThreadEncryptionWrapper'; |
| 13 | +import WalletStatesWrapper from '../../entities/wrappers/WalletStatesWrapper'; |
| 14 | +import GatedWrapper from '../common/GatedWrapper'; |
| 15 | +import { useTheme } from '../common/providers/DialectThemeProvider'; |
| 16 | +import { Route, Router, useRoute } from '../common/providers/Router'; |
| 17 | +import type { Channel } from '../common/types'; |
| 18 | +import { RouteName } from './constants'; |
| 19 | +import Header from './Header'; |
| 20 | +import NotificationsList from './screens/NotificationsList'; |
| 21 | +import Settings from './screens/Settings'; |
| 22 | + |
| 23 | +export type NotificationType = { |
| 24 | + name: string; |
| 25 | + detail?: string; |
| 26 | +}; |
| 27 | + |
| 28 | +interface NotificationsProps { |
| 29 | + onModalClose: () => void; |
| 30 | + notifications?: NotificationType[]; |
| 31 | + channels?: Channel[]; |
| 32 | + onBackClick?: () => void; |
| 33 | + gatedView?: string | JSX.Element; |
| 34 | + pollingInterval?: number; |
| 35 | +} |
| 36 | + |
| 37 | +const addressType = AddressType.Wallet; |
| 38 | + |
| 39 | +function InnerNotifications(props: NotificationsProps): JSX.Element { |
| 40 | + const { dappAddress } = useDialectDapp(); |
| 41 | + if (!dappAddress) { |
| 42 | + throw new Error('dapp address should be provided for notifications'); |
| 43 | + } |
| 44 | + const { thread, isFetchingThread } = useThread({ |
| 45 | + findParams: { otherMembers: [dappAddress] }, |
| 46 | + }); |
| 47 | + |
| 48 | + const [isInitialRoutePicked, setInitialRoutePicked] = useState(false); |
| 49 | + |
| 50 | + const subscription = useNotificationChannelDappSubscription({ |
| 51 | + addressType, |
| 52 | + }); |
| 53 | + |
| 54 | + const { isFetching: isFetchingNotificationsSubscriptions } = |
| 55 | + useNotificationSubscriptions(); |
| 56 | + |
| 57 | + const { scrollbar } = useTheme(); |
| 58 | + const { navigate } = useRoute(); |
| 59 | + |
| 60 | + const showThread = useCallback(() => { |
| 61 | + if (!thread) { |
| 62 | + return; |
| 63 | + } |
| 64 | + navigate(RouteName.Thread, { |
| 65 | + params: { |
| 66 | + threadId: thread.id, |
| 67 | + }, |
| 68 | + }); |
| 69 | + }, [navigate, thread]); |
| 70 | + |
| 71 | + const showSettings = useCallback(() => { |
| 72 | + navigate(RouteName.Settings); |
| 73 | + }, [navigate]); |
| 74 | + |
| 75 | + const isLoading = |
| 76 | + subscription.isFetchingSubscriptions || |
| 77 | + isFetchingThread || |
| 78 | + isFetchingNotificationsSubscriptions; |
| 79 | + |
| 80 | + const isWeb3Enabled = subscription.enabled && Boolean(thread); |
| 81 | + |
| 82 | + useEffect( |
| 83 | + function pickInitialRoute() { |
| 84 | + if (isInitialRoutePicked) { |
| 85 | + return; |
| 86 | + } |
| 87 | + |
| 88 | + if (isLoading) { |
| 89 | + return; |
| 90 | + } |
| 91 | + |
| 92 | + const shouldShowSettings = !isWeb3Enabled; |
| 93 | + |
| 94 | + if (shouldShowSettings) { |
| 95 | + showSettings(); |
| 96 | + setInitialRoutePicked(true); |
| 97 | + return; |
| 98 | + } |
| 99 | + |
| 100 | + showThread(); |
| 101 | + setInitialRoutePicked(true); |
| 102 | + }, |
| 103 | + [isInitialRoutePicked, isLoading, isWeb3Enabled, showSettings, showThread] |
| 104 | + ); |
| 105 | + |
| 106 | + return ( |
| 107 | + <div className="dt-h-full"> |
| 108 | + <Header |
| 109 | + threadId={thread?.id} |
| 110 | + isWeb3Enabled={isWeb3Enabled} |
| 111 | + isReady={!isLoading} |
| 112 | + onModalClose={props.onModalClose} |
| 113 | + onBackClick={props.onBackClick} |
| 114 | + /> |
| 115 | + <div |
| 116 | + className={clsx( |
| 117 | + 'dt-h-full dt-overflow-y-auto dt-pb-[3.5rem]', |
| 118 | + scrollbar |
| 119 | + )} |
| 120 | + > |
| 121 | + {isInitialRoutePicked ? ( |
| 122 | + <> |
| 123 | + <Route name={RouteName.Settings}> |
| 124 | + <Settings |
| 125 | + channels={props.channels || []} |
| 126 | + notifications={props?.notifications} |
| 127 | + /> |
| 128 | + </Route> |
| 129 | + <Route name={RouteName.Thread}> |
| 130 | + <NotificationsList refreshInterval={props.pollingInterval} /> |
| 131 | + </Route> |
| 132 | + </> |
| 133 | + ) : ( |
| 134 | + <LoadingThread /> |
| 135 | + )} |
| 136 | + </div> |
| 137 | + </div> |
| 138 | + ); |
| 139 | +} |
| 140 | + |
| 141 | +export default function WordcelNotifications({ |
| 142 | + gatedView, |
| 143 | + ...props |
| 144 | +}: NotificationsProps) { |
| 145 | + const { dappAddress } = useDialectDapp(); |
| 146 | + const { colors, modal } = useTheme(); |
| 147 | + |
| 148 | + const fallbackHeader = ( |
| 149 | + <Header |
| 150 | + isReady={false} |
| 151 | + isWeb3Enabled={false} |
| 152 | + onBackClick={props.onBackClick} |
| 153 | + onModalClose={props.onModalClose} |
| 154 | + /> |
| 155 | + ); |
| 156 | + |
| 157 | + return ( |
| 158 | + <div className="dialect dt-h-full"> |
| 159 | + <div |
| 160 | + className={clsx( |
| 161 | + 'dt-flex dt-flex-col dt-h-full dt-overflow-hidden', |
| 162 | + colors.textPrimary, |
| 163 | + colors.bg, |
| 164 | + modal |
| 165 | + )} |
| 166 | + > |
| 167 | + <Router> |
| 168 | + <WalletStatesWrapper header={fallbackHeader}> |
| 169 | + <ConnectionWrapper |
| 170 | + header={fallbackHeader} |
| 171 | + pollingInterval={props.pollingInterval} |
| 172 | + > |
| 173 | + <GatedWrapper gatedView={gatedView}> |
| 174 | + <ThreadEncyprionWrapper otherMemberPK={dappAddress}> |
| 175 | + <InnerNotifications {...props} /> |
| 176 | + </ThreadEncyprionWrapper> |
| 177 | + </GatedWrapper> |
| 178 | + </ConnectionWrapper> |
| 179 | + </WalletStatesWrapper> |
| 180 | + </Router> |
| 181 | + </div> |
| 182 | + </div> |
| 183 | + ); |
| 184 | +} |
0 commit comments