Hello from MCP server

List Files | Just Commands | Repo | Logs

← back |
import { computed, onMounted } from 'vue';
import { useSessionStore } from '@/stores/session';
import type { ProblemsRecord, ProblemTagsRecord } from '@/pocketbase-types';

export function useJobsAndTags() {
  const sessionStore = useSessionStore();

  // Load problems and tags on mount
  onMounted(async () => {
    await sessionStore.loadProblemsAndTags();
  });

  // Session store computed properties
  const problems = computed(() => sessionStore.problems);
  const problemTags = computed(() => sessionStore.problemTags);
  const selectedTags = computed(() => sessionStore.selectedTags);
  const appliedSearchQuery = computed(() => sessionStore.appliedSearchQuery);

  // Helper to get problem tag IDs
  const getProblemTagIds = (problem: ProblemsRecord): string[] => {
    return sessionStore.getProblemTagIds(problem);
  };

  // Compute available tags sorted by information gain
  const sortedTagsByFrequency = computed(() => {
    if (problemTags.value.length === 0 || problems.value.length === 0) return [];

    const itemFrequency = new Map<string, {
      tag: ProblemTagsRecord;
      count: number;
    }>();

    problemTags.value.forEach((tag) => {
      if (selectedTags.value.includes(tag.id)) return;

      const problemsWithTag = problems.value.filter((problem) => {
        const tagIds = getProblemTagIds(problem);
        return tagIds.includes(tag.id);
      });

      if (problemsWithTag.length > 0) {
        itemFrequency.set(tag.id, {
          tag,
          count: problemsWithTag.length,
        });
      }
    });

    return Array.from(itemFrequency.values());
  });

  // Top 15 tags as simple objects for UI
  const availableTagObjects = computed(() => {
    return sortedTagsByFrequency.value
      .slice(0, 15)
      .map(item => ({ id: item.tag.id, name: item.tag.name || '' }));
  });

  // Selected tags as simple objects for UI
  const selectedTagObjects = computed(() => {
    return selectedTags.value
      .map(tagId => problemTags.value.find(t => t.id === tagId))
      .filter(tag => tag !== undefined)
      .map(tag => ({ id: tag!.id, name: tag!.name || '' }));
  });

  // Applied search terms
  const appliedSearchTerms = computed(() => {
    if (appliedSearchQuery.value.trim() === "") return [];
    return appliedSearchQuery.value
      .split(/\s+/)
      .filter(term => term.trim() !== "");
  });

  return {
    problems,
    problemTags,
    selectedTags,
    appliedSearchQuery,
    getProblemTagIds,
    sortedTagsByFrequency,
    availableTagObjects,
    selectedTagObjects,
    appliedSearchTerms
  };
}