Skip to content

refactor: centralize constants and eliminate magic strings#4

Open
shivv23 wants to merge 9 commits into
IPFS-Meshkit:mainfrom
shivv23:feat/centralize-constants
Open

refactor: centralize constants and eliminate magic strings#4
shivv23 wants to merge 9 commits into
IPFS-Meshkit:mainfrom
shivv23:feat/centralize-constants

Conversation

@shivv23

@shivv23 shivv23 commented Jul 21, 2026

Copy link
Copy Markdown

Centralizes all magic strings, hardcoded values, and repeated literals into a single src/constants.ts module, eliminating ~110+ scattered occurrences across 10 files.

Motivation

The codebase contained dozens of repeated hardcoded strings — IPFS gateway URLs, activity type literals, metric keys, storage key names, connection status strings, route paths, email configuration, and magic numbers. These were scattered across components with no single source of truth, making renaming, configuration changes, and internationalization fragile and error-prone.

Changes

New file: src/constants.ts — single source of truth for all application constants:

Category Constants Occurrences Eliminated
Infrastructure IPFS_GATEWAY_URL, PROVIDER_NAME 9
Storage Keys STORAGE_KEY_BACKUP_HISTORY, STORAGE_KEY_ACTIVITY_LOGS, STORAGE_KEY_METRICS 7
Activity Types ActivityType (typed const enum) 11
Metric Keys MetricKey (typed const enum) + DEFAULT_METRICS 17+
File Sentinels DEFAULT_FILE_NAME, RESTORED_INVOICE_DEFAULT 12
Routes ROUTES.HOME, ROUTES.MESHKIT, ROUTES.ROOT 6
Connection Status ConnectionStatus (typed const enum) 12
View Modes ViewMode (typed const enum) 8
Toast Config TOAST_DURATION_LONG, TOAST_DURATION_SHORT, TOAST_POSITION 6
Magic Numbers MAX_ACTIVITY_LOGS, MAX_FILENAME_LENGTH, CID_DISPLAY_LENGTH, MAX_ACTIVITY_DISPLAY 5
Email EMAIL_RECIPIENT, EMAIL_BODY_DEFAULT, EMAIL_ATTACHMENT_NAME 3
Report REPORT_FILENAME, REPORT_HEADER 2

Refactored files:

  • LocalStorage.ts — storage keys, activity type union, metric defaults, activity limit all use constants
  • MeshkitService.ts — provider name uses PROVIDER_NAME
  • Dashboard.tsx — gateway URL, activity types, metric keys, file sentinels, connection status, report config, toast config, route paths, CID display length, activity display limit
  • Menu.tsx — email configuration, file sentinels, filename length limit, toast duration/position
  • NewFile.tsx — default file name sentinel
  • Home.tsx — view mode enum, default file name
  • MeshKit.tsx — connection status, metric keys, gateway URL, CID display length, toast config, route paths
  • BackupSuccessModal.tsx — gateway URL
  • App.tsx — route paths

Type safety:

All string union types (ActivityType, MetricKey, ConnectionStatus, ViewMode) are defined as as const objects with companion type exports, providing both runtime values and compile-time type checking.

Type infrastructure:

  • Added src/types/meshkit-ionic.d.ts type declaration stub for @meshkit/ionic

Before / After

// Before: hardcoded in 5 files
"https://gateway.pinata.cloud/ipfs/${cid}"
await store._incrementMetric('invoicesBackedUp')
await store._logActivity({ type: 'BACKUP', ... })
if (connStatus === 'Online') { ... }
history.push("/meshkit")

// After: single import from constants
import { IPFS_GATEWAY_URL, MetricKey, ActivityType, ConnectionStatus, ROUTES } from "../constants";
`${IPFS_GATEWAY_URL}${cid}`
await store._incrementMetric(MetricKey.INVOICES_BACKED_UP)
await store._logActivity({ type: ActivityType.BACKUP, ... })
if (connStatus === ConnectionStatus.ONLINE) { ... }
history.push(ROUTES.MESHKIT)

Verification

npx tsc --noEmit passes with zero errors.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant