refactor: improve type safety, update navigation, and optimize base64 encoding in auth and editor components

This commit is contained in:
Your Name
2026-04-11 19:33:50 +05:30
parent 4f0355d8eb
commit 9953b27385
7 changed files with 29 additions and 22 deletions
+8 -1
View File
@@ -3,7 +3,11 @@ import { forwardRef, useEffect, useImperativeHandle, useRef } from "react";
const PAD = 36; const PAD = 36;
export const ComposeCanvas = forwardRef((_props, ref) => { export type CanvasTools = {
addImage: (url: string) => void;
};
export const ComposeCanvas = forwardRef<CanvasTools>((_props, ref) => {
const wrapperRef = useRef<HTMLDivElement>(null); const wrapperRef = useRef<HTMLDivElement>(null);
const canvasRef = useRef<HTMLCanvasElement>(null); const canvasRef = useRef<HTMLCanvasElement>(null);
const fabricRef = useRef<fabric.Canvas | null>(null); const fabricRef = useRef<fabric.Canvas | null>(null);
@@ -137,6 +141,8 @@ export const ComposeCanvas = forwardRef((_props, ref) => {
fabricRef.current?.add(img); fabricRef.current?.add(img);
fabricRef.current?.setActiveObject(img); fabricRef.current?.setActiveObject(img);
fabricRef.current?.requestRenderAll(); fabricRef.current?.requestRenderAll();
URL.revokeObjectURL(url); // cleanup browser upload
}); });
}, },
})); }));
@@ -155,3 +161,4 @@ export const ComposeCanvas = forwardRef((_props, ref) => {
</div> </div>
); );
}); });
ComposeCanvas.displayName = "ComposeCanvas";
+2 -8
View File
@@ -1,13 +1,7 @@
import { useCallback } from "react"; import { useCallback } from "react";
import { api, publicApi } from "../api/apiClient"; import { api, publicApi } from "../api/apiClient";
import { endpoints } from "../config/endpoints"; import { endpoints } from "../config/endpoints";
import { useAuthStore } from "../store/useAuthStore"; import { type UserProfile, useAuthStore } from "../store/useAuthStore";
interface UserProfile {
public_id: string;
email: string;
full_name: string;
}
export const useAuth = () => { export const useAuth = () => {
const { accessToken, user, isInitializing, setAuth, clearAuth } = const { accessToken, user, isInitializing, setAuth, clearAuth } =
@@ -15,7 +9,7 @@ export const useAuth = () => {
const isAuthenticated = !!accessToken; const isAuthenticated = !!accessToken;
const login = async (access: string, profile: UserProfile) => { const login = (access: string, profile: UserProfile) => {
setAuth(access, profile); setAuth(access, profile);
}; };
+11 -8
View File
@@ -1,15 +1,18 @@
import { ImageIcon, LockIcon, TrayIcon } from "@phosphor-icons/react"; import { ImageIcon, LockIcon, TrayIcon } from "@phosphor-icons/react";
import { useRef, useState } from "react"; import { useRef, useState } from "react";
import { ComposeCanvas } from "../components/ui/ComposeCanvas"; import {
type CanvasTools,
ComposeCanvas,
} from "../components/ui/ComposeCanvas";
import DateDisplay from "../components/ui/DateDisplay"; import DateDisplay from "../components/ui/DateDisplay";
export default function Editor() { export default function Editor() {
const [recipient, setRecipient] = useState(""); const [recipient, setRecipient] = useState("");
const canvasRef = useRef<any>(null); const canvasRef = useRef<CanvasTools>(null);
const _fileInputRef = useRef<HTMLInputElement>(null); const fileInputRef = useRef<HTMLInputElement>(null);
const _handleImageUpload = (e: React.ChangeEvent<HTMLInputElement>) => { const handleImageUpload = (e: React.ChangeEvent<HTMLInputElement>) => {
const file = e.target.files?.[0]; const file = e.target.files?.[0]; // pick one file at a time
if (file) { if (file) {
const url = URL.createObjectURL(file); const url = URL.createObjectURL(file);
canvasRef.current?.addImage(url); canvasRef.current?.addImage(url);
@@ -47,14 +50,14 @@ export default function Editor() {
<button <button
type="button" type="button"
className="btn btn-ghost btn-sm" className="btn btn-ghost btn-sm"
onClick={() => _fileInputRef.current?.click()} onClick={() => fileInputRef.current?.click()}
> >
<ImageIcon size={18} weight="bold" /> <ImageIcon size={18} weight="bold" />
</button> </button>
<input <input
type="file" type="file"
ref={_fileInputRef} ref={fileInputRef}
onChange={_handleImageUpload} onChange={handleImageUpload}
accept="image/*" accept="image/*"
className="hidden" className="hidden"
/> />
+2 -1
View File
@@ -9,6 +9,7 @@ import { publicApi } from "../api/apiClient";
import Logo from "../components/Logo"; import Logo from "../components/Logo";
import FormField from "../components/ui/FormField"; import FormField from "../components/ui/FormField";
import { endpoints } from "../config/endpoints"; import { endpoints } from "../config/endpoints";
import { ROUTES } from "../config/routes";
// validation logic // validation logic
const registerSchema = z const registerSchema = z
@@ -47,7 +48,7 @@ export default function Register() {
email: data.email, email: data.email,
password: data.password, password: data.password,
}); });
navigate("/verify-email"); navigate(ROUTES.VERIFY_EMAIL);
} catch (err) { } catch (err) {
console.error("Registration error:", err); console.error("Registration error:", err);
let message = "Registration failed. Please try again."; let message = "Registration failed. Please try again.";
+3 -2
View File
@@ -29,13 +29,14 @@ export default function VerifyEmail() {
</p> </p>
</div> </div>
<p <button
type="button"
className="text-xs italic opacity-40 cursor-pointer underline" className="text-xs italic opacity-40 cursor-pointer underline"
onClick={() => window.close()} onClick={() => window.close()}
onKeyDown={(e) => e.key === "Enter" && window.close()} onKeyDown={(e) => e.key === "Enter" && window.close()}
> >
You can close this window now. You can close this window now.
</p> </button>
</div> </div>
); );
} }
+1 -1
View File
@@ -1,6 +1,6 @@
import { create } from "zustand"; import { create } from "zustand";
interface UserProfile { export interface UserProfile {
public_id: string; public_id: string;
email: string; email: string;
full_name: string; full_name: string;
+2 -1
View File
@@ -66,7 +66,8 @@ export async function encryptLetter(plaintext: string, masterKey: CryptoKey) {
const rawKey = await crypto.subtle.exportKey("raw", dek); const rawKey = await crypto.subtle.exportKey("raw", dek);
// conversion to base64 for transit // conversion to base64 for transit
const toBase64 = (buffer: Uint8Array) => btoa(String.fromCharCode(...buffer)); const toBase64 = (buf: Uint8Array) =>
btoa(buf.reduce((acc, b) => acc + String.fromCharCode(b), ""));
return { return {
// This goes to the server // This goes to the server