Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

2.0 KB
Raw Blame
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0

import { create } from "zustand";
import { persist } from "zustand/middleware";

import { syncAcrossWindows } from "../../../lib/sync-across-windows.ts";

export const LIBRARY_CHAT_FAVORITES_STORAGE_KEY = "unsloth_library_chat_favorites";

export interface ChatFavoritesState {
  chatIds: string[];
  projectIds: string[];
  sectionIds: string[];
  setChats: (ids: string[], favorite: boolean) => void;
  setProjects: (ids: string[], favorite: boolean) => void;
  setSections: (ids: string[], favorite: boolean) => void;
}

function withIds(current: string[], ids: string[], on: boolean): string[] {
  if (!on) {
    const dropping = new Set(ids);
    return current.filter((id) => !dropping.has(id));
  }
  const additions = ids.filter((id) => !current.includes(id));
  return additions.length ? [...additions, ...current] : current;
}

function readIds(value: unknown): string[] {
  return Array.isArray(value) ? value.filter((id): id is string => typeof id === "string") : [];
}

export const useChatFavoritesStore = create<ChatFavoritesState>()(
  persist(
    (set) => ({
      chatIds: [],
      projectIds: [],
      sectionIds: [],
      setChats: (ids, favorite) =>
        set((state) => ({ chatIds: withIds(state.chatIds, ids, favorite) })),
      setProjects: (ids, favorite) =>
        set((state) => ({ projectIds: withIds(state.projectIds, ids, favorite) })),
      setSections: (ids, favorite) =>
        set((state) => ({ sectionIds: withIds(state.sectionIds, ids, favorite) })),
    }),
    {
      name: LIBRARY_CHAT_FAVORITES_STORAGE_KEY,
      merge: (persisted, current) => {
        const saved = persisted as Partial<ChatFavoritesState> | undefined;
        return {
          ...current,
          chatIds: readIds(saved?.chatIds),
          projectIds: readIds(saved?.projectIds),
          sectionIds: readIds(saved?.sectionIds),
        };
      },
    },
  ),
);

syncAcrossWindows(useChatFavoritesStore);