Skip to content

Commit d95d4c7

Browse files
authored
add error state and update loading state in useAction hook (#42)
* add error state and update loading state in useAction * remove error state
1 parent 24ef199 commit d95d4c7

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

packages/blinks-core/src/hooks/useAction.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@ interface UseActionOptions {
1515

1616
function useActionApiUrl(url: string | URL) {
1717
const [apiUrl, setApiUrl] = useState<string | null>(null);
18+
const [isLoading, setIsLoading] = useState(false);
1819

1920
useEffect(() => {
2021
let ignore = false;
2122

23+
setIsLoading(true);
2224
unfurlUrlToActionApiUrl(new URL(url))
2325
.then((apiUrl) => {
2426
if (ignore) {
@@ -31,23 +33,30 @@ function useActionApiUrl(url: string | URL) {
3133
'[@dialectlabs/blinks-core] Failed to unfurl action URL',
3234
e,
3335
);
34-
setApiUrl(null);
36+
if (!ignore) {
37+
setApiUrl(null);
38+
}
39+
})
40+
.finally(() => {
41+
if (!ignore) {
42+
setIsLoading(false);
43+
}
3544
});
3645

3746
return () => {
3847
ignore = true;
3948
};
4049
}, [url]);
4150

42-
return { actionApiUrl: apiUrl };
51+
return { actionApiUrl: apiUrl, isUrlLoading: isLoading };
4352
}
4453

4554
export function useAction({
4655
url,
4756
supportStrategy = defaultActionSupportStrategy,
4857
}: UseActionOptions) {
4958
const { isRegistryLoaded } = useActionsRegistryInterval();
50-
const { actionApiUrl } = useActionApiUrl(url);
59+
const { actionApiUrl, isUrlLoading } = useActionApiUrl(url);
5160
const [action, setAction] = useState<Action | null>(null);
5261
const [isLoading, setIsLoading] = useState(false);
5362
const [hasFetched, setHasFetched] = useState(false);
@@ -114,5 +123,9 @@ export function useAction({
114123
// eslint-disable-next-line react-hooks/exhaustive-deps -- only update if supportStrategy changes
115124
}, [supportStrategy, hasFetched]);
116125

117-
return { action, isLoading, refresh: fetchAction };
126+
return {
127+
action,
128+
isLoading: !isRegistryLoaded || isUrlLoading || isLoading,
129+
refresh: fetchAction,
130+
};
118131
}

0 commit comments

Comments
 (0)