This commit is contained in:
Stefan Pejcic
2024-05-08 19:52:27 +02:00
parent 9c8d080b57
commit 80303fadd5
2509 changed files with 0 additions and 594172 deletions

View File

@@ -1,24 +0,0 @@
import { ory } from "./ory";
export const isAuthenticated = async () => {
try {
await ory.toSession();
return true;
} catch (error: any) {
return false;
}
};
export const logoutUser = async () => {
try {
await ory.performNativeLogout({
performNativeLogoutBody: {
session_token: "",
},
});
return true;
} catch (_) {
return false;
}
};

View File

@@ -1,33 +0,0 @@
import { cleanFilePath } from ".";
describe("cleanFilePath", () => {
it("should remove the hostname from the path", () => {
expect(
cleanFilePath("http://localhost:3000/src/pages/posts/list.tsx"),
).toEqual("src/pages/posts/list.tsx");
});
it("should remove the cache busting query string", () => {
expect(
cleanFilePath(
"http://localhost:3000/src/pages/posts/list.tsx?ts=1234567890",
),
).toEqual("src/pages/posts/list.tsx");
});
it("should remove webpack-internal:///", () => {
expect(
cleanFilePath("webpack-internal:///src/pages/posts/list.tsx"),
).toEqual("src/pages/posts/list.tsx");
});
it("should return the original path if it doesn't match any custom path", () => {
expect(cleanFilePath("src/pages/posts/list.tsx")).toEqual(
"src/pages/posts/list.tsx",
);
});
it("should return undefined if the path is undefined", () => {
expect(cleanFilePath(undefined)).toEqual(undefined);
});
});

View File

@@ -1,15 +0,0 @@
export const cleanFilePath = (filePath?: string) => {
if (!filePath) return filePath;
let cleaned = filePath;
// if it starts with http, remove the part before the third slash
if (cleaned.startsWith("http")) {
cleaned = cleaned.split("/").slice(3).join("/");
}
if (cleaned.includes("?")) {
cleaned = cleaned.split("?")[0];
}
if (cleaned.startsWith("webpack-internal:///")) {
cleaned = cleaned.replace("webpack-internal:///", "");
}
return cleaned;
};

View File

@@ -1,10 +0,0 @@
export const excludeKeys = <T extends Record<string, any>>(
obj: T,
keys: string[],
): T => {
const newObj = { ...obj };
keys.forEach((key) => {
delete newObj[key];
});
return newObj;
};

View File

@@ -1,5 +0,0 @@
export const getInitials = (name: string) => {
// max 2 initials
const initials = name.split(" ").slice(0, 2);
return initials.map((initial) => initial[0]).join("");
};

View File

@@ -1,5 +0,0 @@
import { Activity } from "src/interfaces/activity";
export const getOwners = (activity: Activity) => {
return activity.trace?.filter((t) => !t.isRefine) ?? [];
};

View File

@@ -1,19 +0,0 @@
import { Activity } from "src/interfaces/activity";
import get from "lodash/get";
export const getResourceValue = (activity: Activity): string => {
const { resourcePath } = activity;
let resource: string | null = null;
if (resourcePath) {
resource = get(activity, resourcePath) ?? "-";
} else {
resource = "-";
}
if (resource) {
resource = resource.charAt(0).toUpperCase() + resource.slice(1);
}
return resource ?? "-";
};

View File

@@ -1,8 +0,0 @@
export const getLastLocation = (): string => {
const lastLocation = localStorage.getItem("devtools:last-location");
return lastLocation || "/overview";
};
export const setLastLocation = (location: string): void => {
localStorage.setItem("devtools:last-location", location);
};

View File

@@ -1,48 +0,0 @@
import { getLocalStorage } from "./local-storage";
describe("getLocalStorage", () => {
const mockLocalStorage = {
getItem: jest.fn(),
setItem: jest.fn(),
removeItem: jest.fn(),
};
beforeEach(() => {
Object.defineProperty(window, "localStorage", {
value: mockLocalStorage,
});
});
afterEach(() => {
jest.clearAllMocks();
});
it("should return default value if localStorage is not available", () => {
mockLocalStorage.getItem.mockImplementation(() => {
throw new Error("test error");
});
const defaultValue = "default";
const value = getLocalStorage("test", defaultValue);
expect(value).toEqual(defaultValue);
});
it("should return the value from localStorage if available", () => {
const name = "test";
const value = "value";
mockLocalStorage.getItem.mockReturnValueOnce(JSON.stringify(value));
const result = getLocalStorage(name, "default");
expect(result).toEqual(value);
expect(mockLocalStorage.getItem).toHaveBeenCalledWith(name);
});
it("should return default value if localStorage throws an error", () => {
const defaultValue = "default";
const name = "test";
mockLocalStorage.getItem.mockImplementation(() => {
throw new Error("test error");
});
const result = getLocalStorage(name, defaultValue);
expect(result).toEqual(defaultValue);
expect(mockLocalStorage.getItem).toHaveBeenCalledWith(name);
});
});

View File

@@ -1,22 +0,0 @@
export const getLocalStorage = <T>(name: string, defaultValue: T): T => {
if (typeof window === "undefined") {
return defaultValue;
}
try {
const value = window.localStorage.getItem(name);
return value ? JSON.parse(value) : defaultValue;
} catch (error) {
return defaultValue;
}
};
export const setLocalStorage = <T>(name: string, newValue: T) => {
if (typeof window === "undefined") {
return;
}
try {
window.localStorage.setItem(name, JSON.stringify(newValue));
} catch (error) {}
};

View File

@@ -1,64 +0,0 @@
import {
MeResponse,
MeUpdateVariables,
RaffleResponse,
} from "src/interfaces/api";
export const getMe = async () => {
try {
const response = await fetch("/api/.refine/users/me");
const data = (await response.json()) as MeResponse;
return data;
} catch (_) {
//
}
return null;
};
export const updateMe = async (variables: MeUpdateVariables) => {
try {
const { status } = await fetch("/api/.refine/users/me", {
method: "PATCH",
headers: {
"Content-Type": "application/json",
Accept: "application/json",
},
body: JSON.stringify(variables),
});
if (status === 200) {
return true;
}
} catch (_) {
//
}
return false;
};
export const raffle = async (): Promise<RaffleResponse> => {
try {
const response = await fetch(
// TODO: Change to real endpoint
"/api/.refine/users/me/raffle",
);
const data = (await response.json()) as RaffleResponse;
return data;
} catch (_) {
//
}
return { raffle: false };
};
export const acknowledgeRaffle = async () => {
try {
await fetch("/api/.refine/users/me/raffle/acknowledge");
} catch (_) {
//
}
};

View File

@@ -1,11 +0,0 @@
import { FrontendApi } from "@ory/client";
const ORY_URL = "/api/.auth";
export const ory = new FrontendApi({
isJsonMime: () => true,
basePath: ORY_URL,
baseOptions: {
withCredentials: true,
},
});

View File

@@ -1,55 +0,0 @@
import {
AvailablePackageType,
PackageLatestVersionType,
PackageType,
} from "@refinedev/devtools-shared";
export const getInstalledPackages = async ({
force,
}: { force?: boolean } = {}) => {
const response = await fetch(
`api/installed-packages${force ? "?force=true" : ""}`,
);
const data = (await response.json()) as { data: PackageType[] };
return data?.data ?? [];
};
export const getAvailablePackages = async () => {
const response = await fetch("api/available-packages");
const data = (await response.json()) as { data: AvailablePackageType[] };
return data?.data ?? [];
};
export const getLatestInfo = async (name: string) => {
const encoded = encodeURIComponent(name);
const response = await fetch(`api/packages/${encoded}/latest`);
const data = (await response.json()) as {
data: { name: string; version?: string };
};
if (data?.data?.version) {
return {
name: data.data?.name ?? name,
latestVersion: data?.data?.version,
} as PackageLatestVersionType;
} else {
return null;
}
};
export const installPackages = async (packages: string[]) => {
const response = await fetch("api/packages/install", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ packages }),
});
const data = (await response.json()) as { success: boolean };
return data?.success;
};

View File

@@ -1,62 +0,0 @@
import { ProjectIdResponse } from "src/interfaces/api";
export const fetchNewProjectId = async () => {
try {
const response = await fetch("/api/.refine/projects", {
method: "POST",
headers: {
"Content-Type": "application/json",
Accept: "application/json",
},
});
const data = (await response.json()) as ProjectIdResponse;
if (data?.projectId) {
return data.projectId;
}
} catch (_) {
//
}
return null;
};
export const getCurrentProjectIdStatus = async () => {
try {
const response = await fetch("/api/project-id/status");
if (response.status === 400) {
return undefined;
}
if (response.status === 404) {
return false;
}
if (response.status === 200) {
return true;
}
} catch (_) {
//
}
return undefined;
};
export const updateProjectId = async (projectId: string) => {
const response = await fetch("/api/project-id/update", {
body: JSON.stringify({ projectId }),
method: "POST",
headers: {
"Content-Type": "application/json",
Accept: "application/json",
},
});
if (response.status === 200) {
return true;
}
return false;
};

View File

@@ -1,11 +0,0 @@
const stringToHash = (str: string) => {
let hash = 0;
for (let i = 0; i < str.length; i++) {
hash = str.charCodeAt(i) + ((hash << 5) - hash);
}
return hash;
};
export const stringToColor = (str: string) => {
return `hsl(${stringToHash(str) % 360}, 100%, 80%)`;
};