Skip to content

Commit

Permalink
URLInput fixes (#319)
Browse files Browse the repository at this point in the history
* fix removing sites

if a duplicate url was added, removing it would cause the first url to be removed
and then the remove buttons would not remove the correct urls.
same issue as BodyMods had previously, fixed by using `remove` directly.

* force site detection on paste event

also reset site select on paste if url does not match any site
prevents mistakenly adding urls for unmatching sites
  • Loading branch information
peolic authored Feb 18, 2022
1 parent 7d98049 commit 9aee008
Showing 1 changed file with 5 additions and 9 deletions.
14 changes: 5 additions & 9 deletions frontend/src/components/urlInput/urlInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,14 @@ const URLInput: FC<URLInputProps> = ({ control, type, errors }) => {
const handleInput = (url: string) => {
if (!inputRef.current || !selectRef.current) return;

const site =
selectedSite ??
sites.find((s) => s.regex && new RegExp(s.regex).test(url));
const site = sites.find((s) => s.regex && new RegExp(s.regex).test(url));

if (site && selectedSite?.id !== site.id) {
setSelectedSite(site);
selectRef.current.value = site.id;
} else if (url && !site && selectedSite?.regex) {
setSelectedSite(undefined);
selectRef.current.value = "";
}

if (site?.regex && url) {
Expand All @@ -96,11 +97,6 @@ const URLInput: FC<URLInputProps> = ({ control, type, errors }) => {
}
};

const handleRemove = (url: string) => {
const index = urls.findIndex((u) => u.url === url);
if (index !== -1) remove(index);
};

const handleSiteSelect = (e: React.ChangeEvent<HTMLInputElement>) => {
const site = sites.find((s) => s.id === e.currentTarget.value);
if (site) setSelectedSite(site);
Expand All @@ -112,7 +108,7 @@ const URLInput: FC<URLInputProps> = ({ control, type, errors }) => {
{urls.map((u, i) => (
<li key={u.url}>
<InputGroup>
<Button variant="danger" onClick={() => handleRemove(u.url)}>
<Button variant="danger" onClick={() => remove(i)}>
Remove
</Button>
<InputGroup.Text>
Expand Down

0 comments on commit 9aee008

Please sign in to comment.