Skip to content

Commit

Permalink
BC-8404 - rename config props (#118)
Browse files Browse the repository at this point in the history
  • Loading branch information
SevenWaysDP authored Dec 2, 2024
1 parent 6fddb75 commit 5a632a5
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 34 deletions.
6 changes: 3 additions & 3 deletions src/hooks/useMultiplayerState.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ vi.mock("../stores/setup", () => ({
firstName: "John",
},
envs: {
TLDRAW__ASSETS_ENABLED: true,
TLDRAW__ASSETS_MAX_SIZE_BYTES: 1000000,
TLDRAW__ASSETS_ALLOWED_MIME_TYPES_LIST: ["image/png", "image/jpeg"],
TLDRAW_ASSETS_ENABLED: true,
TLDRAW_ASSETS_MAX_SIZE_BYTES: 1000000,
TLDRAW_ASSETS_ALLOWED_MIME_TYPES_LIST: ["image/png", "image/jpeg"],
},
}));

Expand Down
10 changes: 5 additions & 5 deletions src/hooks/useMultiplayerState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -350,21 +350,21 @@ export function useMultiplayerState({
file: File,
id: string,
): Promise<string | false> => {
if (!envs.TLDRAW__ASSETS_ENABLED) {
if (!envs.TLDRAW_ASSETS_ENABLED) {
toast.info("Asset uploading is disabled");
return false;
}

if (file.size > envs.TLDRAW__ASSETS_MAX_SIZE_BYTES) {
if (file.size > envs.TLDRAW_ASSETS_MAX_SIZE_BYTES) {
const bytesInMb = 1048576;
const sizeInMb = envs.TLDRAW__ASSETS_MAX_SIZE_BYTES / bytesInMb;
const sizeInMb = envs.TLDRAW_ASSETS_MAX_SIZE_BYTES / bytesInMb;
toast.info(`Asset is too big - max. ${sizeInMb}MB`);
return false;
}

const isMimeTypeDisallowed =
envs.TLDRAW__ASSETS_ALLOWED_MIME_TYPES_LIST &&
!envs.TLDRAW__ASSETS_ALLOWED_MIME_TYPES_LIST.includes(file.type);
envs.TLDRAW_ASSETS_ALLOWED_MIME_TYPES_LIST &&
!envs.TLDRAW_ASSETS_ALLOWED_MIME_TYPES_LIST.includes(file.type);

if (isMimeTypeDisallowed) {
toast.info("Asset of this type is not allowed");
Expand Down
23 changes: 11 additions & 12 deletions src/mapper/configuration.mapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ import { TypeGuard } from "../guards/type.guard";
import { Envs } from "../types/Envs";

const checkEnvType = (obj: Record<string, unknown>): void => {
TypeGuard.checkKeyAndValueExists(obj, "TLDRAW__WEBSOCKET_URL");
TypeGuard.checkKeyAndValueExists(obj, "TLDRAW_WEBSOCKET_URL");
TypeGuard.checkKeyAndValueExists(obj, "FEATURE_TLDRAW_ENABLED");
TypeGuard.checkKeyAndValueExists(obj, "TLDRAW__ASSETS_ENABLED");
TypeGuard.checkKeyAndValueExists(obj, "TLDRAW__ASSETS_MAX_SIZE_BYTES");
TypeGuard.checkKeyAndValueExists(obj, "TLDRAW_ASSETS_ENABLED");
TypeGuard.checkKeyAndValueExists(obj, "TLDRAW_ASSETS_MAX_SIZE_BYTES");
TypeGuard.checkKeyAndValueExists(
obj,
"TLDRAW__ASSETS_ALLOWED_MIME_TYPES_LIST",
"TLDRAW_ASSETS_ALLOWED_MIME_TYPES_LIST",
);
TypeGuard.checkBoolean(obj.FEATURE_TLDRAW_ENABLED);
TypeGuard.checkNumber(obj.TLDRAW__ASSETS_MAX_SIZE_BYTES);
TypeGuard.checkArray(obj.TLDRAW__ASSETS_ALLOWED_MIME_TYPES_LIST);
TypeGuard.checkNumber(obj.TLDRAW_ASSETS_MAX_SIZE_BYTES);
TypeGuard.checkArray(obj.TLDRAW_ASSETS_ALLOWED_MIME_TYPES_LIST);
};

const castToEnv = (obj: Record<string, unknown>): Envs => {
Expand All @@ -27,13 +27,12 @@ export class ConfigurationMapper {
const configuration = castToEnv(obj);

const mappedConfiguration: Envs = {
TLDRAW__WEBSOCKET_URL: configuration.TLDRAW__WEBSOCKET_URL,
TLDRAW_WEBSOCKET_URL: configuration.TLDRAW_WEBSOCKET_URL,
FEATURE_TLDRAW_ENABLED: configuration.FEATURE_TLDRAW_ENABLED,
TLDRAW__ASSETS_ENABLED: configuration.TLDRAW__ASSETS_ENABLED,
TLDRAW__ASSETS_MAX_SIZE_BYTES:
configuration.TLDRAW__ASSETS_MAX_SIZE_BYTES,
TLDRAW__ASSETS_ALLOWED_MIME_TYPES_LIST:
configuration.TLDRAW__ASSETS_ALLOWED_MIME_TYPES_LIST,
TLDRAW_ASSETS_ENABLED: configuration.TLDRAW_ASSETS_ENABLED,
TLDRAW_ASSETS_MAX_SIZE_BYTES: configuration.TLDRAW_ASSETS_MAX_SIZE_BYTES,
TLDRAW_ASSETS_ALLOWED_MIME_TYPES_LIST:
configuration.TLDRAW_ASSETS_ALLOWED_MIME_TYPES_LIST,
};

return mappedConfiguration;
Expand Down
2 changes: 1 addition & 1 deletion src/stores/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const user = userResult.user;
const parentId = getParentId();
const doc = new Doc();
const provider = new WebsocketProvider(
envs?.TLDRAW__WEBSOCKET_URL,
envs?.TLDRAW_WEBSOCKET_URL,
parentId,
doc,
{
Expand Down
8 changes: 4 additions & 4 deletions src/types/Envs.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export type Envs = {
FEATURE_TLDRAW_ENABLED: boolean;
TLDRAW__ASSETS_ENABLED: boolean;
TLDRAW__ASSETS_MAX_SIZE_BYTES: number;
TLDRAW__ASSETS_ALLOWED_MIME_TYPES_LIST: string[];
TLDRAW__WEBSOCKET_URL: string;
TLDRAW_ASSETS_ENABLED: boolean;
TLDRAW_ASSETS_MAX_SIZE_BYTES: number;
TLDRAW_ASSETS_ALLOWED_MIME_TYPES_LIST: string[];
TLDRAW_WEBSOCKET_URL: string;
};
8 changes: 4 additions & 4 deletions src/utils/envConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ export const getEnvs = async (): Promise<Envs> => {
throw error;
} else {
const configuration: Envs = {
TLDRAW__WEBSOCKET_URL: "ws://localhost:3345",
TLDRAW__ASSETS_ENABLED: true,
TLDRAW__ASSETS_MAX_SIZE_BYTES: 10485760,
TLDRAW__ASSETS_ALLOWED_MIME_TYPES_LIST: [
TLDRAW_WEBSOCKET_URL: "ws://localhost:3345",
TLDRAW_ASSETS_ENABLED: true,
TLDRAW_ASSETS_MAX_SIZE_BYTES: 10485760,
TLDRAW_ASSETS_ALLOWED_MIME_TYPES_LIST: [
"image/png",
"image/jpeg",
"image/gif",
Expand Down
10 changes: 5 additions & 5 deletions src/utils/tldrawFileUploadUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
* @see Function we replaced with our own: openAssetsFromFileSystem
**/

import { envs } from "../stores/setup";
import { fileOpen } from "browser-fs-access";
import { TldrawApp } from "@tldraw/tldraw";
import { fileOpen } from "browser-fs-access";
import { envs } from "../stores/setup";

// those are all the image extensions that are supported by tldraw
const IMAGE_EXTENSIONS = [".png", ".svg", ".jpg", ".jpeg", ".gif"];
Expand All @@ -29,7 +29,7 @@ const fileMimeExtensions: { [key: string]: string[] } = {
"image/gif": [".gif"],
};

const allowedMimeTypes = envs?.TLDRAW__ASSETS_ALLOWED_MIME_TYPES_LIST || [];
const allowedMimeTypes = envs?.TLDRAW_ASSETS_ALLOWED_MIME_TYPES_LIST || [];

const allowedExtensions = allowedMimeTypes.flatMap(
(mimeType) => fileMimeExtensions[mimeType] || [],
Expand Down Expand Up @@ -81,10 +81,10 @@ const getVideoSizeFromSrc = (src: string): Promise<number[]> => {
};

export {
openAssetsFromFileSystem,
getViewboxFromSVG,
getImageSizeFromSrc,
getVideoSizeFromSrc,
getViewboxFromSVG,
IMAGE_EXTENSIONS,
openAssetsFromFileSystem,
VIDEO_EXTENSIONS,
};

0 comments on commit 5a632a5

Please sign in to comment.