Skip to content

Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.

1.2 KB
Raw Blame
import { AppInfo } from '@aliasvault/client/platform/AppInfo';
import { useState } from 'react';

import { StorageKeys } from '@/utils/constants/storageKeys';

import { storage } from '#imports';

/**
 * Hook to manage API URL state and display logic.
 * @returns Object containing apiUrl state and utility functions
 */
export const useApiUrl = (): {
  apiUrl: string;
  setApiUrl: (url: string) => void;
  loadApiUrl: () => Promise<void>;
  getDisplayUrl: () => string;
} => {
  const [apiUrl, setApiUrl] = useState<string>(AppInfo.DEFAULT_API_URL);

  /**
   * Load the API URL from storage.
   */
  const loadApiUrl = async (): Promise<void> => {
    const storedUrl = await storage.getItem(StorageKeys.API_URL) as string;
    if (storedUrl && storedUrl.length > 0) {
      setApiUrl(storedUrl);
    } else {
      setApiUrl(AppInfo.DEFAULT_API_URL);
    }
  };

  /**
   * Get the display URL for UI presentation.
   * @returns Formatted display URL
   */
  const getDisplayUrl = (): string => {
    const cleanUrl = apiUrl.replace('https://', '').replace('http://', '').replace(':443', '').replace('/api', '');
    return cleanUrl === 'app.aliasvault.com' ? 'aliasvault.com' : cleanUrl;
  };

  return {
    apiUrl,
    setApiUrl,
    loadApiUrl,
    getDisplayUrl,
  };
};