1
1
import { useCallback , useMemo } from "react" ;
2
2
3
3
import { isNonNullish } from "remeda" ;
4
+ import useSWR from "swr" ;
4
5
5
6
import type { ByPotId } from "@/common/api/indexer" ;
6
7
import { potContractHooks } from "@/common/contracts/core/pot" ;
@@ -9,7 +10,8 @@ import { usePotAuthorization } from "@/entities/pot";
9
10
import { useVotingRoundResults } from "@/entities/voting-round" ;
10
11
11
12
import { publishPayoutJustification } from "../model/effects" ;
12
- import { pfPayoutChallengeToJustification } from "../utils/converters" ;
13
+ import type { PFPayoutJustificationV1 } from "../model/types" ;
14
+ import { pfPayoutChallengeToJustificationUrl } from "../utils/converters" ;
13
15
14
16
export type PFPayoutJustificationParams = ByPotId & {
15
17
onPublishSuccess ?: ( ) => void ;
@@ -31,12 +33,23 @@ export const usePFPayoutJustification = ({
31
33
mutate : refetchPayoutChallenges ,
32
34
} = potContractHooks . usePayoutChallenges ( { potId } ) ;
33
35
34
- const currentJustification = useMemo (
35
- ( ) => potPayoutChallengeList ?. map ( pfPayoutChallengeToJustification ) . find ( isNonNullish ) ,
36
+ const documentUrl = useMemo (
37
+ ( ) => potPayoutChallengeList ?. map ( pfPayoutChallengeToJustificationUrl ) . find ( isNonNullish ) ,
36
38
[ potPayoutChallengeList ] ,
37
39
) ;
38
40
39
- const isPublished = useMemo ( ( ) => isNonNullish ( currentJustification ) , [ currentJustification ] ) ;
41
+ const { isLoading : isDocumentLoading , data : document } = useSWR (
42
+ [ "payout-justification" , documentUrl ] ,
43
+
44
+ ( [ _queryKeyHead , urlQueryKey ] ) =>
45
+ urlQueryKey === undefined
46
+ ? undefined
47
+ : fetch ( urlQueryKey )
48
+ . then ( ( res ) => res . json ( ) )
49
+ . then ( ( data : PFPayoutJustificationV1 ) => data ) ,
50
+ ) ;
51
+
52
+ const isLoading = votingRound . isLoading || isPayoutChallengeListLoading || isDocumentLoading ;
40
53
41
54
const publish = useCallback ( ( ) => {
42
55
if ( viewer . isSignedIn && votingRound . data !== undefined ) {
@@ -67,14 +80,16 @@ export const usePFPayoutJustification = ({
67
80
return ! viewer . isSignedIn || ( ! votingRound . isLoading && votingRound . data === undefined )
68
81
? {
69
82
isLoading : false as const ,
70
- isPublished : false ,
71
83
data : undefined ,
72
84
publish : undefined ,
73
85
}
74
86
: {
75
- isLoading : votingRound . isLoading || isPayoutChallengeListLoading ,
76
- isPublished,
77
- data : currentJustification ,
78
- publish : isPublished || ! viewerPower . isAdminOrGreater ? undefined : publish ,
87
+ isLoading,
88
+ data : document ,
89
+
90
+ publish :
91
+ ! isLoading && document === undefined && viewerPower . isAdminOrGreater
92
+ ? publish
93
+ : undefined ,
79
94
} ;
80
95
} ;
0 commit comments