mterm-io / mterm

An electron terminal written with TypeScript, and rendered with React. Extend your terminal with themes, plugins and commands written in typescript. Scripting for the modern age.
https://mterm.io
8 stars 0 forks source link

cleanup the vault component #62

Open daretodave opened 2 months ago

daretodave commented 2 months ago

sorta made this in a rush and it needs some cleanup and general react best practices

daretodave commented 2 months ago

here is the component - https://github.com/mterm-io/mterm/blob/main/src/renderer/src/routes/store.tsx

daretodave commented 2 months ago

Breaking this component into smaller, more focused components and managing the state using React Context can help improve the code structure and performance. Here's a high-level plan to refactor the component:

Here's a basic structure of how you can refactor the component

// StoreContext.tsx
import { createContext, useState, useEffect } from 'react';

export const StoreContext = createContext();

export const StoreProvider = ({ children }) => {
  // Define the state and functions here
  const [loading, setIsLoading] = useState(true);
  const [hasStore, setHasStore] = useState(false);
  // ...

  const getStore = async () => {
    // ...
  };

  const onClickSetup = () => {
    // ...
  };

  // ...

  useEffect(() => {
    getStore().catch((error) => {
      setIsLoading(false);
      setGeneralError(error.message);
    });
  }, []);

  return (
    <StoreContext.Provider value={{
      loading,
      hasStore,
      setup,
      store,
      storeKeys,
      storeIsUnlocked,
      setupError,
      saveError,
      password,
      loginError,
      generalError,
      passwordSetup,
      onClickSetup,
      onClickCreate,
      onClickOpen,
      onClickSave,
      onClickAdd,
      onClickDelete,
      handlePasswordSetupChange,
      handlePasswordChange,
      handleStoreKeyChange,
      handleStoreValueChange,
    }}>
      {children}
    </StoreContext.Provider>
  );
};

// Store.tsx
import { useContext } from 'react';
import { StoreContext } from './StoreContext';
import StoreSetup from './StoreSetup';
import StoreLogin from './StoreLogin';
import StoreEditor from './StoreEditor';

export default function Store() {
  const {
    loading,
    hasStore,
    setup,
    storeIsUnlocked,
    generalError,
  } = useContext(StoreContext);

  if (generalError) {
    return <div className="info-text">{generalError}</div>;
  }

  if (loading) {
    return <div className="info-text">Loading...</div>;
  }

  if (setup) {
    return <StoreSetup />;
  }

  if (!hasStore) {
    return (
      // JSX for the info text when the store is not set up
    );
  }

  if (!storeIsUnlocked) {
    return <StoreLogin />;
  }

  return <StoreEditor />;
}

// StoreSetup.tsx
import { useContext } from 'react';
import { StoreContext } from './StoreContext';

export default function StoreSetup() {
  const {
    passwordSetup,
    setupError,
    onClickCreate,
    handlePasswordSetupChange,
  } = useContext(StoreContext);

  return (
    // JSX for the store setup form
  );
}

// StoreLogin.tsx
import { useContext } from 'react';
import { StoreContext } from './StoreContext';

export default function StoreLogin() {
  const {
    password,
    loginError,
    onClickOpen,
    handlePasswordChange,
  } = useContext(StoreContext);

  return (
    // JSX for the store login form
  );
}

// StoreEditor.tsx
import { useContext } from 'react';
import { StoreContext } from './StoreContext';

export default function StoreEditor() {
  const {
    store,
    storeKeys,
    saveError,
    onClickAdd,
    onClickSave,
    onClickDelete,
    handleStoreKeyChange,
    handleStoreValueChange,
  } = useContext(StoreContext);

  return (
    // JSX for the store editor
  );
}

This is a basic structure to give you an idea of how you can refactor the component. You'll need to move the corresponding JSX and functions to the respective components and update the context provider accordingly.

Remember to wrap your main component with the StoreProvider to make the context available to all the child components.

import Store from './Store';
import { StoreProvider } from './StoreContext';

export default function App() {
  return (
    <StoreProvider>
      <Store />
    </StoreProvider>
  );
}

By breaking down the component into smaller, focused components and managing the state using React Context, you can improve the code organization, reusability, and performance.