| 409 | const model = createModel() |
| 410 | |
| 411 | function createSession() { |
| 412 | const [sessionStore, setSessionStore] = createStore<{ |
| 413 | ready: boolean |
| 414 | pinned: string[] |
| 415 | }>({ |
| 416 | ready: false, |
| 417 | pinned: [], |
| 418 | }) |
| 419 | |
| 420 | const filePath = path.join(paths.state, "session.json") |
| 421 | const state = { |
| 422 | pending: false, |
| 423 | } |
| 424 | |
| 425 | function save() { |
| 426 | if (!sessionStore.ready) { |
| 427 | state.pending = true |
| 428 | return |
| 429 | } |
| 430 | state.pending = false |
| 431 | void writeJsonAtomic(filePath, { |
| 432 | pinned: sessionStore.pinned, |
| 433 | }) |
| 434 | } |
| 435 | |
| 436 | readJson<unknown>(filePath) |
| 437 | .then((x) => { |
| 438 | if (!x || typeof x !== "object") return |
| 439 | const pinned = (x as Record<string, unknown>).pinned |
| 440 | if (Array.isArray(pinned)) |
| 441 | setSessionStore( |
| 442 | "pinned", |
| 443 | pinned.filter((item): item is string => typeof item === "string"), |
| 444 | ) |
| 445 | }) |
| 446 | .catch(() => {}) |
| 447 | .finally(() => { |
| 448 | setSessionStore("ready", true) |
| 449 | if (state.pending) save() |
| 450 | }) |
| 451 | |
| 452 | const slots = createMemo(() => { |
| 453 | const existing = new Set(sync.data.session.filter((x) => x.parentID === undefined).map((x) => x.id)) |
| 454 | return sessionStore.pinned.filter((id) => existing.has(id)).slice(0, 9) |
| 455 | }) |
| 456 | |
| 457 | function prune(sessionID: string) { |
| 458 | batch(() => { |
| 459 | if (sessionStore.pinned.includes(sessionID)) { |
| 460 | setSessionStore( |
| 461 | "pinned", |
| 462 | sessionStore.pinned.filter((x) => x !== sessionID), |
| 463 | ) |
| 464 | } |
| 465 | save() |
| 466 | }) |
| 467 | } |
| 468 | |