<commit_before>import MetadataItem from './MetadataItem';
import { Set } from '../shims/SetOps.js';

let UserPreferences = MetadataItem.extend({
  /*
    resetToDefaults doesn't work unless defaults
    is a function. If defaults are a simple object,
    changes to the model mutate the defaults object.
  */
  defaults: () => {
    return {
      name: 'Resonant Laboratory Preferences',
      description: `
Contains your preferences for the Resonant Laboratory application. If
you move or delete this item, your preferences will be lost.`,
      meta: {
        rlab: {
          seenTips: {},
          achievements: {}
        }
      }
    };
  },
  initialize: function () {
    let seenTips = window.localStorage.getItem('seenTips');
    if (seenTips) {
      this.setMeta('seenTips', JSON.parse(seenTips));
    }
    let achievements = window.localStorage.getItem('achievements');
    if (achievements) {
      this.setMeta('achievements', JSON.parse(achievements));
    }
  },
  fetch: function () {
    return MetadataItem.prototype.fetch.apply(this, arguments)
      .catch(() => {
        // Fallback: attempt to retrieve the user's preferences from localStorage
        this.setMeta('seenTips', window.localStorage.getItem('seenTips') || {});
        this.setMeta('achievements', window.localStorage.getItem('achievements') || {});
      });
  },
  save: function () {
    return MetadataItem.prototype.save.apply(this, arguments)
      .catch(() => {
        // Fallback: store the user's preferences in localStorage
        let seenTips = this.getMeta('seenTips') || {};
        window.localStorage.setItem('seenTips', JSON.stringify(seenTips));
        let achievements = this.getMeta('achievements') || {};
        window.localStorage.setItem('achievements', JSON.stringify(achievements));
      });
  },
  addListeners: function () {
    this.listenTo(window.mainPage.currentUser, 'rl:login',
      this.adoptScratchProjects);
    this.stopListening(window.mainPage, 'rl:createProject');
    this.listenTo(window.mainPage, 'rl:createProject',
      this.claimProject);
    this.stopListening(window.mainPage, 'rl:changeProject');
    this.listenTo(window.mainPage, 'rl:changeProject',
      this.addProjectListeners);
    this.addProjectListeners();
  },
  addProjectListeners: function () {
    if (window.mainPage.project) {
      this.stopListening(window.mainPage.project, 'rl:swappedId');
      this.listenTo(window.mainPage.project, 'rl:swappedId',
        this.handleCopiedProject);
      this.stopListening(window.mainPage.project, 'rl:swappedDatasetId');
      this.listenTo(window.mainPage.project, 'rl:swappedDatasetId',
        this.handleCopiedDataset);
    }
  },
  handleCopiedProject: function () {
    // Once we know the change in status (e.g. the ID can swap if a copy is
    // made), display a notification about where the project was moved.
    // Because we're now working on a copy, we should also fire the project
    // creation event
    window.mainPage.project.cache.status.then(status => {
      let notification = 'You are now working on a copy of this project in ';
      if (status.visibility === 'PublicScratch') {
        notification = 'the public scratch space. Log in to take ownership of this project.';
      } else if (status.visibility === 'PrivateUser') {
        notification += 'your Private folder.';
      } else {
        window.mainPage.trigger('rl:error', new Error('Project copied to an unknown location.'));
      }
      window.mainPage.notificationLayer.displayNotification(notification);
      window.mainPage.trigger('rl:createProject');
    });
  },
  handleCopiedDataset: function () {
    // TODO: display a notification, like the one for the copied project
  },
  claimProject: function () {
    if (!window.mainPage.currentUser.isLoggedIn()) {
      // Because we created this project while not logged in,
      // store the project's ID in window.localStorage so that we
      // can claim "ownership" when we log in / visit this page again
      // (of course, this is easy to hack, but that's the assumption
      // with public scratch space)<commit_msg>Fix status caching issue when reassigning project<commit_after>import MetadataItem from './MetadataItem';
import { Set } from '../shims/SetOps.js';

let UserPreferences = MetadataItem.extend({
  /*
    resetToDefaults doesn't work unless defaults
    is a function. If defaults are a simple object,
    changes to the model mutate the defaults object.
  */
  defaults: () => {
    return {
      name: 'Resonant Laboratory Preferences',
      description: `
Contains your preferences for the Resonant Laboratory application. If
you move or delete this item, your preferences will be lost.`,
      meta: {
        rlab: {
          seenTips: {},
          achievements: {}
        }
      }
    };
  },
  initialize: function () {
    let seenTips = window.localStorage.getItem('seenTips');
    if (seenTips) {
      this.setMeta('seenTips', JSON.parse(seenTips));
    }
    let achievements = window.localStorage.getItem('achievements');
    if (achievements) {
      this.setMeta('achievements', JSON.parse(achievements));
    }
  },
  fetch: function () {
    return MetadataItem.prototype.fetch.apply(this, arguments)
      .catch(() => {
        // Fallback: attempt to retrieve the user's preferences from localStorage
        this.setMeta('seenTips', window.localStorage.getItem('seenTips') || {});
        this.setMeta('achievements', window.localStorage.getItem('achievements') || {});
      });
  },
  save: function () {
    return MetadataItem.prototype.save.apply(this, arguments)
      .catch(() => {
        // Fallback: store the user's preferences in localStorage
        let seenTips = this.getMeta('seenTips') || {};
        window.localStorage.setItem('seenTips', JSON.stringify(seenTips));
        let achievements = this.getMeta('achievements') || {};
        window.localStorage.setItem('achievements', JSON.stringify(achievements));
      });
  },
  addListeners: function () {
    this.listenTo(window.mainPage.currentUser, 'rl:login',
      this.adoptScratchProjects);
    this.stopListening(window.mainPage, 'rl:createProject');
    this.listenTo(window.mainPage, 'rl:createProject',
      this.claimProject);
    this.stopListening(window.mainPage, 'rl:changeProject');
    this.listenTo(window.mainPage, 'rl:changeProject',
      this.addProjectListeners);
    this.addProjectListeners();
  },
  addProjectListeners: function () {
    if (window.mainPage.project) {
      this.stopListening(window.mainPage.project, 'rl:swappedId');
      this.listenTo(window.mainPage.project, 'rl:swappedId',
        this.handleCopiedProject);
      this.stopListening(window.mainPage.project, 'rl:swappedDatasetId');
      this.listenTo(window.mainPage.project, 'rl:swappedDatasetId',
        this.handleCopiedDataset);
    }
  },
  handleCopiedProject: function () {
    // Once we know the change in status (e.g. the ID can swap if a copy is
    // made), display a notification about where the project was moved.
    // Because we're now working on a copy, we should also fire the project
    // creation event
    window.mainPage.project.updateStatus();
    window.mainPage.project.cache.status.then(status => {
      let notification = 'You are now working on a copy of this project in ';
      if (status.visibility === 'PublicScratch') {
        notification += 'the public scratch space. Log in to take ownership of this project.';
      } else if (status.visibility === 'PrivateUser') {
        notification += 'your Private folder.';
      } else {
        window.mainPage.trigger('rl:error', new Error('Project copied to an unknown location.'));
      }
      window.mainPage.notificationLayer.displayNotification(notification);
      window.mainPage.trigger('rl:createProject');
    });
  },
  handleCopiedDataset: function () {
    // TODO: display a notification, like the one for the copied project
  },
  claimProject: function () {
    if (!window.mainPage.currentUser.isLoggedIn()) {
      // Because we created this project while not logged in,
      // store the project's ID in window.localStorage so that we
      // can claim "ownership" when we log in / visit this page again
      // (of course, this is easy to hack, but that's the assumption
      // with public scratch space)
{'fraction_non_alphanumeric': 0.12220227408737283, 'fraction_numerical': 0.0, 'mean_word_length': 3.1800900450225114, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4816789', 'n_tokens_mistral': 2344, 'n_tokens_neox': 2343, 'n_words': 616}