umami/hooks/useShareToken.js
2022-03-01 21:13:15 -08:00

27 lines
532 B
JavaScript

import { useEffect } from 'react';
import useStore, { setShareToken } from 'store/app';
import useApi from './useApi';
const selector = state => state.shareToken;
export default function useShareToken(shareId) {
const shareToken = useStore(selector);
const { get } = useApi();
async function loadToken(id) {
const { data } = await get(`/share/${id}`);
if (data) {
setShareToken(data);
}
}
useEffect(() => {
if (shareId) {
loadToken(shareId);
}
}, [shareId]);
return shareToken;
}