enh: secrets store & logging

This commit is contained in:
Ben Allfree 2023-06-19 05:13:04 -07:00
parent a0f1e029cf
commit 2e66b827cd

View File

@ -1,3 +1,4 @@
import { logger } from '@pockethost/common'
import { scaleOrdinal } from 'd3-scale'
import { schemeTableau10 } from 'd3-scale-chromatic'
import { writable } from 'svelte/store'
@ -35,6 +36,8 @@ const sanitize = (item: SecretItem) => {
// create a custom store fulfilling the CRUD operations
function createItems(initialItems: SecretsArray) {
const { dbg } = logger().create(`Secrets/store.ts`)
const { subscribe, set, update } = writable(initialItems)
return {
@ -44,6 +47,7 @@ function createItems(initialItems: SecretsArray) {
},
// create: add an object for the item at the end of the store's array
create: (item: SecretItem) => {
dbg(`Creating`, item)
const { name, value } = sanitize(item)
return update((n) => {
n = [
@ -56,18 +60,10 @@ function createItems(initialItems: SecretsArray) {
return formatInput(n)
})
},
// update: increase the value of the selected item
update: (item: SecretItem) => {
const { name, value } = sanitize(item)
return update((n) => {
const index = n.findIndex((item) => item.name === name)
n[index].value += value
return formatInput(n)
})
},
// delete: remove the item from the array
delete: (name: string) => {
dbg(`Delete`, name)
return update((n) => {
const index = n.findIndex((item) => item.name === name)
n = [...n.slice(0, index), ...n.slice(index + 1)]