Skip to content

armgit / memos

Public Imported from GitHub
0

Memos is an open-source, self-hosted note-taking app that brings your everyday thoughts together in a personal timeline. Your data stays yours, and you choose what to keep private or share.

2.4 KB
Raw Blame
import type { EditorAction, EditorState } from "./types";
import { createInitialState } from "./types";

export function editorReducer(state: EditorState, action: EditorAction): EditorState {
  switch (action.type) {
    case "INIT_MEMO":
      return {
        ...state,
        content: action.payload.content,
        metadata: action.payload.metadata,
        timestamps: action.payload.timestamps,
      };

    case "UPDATE_CONTENT": {
      if (state.content === action.payload.content) {
        return state;
      }
      return {
        ...state,
        content: action.payload.content,
      };
    }

    case "SET_METADATA":
      return {
        ...state,
        metadata: {
          ...state.metadata,
          ...action.payload,
        },
      };

    case "ADD_LOCAL_FILE":
      return {
        ...state,
        localFiles: [...state.localFiles, action.payload],
      };

    case "REMOVE_LOCAL_FILE":
      return {
        ...state,
        localFiles: state.localFiles.filter((f) => f.previewUrl !== action.payload),
      };

    case "SET_LOCAL_FILES":
      return {
        ...state,
        localFiles: action.payload,
      };

    case "TOGGLE_FOCUS_MODE":
      return {
        ...state,
        ui: {
          ...state.ui,
          isFocusMode: !state.ui.isFocusMode,
        },
      };

    case "SET_LOADING":
      return {
        ...state,
        ui: {
          ...state.ui,
          isLoading: {
            ...state.ui.isLoading,
            [action.payload.key]: action.payload.value,
          },
        },
      };

    case "SET_PENDING_INLINE_IMAGE_INSERTIONS":
      return {
        ...state,
        ui: {
          ...state.ui,
          pendingInlineImageInsertions: action.payload,
        },
      };

    case "SET_TIMESTAMPS":
      return {
        ...state,
        timestamps: {
          ...state.timestamps,
          ...action.payload,
        },
      };

    case "SET_RECORDER_BUSY":
      return {
        ...state,
        recorderBusy: action.payload,
      };

    case "SET_JUST_SAVED":
      return {
        ...state,
        ui: {
          ...state.ui,
          justSaved: action.payload,
        },
      };

    case "RESET":
      return createInitialState();

    case "ACCEPT_SUGGESTION":
      return state.acceptedSuggestionIds.includes(action.payload)
        ? state
        : { ...state, acceptedSuggestionIds: [...state.acceptedSuggestionIds, action.payload] };

    default:
      return state;
  }
}