import { QuestionOutlined, StarOutlined } from '@ant-design/icons'; import { Alert, AutoComplete, Button, Checkbox, Col, Row } from 'antd'; import { shell } from 'electron'; import { usePlugin } from 'flipper-plugin'; import React, { useState } from 'react'; import { plugin } from '..'; type InputType = { execute: (query: string) => Promise; }; const wrapItem = (query: string, id: number) => ({ label: query, value: query, key: id, }); let favorites: Array = []; export const RealmQueryInput = ({ execute }: InputType) => { const { state } = usePlugin(plugin); const [query, setQuery] = useState(''); const [showSuggestions, setShowSuggestions] = useState(true); const [_, setReset] = useState(0); const executeQuery = () => { execute(query.trim()); }; queryHistory = JSON.parse( localStorage.getItem('history') || '{ "history": [] }' ).history; favorites = JSON.parse( localStorage.getItem('favorites') || '{ "favorites": [] }' ).favorites; const addToFavorites = (query: string) => { if (query !== '' && !favorites.some((qr) => qr === query)) { favorites = [...favorites, query]; } localStorage.setItem('favorites', JSON.stringify({ favorites })); setReset(v => v + 1); }; return ( <> {state.get().errorMessage ? ( { state.get().errorMessage = ''; }} /> ) : null} setShowSuggestions((v) => !v)} style={{ paddingLeft: '4px'}} > Query History { if (ev.key == 'Enter') executeQuery(); }} allowClear showSearch options={ showSuggestions ? [ { label: 'History', options: queryHistory .map((val, id) => wrapItem(val, 2 * id)) .filter((suggestion) => suggestion.value.startsWith(query) ) .reverse(), }, { label: 'Favourites', options: favorites .map((val, id) => wrapItem(val, 2 * id + 1)) .reverse(), }, ] : undefined } /> ); }; let queryHistory: Array = []; export const addToHistory = (query: string) => { if ( query !== '' && (queryHistory.length == 0 || queryHistory[queryHistory.length - 1] != query) ) { if (queryHistory.length + 1 > 10) { queryHistory.shift(); } queryHistory = [...queryHistory, query]; } localStorage.setItem('history', JSON.stringify({ history: queryHistory })); };