import { useCallback, useState } from 'react'; import { shareTryOn, type ShareOutcome, type ShareRequest } from './share'; import type { WidgetTranslations } from '../i18n'; export interface ShareNotice { text: string; isError: boolean; } /** * Wraps `shareTryOn` with the one line of feedback the shopper needs when the * browser quietly took a different route than the share sheet. * * Nothing to say when the OS handled it or when the shopper closed the sheet: * they saw that happen. */ export function useShareAction(t: WidgetTranslations['share']) { const [notice, setNotice] = useState(null); const share = useCallback( async (request: ShareRequest): Promise => { setNotice(null); const outcome = await shareTryOn(request); if (outcome === 'downloaded') setNotice({ text: t.downloaded, isError: false }); else if (outcome === 'copied') setNotice({ text: t.copied, isError: false }); else if (outcome === 'failed') setNotice({ text: t.failed, isError: true }); return outcome; }, [t] ); return { notice, share, clearNotice: useCallback(() => setNotice(null), []) }; }