-
Notifications
You must be signed in to change notification settings - Fork 117
Expand file tree
/
Copy pathhooks.ts
More file actions
263 lines (233 loc) · 7.07 KB
/
Copy pathhooks.ts
File metadata and controls
263 lines (233 loc) · 7.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
import { useComputedColorScheme, useMantineTheme } from "@mantine/core";
import { useForceUpdate } from "@mantine/hooks";
import type {
EntityLot,
MediaLot,
} from "@ryot/generated/graphql/backend/graphql";
import { useQuery } from "@tanstack/react-query";
import Cookies from "js-cookie";
import type { FormEvent } from "react";
import { useNavigate } from "react-router";
import {
useRevalidator,
useRouteLoaderData,
useSearchParams,
useSubmit,
} from "react-router";
import { $path } from "safe-routes";
import invariant from "tiny-invariant";
import { useInterval, useMediaQuery } from "usehooks-ts";
import {
type FitnessAction,
dayjsLib,
getMetadataDetailsQuery,
getMetadataGroupDetailsQuery,
getPersonDetailsQuery,
getUserMetadataDetailsQuery,
getUserMetadataGroupDetailsQuery,
getUserPersonDetailsQuery,
selectRandomElement,
} from "~/lib/common";
import {
type InProgressWorkout,
useCurrentWorkout,
useCurrentWorkoutStopwatchAtom,
useCurrentWorkoutTimerAtom,
} from "~/lib/state/fitness";
import type { loader as dashboardLoader } from "~/routes/_dashboard";
export const useGetMantineColors = () => {
const theme = useMantineTheme();
const colors = Object.keys(theme.colors);
return colors;
};
export const useGetRandomMantineColor = (input: string) => {
const colors = useGetMantineColors();
return selectRandomElement(colors, input);
};
export const useFallbackImageUrl = (text = "No Image") => {
const colorScheme = useComputedColorScheme("dark");
return `https://placehold.co/100x200/${
colorScheme === "dark" ? "343632" : "c1c4bb"
}/${colorScheme === "dark" ? "FFF" : "121211"}?text=${text}`;
};
export const useAppSearchParam = (cookieKey: string) => {
const [searchParams, setSearchParams] = useSearchParams();
const updateCookieP = (key: string, value?: string | null) => {
const cookieValue = Cookies.get(cookieKey);
const cookieSearchParams = new URLSearchParams(cookieValue);
if (!value) cookieSearchParams.delete(key);
else cookieSearchParams.set(key, value);
Cookies.set(cookieKey, cookieSearchParams.toString(), {
expires: dayjsLib().add(10, "day").toDate(),
});
};
const delP = (key: string) => {
setSearchParams(
(prev) => {
prev.delete(key);
return prev;
},
{ replace: true },
);
updateCookieP(key);
};
const setP = (key: string, value?: string | null) => {
setSearchParams(
(prev) => {
if (!value) delP(key);
else prev.set(key, value);
return prev;
},
{ replace: true },
);
updateCookieP(key, value);
};
return [searchParams, { setP, delP }] as const;
};
export const useConfirmSubmit = () => {
const submit = useSubmit();
const fn = (e: FormEvent<HTMLFormElement> | HTMLFormElement | null) => {
if (!e) return;
if (e.preventDefault) e.preventDefault();
submit(e.currentTarget || e, { navigate: false });
};
return fn;
};
export const useGetWorkoutStarter = () => {
const revalidator = useRevalidator();
const navigate = useNavigate();
const [_w, setCurrentWorkout] = useCurrentWorkout();
const [_t, setTimer] = useCurrentWorkoutTimerAtom();
const [_s, setStopwatch] = useCurrentWorkoutStopwatchAtom();
const fn = (wkt: InProgressWorkout, action: FitnessAction) => {
setTimer(null);
setStopwatch(null);
setCurrentWorkout(wkt);
navigate($path("/fitness/:action", { action }));
revalidator.revalidate();
};
return fn;
};
export const useMetadataDetails = (metadataId?: string, enabled?: boolean) => {
return useQuery({ ...getMetadataDetailsQuery(metadataId), enabled });
};
export const useUserMetadataDetails = (
metadataId?: string,
enabled?: boolean,
) => {
return useQuery({
...getUserMetadataDetailsQuery(metadataId),
enabled,
});
};
export const usePersonDetails = (personId?: string, enabled?: boolean) => {
return useQuery({ ...getPersonDetailsQuery(personId), enabled });
};
export const useUserPersonDetails = (personId?: string, enabled?: boolean) => {
return useQuery({ ...getUserPersonDetailsQuery(personId), enabled });
};
export const useMetadataGroupDetails = (
metadataGroupId?: string,
enabled?: boolean,
) => {
return useQuery({
...getMetadataGroupDetailsQuery(metadataGroupId),
enabled,
});
};
export const useUserMetadataGroupDetails = (
metadataGroupId?: string,
enabled?: boolean,
) => {
return useQuery({
...getUserMetadataGroupDetailsQuery(metadataGroupId),
enabled,
});
};
export const useDashboardLayoutData = () => {
const loaderData =
useRouteLoaderData<typeof dashboardLoader>("routes/_dashboard");
invariant(loaderData);
return loaderData;
};
export const useCoreDetails = () => useDashboardLayoutData().coreDetails;
export const useUserDetails = () => useDashboardLayoutData().userDetails;
export const useUserPreferences = () => useUserDetails().preferences;
export const useUserCollections = () =>
useDashboardLayoutData().userCollections;
export const useNonHiddenUserCollections = () => {
const userCollections = useUserCollections();
const userDetails = useUserDetails();
const toDisplay = userCollections.filter(
(c) =>
c.collaborators.find((c) => c.collaborator.id === userDetails.id)
?.extraInformation?.isHidden !== true,
);
return toDisplay;
};
export const useUserUnitSystem = () =>
useUserPreferences().fitness.exercises.unitSystem;
export const useApplicationEvents = () => {
const { version, isServerKeyValidated: isPro } = useCoreDetails();
const sendEvent = (eventName: string, data: Record<string, unknown>) => {
window.umami?.track(eventName, { isPro, version, ...data });
};
const updateProgress = (title: string) => {
sendEvent("Update Progress", { title });
};
const postReview = (title: string) => {
sendEvent("Post Review", { title });
};
const deployImport = (source: string) => {
sendEvent("Deploy Import", { source });
};
const createWorkout = () => {
sendEvent("Create Workout", {});
};
const createMeasurement = () => {
sendEvent("Create Measurement", {});
};
const addToCollection = (entityLot: EntityLot) => {
sendEvent("Add To Collection", { entityLot });
};
const startOnboardingTour = () => {
sendEvent("Start Onboarding Tour", {});
};
const completeOnboardingTour = () => {
sendEvent("Complete Onboarding Tour", {});
};
return {
postReview,
deployImport,
createWorkout,
updateProgress,
addToCollection,
createMeasurement,
startOnboardingTour,
completeOnboardingTour,
};
};
export const forceUpdateEverySecond = () => {
const forceUpdate = useForceUpdate();
useInterval(forceUpdate, 1000);
};
export const useGetWatchProviders = (mediaLot: MediaLot) => {
const userPreferences = useUserPreferences();
const watchProviders =
userPreferences.general.watchProviders.find((l) => l.lot === mediaLot)
?.values || [];
return watchProviders;
};
export const useIsFitnessActionActive = () => {
const [currentWorkout] = useCurrentWorkout();
const action = currentWorkout?.currentAction;
return action !== undefined;
};
export const useIsMobile = () => {
const isMobile = useMediaQuery("(max-width: 768px)");
return isMobile;
};
export const useIsOnboardingTourCompleted = () => {
const dashboardData = useDashboardLayoutData();
return dashboardData.isOnboardingTourCompleted;
};