Clean up and typing

This commit is contained in:
Sean Edmunds
2025-03-27 16:22:34 -05:00
parent b46d89721e
commit 67293b20ca
47 changed files with 450 additions and 540 deletions

View File

@@ -12,6 +12,7 @@ import { useCommonHooks, useMutationAPI, useQueryAPI } from 'hooks';
import { useEffect, useState } from 'react';
import { FiEdit, FiTrash } from 'react-icons/fi';
import { actionIconClasses, selectClasses } from 'styles';
import type { Application, Provider } from 'types';
export default function Search({
current,
@@ -24,7 +25,7 @@ export default function Search({
const { data: providers, isFetched: providersIsFetched } = useQueryAPI({
endpoint: 'api/providers',
});
const [providerData, setProviderData] = useState<any>([]);
const [providerData, setProviderData] = useState<Provider[]>([]);
const { data: apps, isFetching: isAppsFetching } = useQueryAPI({
endpoint: 'api/apps',
});
@@ -42,6 +43,7 @@ export default function Search({
const [listIndex, setListIndex] = useState(0);
const {
theme,
urlPattern,
providerLabel,
setProviderLabel,
providerURL,
@@ -53,15 +55,6 @@ export default function Search({
} = useCommonHooks();
const [placeholderValue, setPlaceholderValue] = useState('');
const [value, setValue] = useState(0);
const urlPattern = new RegExp(
'^(https?:\\/\\/)?' + // protocol
'((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|' + // domain name
'((\\d{1,3}\\.){3}\\d{1,3}))' + // OR ip (v4) address
'(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*' + // port and path
'(\\?[;&a-z\\d%_.~+=-]*)?' + // query string
'(\\#[-a-z\\d_]*)?$',
'i',
); // fragment locator
const handleSearchHistoryItem = (item: any) => {
const url =
@@ -104,7 +97,7 @@ export default function Search({
openWeatherModal();
} else if (providerLabel.startsWith('Launch')) {
open(
`https://${applicationData?.find((app: any) => app.baseUrl.substr(1).toLowerCase() === item.target.value.substr(1)).url}`,
`https://${applicationData ?? applicationData.find((app: Application) => app.baseUrl.substring(1).toLowerCase() === item.target.value.substr(1)).url}`,
'_self',
);
} else if (urlPattern.test(item.target.value)) {
@@ -213,11 +206,11 @@ export default function Search({
setProviderPrefix('/g');
} else if (item.target.value.startsWith('/')) {
const provider = providerData?.find(
(p: any) => p.prefix === item.target.value.split(' ')[0],
(p: Provider) => p.prefix === item.target.value.split(' ')[0],
);
setProviderLabel(provider?.name || 'Invalid Search');
setProviderURL(provider?.url);
setProviderPrefix(provider?.prefix);
setProviderLabel(provider?.name ?? 'Invalid Search');
setProviderURL(provider?.url ?? '');
setProviderPrefix(provider?.prefix ?? '');
} else if (item.target.value.startsWith('!')) {
if (item.target.value === '!w' || item.target.value === '!weather') {
setProviderLabel('Show weather');
@@ -255,9 +248,12 @@ export default function Search({
}
return options
.filter((option: any) => {
const words = option.value.toLowerCase().trim().split(' ');
return splittedSearch.every((searchWord) =>
words.some((word: any) => word.includes(searchWord)),
option.value
.toLowerCase()
.trim()
.split(' ')
.some((word: any) => word.includes(searchWord)),
);
})
.slice(0, 10);