initial commit

This commit is contained in:
Alex Holliday committed 2025-11-13 13:47:26 -08:00
1 parent 7cb985116e
commit 75acf7b38f
955 files changed
+37333 -87054

No files matched your search

+5
View File
@@ -0,0 +1,5 @@
import { useDispatch, useSelector } from "react-redux";
import type { RootState, AppDispatch } from "@/store";
export const useAppDispatch = useDispatch.withTypes<AppDispatch>();
export const useAppSelector = useSelector.withTypes<RootState>();
+179
View File
@@ -0,0 +1,179 @@
import { useState } from "react";
import useSWR from "swr";
import type { SWRConfiguration } from "swr";
import type { AxiosRequestConfig } from "axios";
import { get, post, patch, deleteOp } from "@/utils/ApiClient";
import { useAppSelector } from "@/hooks/AppHooks";
import { useToast } from "@/hooks//UseToast";
export type ApiResponse<T> = {
message: string;
data: T;
};
export interface IExtraConfig {
useTeamIdAsKey?: boolean;
}
// Generic fetcher for GET requests
const fetcher = async <T,>(url: string, config?: AxiosRequestConfig) => {
const res = await get<T>(url, config);
return res.data;
};
export const useGet = <T,>(
url: string,
axiosConfig?: AxiosRequestConfig,
swrConfig?: SWRConfiguration<T, Error>,
extraConfig?: IExtraConfig
) => {
const { toastError } = useToast();
const currentTeamId = useAppSelector((state) => state.auth.selectedTeamId);
const { data, error, isLoading, isValidating, mutate } = useSWR<T>(
// Key
extraConfig?.useTeamIdAsKey && currentTeamId ? [url, currentTeamId] : url,
// Fetcher
(key) => {
// Fetcher receives the key as an argument
const targetUrl = Array.isArray(key) ? key[0] : key;
return fetcher<T>(targetUrl, {
...axiosConfig,
headers: {
...axiosConfig?.headers,
"x-team-id": currentTeamId,
},
});
},
// Config
{
onError: (err) => {
console.log(err);
toastError(
err?.response?.data?.message ||
err.message ||
"An unexpected error occurred"
);
},
...swrConfig,
}
);
return {
response: data ?? null,
loading: isLoading,
isValidating,
error: error?.message ?? null,
refetch: mutate,
};
};
export const usePost = <B = any, R = any>() => {
const currentTeamId = useAppSelector((state) => state.auth.selectedTeamId);
const [loading, setLoading] = useState(false);
const [error, setError] = useState<string | null>(null);
const { toastError } = useToast();
const postFn = async (
endpoint: string,
body: B,
config?: AxiosRequestConfig
): Promise<ApiResponse<R> | null> => {
setLoading(true);
setError(null);
try {
const res = await post<ApiResponse<R>>(endpoint, body, {
...config,
headers: {
...config?.headers,
"x-team-id": currentTeamId,
},
});
return res.data;
} catch (err: any) {
const errMsg =
err?.response?.data?.msg || err.message || "An error occurred";
toastError(errMsg);
setError(errMsg);
return null;
} finally {
setLoading(false);
}
};
return { post: postFn, loading, error };
};
export const usePatch = <B = any, R = any>() => {
const currentTeamId = useAppSelector((state) => state.auth.selectedTeamId);
const [loading, setLoading] = useState(false);
const [error, setError] = useState<string | null>(null);
const { toastError, toastSuccess } = useToast();
const patchFn = async (
endpoint: string,
body?: B,
config?: AxiosRequestConfig
): Promise<ApiResponse<R> | null> => {
setLoading(true);
setError(null);
try {
const res = await patch<ApiResponse<R>>(endpoint, body, {
...config,
headers: {
...config?.headers,
"x-team-id": currentTeamId,
},
});
toastSuccess(res.data?.message || "Operation successful");
return res.data;
} catch (err: any) {
const errMsg =
err?.response?.data?.msg || err.message || "An error occurred";
toastError(errMsg);
setError(errMsg);
return null;
} finally {
setLoading(false);
}
};
return { patch: patchFn, loading, error };
};
export const useDelete = <R = any,>() => {
const currentTeamId = useAppSelector((state) => state.auth.selectedTeamId);
const [loading, setLoading] = useState(false);
const [error, setError] = useState<string | null>(null);
const { toastError, toastSuccess } = useToast();
const deleteFn = async (
endpoint: string,
config?: AxiosRequestConfig
): Promise<ApiResponse<R> | null> => {
setLoading(true);
setError(null);
try {
const res = await deleteOp<ApiResponse<R>>(endpoint, {
...config,
headers: {
...config?.headers,
"x-team-id": currentTeamId,
},
});
toastSuccess(res.data?.message || "Operation successful");
return res.data;
} catch (err: any) {
const errMsg =
err?.response?.data?.msg || err.message || "An error occurred";
toastError(errMsg);
setError(errMsg);
return null;
} finally {
setLoading(false);
}
};
return { deleteFn, loading, error };
};
+16
View File
@@ -0,0 +1,16 @@
import { toast, type ToastOptions } from "react-toastify";
export const useToast = () => {
const showToast = (message: string, options?: ToastOptions) => {
toast.dismiss();
toast(message, options);
};
const toastSuccess = (msg: string, opts?: ToastOptions) =>
showToast(msg, { ...opts, type: "success" });
const toastError = (msg: string, opts?: ToastOptions) =>
showToast(msg, { ...opts, type: "error" });
const toastInfo = (msg: string, opts?: ToastOptions) =>
showToast(msg, { ...opts, type: "info" });
return { showToast, toastSuccess, toastError, toastInfo };
};
@@ -0,0 +1,31 @@
import { monitorSchemaInfra } from "@/validation/zod";
import ms from "ms";
import { useMemo } from "react";
import { z } from "zod";
export const useInitForm = ({
initialData,
}: {
initialData: Partial<z.infer<typeof monitorSchemaInfra>> | undefined;
}) => {
return useMemo(() => {
let humanInterval = "1 minute";
if (initialData?.interval) {
const parsed = Number(initialData.interval);
if (!isNaN(parsed)) {
humanInterval = ms(parsed, { long: true });
}
}
const defaults: z.infer<typeof monitorSchemaInfra> = {
type: "infrastructure",
url: initialData?.url || "",
secret: initialData?.secret || "",
n: initialData?.n || 3,
notificationChannels: initialData?.notificationChannels || [],
name: initialData?.name || "",
interval: humanInterval,
};
return { defaults };
}, [initialData]);
};
@@ -0,0 +1,24 @@
import { maintenanceSchema } from "@/validation/zod";
import { useMemo } from "react";
import { z } from "zod";
export const useInitForm = ({
initialData,
}: {
initialData: Partial<z.infer<typeof maintenanceSchema>> | undefined;
}) => {
return useMemo(() => {
const defaults: z.infer<typeof maintenanceSchema> = {
name: initialData?.name || "",
repeat: initialData?.repeat || "no repeat",
startTime: initialData?.startTime
? new Date(initialData.startTime)
: new Date(),
endTime: initialData?.endTime
? new Date(initialData.endTime)
: new Date(),
monitors: initialData?.monitors || [],
};
return { defaults };
}, [initialData]);
};
@@ -0,0 +1,30 @@
import { monitorSchema } from "@/validation/zod";
import ms from "ms";
import { useMemo } from "react";
import { z } from "zod";
export const useInitForm = ({
initialData,
}: {
initialData: Partial<z.infer<typeof monitorSchema>> | undefined;
}) => {
return useMemo(() => {
let humanInterval = "1 minute";
if (initialData?.interval) {
const parsed = Number(initialData.interval);
if (!isNaN(parsed)) {
humanInterval = ms(parsed, { long: true });
}
}
const defaults: z.infer<typeof monitorSchema> = {
type: initialData?.type || "https",
url: initialData?.url || "",
n: initialData?.n || 3,
notificationChannels: initialData?.notificationChannels || [],
name: initialData?.name || "",
interval: humanInterval,
};
return { defaults };
}, [initialData]);
};
@@ -0,0 +1,21 @@
import { notificationChannelSchema } from "@/validation/zod";
import { useMemo } from "react";
import { z } from "zod";
export const useInitForm = ({
initialData,
}: {
initialData: Partial<z.infer<typeof notificationChannelSchema>> | undefined;
}) => {
return useMemo(() => {
const defaults: z.infer<typeof notificationChannelSchema> = {
name: initialData?.name || "",
type: initialData?.type || "email",
config: {
url: initialData?.config?.url || "",
emailAddress: initialData?.config?.emailAddress || "",
},
};
return { defaults };
}, [initialData]);
};
@@ -0,0 +1,30 @@
import { monitorSchemaPageSpeed } from "@/validation/zod";
import ms from "ms";
import { useMemo } from "react";
import { z } from "zod";
export const useInitForm = ({
initialData,
}: {
initialData: Partial<z.infer<typeof monitorSchemaPageSpeed>> | undefined;
}) => {
return useMemo(() => {
let humanInterval = "3 minutes";
if (initialData?.interval) {
const parsed = Number(initialData.interval);
if (!isNaN(parsed)) {
humanInterval = ms(parsed, { long: true });
}
}
const defaults: z.infer<typeof monitorSchemaPageSpeed> = {
type: "pagespeed",
url: initialData?.url || "",
n: initialData?.n || 3,
notificationChannels: initialData?.notificationChannels || [],
name: initialData?.name || "",
interval: humanInterval,
};
return { defaults };
}, [initialData]);
};
@@ -0,0 +1,19 @@
import { profileSchema } from "@/validation/zod";
import { useMemo } from "react";
import { z } from "zod";
export const useInitForm = ({
initialData,
}: {
initialData: Partial<z.infer<typeof profileSchema>> | undefined;
}) => {
return useMemo(() => {
const defaults: z.infer<typeof profileSchema> = {
firstName: initialData?.firstName || "",
lastName: initialData?.lastName || "",
password: undefined,
confirmPassword: undefined,
};
return { defaults };
}, [initialData]);
};
@@ -0,0 +1,16 @@
import { recoverySchema } from "@/validation/zod";
import { useMemo } from "react";
import { z } from "zod";
export const useInitForm = ({
initialData,
}: {
initialData: Partial<z.infer<typeof recoverySchema>> | undefined;
}) => {
return useMemo(() => {
const defaults: z.infer<typeof recoverySchema> = {
email: initialData?.email || "",
};
return { defaults };
}, [initialData]);
};
@@ -0,0 +1,20 @@
import { registerSchema } from "@/validation/zod";
import { useMemo } from "react";
import { z } from "zod";
export const useInitForm = ({
initialData,
}: {
initialData: Partial<z.infer<typeof registerSchema>> | undefined;
}) => {
return useMemo(() => {
const defaults: z.infer<typeof registerSchema> = {
email: initialData?.email || "",
password: initialData?.password || "",
firstName: initialData?.firstName || "",
lastName: initialData?.lastName || "",
confirmPassword: initialData?.confirmPassword || "",
};
return { defaults };
}, [initialData]);
};
@@ -0,0 +1,17 @@
import { resetSchema } from "@/validation/zod";
import { useMemo } from "react";
import { z } from "zod";
export const useInitForm = ({
initialData,
}: {
initialData: Partial<z.infer<typeof resetSchema>> | undefined;
}) => {
return useMemo(() => {
const defaults: z.infer<typeof resetSchema> = {
password: initialData?.password || "",
confirmPassword: initialData?.confirmPassword || "",
};
return { defaults };
}, [initialData]);
};
@@ -0,0 +1,20 @@
import { statusPageSchema } from "@/validation/zod";
import { useMemo } from "react";
import { z } from "zod";
export const useInitForm = ({
initialData,
}: {
initialData: Partial<z.infer<typeof statusPageSchema>> | undefined;
}) => {
return useMemo(() => {
const defaults: z.infer<typeof statusPageSchema> = {
name: initialData?.name || "",
description: initialData?.description || "",
url: initialData?.url || Math.floor(Math.random() * 1000000).toFixed(0),
isPublished: initialData?.isPublished || false,
monitors: initialData?.monitors || [],
};
return { defaults };
}, [initialData]);
};
@@ -0,0 +1,18 @@
import { teamMemberSchema } from "@/validation/zod";
import { useMemo } from "react";
import { z } from "zod";
export const useInitForm = ({
initialData,
}: {
initialData: Partial<z.infer<typeof teamMemberSchema>> | undefined;
}) => {
return useMemo(() => {
const defaults: z.infer<typeof teamMemberSchema> = {
userId: initialData?.userId || "",
teamId: initialData?.teamId || "",
roleId: initialData?.roleId || "",
};
return { defaults };
}, [initialData]);
};
@@ -0,0 +1,18 @@
import { teamSchema } from "@/validation/zod";
import { useMemo } from "react";
import { z } from "zod";
export const useInitForm = ({
initialData,
}: {
initialData: Partial<z.infer<typeof teamSchema>> | undefined;
}) => {
return useMemo(() => {
const defaults: z.infer<typeof teamSchema> = {
name: initialData?.name || "",
description: initialData?.description || "",
roleId: initialData?.roleId || "",
};
return { defaults };
}, [initialData]);
};