|
|
@ -1,13 +1,12 @@ |
|
|
|
import React, { FC, useState, useEffect } from 'react' |
|
|
|
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome' |
|
|
|
import { faSearch } from '@fortawesome/free-solid-svg-icons' |
|
|
|
import { Communicator, Theme } from '../../../communicator' |
|
|
|
import { faSearch, faSpinner } from '@fortawesome/free-solid-svg-icons' |
|
|
|
import { Communicator, Theme, InstallationSettings } from '../../../communicator' |
|
|
|
import Gif from './gif' |
|
|
|
import { GiphyGif } from '../../../types' |
|
|
|
|
|
|
|
const giphyIcon = require('./giphy.png') |
|
|
|
import '../../../styles/default.css' |
|
|
|
import '../../../styles/spinner.css' |
|
|
|
|
|
|
|
type APIResponse = GiphyGif[] |
|
|
|
|
|
|
@ -35,6 +34,7 @@ const App: FC<Props> = ({ communicator }) => { |
|
|
|
const [posting, setPosting] = useState(false) |
|
|
|
const [searching, setSearching] = useState(false) |
|
|
|
const [search, setSearch] = useState('') |
|
|
|
const [settings, setSettings] = useState<InstallationSettings>({}) |
|
|
|
const searchD = useDebounce(search) |
|
|
|
const [gifs, setGifs] = useState<GiphyGif[]>([]) |
|
|
|
const [selected, setSelected] = useState('') |
|
|
@ -55,10 +55,14 @@ const App: FC<Props> = ({ communicator }) => { |
|
|
|
const init = async () => { |
|
|
|
try { |
|
|
|
const content = await communicator.init() |
|
|
|
if (content!.theme) setTheme(content!.theme) |
|
|
|
|
|
|
|
if (content && content.parent && content.parent.data && content.parent.data.search) { |
|
|
|
setSearch(content.parent.data.search) |
|
|
|
if (content) { |
|
|
|
if (content.settings) setSettings(content.settings) |
|
|
|
if (content.theme) setTheme(content.theme) |
|
|
|
|
|
|
|
if (content.parent && content.parent.data && content.parent.data.search) { |
|
|
|
setSearch(content.parent.data.search) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
await communicator.setHeight(1000) |
|
|
@ -79,9 +83,26 @@ const App: FC<Props> = ({ communicator }) => { |
|
|
|
const doSearch = async () => { |
|
|
|
try { |
|
|
|
setSearching(true) |
|
|
|
|
|
|
|
const response = await fetch(`/api/gifs/search?q=${search}`) |
|
|
|
setGifs(await response.json() as APIResponse) |
|
|
|
} catch (err) {} |
|
|
|
|
|
|
|
const previousSearches: string[] = settings.searches ?? [] |
|
|
|
const searches = [ |
|
|
|
search, |
|
|
|
...previousSearches.slice(0, 9), |
|
|
|
] |
|
|
|
|
|
|
|
const newSettings = { |
|
|
|
...settings, |
|
|
|
searches, |
|
|
|
} |
|
|
|
|
|
|
|
await communicator.saveSettings(newSettings) |
|
|
|
setSettings(newSettings) |
|
|
|
} catch (err) { |
|
|
|
console.error(err) |
|
|
|
} |
|
|
|
|
|
|
|
setSearching(false) |
|
|
|
} |
|
|
@ -124,6 +145,11 @@ const App: FC<Props> = ({ communicator }) => { |
|
|
|
setPosting(false) |
|
|
|
} |
|
|
|
|
|
|
|
const getPlaceholder = () => { |
|
|
|
const searches = settings.searches as string[] ?? [] |
|
|
|
return searches[0] ? `Last Search: ${searches[0]}` : 'Search' |
|
|
|
} |
|
|
|
|
|
|
|
return ( |
|
|
|
<div style={{ backgroundColor: theme.backgroundPrimary }}> |
|
|
|
<div className="field"> |
|
|
@ -135,7 +161,7 @@ const App: FC<Props> = ({ communicator }) => { |
|
|
|
<input |
|
|
|
style={{ backgroundColor: theme.backgroundSecondary, borderColor: theme.secondary, color: theme.text }} |
|
|
|
type="text" |
|
|
|
placeholder="Search" |
|
|
|
placeholder={getPlaceholder()} |
|
|
|
value={search} |
|
|
|
onChange={(e) => handleSearchChange(e.target.value)} /> |
|
|
|
</div> |
|
|
@ -153,7 +179,7 @@ const App: FC<Props> = ({ communicator }) => { |
|
|
|
|
|
|
|
<div> |
|
|
|
<button style={{ backgroundColor: theme.primary, color: theme.primaryAlternate }} onClick={() => post()} disabled={posting || !selected}> |
|
|
|
{posting ? <div className="spinner" style={{ width: 20 }}></div> : <span>Post</span>} |
|
|
|
{posting ? <FontAwesomeIcon icon={faSpinner} spin /> : <span>Post</span>} |
|
|
|
</button> |
|
|
|
</div> |
|
|
|
</nav> |
|
|
|