'use client'; import { EventEmitter } from 'events'; import React, { FC, useCallback, useEffect, useMemo, 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 dayjs from 'dayjs'; import useSWR from 'swr'; import { useFetch } from '@gitroom/helpers/utils/custom.fetch'; import removeMd from 'remove-markdown'; import clsx from 'clsx'; const postUrlEmitter = new EventEmitter(); export const ShowPostSelector = () => { const [showPostSelector, setShowPostSelector] = useState(false); const [callback, setCallback] = useState<{ callback: (tag: string | undefined) => void; } | null>({ callback: (tag: string | undefined) => { return tag; }, } as any); const [date, setDate] = useState(dayjs()); useEffect(() => { postUrlEmitter.on( 'show', (params: { date: dayjs.Dayjs; callback: (url: string | undefined) => void; }) => { setCallback(params); setDate(params.date); setShowPostSelector(true); } ); return () => { setShowPostSelector(false); setCallback(null); setDate(dayjs()); postUrlEmitter.removeAllListeners(); }; }, []); const close = useCallback(() => { setShowPostSelector(false); setCallback(null); setDate(dayjs()); }, []); if (!showPostSelector) { return <>; } return ( ); }; export const showPostSelector = (date: dayjs.Dayjs) => { return new Promise((resolve) => { postUrlEmitter.emit('show', { date, callback: (tag: string) => { resolve(tag); }, }); }); }; export const useShowPostSelector = (day: dayjs.Dayjs) => { return useCallback(() => { return showPostSelector(day); }, [day]); }; export const PostSelector: FC<{ onClose: () => void; onSelect: (tag: string | undefined) => void; only?: 'article' | 'social'; noModal?: boolean; date: dayjs.Dayjs; }> = (props) => { const { onClose, onSelect, only, date, noModal } = props; const fetch = useFetch(); const fetchOldPosts = useCallback(() => { return fetch( '/posts/old?date=' + date.utc().format('YYYY-MM-DDTHH:mm:00'), { method: 'GET', headers: { 'Content-Type': 'application/json', }, } ).then((res) => res.json()); }, [date]); const onCloseWithEmptyString = useCallback(() => { onSelect(''); onClose(); }, []); const [current, setCurrent] = useState(undefined); const select = useCallback( (id: string) => () => { setCurrent(current === id ? undefined : id); onSelect(current === id ? undefined : `(post:${id})`); onClose(); }, [current] ); const { data: loadData } = useSWR('old-posts', fetchOldPosts); const data = useMemo(() => { if (!only) { return loadData; } return loadData?.filter((p: any) => p.integration.type === only); }, [loadData, only]); return ( <> {!noModal || (data?.length > 0 && (
{!noModal && (
)} {!!data && data.length > 0 && (
{data.map((p: any) => (
{p.integration.name}
{removeMd(p.content)}
Status: {p.state}
))}
)}
))} ); }; export const postSelector = (date: dayjs.Dayjs): ICommand => ({ name: 'postselector', keyCommand: 'postselector', shortcuts: 'ctrlcmd+p', prefix: '(post:', 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(date); executeCommand({ api, selectedText: state1.selectedText, selection: state.selection, prefix: media, suffix: '', }); }, });