Skip to content

refactor(types): use TypeScript enum for viewMode instead of hard-coded strings #2

@gzordrai

Description

@gzordrai

Refactor to use a shared ViewMode type from the types folder instead of hardcoding string literals:

const [viewMode, setViewMode] = useState<"code" | "both" | "preview">("both"); // before 
const [viewMode, setViewMode] = useState<ViewMode>(ViewMode.Both); // after

src/types/view.ts

export enum ViewMode {
  Code = "code",
  Both = "both",
  Preview = "preview",
}

To simplify imports, you can re-export all types via:
src/types/index.ts

export * from "./view.ts";

Then just import from the folder:

import { ViewMode } from "./types";

// before
const isEditorVisible = viewMode === "code" || viewMode === "both";
const isPreviewVisible = viewMode === "preview" || viewMode === "both";

// after
const isEditorVisible = viewMode === ViewMode.Code || viewMode === ViewMode.Both;
const isPreviewVisible = viewMode === ViewMode.Preview || viewMode === ViewMode.Both;

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions