-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add filters by site for facebook, linked, & twitter
Handy dandy checkboxes to scope your search to one or more of these sites. Closes #13
- Loading branch information
Showing
5 changed files
with
97 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import React, { useState } from 'react'; | ||
import TagsEditor from './TagsEditor'; | ||
import SiteFilters from './SiteFilters'; | ||
|
||
function constructGoogleQuery(names, sites) { | ||
const nameQuery = names.map((name) => `\"${name}\"`).join(" OR "); | ||
|
||
if (sites.length) { | ||
const siteFilter = sites.map((site) => `site:${site}`).join(" OR "); | ||
return `${nameQuery} (${siteFilter})`; | ||
} else { | ||
return nameQuery; | ||
} | ||
} | ||
|
||
const App = () => { | ||
const [names, setNames] = useState([]); | ||
const [sites, setSites] = useState([]); | ||
|
||
const searchInput = document.querySelector('input.gsc-input'); | ||
if (searchInput) { | ||
searchInput.value = constructGoogleQuery(names, sites); | ||
const searchButton = document.querySelector('button.gsc-search-button'); | ||
searchButton.click(); | ||
} | ||
|
||
const searchResults = document.querySelector('#searchResults'); | ||
if (searchResults) { | ||
if (names.length > 0) { | ||
searchResults.className = ''; | ||
} else { | ||
searchResults.className = 'hide'; | ||
} | ||
} | ||
|
||
return ( | ||
<> | ||
<div className='left'> | ||
<TagsEditor onChange={setNames} /> | ||
{ names.length > 0 ? <SiteFilters onChange={setSites} /> : null } | ||
</div> | ||
</> | ||
); | ||
}; | ||
|
||
export default App; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import React, { useState } from 'react'; | ||
|
||
const SiteFilters = (props) => { | ||
const SITES = [ | ||
{name: "facebook", url: "facebook.com"}, | ||
{name: "linkedin", url: "linkedin.com"}, | ||
{name: "twitter", url: "twitter.com"}, | ||
]; | ||
|
||
const [selected, setSelected] = useState([]); | ||
|
||
function onChangeHandler(e) { | ||
let value = e.currentTarget.value; | ||
let checked = e.currentTarget.checked; | ||
let newSelected = [...selected]; | ||
if (checked) { | ||
if (selected.indexOf(value) == -1) { | ||
newSelected = [...selected, value]; | ||
} | ||
} else { | ||
newSelected = selected.filter((site) => site != value); | ||
} | ||
setSelected(newSelected); | ||
props.onChange(newSelected); | ||
} | ||
|
||
const siteCheckbox = function(site) { | ||
return <div className="sitefilters_sites_site"> | ||
<input name={site.name} type="checkbox" value={site.url} onChange={onChangeHandler} /> | ||
<label for={site.name}>{site.name}</label> | ||
</div>; | ||
} | ||
|
||
return ( | ||
<div className="sitefilters"> | ||
<h3>Sites</h3> | ||
<p>Filter by website:</p> | ||
<div className="sitefilters_sites"> | ||
{SITES.map(siteCheckbox)} | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
export default SiteFilters; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
import './index.css'; | ||
import React from 'react'; | ||
import { createRoot } from 'react-dom/client'; | ||
import TagsEditor from './TagsEditor'; | ||
import App from './App'; | ||
|
||
let output = document.querySelector("#output"); | ||
const root = createRoot(output); | ||
root.render( <TagsEditor />); | ||
root.render( <App />); | ||
|