Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

3.2 KB
Raw Blame
import {
  storageKeyIntroState,
  storageKeyRecentProjects,
  storageKeyRecentVisit,
} from "@/utils/storage-keys";
import type { AppSliceCreator, PreferencesSlice } from "./types";
import {
  getCurrentUserEmail,
  getWorkspaceCacheScope,
  getWorkspaceResourceScope,
  MAX_RECENT_PROJECT,
  MAX_RECENT_VISIT,
  readJson,
  writeJson,
} from "./utils";

export const createPreferencesSlice: AppSliceCreator<PreferencesSlice> = (
  set,
  get
) => ({
  introStateVersion: 0,
  setRecentProject: (name) => {
    const email = getCurrentUserEmail(get);
    if (!email || !name) return;
    const key = storageKeyRecentProjects(getWorkspaceCacheScope(get), email);
    const previous = readJson<string[]>(key, []);
    writeJson(
      key,
      [name, ...previous.filter((projectName) => projectName !== name)].slice(
        0,
        MAX_RECENT_PROJECT
      )
    );
  },

  recordRecentVisit: (path, workspaceName) => {
    const email = getCurrentUserEmail(get);
    if (!email) return;
    const key = storageKeyRecentVisit(
      getWorkspaceCacheScope(get, workspaceName),
      email
    );
    const previous = readJson<string[]>(key, []);
    const pathOnly = path.replace(/[?#].*$/, "");
    const next = [
      path,
      ...previous.filter((item) => item.replace(/[?#].*$/, "") !== pathOnly),
    ].slice(0, MAX_RECENT_VISIT + 1);
    writeJson(key, next);
  },

  removeRecentVisit: (path) => {
    const email = getCurrentUserEmail(get);
    if (!email) return;
    const key = storageKeyRecentVisit(getWorkspaceCacheScope(get), email);
    const previous = readJson<string[]>(key, []);
    writeJson(
      key,
      previous.filter((item) => item !== path)
    );
  },

  resumeWorkspaceSetupGuide: () => {
    const email = getCurrentUserEmail(get);
    if (!email) return;
    const key = storageKeyIntroState(getWorkspaceResourceScope(get), email);
    const previous = readJson<Record<string, boolean>>(key, {});
    const next: Record<string, boolean> = { ...previous };
    next["workspace-setup-guide.dismissed"] = false;
    for (const introStateKey of Object.keys(previous)) {
      if (introStateKey.startsWith("workspace-setup-guide.completed.")) {
        next[introStateKey] = false;
      }
    }
    writeJson(key, next);
    set((state) => ({ introStateVersion: state.introStateVersion + 1 }));
  },

  // Reads the per-user localStorage map scoped to the current workspace. React
  // callers should use `useIntroStateByKey` so writes can trigger a fresh read.
  getIntroStateByKey: (key) => {
    const email = getCurrentUserEmail(get);
    if (!email) return false;
    const map = readJson<Record<string, boolean>>(
      storageKeyIntroState(getWorkspaceResourceScope(get), email),
      {}
    );
    return map[key] ?? false;
  },

  // Persists a single intro flag to the per-user localStorage map.
  saveIntroStateByKey: ({ key, newState }) => {
    const email = getCurrentUserEmail(get);
    if (!email) return;
    const storageKey = storageKeyIntroState(
      getWorkspaceResourceScope(get),
      email
    );
    const previous = readJson<Record<string, boolean>>(storageKey, {});
    writeJson(storageKey, { ...previous, [key]: newState });
    set((state) => ({ introStateVersion: state.introStateVersion + 1 }));
  },
});