-
-
Notifications
You must be signed in to change notification settings - Fork 11
OBLS-688 Add server profiles feature with multi-env support #381
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0fdbe97
OBLS-688 Add server profiles feature with multi-env support
olewandowski1 e116578
OBLS-688 Fix profile switching: proper logout, error handling, edge c…
olewandowski1 48b91a4
OBLS-688 Fix profiles switching, migration edge cases, add loading state
olewandowski1 44b7ba5
OBLS-688 Fix active profile deletion logout, fix race condition,and h…
olewandowski1 b86960b
OBLS-688 Add default Staging and Test server profiles, clean up dead …
olewandowski1 724a327
OBLS-688 Fix default profile url
olewandowski1 c9e24a2
Update src/components/ProfileStorage.ts
awalkowiak File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| import React from 'react'; | ||
| import { StyleSheet, View } from 'react-native'; | ||
|
|
||
| export default function ProfileCardSkeleton() { | ||
| return ( | ||
| <View style={styles.card}> | ||
| <View style={styles.row}> | ||
| <View style={styles.inner}> | ||
| <View style={[styles.block, styles.title]} /> | ||
| <View style={styles.row}> | ||
| <View style={[styles.block, styles.icon]} /> | ||
| <View style={styles.inner}> | ||
| <View style={[styles.block, styles.label]} /> | ||
| <View style={[styles.block, styles.url]} /> | ||
| </View> | ||
| </View> | ||
| </View> | ||
| </View> | ||
| </View> | ||
| ); | ||
| } | ||
|
|
||
| const styles = StyleSheet.create({ | ||
| card: { | ||
| backgroundColor: '#f5f6f8', | ||
| borderRadius: 8, | ||
| borderWidth: 1, | ||
| borderColor: '#e0e0e0', | ||
| paddingVertical: 10, | ||
| paddingHorizontal: 14, | ||
| marginBottom: 20 | ||
| }, | ||
| row: { | ||
| flexDirection: 'row', | ||
| alignItems: 'center' | ||
| }, | ||
| inner: { | ||
| flex: 1 | ||
| }, | ||
| block: { | ||
| backgroundColor: '#e0e0e0', | ||
| borderRadius: 4 | ||
| }, | ||
| title: { | ||
| width: 90, | ||
| height: 10, | ||
| marginBottom: 8 | ||
| }, | ||
| icon: { | ||
| width: 22, | ||
| height: 22, | ||
| marginRight: 12 | ||
| }, | ||
| label: { | ||
| width: 120, | ||
| height: 14, | ||
| marginBottom: 6 | ||
| }, | ||
| url: { | ||
| width: 180, | ||
| height: 10 | ||
| } | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,196 @@ | ||
| import AsyncStorage from '@react-native-async-storage/async-storage'; | ||
|
|
||
| import { Profile, ProfileStorageData } from '../types/profile'; | ||
| import { createEventEmitter } from '../utils/EventEmitter'; | ||
|
|
||
| function generateId(): string { | ||
| return Date.now().toString(36) + Math.random().toString(36).slice(2); | ||
| } | ||
|
|
||
| const PROFILES_KEY = 'PROFILES'; | ||
| const LEGACY_API_URL_KEY = 'API_URL'; | ||
| const CURRENT_VERSION = 1; | ||
|
|
||
| const DEFAULT_SERVERS = [ | ||
| { label: 'Staging Server', serverUrl: 'https://stag.vtc.openboxes.com/openboxes/api' }, | ||
| { label: 'Test Server', serverUrl: 'https://vvg.openboxes.com/openboxes/api' } | ||
| ]; | ||
|
|
||
| const emitter = createEventEmitter(); | ||
| export const subscribe = emitter.subscribe; | ||
|
|
||
| export function createStorage(profiles: Profile[] = [], activeProfileId?: string): ProfileStorageData { | ||
| return { | ||
| version: CURRENT_VERSION, | ||
| activeProfileId: activeProfileId ?? profiles[0]?.id ?? null, | ||
| profiles | ||
| }; | ||
| } | ||
|
|
||
| function parseStorageData(raw: string | null): ProfileStorageData { | ||
| if (!raw) { | ||
| return createStorage(); | ||
| } | ||
|
|
||
| try { | ||
| const parsed = JSON.parse(raw); | ||
|
|
||
| return { | ||
| version: parsed.version ?? CURRENT_VERSION, | ||
| activeProfileId: parsed.activeProfileId ?? null, | ||
| profiles: Array.isArray(parsed.profiles) | ||
| ? parsed.profiles.map((p: any) => ({ | ||
| id: p.id ?? generateId(), | ||
| label: p.label ?? 'Unknown', | ||
| serverUrl: p.serverUrl ?? '', | ||
| settings: p.settings ?? {} | ||
| })) | ||
| : [] | ||
| }; | ||
| } catch { | ||
| return createStorage(); | ||
| } | ||
| } | ||
|
|
||
| async function readStorage(): Promise<ProfileStorageData> { | ||
| const raw = await AsyncStorage.getItem(PROFILES_KEY); | ||
| return parseStorageData(raw); | ||
| } | ||
|
|
||
| async function writeStorage(data: ProfileStorageData): Promise<void> { | ||
| await AsyncStorage.setItem(PROFILES_KEY, JSON.stringify(data)); | ||
| emitter.emit(); | ||
| } | ||
|
|
||
| export function createProfile(label: string, serverUrl: string): Profile { | ||
| return { | ||
| id: generateId(), | ||
| label, | ||
| serverUrl: serverUrl.trim(), | ||
| settings: {} | ||
| }; | ||
| } | ||
|
|
||
| function normalizeUrl(url: string): string { | ||
| return url.trim().toLowerCase().replace(/\/+$/, ''); | ||
| } | ||
|
|
||
| export function validateUrl(url: string): string | null { | ||
| const trimmed = url.trim(); | ||
| if (!trimmed) { | ||
| return 'URL is required'; | ||
| } | ||
| if (!/^https?:\/\/.+/i.test(trimmed)) { | ||
| return 'URL must start with http:// or https://'; | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| export function validateProfile(label: string, serverUrl: string): string | null { | ||
| if (!label.trim()) { | ||
| return 'Profile label is required'; | ||
| } | ||
| return validateUrl(serverUrl); | ||
| } | ||
|
|
||
| /** | ||
| * Initializes profiles and returns the active server URL. | ||
| * 1. Profiles exist → use them as-is | ||
| * 2. No profiles, legacy API_URL exists → create Default (from legacy) + default servers | ||
| * 3. No profiles, no legacy → create default servers | ||
| */ | ||
| export async function migrate(): Promise<string | null> { | ||
| const data = await readStorage(); | ||
|
|
||
| // Path 1: profiles already exist | ||
| if (data.profiles.length > 0) { | ||
| let active = data.profiles.find((p) => p.id === data.activeProfileId); | ||
|
|
||
| if (!active) { | ||
| active = data.profiles[0]; | ||
| await writeStorage(createStorage(data.profiles, active.id)); | ||
| } | ||
|
|
||
| return active.serverUrl; | ||
| } | ||
|
|
||
| // Path 2: legacy API_URL migration | ||
| const legacyUrl = await AsyncStorage.getItem(LEGACY_API_URL_KEY); | ||
|
|
||
| if (legacyUrl) { | ||
| const legacyProfile = createProfile('Default', legacyUrl); | ||
| const normalizedLegacy = normalizeUrl(legacyUrl); | ||
| const profiles = [legacyProfile]; | ||
|
|
||
| for (const server of DEFAULT_SERVERS) { | ||
| if (normalizeUrl(server.serverUrl) !== normalizedLegacy) { | ||
| profiles.push(createProfile(server.label, server.serverUrl)); | ||
| } | ||
| } | ||
|
|
||
| await writeStorage(createStorage(profiles, legacyProfile.id)); | ||
| await AsyncStorage.removeItem(LEGACY_API_URL_KEY); | ||
| return legacyProfile.serverUrl; | ||
| } | ||
|
|
||
| // Path 3: fresh install | ||
| const profiles = DEFAULT_SERVERS.map((s) => createProfile(s.label, s.serverUrl)); | ||
| await writeStorage(createStorage(profiles)); | ||
|
|
||
| return profiles[0].serverUrl; | ||
| } | ||
|
|
||
| export async function getProfiles(): Promise<ProfileStorageData> { | ||
| return readStorage(); | ||
| } | ||
|
|
||
| export async function getActiveProfile(): Promise<Profile | null> { | ||
| const data = await readStorage(); | ||
| if (!data.activeProfileId) { | ||
| return data.profiles[0] ?? null; | ||
| } | ||
| return data.profiles.find((p) => p.id === data.activeProfileId) ?? data.profiles[0] ?? null; | ||
| } | ||
|
|
||
| export async function setActiveProfileId(id: string): Promise<void> { | ||
| const data = await readStorage(); | ||
| const exists = data.profiles.some((p) => p.id === id); | ||
| if (exists) { | ||
| data.activeProfileId = id; | ||
| await writeStorage(data); | ||
| } | ||
| } | ||
|
|
||
| export async function saveProfile(profile: Profile): Promise<void> { | ||
| const data = await readStorage(); | ||
| const index = data.profiles.findIndex((p) => p.id === profile.id); | ||
|
|
||
| if (index >= 0) { | ||
| data.profiles[index] = profile; | ||
| } else { | ||
| data.profiles.push(profile); | ||
| } | ||
|
|
||
| if (!data.activeProfileId) { | ||
| data.activeProfileId = profile.id; | ||
| } | ||
|
|
||
| await writeStorage(data); | ||
| } | ||
|
|
||
| export async function deleteProfile(id: string): Promise<boolean> { | ||
| const data = await readStorage(); | ||
|
|
||
| if (data.profiles.length <= 1) { | ||
| return false; | ||
| } | ||
|
|
||
| data.profiles = data.profiles.filter((p) => p.id !== id); | ||
|
|
||
| if (data.activeProfileId === id) { | ||
| data.activeProfileId = data.profiles[0]?.id ?? null; | ||
| } | ||
|
|
||
| await writeStorage(data); | ||
| return true; | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.