SeonHyungJo / Tip-Note

:round_pushpin: 개발을 하면서 느끼고 알게된 Tip:round_pushpin:
7 stars 0 forks source link

Web Share API #81

Open SeonHyungJo opened 3 years ago

SeonHyungJo commented 3 years ago

Web Share API

SeonHyungJo commented 3 years ago
type ShareData = {
  title?: string;
  text?: string;
  url?: string;
  files?: File[];
};

interface WebShareAPIProps {
  shareData: ShareData;
  copyData: string;
  children?: string | ReactElement | Array<string | ReactElement>;
  errorDispatch: (httpMethod: string, errOccurUrl: string, errOccurData: any, responseErr: any) => Promise<any>
}

export const WebShareAPI = ({ children, errorDispatch, shareData, copyData }: WebShareAPIProps): ReactElement => {
  const [token] = useCookie(ZARITALK_TOKEN, '');
  const [copied, setCopied, copy] = useCopyToClipboard(copyData);

  const onShared = useCallback(() => {
    if (navigator.share) {
      navigator.share(shareData).then(() => {
        sendActivityLog(ActionType.WEB_SHARE_API_FREE_TICKET, token, JSON.stringify(shareData)).then();
      }).catch((err) => {
        errorDispatch('GET', '/web/share', err, err).then();
      });
    } else {
      sendActivityLog(ActionType.WEB_SHARE_API_FREE_TICKET, token, JSON.stringify(shareData)).then();
      // @ts-ignore
      copy();
    }
  }, [shareData]);

  return (
      <>
        <section className={'z-web-share'} onClick={onShared}>
          {children}
        </section>
      </>
  );
};