|
| 1 | +import React, { |
| 2 | + ReactNode, |
| 3 | + useCallback, |
| 4 | + useMemo, |
| 5 | + useState, |
| 6 | +} from 'react'; |
| 7 | +import ReactDOM from 'react-dom'; |
| 8 | + |
| 9 | +import $ from 'jquery'; |
| 10 | +import { |
| 11 | + Check, |
| 12 | + Download, |
| 13 | + LoaderCircle, |
| 14 | +} from 'lucide-react'; |
| 15 | + |
| 16 | +import { ThreadInfoAPIResponse } from '@/types/PPLXApi'; |
| 17 | +import { ui } from '@/utils/ui'; |
| 18 | +import { |
| 19 | + jsonUtils, |
| 20 | + whereAmI, |
| 21 | +} from '@/utils/utils'; |
| 22 | +import { useQuery } from '@tanstack/react-query'; |
| 23 | + |
| 24 | +import useElementObserver from './hooks/useElementObserver'; |
| 25 | +import { Button } from './ui/button'; |
| 26 | + |
| 27 | +export default function ThreadExportButton() { |
| 28 | + const { refetch, isFetching: isFetchingCurrentThreadInfo } = useQuery< |
| 29 | + ThreadInfoAPIResponse[] |
| 30 | + >({ |
| 31 | + queryKey: ['currentThreadInfo'], |
| 32 | + enabled: false, |
| 33 | + }); |
| 34 | + |
| 35 | + const [container, setContainer] = React.useState<Element>(); |
| 36 | + |
| 37 | + const idleSaveButtonText = useMemo( |
| 38 | + () => ( |
| 39 | + <> |
| 40 | + <Download className="tw-mr-2 tw-w-4 tw-h-4" /> |
| 41 | + <span>Export</span> |
| 42 | + </> |
| 43 | + ), |
| 44 | + [] |
| 45 | + ); |
| 46 | + |
| 47 | + const [saveButtonText, setSaveButtonText] = |
| 48 | + useState<ReactNode>(idleSaveButtonText); |
| 49 | + |
| 50 | + useElementObserver({ |
| 51 | + selector: () => [ui.getStickyHeader()[0]], |
| 52 | + callback: ({ element }) => { |
| 53 | + const myContainer = $('<div>'); |
| 54 | + |
| 55 | + $(element).find('>div>div:last>div:last').before(myContainer); |
| 56 | + |
| 57 | + setContainer(myContainer[0]); |
| 58 | + }, |
| 59 | + observedIdentifier: 'thread-export-button', |
| 60 | + }); |
| 61 | + |
| 62 | + const handleExportThread = useCallback(async () => { |
| 63 | + const result = await refetch(); |
| 64 | + |
| 65 | + if (!result.data) return; |
| 66 | + |
| 67 | + let outputText = ''; |
| 68 | + |
| 69 | + result.data?.map((message) => { |
| 70 | + outputText += `Question: ${message.query_str}\n\n`; |
| 71 | + |
| 72 | + const answer = |
| 73 | + jsonUtils.safeParse(message.text)?.answer || |
| 74 | + jsonUtils.safeParse( |
| 75 | + jsonUtils.safeParse(message.text)?.[4].content.answer |
| 76 | + )?.answer; |
| 77 | + |
| 78 | + outputText += `Answer: ${answer}\n\n`; |
| 79 | + |
| 80 | + const proSearchWebResults = jsonUtils.safeParse(message.text)?.[2] |
| 81 | + ?.content.web_results; |
| 82 | + const normalSearchWebResults = jsonUtils.safeParse( |
| 83 | + message.text |
| 84 | + ).web_results; |
| 85 | + |
| 86 | + (proSearchWebResults || normalSearchWebResults).map( |
| 87 | + ( |
| 88 | + webResult: { |
| 89 | + name: string; |
| 90 | + url: string; |
| 91 | + }, |
| 92 | + index: number |
| 93 | + ) => { |
| 94 | + outputText += `[${index + 1}] [${webResult.name}](${webResult.url}) \n`; |
| 95 | + } |
| 96 | + ); |
| 97 | + |
| 98 | + outputText += '\n---\n\n'; |
| 99 | + }); |
| 100 | + |
| 101 | + navigator.clipboard.writeText(outputText); |
| 102 | + |
| 103 | + setSaveButtonText( |
| 104 | + <> |
| 105 | + <Check className="tw-mr-2 tw-w-4 tw-h-4" /> |
| 106 | + <span>Copied</span> |
| 107 | + </> |
| 108 | + ); |
| 109 | + |
| 110 | + setTimeout(() => { |
| 111 | + setSaveButtonText(idleSaveButtonText); |
| 112 | + }, 2000); |
| 113 | + }, [refetch, idleSaveButtonText]); |
| 114 | + |
| 115 | + if (whereAmI() !== 'thread' || !container) return null; |
| 116 | + |
| 117 | + return ReactDOM.createPortal( |
| 118 | + <Button |
| 119 | + className="tw-h-[2rem] tw-text-muted-foreground hover:tw-text-foreground !tw-p-2" |
| 120 | + variant="outline" |
| 121 | + onClick={() => handleExportThread()} |
| 122 | + disabled={isFetchingCurrentThreadInfo} |
| 123 | + > |
| 124 | + {isFetchingCurrentThreadInfo ? ( |
| 125 | + <LoaderCircle className="tw-w-4 tw-h-4 tw-animate-spin" /> |
| 126 | + ) : ( |
| 127 | + saveButtonText |
| 128 | + )} |
| 129 | + </Button>, |
| 130 | + container |
| 131 | + ); |
| 132 | +} |
0 commit comments