mirror of
https://github.com/appwrite/console.git
synced 2026-06-06 19:27:48 +00:00
Merge pull request #2885 from appwrite/fix-pref-bug
fix: handle non-string user/team prefs to prevent Preferences UI crash
This commit is contained in:
@@ -1,12 +1,19 @@
|
||||
export type PrefRow = { key: string; value: string };
|
||||
|
||||
export function normalizePrefs(entries: [string, string][] | PrefRow[]): [string, string][] {
|
||||
function stringTrim(s: unknown): string {
|
||||
return String(s ?? '').trim();
|
||||
}
|
||||
|
||||
export function normalizePrefs(
|
||||
entries: [string, unknown][] | PrefRow[] | { key: unknown; value: unknown }[]
|
||||
): [string, string][] {
|
||||
return entries
|
||||
.map((item): [string, string] =>
|
||||
Array.isArray(item) ? [item[0], item[1]] : [item.key, item.value]
|
||||
Array.isArray(item)
|
||||
? [String(item[0] ?? ''), String(item[1] ?? '')]
|
||||
: [String(item.key ?? ''), String(item.value ?? '')]
|
||||
)
|
||||
|
||||
.filter(([k, v]) => k.trim() && v.trim())
|
||||
.filter(([k, v]) => stringTrim(k).length > 0 && stringTrim(v).length > 0)
|
||||
.sort(([a], [b]) => a.localeCompare(b));
|
||||
}
|
||||
|
||||
@@ -23,5 +30,5 @@ export function isAddDisabled(prefs: PrefRow[] | null): boolean {
|
||||
}
|
||||
|
||||
export function sanitizePrefs(prefs: PrefRow[]) {
|
||||
return prefs.filter((p) => p.key.trim() && p.value.trim());
|
||||
return prefs.filter((p) => stringTrim(p.key).length > 0 && stringTrim(p.value).length > 0);
|
||||
}
|
||||
|
||||
+5
-5
@@ -22,9 +22,7 @@
|
||||
|
||||
$: if (prefs) {
|
||||
const currentNormalized = normalizePrefs(prefs);
|
||||
const originalNormalized = normalizePrefs(
|
||||
Object.entries(($team?.prefs ?? {}) as Record<string, string>)
|
||||
);
|
||||
const originalNormalized = normalizePrefs(Object.entries($team?.prefs ?? {}));
|
||||
|
||||
arePrefsDisabled = deepEqual(currentNormalized, originalNormalized);
|
||||
}
|
||||
@@ -33,10 +31,12 @@
|
||||
let arePrefsDisabled = true;
|
||||
|
||||
onMount(async () => {
|
||||
const entries = Object.entries(($team?.prefs ?? {}) as Record<string, string>);
|
||||
const entries = Object.entries($team?.prefs ?? {});
|
||||
prefs =
|
||||
entries.length > 0
|
||||
? entries.map(([key, value]) => createPrefRow(key, value))
|
||||
? entries.map(([key, value]) =>
|
||||
createPrefRow(String(key ?? ''), String(value ?? ''))
|
||||
)
|
||||
: [createPrefRow()];
|
||||
});
|
||||
|
||||
|
||||
@@ -34,7 +34,9 @@
|
||||
const entries = Object.entries($user?.prefs ?? {});
|
||||
prefs =
|
||||
entries.length > 0
|
||||
? entries.map(([key, value]) => createPrefRow(key, value))
|
||||
? entries.map(([key, value]) =>
|
||||
createPrefRow(String(key ?? ''), String(value ?? ''))
|
||||
)
|
||||
: [createPrefRow()];
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user