Files
t3code/apps/mobile/src/lib/storage.ts
T
Julius Marminge 131f25ce8f Update ESLint configuration, remove unused dependencies, and refactor connection handling
- Added "apps/mobile/uniwind-types.d.ts" to .oxlintrc.json ignore list.
- Disabled the "unicorn/no-array-sort" rule in ESLint.
- Removed "zustand" from bun.lock and package.json.
- Refactored connection handling in various components, including the introduction of ConnectionSheetButton and updates to connection-related functions.
- Updated type imports to use EnvironmentScoped types across multiple files.
- Cleaned up unused imports and improved code structure in several components.
2026-04-13 18:26:36 -07:00

56 lines
1.7 KiB
TypeScript

import * as Arr from "effect/Array";
import { pipe } from "effect/Function";
import * as SecureStore from "expo-secure-store";
import type { SavedRemoteConnection } from "./connection";
const CONNECTIONS_KEY = "t3code.connections";
async function readStorageItem(key: string): Promise<string | null> {
return await SecureStore.getItemAsync(key);
}
async function writeStorageItem(key: string, value: string): Promise<void> {
await SecureStore.setItemAsync(key, value);
}
export async function loadSavedConnections(): Promise<ReadonlyArray<SavedRemoteConnection>> {
const raw = (await readStorageItem(CONNECTIONS_KEY)) ?? "";
if (!raw.trim()) {
return [];
}
try {
const parsed = JSON.parse(raw) as {
readonly connections?: ReadonlyArray<SavedRemoteConnection>;
};
return pipe(
parsed.connections ?? [],
Arr.filter((c) => !!c.environmentId && !!c.bearerToken?.trim()),
);
} catch {
return [];
}
}
export async function saveConnection(connection: SavedRemoteConnection): Promise<void> {
const current = await loadSavedConnections();
const next = current.some((entry) => entry.environmentId === connection.environmentId)
? pipe(
current,
Arr.map((entry) => (entry.environmentId === connection.environmentId ? connection : entry)),
)
: pipe(current, Arr.append(connection));
await writeStorageItem(CONNECTIONS_KEY, JSON.stringify({ connections: next }));
}
export async function clearSavedConnection(environmentId: string): Promise<void> {
const current = await loadSavedConnections();
const next = pipe(
current,
Arr.filter((entry) => entry.environmentId !== environmentId),
);
await writeStorageItem(CONNECTIONS_KEY, JSON.stringify({ connections: next }));
}