2025-06-22 00:21:09 +03:00

150 lines
4.6 KiB
JavaScript

import { w as writable } from "./index2.js";
const createContactsStore = () => {
const defaultState = {
contacts: [],
lastUpdated: null
};
const initialState = defaultState;
const { subscribe, set, update } = writable(initialState);
return {
subscribe,
// Initialize the store
initialize: () => {
update((state) => ({
...state,
isLoading: true,
error: null
}));
try {
const contacts = loadStoredContacts();
set({
contacts,
isLoading: false,
error: null
});
} catch (error) {
set({
contacts: [],
isLoading: false,
error: "Failed to load contacts"
});
}
},
/**
* Adds a new contact entry to the list with the provided details.
* @function addContact
* @param {string} name Human-readable name to identify this contact with
* @param {string} address Public Stellar address associated with this contact
* @throws Will throw an error if the new contact entry contains an invalid public key
*/
addContact: (name, address) => update((state) => {
if (!address || address.length !== 56 || !address.startsWith("G")) {
return {
...state,
error: "Invalid Stellar address"
};
}
const exists = state.contacts.some((c) => c.address === address);
if (exists) {
return {
...state,
error: "Contact with this address already exists"
};
}
const newContact = {
id: Date.now().toString(),
name: name || "Unnamed Contact",
address,
createdAt: (/* @__PURE__ */ new Date()).toISOString()
};
const updatedContacts = [...state.contacts, newContact];
if (typeof window !== "undefined") {
localStorage.setItem("stellar_contacts", JSON.stringify(updatedContacts));
}
return {
...state,
contacts: updatedContacts,
error: null
};
}),
/**
* Removes the specified contact entry from the list.
* @param {string} id Unique identifier of the contact to be removed from the list
*/
removeContact: (id) => update((state) => {
const updatedContacts = state.contacts.filter((contact) => contact.id !== id);
if (typeof window !== "undefined") {
localStorage.setItem("stellar_contacts", JSON.stringify(updatedContacts));
}
return {
...state,
contacts: updatedContacts,
error: null
};
}),
/**
* Updates an existing contact entry with the provided updates.
* @function updateContact
* @param {string} id Unique identifier of the contact to be updated
* @param {Object} updates Object containing the fields to update
*/
updateContact: (id, updates) => update((state) => {
const updatedContacts = state.contacts.map(
(contact) => contact.id === id ? { ...contact, ...updates } : contact
);
if (typeof window !== "undefined") {
localStorage.setItem("stellar_contacts", JSON.stringify(updatedContacts));
}
return {
...state,
contacts: updatedContacts,
error: null
};
}),
/**
* Imports contacts from an array of contact objects.
* @param {Array<Object>} contactArray Array of contact objects to import
*/
importContacts: (contactArray) => update((state) => {
try {
const validContacts = contactArray.filter((c) => c.address && c.address.length === 56).map((c) => ({
id: c.id || Date.now().toString() + Math.random().toString(36).substring(2, 10),
name: c.name || "Imported Contact",
address: c.address,
createdAt: c.createdAt || (/* @__PURE__ */ new Date()).toISOString()
}));
const existingAddresses = new Set(state.contacts.map((c) => c.address));
const newContacts = validContacts.filter((c) => !existingAddresses.has(c.address));
const updatedContacts = [...state.contacts, ...newContacts];
if (typeof window !== "undefined") {
localStorage.setItem("stellar_contacts", JSON.stringify(updatedContacts));
}
return {
...state,
contacts: updatedContacts,
error: null
};
} catch (error) {
return {
...state,
error: "Failed to import contacts"
};
}
}),
/**
* Clears the error state.
*/
clearError: () => update((state) => ({
...state,
error: null
}))
};
};
const contactsStore = createContactsStore();
if (typeof window !== "undefined") {
contactsStore.initialize();
}
export {
contactsStore as c
};