Skip to content

Commit 4029f57

Browse files
committed
fix disallowed varying hook order
1 parent 97ecd49 commit 4029f57

File tree

2 files changed

+21
-10
lines changed

2 files changed

+21
-10
lines changed

web/src/components/ExpandedViewMode/ExpandedViewMode.connector.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,11 @@ const ConnectedExpandedViewMode: React.FC<ConnectedExpandedViewModeProps> = ({
113113
// Run only once on mount when initial props are provided
114114
}, [initialProjectId, initialOutcomeActionHash, dispatch, projectId])
115115

116-
const { attachmentsInfo } = outcome && projectId ? useAttachments({
116+
const { attachmentsInfo } = useAttachments({
117117
projectId: projectId, // Use the determined projectId
118118
outcome: outcome,
119-
}) : { attachmentsInfo: [] };
119+
useFallback: !outcome || !projectId // if this is true it will return an empty default value
120+
});
120121

121122
// Function to add attachments
122123
const addAttachment = async () => {

web/src/hooks/useAttachments.ts

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@ export type AssetMeta = AssetLocationAndInfo & {
1919
export function useAttachments({
2020
projectId,
2121
outcome,
22+
useFallback,
2223
}: {
2324
projectId: string
2425
outcome: ComputedOutcome
26+
useFallback: boolean
2527
}) {
2628
if (!isWeaveContext()) {
2729
return {
@@ -30,17 +32,25 @@ export function useAttachments({
3032
error: null,
3133
}
3234
}
33-
const cellIdWrapper = CellIdWrapper.fromCellIdString(projectId)
34-
const wal: WAL = {
35-
hrl: [cellIdWrapper.getDnaHash(), decodeHashFromBase64(outcome.actionHash)],
36-
context: 'outcome',
37-
}
3835
const [attachmentWALs, setAttachmentWALs] = useState<WAL[] | null>(null)
3936
const [attachmentsInfo, setAttachmentsInfo] = useState<AssetMeta[]>([])
4037
const [error, setError] = useState(null)
4138
const subscriptionRef = useRef<any>(null)
4239

4340
useEffect(() => {
41+
if (useFallback) {
42+
setAttachmentWALs([])
43+
setAttachmentsInfo([])
44+
return
45+
}
46+
const cellIdWrapper = CellIdWrapper.fromCellIdString(projectId)
47+
const wal: WAL = {
48+
hrl: [
49+
cellIdWrapper.getDnaHash(),
50+
decodeHashFromBase64(outcome.actionHash),
51+
],
52+
context: 'outcome',
53+
}
4454
const fetchAssetInfo = async () => {
4555
const weaveClient = getWeaveClient()
4656

@@ -49,7 +59,7 @@ export function useAttachments({
4959
subscriptionRef.current &&
5060
typeof subscriptionRef.current === 'function'
5161
) {
52-
const unsubscribe = subscriptionRef.current;
62+
const unsubscribe = subscriptionRef.current
5363
unsubscribe()
5464
}
5565
subscriptionRef.current = null
@@ -96,12 +106,12 @@ export function useAttachments({
96106
subscriptionRef.current &&
97107
typeof subscriptionRef.current === 'function'
98108
) {
99-
const unsubscribe = subscriptionRef.current;
109+
const unsubscribe = subscriptionRef.current
100110
unsubscribe()
101111
}
102112
subscriptionRef.current = null
103113
}
104-
}, [projectId, outcome.actionHash]) // Dependencies that should trigger a refetch
114+
}, [projectId, outcome?.actionHash]) // Dependencies that should trigger a refetch
105115

106116
return { attachmentWALs, attachmentsInfo, error }
107117
}

0 commit comments

Comments
 (0)