- 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.
56 lines
1.7 KiB
TypeScript
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 }));
|
|
}
|