@basmilius/common 3.1.0 → 3.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- export default function(baseHex: string, prefix?: string);
1
+ export default function(baseHex: string, prefix?: string): Record<string, string>;
@@ -1,2 +1,2 @@
1
- declare const _default;
1
+ declare const _default: <T extends Function>() => T;
2
2
  export default _default;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@basmilius/common",
3
3
  "description": "Common code shared in personal projects.",
4
- "version": "3.1.0",
4
+ "version": "3.3.0",
5
5
  "type": "module",
6
6
  "license": "MIT",
7
7
  "funding": "https://github.com/sponsors/basmilius",
@@ -48,8 +48,8 @@
48
48
  "./*": "./*"
49
49
  },
50
50
  "dependencies": {
51
- "@basmilius/http-client": "3.1.0",
52
- "@basmilius/utils": "3.1.0",
51
+ "@basmilius/http-client": "3.3.0",
52
+ "@basmilius/utils": "3.3.0",
53
53
  "@flux-ui/components": "^3.0.0-next.26",
54
54
  "@flux-ui/internals": "^3.0.0-next.26",
55
55
  "pinia": "^3.0.4",
@@ -57,7 +57,7 @@
57
57
  "vue-router": "^5.0.3"
58
58
  },
59
59
  "devDependencies": {
60
- "@basmilius/tools": "3.1.0",
60
+ "@basmilius/tools": "3.3.0",
61
61
  "@types/culori": "^4.0.1",
62
62
  "@types/lodash-es": "^4.17.12",
63
63
  "culori": "^4.0.2",
@@ -1,4 +1,5 @@
1
1
  export { default as useCopy } from './useCopy';
2
+ export { default as useDataTable } from './useDataTable';
2
3
  export { default as useDebounced } from './useDebounced';
3
4
  export { default as useDtoForm } from './useDtoForm';
4
5
  export { default as useInterval } from './useInterval';
@@ -0,0 +1,90 @@
1
+ import { BaseResponse, Paginated } from '@basmilius/http-client';
2
+ import { type ComputedRef, type MultiWatchSources, ref, type Ref, unref, watch } from 'vue';
3
+ import useLoaded from './useLoaded';
4
+ import usePagination from './usePagination';
5
+
6
+ export default function <T>(fetcher: (offset: number, limit: number) => Promise<BaseResponse<Paginated<T>> | false>, dependencies: MultiWatchSources = []): UseDataTable<T> {
7
+ const {
8
+ isLoading,
9
+ loaded
10
+ } = useLoaded();
11
+
12
+ const {
13
+ limits,
14
+ page,
15
+ perPage,
16
+ total,
17
+ setPage,
18
+ setPerPage,
19
+ setTotal
20
+ } = usePagination();
21
+
22
+ const displayEmpty = ref(false);
23
+ const isFirstLoad = ref(true);
24
+ const items = ref<T[]>([]) as Ref<T[]>;
25
+
26
+ async function fetch(): Promise<void> {
27
+ const _page = unref(page);
28
+ const _perPage = unref(perPage);
29
+
30
+ try {
31
+ const response = await loaded(fetcher)((_page - 1) * _perPage, _perPage);
32
+
33
+ if (response === false) {
34
+ return;
35
+ }
36
+
37
+ if (isFirstLoad.value) {
38
+ isFirstLoad.value = false;
39
+
40
+ if (response.data.items.length === 0) {
41
+ displayEmpty.value = true;
42
+ }
43
+ } else if (response.data.items.length > 0) {
44
+ displayEmpty.value = false;
45
+ }
46
+
47
+ items.value = response.data.items;
48
+ setTotal(response.data.total);
49
+ } catch (err) {
50
+ if (err instanceof Error && err.message === 'Dep is null or undefined.') {
51
+ // In this case, the error is expected and valid. Something is probably still loading.
52
+ return;
53
+ }
54
+
55
+ throw err;
56
+ }
57
+ }
58
+
59
+ watch([page, perPage, ...dependencies], fetch, {immediate: true});
60
+
61
+ return {
62
+ displayEmpty,
63
+ isLoading,
64
+ items,
65
+ limits,
66
+ page,
67
+ perPage,
68
+ total,
69
+
70
+ reload: fetch,
71
+ setPage,
72
+ setPerPage,
73
+ setTotal
74
+ };
75
+ }
76
+
77
+ type UseDataTable<T> = {
78
+ readonly displayEmpty: Ref<boolean>;
79
+ readonly isLoading: ComputedRef<boolean>;
80
+ readonly items: Ref<T[]>;
81
+ readonly limits: Ref<number[]>;
82
+ readonly page: Ref<number>;
83
+ readonly perPage: Ref<number>;
84
+ readonly total: Ref<number>;
85
+
86
+ reload(): Promise<void>;
87
+ setPage(num: number): void;
88
+ setPerPage(num: number): void;
89
+ setTotal(num: number): void;
90
+ };
@@ -33,11 +33,11 @@ const POSITION: Record<Shade, number> = {
33
33
  950: 0.98
34
34
  };
35
35
 
36
- function clamp(v: number, min = 0, max = 1) {
36
+ function clamp(v: number, min = 0, max = 1): number {
37
37
  return Math.min(max, Math.max(min, v));
38
38
  }
39
39
 
40
- export default function (baseHex: string, prefix = 'color') {
40
+ export default function (baseHex: string, prefix = 'color'): Record<string, string> {
41
41
  const base = toOklch(baseHex);
42
42
 
43
43
  if (!base || base.mode !== 'oklch') {
@@ -2,7 +2,7 @@ import { isRequestError } from '@basmilius/http-client';
2
2
  import { showSnackbar } from '@flux-ui/components';
3
3
  import onError from './onError';
4
4
 
5
- export default <T extends Function>() => onError<T>(err => {
5
+ export default <T extends Function>(): T => onError<T>(err => {
6
6
  if (!isRequestError(err)) {
7
7
  throw err;
8
8
  }