@@ -48,9 +48,6 @@ function PublishView(props: PublishViewProps) {
4848 const [ selfDestruct , setSelfDestruct ] = useState (
4949 props . monograph ?. selfDestruct
5050 ) ;
51- const [ analytics , setAnalytics ] = useState < MonographAnalytics > (
52- props . monograph ?. analytics || { totalViews : 0 }
53- ) ;
5451 const [ status , setStatus ] = useState < {
5552 action : "publish" | "unpublish" | "analytics" ;
5653 } > ( ) ;
@@ -63,6 +60,10 @@ function PublishView(props: PublishViewProps) {
6360 const unpublishNote = useStore ( ( store ) => store . unpublish ) ;
6461 const [ monograph , setMonograph ] = useState ( props . monograph ) ;
6562 const monographAnalytics = useIsFeatureAvailable ( "monographAnalytics" ) ;
63+ const analytics = usePromise ( async ( ) => {
64+ if ( ! monographAnalytics ?. isAllowed || ! monograph ) return { totalViews : 0 } ;
65+ return await db . monographs . analytics ( monograph ?. id ) ;
66+ } , [ monograph ?. id , monographAnalytics ] ) ;
6667
6768 useEffect ( ( ) => {
6869 const fileDownloadedEvent = EV . subscribe (
@@ -151,32 +152,36 @@ function PublishView(props: PublishViewProps) {
151152 >
152153 < Text variant = "body" > { strings . views ( ) } </ Text >
153154 { monographAnalytics ?. isAllowed ? (
154- < Flex sx = { { alignItems : "center" , gap : 1 } } >
155- < Text
156- variant = "body"
157- sx = { {
158- color : "paragraph-secondary"
159- } }
160- >
161- { analytics . totalViews }
162- </ Text >
163- < Button
164- variant = "tertiary"
165- onClick = { async ( ) => {
166- try {
167- setStatus ( { action : "analytics" } ) ;
168- const analytics = await db . monographs . analytics (
169- monograph ?. id
170- ) ;
171- setAnalytics ( analytics ) ;
172- } finally {
173- setStatus ( undefined ) ;
174- }
175- } }
176- >
177- < Refresh size = { 14 } rotate = { status ?. action === "analytics" } />
178- </ Button >
179- </ Flex >
155+ analytics . status === "fulfilled" ? (
156+ < Flex sx = { { alignItems : "center" , gap : 1 } } >
157+ < Text
158+ variant = "body"
159+ sx = { {
160+ color : "paragraph-secondary"
161+ } }
162+ >
163+ { analytics . value . totalViews }
164+ </ Text >
165+ < Button
166+ variant = "tertiary"
167+ onClick = { async ( ) => {
168+ try {
169+ setStatus ( { action : "analytics" } ) ;
170+ analytics . refresh ( ) ;
171+ } finally {
172+ setStatus ( undefined ) ;
173+ }
174+ } }
175+ >
176+ < Refresh
177+ size = { 14 }
178+ rotate = { status ?. action === "analytics" }
179+ />
180+ </ Button >
181+ </ Flex >
182+ ) : (
183+ < Loading size = { 14 } />
184+ )
180185 ) : monographAnalytics ? (
181186 < Button
182187 variant = "anchor"
@@ -420,7 +425,6 @@ type ResolvedMonograph = {
420425 id : string ;
421426 selfDestruct : boolean ;
422427 publishedAt ?: number ;
423- analytics : MonographAnalytics ;
424428 password ?: string ;
425429} ;
426430
@@ -433,7 +437,6 @@ async function resolveMonograph(
433437 id : monographId ,
434438 selfDestruct : ! ! monograph . selfDestruct ,
435439 publishedAt : monograph . datePublished ,
436- analytics : await db . monographs . analytics ( monographId ) ,
437440 password : monograph . password
438441 ? await db . monographs . decryptPassword ( monograph . password )
439442 : undefined
0 commit comments