umami/hooks/useShareToken.js

29 lines
551 B
JavaScript
Raw Normal View History

2020-10-11 11:29:55 +02:00
import { useEffect } from 'react';
import useStore, { setShareToken } from 'store/app';
2022-03-02 06:13:15 +01:00
import useApi from './useApi';
const selector = state => state.shareToken;
2020-10-11 11:29:55 +02:00
2023-05-18 08:20:06 +02:00
export function useShareToken(shareId) {
const shareToken = useStore(selector);
2022-03-02 06:13:15 +01:00
const { get } = useApi();
2020-10-11 11:29:55 +02:00
async function loadToken(id) {
const data = await get(`/share/${id}`);
2020-10-11 11:29:55 +02:00
if (data) {
setShareToken(data);
2020-10-11 11:29:55 +02:00
}
}
useEffect(() => {
if (shareId) {
loadToken(shareId);
}
}, [shareId]);
return shareToken;
}
2023-05-18 08:20:06 +02:00
export default useShareToken;