Files
TriggerFrontend/src/hooks/useCommonHooks.ts
2025-07-10 14:37:44 -05:00

173 lines
4.5 KiB
TypeScript

import { useLocalStorage } from '@mantine/hooks';
import { appTheme } from 'contexts';
import { useAtom } from 'jotai';
import { useState } from 'react';
import type { Location, Theme, User } from 'types';
import { useMutationAPI } from './useAPI';
export function useCommonHooks() {
const { mutateAsync } = useMutationAPI();
const isMobile = window.matchMedia('(max-width: 600px)').matches;
const [theme, setTheme] = useAtom(appTheme);
const [themeName, setThemeName] = useLocalStorage<string>({
key: 'theme',
defaultValue: 'random',
});
const [appFilter, setAppFilter] = useLocalStorage<string[]>({
key: 'appFilter',
defaultValue: ['All'],
});
const [appSort, setAppSort] = useLocalStorage<string>({
key: 'appSort',
defaultValue: 'A>Z',
});
const [appDensity, setAppDensity] = useLocalStorage<number>({
key: 'appDensity',
defaultValue: 6,
});
const [appScale, setAppScale] = useLocalStorage<number>({
key: 'appScale',
defaultValue: 1,
});
const [bookmarkSort, setBookmarkSort] = useLocalStorage<string>({
key: 'bookmarkSort',
defaultValue: 'A>Z',
});
const [bookmarkFilter, setBookmarkFilter] = useLocalStorage<string[]>({
key: 'bookmarkFilter',
defaultValue: ['All'],
});
const [bookmarkDensity, setBookmarkDensity] = useLocalStorage<number>({
key: 'bookmarkDensity',
defaultValue: 6,
});
const [bookmarkScale, setBookmarkScale] = useLocalStorage<number>({
key: 'bookmarkScale',
defaultValue: 1,
});
const [currentTheme, setCurrentTheme] = useLocalStorage<string>({
key: 'theme',
defaultValue: 'random',
});
const [currentThemePlaylist, setCurrentThemePlaylist] = useLocalStorage<string>({
key: 'themePlaylist',
defaultValue: 'All',
});
const [searchValue, setSearchValue] = useState('');
const [providerLabel, setProviderLabel] = useState('Google Search');
const [providerURL, setProviderURL] = useState('https://www.google.com/search?q=');
const [providerPrefix, setProviderPrefix] = useState('/g');
const [currentUser, setCurrentUser] = useLocalStorage<User>({
key: 'user',
defaultValue: {
firstName: 'First',
lastName: 'Last',
email: 'triggerUser@default.com',
physicalAddress: '1510 Polk St, Houston, TX 77002',
},
});
const [currentLocation, setCurrentLocation] = useLocalStorage<Location>({
key: 'location',
defaultValue: {
office: 'HGX',
station: 'KHOU',
x: 65,
y: 96,
lat: 29.750950134386066,
lon: -95.36220654860034,
},
});
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',
);
const reorder = (list: any, startIndex: number, endIndex: number) => {
const result = Array.from(list);
const [removed]: any = result.splice(startIndex, 1);
result.splice(endIndex, 0, removed);
return result;
};
const handleThemeChange = (themes: Theme[], save: boolean) => {
const enabled = themes
.filter((t: Theme) => t.randomEnabled === 1)
.filter((t: Theme) => t.name !== theme.name);
if (enabled.length === 0 && currentThemePlaylist === 'All') {
setTheme(themes[0]);
save && setThemeName(themes[0].name);
} else if (currentThemePlaylist === 'All') {
const index = Math.floor(Math.random() * enabled.length);
setTheme(enabled[index]);
save && setThemeName(enabled[index].name);
} else {
const index = Math.floor(Math.random() * themes.length);
setTheme(themes[index]);
save && setThemeName(themes[index].name);
}
};
const handleDelete = (item: any, endpoint: string) => {
mutateAsync({ method: 'DELETE', endpoint: endpoint, body: item });
};
function fixDigits(k: number) {
if (k < 10) {
return `0${k}`;
}
return k;
}
return {
isMobile,
theme,
setTheme,
urlPattern,
fixDigits,
reorder,
handleThemeChange,
handleDelete,
themeName,
setThemeName,
appFilter,
setAppFilter,
appSort,
setAppSort,
appDensity,
setAppDensity,
appScale,
setAppScale,
bookmarkSort,
setBookmarkSort,
bookmarkFilter,
setBookmarkFilter,
bookmarkDensity,
setBookmarkDensity,
bookmarkScale,
setBookmarkScale,
currentTheme,
setCurrentTheme,
currentThemePlaylist,
setCurrentThemePlaylist,
searchValue,
setSearchValue,
providerLabel,
setProviderLabel,
providerURL,
setProviderURL,
providerPrefix,
setProviderPrefix,
currentUser,
setCurrentUser,
currentLocation,
setCurrentLocation,
};
}