← Files ItalicARCHIVED FILE

ui/state.js

2.31 KB · Nov 25, 2026 · 00:20 UTC

↓ Download file

/** The shared ChatGPT/Claude shell. Account changes retire every request and
 * draft before the next account's snapshot is accepted. */
export function createItalicState() {
  return {
    navigation: {phase: 'empty'},
    interaction: {phase: 'idle'},
    operation: {phase: 'idle'},
    account: {phase: 'unbound'},
    generation: 0,
    permissions: [], filters: {}, devices: [], devicesAt: 0, deviceHashes: new Map(),
    get view() { return this.navigation.phase === 'ready' ? this.navigation.view : 'library'; },
    get data() { return this.navigation.phase === 'ready' ? this.navigation.data : {}; },
    get accountId() { return this.account.phase === 'bound' ? this.account.id : null; },
    get busy() { return this.operation.phase !== 'idle'; },
    get draft() { return this.interaction.phase === 'editing' ? this.interaction.draft : null; },
    get confirm() { return this.interaction.phase === 'confirming' ? this.interaction.confirmation : null; },
    show(view, data) { this.navigation = {phase: 'ready', view, data}; },
    edit(draft) { this.interaction = {phase: 'editing', draft}; },
    confirmWith(confirmation) { this.interaction = {phase: 'confirming', confirmation}; },
    clearEdit() { if (this.interaction.phase === 'editing') this.interaction = {phase: 'idle'}; },
    clearConfirmation() { if (this.interaction.phase === 'confirming') this.interaction = {phase: 'idle'}; },
    invalidate() { this.generation++; this.operation = {phase: 'idle'}; },
    bind(id) {
      if (!id || id === this.accountId) return;
      this.invalidate(); this.account = {phase: 'bound', id};
      this.navigation = {phase: 'empty'}; this.interaction = {phase: 'idle'};
      this.permissions = []; this.filters = {}; this.devices = []; this.devicesAt = 0; this.deviceHashes = new Map();
    },
    begin(phase, details = {}) {
      if (!['loading', 'saving'].includes(phase)) throw new Error('Unknown operation');
      const id = ++this.generation;
      this.operation = phase === 'saving' ? {...details, phase, id, stage: 'sending'} : {...details, phase, id};
      return id;
    },
    acknowledged(id) {
      if (this.operation.id === id && this.operation.phase === 'saving')
        this.operation = {...this.operation, stage: 'acknowledged'};
    },
    finish(id) { if (this.operation.id === id) this.operation = {phase: 'idle'}; },
  };
}

SHA-256: 497073e63b3dbc591033a188059344cf0b8bb6b0cba8d7ef3b60653f4726456e