Home / File/ store.ts — react Source File

store.ts — react Source File

Architecture documentation for store.ts, a typescript file in the react codebase. 3 imports, 0 dependents.

File typescript PlaygroundApp Stores 3 imports 5 functions

Entity Profile

Dependency Diagram

graph LR
  4949445b_a2de_ca5c_8e91_50e8a7ed966a["store.ts"]
  f69df4a5_0121_dc26_8c32_aacc01afafd1["defaultStore.ts"]
  4949445b_a2de_ca5c_8e91_50e8a7ed966a --> f69df4a5_0121_dc26_8c32_aacc01afafd1
  7b0f5d46_efcb_4a2d_89c1_7cee4a0f9bc8["invariant"]
  4949445b_a2de_ca5c_8e91_50e8a7ed966a --> 7b0f5d46_efcb_4a2d_89c1_7cee4a0f9bc8
  4c41bc7d_b5a6_7dab_08fd_dadd874b58c0["lz-string"]
  4949445b_a2de_ca5c_8e91_50e8a7ed966a --> 4c41bc7d_b5a6_7dab_08fd_dadd874b58c0
  style 4949445b_a2de_ca5c_8e91_50e8a7ed966a fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

/**
 * Copyright (c) Meta Platforms, Inc. and affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */

import invariant from 'invariant';
import {
  compressToEncodedURIComponent,
  decompressFromEncodedURIComponent,
} from 'lz-string';
import {defaultStore, defaultConfig} from '../defaultStore';

/**
 * Global Store for Playground
 */
export interface Store {
  source: string;
  config: string;
  showInternals: boolean;
}
export function encodeStore(store: Store): string {
  return compressToEncodedURIComponent(JSON.stringify(store));
}
export function decodeStore(hash: string): any {
  return JSON.parse(decompressFromEncodedURIComponent(hash));
}

/**
 * Serialize, encode, and save @param store to localStorage and update URL.
 */
export function saveStore(store: Store): void {
  const hash = encodeStore(store);
  localStorage.setItem('playgroundStore', hash);
  history.replaceState({}, '', `#${hash}`);
}

/**
 * Check if @param raw is a valid Store by if
 * - it has a `source` property and is a string
 */
function isValidStore(raw: unknown): raw is Store {
  return (
    raw != null &&
    typeof raw == 'object' &&
    'source' in raw &&
    typeof raw['source'] === 'string'
  );
}

/**
 * Deserialize, decode, and initialize @param store from URL and then
 * localStorage. Throw an error if Store is malformed.
 */
export function initStoreFromUrlOrLocalStorage(): Store {
  const encodedSourceFromUrl = location.hash.slice(1);
  const encodedSourceFromLocal = localStorage.getItem('playgroundStore');
  const encodedSource = encodedSourceFromUrl || encodedSourceFromLocal;

  /**
   * No data in the URL and no data in the localStorage to fallback to.
   * Initialize with the default store.
   */
  if (!encodedSource) return defaultStore;

  const raw: any = decodeStore(encodedSource);

  invariant(isValidStore(raw), 'Invalid Store');

  // Make sure all properties are populated
  return {
    source: raw.source,
    config: 'config' in raw && raw['config'] ? raw.config : defaultConfig,
    showInternals: 'showInternals' in raw ? raw.showInternals : false,
  };
}

Domain

Subdomains

Types

Dependencies

Frequently Asked Questions

What does store.ts do?
store.ts is a source file in the react codebase, written in typescript. It belongs to the PlaygroundApp domain, Stores subdomain.
What functions are defined in store.ts?
store.ts defines 5 function(s): decodeStore, encodeStore, initStoreFromUrlOrLocalStorage, isValidStore, saveStore.
What does store.ts depend on?
store.ts imports 3 module(s): defaultStore.ts, invariant, lz-string.
Where is store.ts in the architecture?
store.ts is located at compiler/apps/playground/lib/stores/store.ts (domain: PlaygroundApp, subdomain: Stores, directory: compiler/apps/playground/lib/stores).

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free