@ossy/resources 1.0.1 → 1.2.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.
@@ -0,0 +1,23 @@
1
+ import {
2
+ unless,
3
+ startsWith,
4
+ o,
5
+ endsWith
6
+ } from 'ramda'
7
+ import { useRouter } from '@ossy/router-react'
8
+ import React, { useEffect } from 'react'
9
+
10
+ const prependSlash = unless(startsWith('/'), path => `/${path}`)
11
+ const appendSlash = unless(endsWith('/'), path => `${path}/`)
12
+ const addSlashes = o(prependSlash, appendSlash)
13
+
14
+ export const useActivePath = () => {
15
+ const router = useRouter()
16
+ const activePath = addSlashes(router.searchParams.location || '/')
17
+
18
+ // useEffect(() => {
19
+ // console.log('useActivePath', activePath)
20
+ // }, [activePath])
21
+
22
+ return activePath
23
+ }
package/src/useForm.js ADDED
@@ -0,0 +1,61 @@
1
+ import { useState } from 'react'
2
+ import { applyFieldChange } from '@ossy/design-system'
3
+
4
+ export const useForm = ({ defaultData }) => {
5
+ const [inputData, setInputdata] = useState()
6
+
7
+ let data;
8
+
9
+ if (inputData === undefined && defaultData !== undefined) {
10
+ data = defaultData
11
+ } else if (inputData === undefined && defaultData === undefined) {
12
+ data = {}
13
+ } else {
14
+ data = inputData
15
+ }
16
+
17
+ const setField = (field, value) => {
18
+ setInputdata(inputData => {
19
+
20
+ let data;
21
+
22
+ if (inputData === undefined && defaultData !== undefined) {
23
+ data = defaultData
24
+ } else if (inputData === undefined && defaultData === undefined) {
25
+ data = {}
26
+ } else {
27
+ data = inputData
28
+ }
29
+
30
+ return {
31
+ ...data,
32
+ [field]: value
33
+ }
34
+ })
35
+ }
36
+
37
+ const onChange = event => {
38
+ setInputdata(inputData => {
39
+ let data
40
+
41
+ if (inputData === undefined && defaultData !== undefined) {
42
+ data = defaultData
43
+ } else if (inputData === undefined && defaultData === undefined) {
44
+ data = {}
45
+ } else {
46
+ data = inputData
47
+ }
48
+
49
+ return applyFieldChange(data, event)
50
+ })
51
+ }
52
+
53
+
54
+ return {
55
+ data,
56
+ setField,
57
+ onChange
58
+ }
59
+
60
+
61
+ }
@@ -0,0 +1,14 @@
1
+ /**
2
+ * Converts bytes to a human-readable format.
3
+ * @param {number} bytes - The number of bytes.
4
+ * @param {number} decimals - The number of decimal places to include (default is 2).
5
+ * @returns {string} - The human-readable format.
6
+ */
7
+ export function formatBytes(bytes, decimals = 2) {
8
+ if (!bytes) return
9
+ const k = 1024;
10
+ const dm = decimals < 0 ? 0 : decimals;
11
+ const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
12
+ const i = Math.floor(Math.log(bytes) / Math.log(k));
13
+ return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i];
14
+ }