Updated names for uniformity
This commit is contained in:
@@ -33,16 +33,17 @@ export default function Search({
|
||||
closeWeatherModal: () => void;
|
||||
openWeatherModal: () => void;
|
||||
}) {
|
||||
const { data: providers, isFetched: providersIsFetched } = useQueryAPI({
|
||||
const { data: providerData, isFetched: providerDataIsFetched } = useQueryAPI({
|
||||
endpoint: 'api/providers',
|
||||
});
|
||||
const [providerData, setProviderData] = useState<Provider[]>([]);
|
||||
const { data: apps, isFetching: isAppsFetching } = useQueryAPI({
|
||||
endpoint: 'api/apps',
|
||||
const [providers, setProviders] = useState<Provider[]>([]);
|
||||
const { data: applicationData, isFetching: isApplicationDataFetching } = useQueryAPI({
|
||||
endpoint: 'api/enabledApplications',
|
||||
});
|
||||
const [applicationData, setApplicationData] = useState<Application[]>([]);
|
||||
const { data: history, isFetched: historyIsFetched } = useQueryAPI({
|
||||
endpoint: 'api/searchHistory',
|
||||
const [applications, setApplications] = useState<Application[]>([]);
|
||||
const searchEndpoint = 'api/searchHistory';
|
||||
const { data: historyData, isFetched: historyDataIsFetched } = useQueryAPI({
|
||||
endpoint: searchEndpoint,
|
||||
});
|
||||
const [searchHistory, setSearchHistory] = useState<SearchHistoryItem[]>([]);
|
||||
const [filteredHistory, setFilteredHistory] = useState<SearchHistoryItem[]>([]);
|
||||
@@ -69,7 +70,7 @@ export default function Search({
|
||||
|
||||
const handleSearchHistoryItem = ({ option }: { option: SearchHistoryItem }) => {
|
||||
const url =
|
||||
providers.find((provider: Provider) => provider.prefix === option.prefix)?.url ??
|
||||
providerData.find((provider: Provider) => provider.prefix === option.prefix)?.url ??
|
||||
'https://www.google.com/search?q=';
|
||||
open(`${url}${option.searchTerm}`, '_self');
|
||||
};
|
||||
@@ -99,7 +100,7 @@ export default function Search({
|
||||
} else if (item.target.value.startsWith('/')) {
|
||||
mutateAsync({
|
||||
method: 'POST',
|
||||
endpoint: 'api/searchHistory',
|
||||
endpoint: searchEndpoint,
|
||||
body: { prefix: providerPrefix, provider: providerLabel, searchTerm: subtext },
|
||||
}).then(() => {
|
||||
open(`${providerURL}${subtext}`, '_self');
|
||||
@@ -107,7 +108,7 @@ export default function Search({
|
||||
} else if (providerLabel === 'Show weather') {
|
||||
openWeatherModal();
|
||||
} else if (providerLabel.startsWith('Launch')) {
|
||||
const url = applicationData.find(
|
||||
const url = applications.find(
|
||||
(app: Application) =>
|
||||
app.baseUrl.substring(1).toLowerCase() === item.target.value.substr(1),
|
||||
)?.url;
|
||||
@@ -123,7 +124,7 @@ export default function Search({
|
||||
} else {
|
||||
mutateAsync({
|
||||
method: 'POST',
|
||||
endpoint: 'api/searchHistory',
|
||||
endpoint: searchEndpoint,
|
||||
body: { prefix: providerPrefix, provider: providerLabel, searchTerm: subtext },
|
||||
}).then(() => {
|
||||
open(`${providerURL}${subtext}`, '_self');
|
||||
@@ -221,7 +222,7 @@ export default function Search({
|
||||
setProviderURL('https://www.google.com/search?q=');
|
||||
setProviderPrefix('/g');
|
||||
} else if (item.target.value.startsWith('/')) {
|
||||
const provider = providerData?.find(
|
||||
const provider = providers?.find(
|
||||
(p: Provider) => p.prefix === item.target.value.split(' ')[0],
|
||||
);
|
||||
setProviderLabel(provider?.name ?? 'Invalid Search');
|
||||
@@ -231,7 +232,7 @@ export default function Search({
|
||||
if (item.target.value === '!w' || item.target.value === '!weather') {
|
||||
setProviderLabel('Show weather');
|
||||
} else if (
|
||||
applicationData?.find(
|
||||
applications?.find(
|
||||
(app: Application) =>
|
||||
app.baseUrl.substring(1).toLowerCase() === item.target.value.substr(1),
|
||||
)
|
||||
@@ -250,7 +251,7 @@ export default function Search({
|
||||
const optionsFilter: OptionsFilter = ({ options, search }) => {
|
||||
const splittedSearch = search.toLowerCase().trim().split(' ');
|
||||
if (splittedSearch[0].startsWith('!')) {
|
||||
return apps
|
||||
return applicationData
|
||||
.filter((app: Application) =>
|
||||
app.name.toLowerCase().includes(splittedSearch[0].substring(1).toLowerCase()),
|
||||
)
|
||||
@@ -341,7 +342,7 @@ export default function Search({
|
||||
e.stopPropagation();
|
||||
mutateAsync({
|
||||
method: 'DELETE',
|
||||
endpoint: 'api/searchHistory',
|
||||
endpoint: searchEndpoint,
|
||||
body: { id: item.option.id },
|
||||
});
|
||||
}}
|
||||
@@ -392,25 +393,25 @@ export default function Search({
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
if (providers && providersIsFetched) {
|
||||
setProviderData(providers);
|
||||
if (providerData && providerDataIsFetched) {
|
||||
setProviders(providerData);
|
||||
}
|
||||
}, [providers, providersIsFetched]);
|
||||
}, [providerData, providerDataIsFetched]);
|
||||
|
||||
useEffect(() => {
|
||||
if (apps && !isAppsFetching) {
|
||||
setApplicationData(apps);
|
||||
if (applicationData && !isApplicationDataFetching) {
|
||||
setApplications(applicationData);
|
||||
}
|
||||
}, [apps, isAppsFetching]);
|
||||
}, [applicationData, isApplicationDataFetching]);
|
||||
|
||||
useEffect(() => {
|
||||
if (history && historyIsFetched) {
|
||||
setSearchHistory(history);
|
||||
if (historyData && historyDataIsFetched) {
|
||||
setSearchHistory(historyData);
|
||||
if (filteredHistory.length === 0) {
|
||||
setFilteredHistory(history.slice(0, 10));
|
||||
setFilteredHistory(historyData.slice(0, 10));
|
||||
}
|
||||
}
|
||||
}, [history, historyIsFetched]);
|
||||
}, [historyData, historyDataIsFetched]);
|
||||
|
||||
useEffect(() => {
|
||||
if (dropdownOpened) {
|
||||
@@ -494,8 +495,8 @@ export default function Search({
|
||||
setSearchValue={setSearchValue}
|
||||
setProviderLabel={setProviderLabel}
|
||||
setProviderURL={setProviderURL}
|
||||
applicationData={applicationData}
|
||||
providerData={providerData}
|
||||
applicationData={applications}
|
||||
providerData={providers}
|
||||
providerModalOpened={providerModalOpened}
|
||||
closeProviderModal={closeProviderModal}
|
||||
/>
|
||||
|
||||
Reference in New Issue
Block a user