summaryrefslogtreecommitdiffstatshomepage
path: root/frontend/src/lib/Utils.ts
diff options
context:
space:
mode:
Diffstat (limited to 'frontend/src/lib/Utils.ts')
-rw-r--r--frontend/src/lib/Utils.ts30
1 files changed, 22 insertions, 8 deletions
diff --git a/frontend/src/lib/Utils.ts b/frontend/src/lib/Utils.ts
index 1a07be1..c347544 100644
--- a/frontend/src/lib/Utils.ts
+++ b/frontend/src/lib/Utils.ts
@@ -2,7 +2,8 @@ import { isError } from '$gql/Utils';
import type { ImageFragment } from '$gql/graphql';
import type { BeforeNavigate } from '@sveltejs/kit';
import type { OperationResultState } from '@urql/svelte';
-import { openModal } from 'svelte-modals';
+import { modals } from 'svelte-modals';
+import { toastFinally } from './Toasts';
import ConfirmDeletion from './dialogs/ConfirmDeletion.svelte';
export function range(from: number, to: number) {
@@ -16,6 +17,8 @@ export function getRandomInt(min: number, max: number) {
return Math.floor(Math.random() * (maxFloored - minCeiled) + minCeiled);
}
+export type Key = string | number | symbol;
+
export interface ListItem {
id: number | string;
name: string;
@@ -32,7 +35,6 @@ export function getResultState(state: OperationResultState): ResultState {
if (state.error) {
message = `${state.error.name}: ${state.error.message}`;
} else if (state.data) {
- // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
const obj = Object.values(state.data)[0];
if (isError(obj)) {
message = obj.message;
@@ -68,11 +70,14 @@ export function confirmDeletion(
callback: () => void,
warning?: string
) {
- openModal(
- ConfirmDeletion,
- { names: Array.isArray(names) ? names : [names], typename, callback: callback, warning },
- { replace: true }
- );
+ modals
+ .open(ConfirmDeletion, {
+ names: Array.isArray(names) ? names : [names],
+ typename,
+ callback,
+ warning
+ })
+ .catch(toastFinally);
}
export function idFromLabel(label: string) {
@@ -87,7 +92,7 @@ export function formatListSize(word: string, size: number) {
return `${size} ${pluralize(word, size)}`;
}
-export function joinText(items: string[], separator = ', ') {
+export function joinText(items: (string | undefined | null)[], separator = ', ') {
return items.filter((i) => i).join(separator);
}
@@ -106,3 +111,12 @@ export function preventOnPending({ to, cancel }: BeforeNavigate, pending: boolea
cancel();
}
+export function omit<T, K extends keyof T>(obj: T, ...props: K[]): Omit<T, K> {
+ return props.reduce(
+ (o, k) => {
+ delete o[k];
+ return o;
+ },
+ { ...obj }
+ );
+}