'use client'; import { EventEmitter } from 'events'; import React, { FC, useCallback, useEffect, useState } from 'react'; import { TopTitle } from '@gitroom/frontend/components/launches/helpers/top.title.component'; import { executeCommand, ExecuteState, ICommand, selectWord, TextAreaTextApi, } from '@uiw/react-md-editor'; import { useFetch } from '@gitroom/helpers/utils/custom.fetch'; import { Input } from '@gitroom/react/form/input'; import { Button } from '@gitroom/react/form/button'; import { useToaster } from '@gitroom/react/toaster/toaster'; import { useT } from '@gitroom/react/translation/get.transation.service.client'; import { useLaunchStore } from '@gitroom/frontend/components/new-launch/store'; const postUrlEmitter = new EventEmitter(); export const ShowLinkedinCompany = () => { const [showPostSelector, setShowPostSelector] = useState(false); const [id, setId] = useState(''); const [callback, setCallback] = useState<{ callback: (tag: string) => void; // eslint-disable-next-line @typescript-eslint/no-empty-function } | null>({ callback: (tag: string) => {}, } as any); useEffect(() => { postUrlEmitter.on( 'show', (params: { id: string; callback: (url: string) => void }) => { setCallback(params); setId(params.id); setShowPostSelector(true); } ); return () => { setShowPostSelector(false); setCallback(null); setId(''); postUrlEmitter.removeAllListeners(); }; }, []); const close = useCallback(() => { setShowPostSelector(false); setCallback(null); setId(''); }, []); if (!showPostSelector) { return <>; } return ( ); }; export const LinkedinCompanyPop: FC<{ addText: (value: any) => void; }> = (props) => { const current = useLaunchStore((state) => state.current); return ( { postUrlEmitter.emit('show', { id: current, callback: (value: any) => { props.addText(value); }, }); }} data-tooltip-id="tooltip" data-tooltip-content="Add a LinkedIn Company" className="mx-[10px] cursor-pointer" width="20" height="20" viewBox="0 0 26 26" fill="none" xmlns="http://www.w3.org/2000/svg" > ); }; export const showPostSelector = (id: string) => { return new Promise((resolve) => { postUrlEmitter.emit('show', { id, callback: (tag: string) => { resolve(tag); }, }); }); }; export const LinkedinCompany: FC<{ onClose: () => void; onSelect: (tag: string) => void; id: string; }> = (props) => { const { onClose, onSelect, id } = props; const setActivateExitButton = useLaunchStore((e) => e.setActivateExitButton); useEffect(() => { setActivateExitButton(false); return () => { setActivateExitButton(true); }; }, []); const fetch = useFetch(); const [company, setCompany] = useState(null); const toast = useToaster(); const t = useT(); const getCompany = async () => { if (!company) { return; } try { const { options } = await ( await fetch('/integrations/function', { method: 'POST', body: JSON.stringify({ id, name: 'company', data: { url: company, }, }), }) ).json(); onSelect(options.value); onClose(); } catch (e) { toast.show('Failed to load profile', 'warning'); } }; return (
setCompany(e.target.value)} placeholder="https://www.linkedin.com/company/gitroom" />
); }; export const linkedinCompany = (identifier: string, id: string): ICommand[] => { if (identifier !== 'linkedin' && identifier !== 'linkedin-page') { return []; } return [ { name: 'linkedinCompany', keyCommand: 'linkedinCompany', shortcuts: 'ctrlcmd+p', prefix: '', suffix: '', buttonProps: { 'aria-label': 'Add Post Url', title: 'Add Post Url', }, icon: ( ), execute: async (state: ExecuteState, api: TextAreaTextApi) => { const newSelectionRange = selectWord({ text: state.text, selection: state.selection, prefix: state.command.prefix!, suffix: state.command.suffix, }); const state1 = api.setSelectionRange(newSelectionRange); const media = await showPostSelector(id); executeCommand({ api, selectedText: state1.selectedText, selection: state.selection, prefix: media, suffix: '', }); }, }, ]; };