Open History to see the last commit
1import { createContext } from "react";
2
3/** Names the open workspace file per environment and thread, so viewers outlive remounts. */
4export const PreviewPositionKey = createContext<string | null>(null);
5
6export function previewPositionKey(
7 ...parts: readonly [environmentId: string, threadId: string, cwd: string, path: string]
8): string {
9 return JSON.stringify(parts);
10}
11
12/** Reading positions outlive the preview, which unmounts when the file or thread changes. */
13export function createPositionMemory<T>(limit = 100) {
14 const positions = new Map<string, T>();
15 return {
16 get: (key: string | null) => (key === null ? undefined : positions.get(key)),
17 set(key: string | null, position: T) {
18 if (key === null) return;
19 positions.delete(key);
20 positions.set(key, position);
21 const oldest = positions.keys().next();
22 if (positions.size > limit && !oldest.done) positions.delete(oldest.value);
23 },
24 };
25}