Skip to content

Commit

Permalink
improve performance
Browse files Browse the repository at this point in the history
  • Loading branch information
scobru committed Oct 15, 2024
1 parent dab1cf4 commit a57ce7a
Show file tree
Hide file tree
Showing 6 changed files with 212 additions and 210 deletions.
2 changes: 2 additions & 0 deletions packages/svelte/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"@types/d3": "^7",
"@types/dompurify": "^3",
"@types/eslint": "^8.56.0",
"@types/lodash-es": "^4",
"@types/react": "^18",
"@typescript-eslint/eslint-plugin": "^7.0.0",
"@typescript-eslint/parser": "^7.0.0",
Expand Down Expand Up @@ -73,6 +74,7 @@
"ip": "^2.0.1",
"js-sha256": "^0.11.0",
"js-sha3": "^0.9.3",
"lodash-es": "^4.17.21",
"lodash.debounce": "^4.0.8",
"react": "^18.3.1",
"self-adjusting-interval": "^1.0.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,14 @@
let { user } = useUser();
let { account } = useAccount(pub || $user?.pub);
let globalAccount: any;
$: globalAccount = $account;
globalAccount = account;
console.log("account", globalAccount);
$: lastPulse = globalAccount?.pulse;
let lastPulse = 0;
if ($globalAccount && $globalAccount.pulse !== lastPulse) {
lastPulse = $globalAccount.pulse;
console.log("Pulse updated:", $globalAccount.pulse);
if (globalAccount && globalAccount.pulse !== lastPulse) {
lastPulse = globalAccount.pulse;
console.log("Pulse updated:", globalAccount.pulse);
}
const accountFields = [
Expand All @@ -45,27 +43,27 @@
<div class="card-body">
<h2 class="card-title text-black font-medium text-2xl">Account Information</h2>
<AccountAvatar pub={pub || $user?.pub} />
{#if $globalAccount}
{#if globalAccount}
{#each accountFields as { key, label }}
<div class="form-control gap-2">
<label class="label ">
<span class="label-text text-black font-medium">{label}</span>
</label>
{#if key === 'color'}
<div class="flex items-center">
<div class="w-6 h-6 rounded-full " style="background-color: {$globalAccount[key]};"></div>
<span class="badge">{$globalAccount[key]}</span>
<div class="w-6 h-6 rounded-full " style="background-color: {globalAccount[key]};"></div>
<span class="badge">{globalAccount[key]}</span>
</div>
{:else if key === 'blink'}
<span class="badge badge-outline">{$globalAccount[key] ? 'Yes' : 'No'}</span>
<span class="badge badge-outline">{globalAccount[key] ? 'Yes' : 'No'}</span>
{:else if key === 'lastSeen'}
<span class="badge badge-{typeof $globalAccount[key] === 'number' ? 'success' : 'error'}">
{typeof $globalAccount[key] === 'number' ? `${$globalAccount[key]}s ago` : get($globalAccount[key])}
<span class="badge badge-{typeof globalAccount[key] === 'number' ? 'success' : 'error'}">
{typeof globalAccount[key] === 'number' ? `${globalAccount[key]}s ago` : get(globalAccount[key])}
</span>
{:else if key === 'pulse'}
<span class="badge badge-neutral">{$globalAccount[key]}</span>
<span class="badge badge-neutral">{globalAccount[key]}</span>
{:else}
<span readonly class="w-full text-left text-xs" >{$globalAccount[key]}</span>
<span readonly class="w-full text-left text-xs" >{globalAccount[key]}</span>
{/if}
</div>
{/each}
Expand Down
185 changes: 81 additions & 104 deletions packages/svelte/src/lib/components/gun/profile/ProfileDisplay.svelte
Original file line number Diff line number Diff line change
@@ -1,121 +1,98 @@
<script lang="ts">
import { useUser, updateProfileField, addProfileField, removeProfileField } from "$lib/gun/user";
import { onMount } from "svelte";
import { useUser, updateProfile, addProfileField } from "$lib/gun/user";
import { onMount } from "svelte";
let { user } = useUser();
let newFieldName = "";
let newFieldValue = "";
let editMode: { [key: string]: boolean } = {};
let editFields: { [key: string]: string } = {};
let { user } = useUser();
let newFieldName = "";
let newFieldValue = "";
let editMode: { [key: string]: boolean } = {};
let editFields: { [key: string]: string } = {};
$: profileFields = Object.entries($user.profile || {});
$: profileFields = $user.profile ?
Object.entries($user.profile)
.filter(([key, value]) =>
key !== '-' &&
key !== '#' &&
!key.startsWith('~') &&
key !== '>' &&
typeof value !== 'object' &&
value !== null &&
value !== undefined
)
.reduce((acc, [key, value]) => {
if (!acc.some(item => item.key.toLowerCase() === key.toLowerCase())) {
acc.push({ key, label: key.charAt(0).toUpperCase() + key.slice(1), value });
}
return acc;
}, [])
: [];
function handleUpdate(key: string) {
updateProfile(key, editFields[key]);
editMode[key] = false;
delete editFields[key];
}
function handleUpdate(key: string) {
const newValue = editFields[key];
updateProfileField(key, newValue);
editMode[key] = false;
delete editFields[key];
}
function handleAddField() {
if (newFieldName && newFieldValue) {
addProfileField(newFieldName.toLowerCase(), newFieldValue);
newFieldName = "";
newFieldValue = "";
}
function handleAddField() {
if (newFieldName && newFieldValue) {
addProfileField(newFieldName, newFieldValue);
newFieldName = "";
newFieldValue = "";
}
}
function toggleEditMode(key: string, value: string) {
editMode[key] = !editMode[key];
if (editMode[key]) {
editFields[key] = value;
} else {
delete editFields[key];
}
function toggleEditMode(key: string, value: string) {
editMode[key] = !editMode[key];
if (editMode[key]) {
editFields[key] = value;
} else {
delete editFields[key];
}
}
onMount(() => {
// Carica il profilo dell'utente se necessario
});
onMount(() => {
loadUserProfile();
});
</script>

<div class="card w-90 bg-ableton-yellow text-black rounded-none p-4 font-sans">
<div class="card-body">
<h2 class="card-title text-black font-medium text-2xl">Profile</h2>
{#if profileFields.length > 0}
{#each profileFields as { key, label, value }}
<div class="form-control gap-2 mb-4">
{#if editMode[key]}
<div class="flex gap-2">
<span class="text-black font-medium flex-grow">{label}</span>
<input
type="text"
class="input input-bordered flex-grow"
bind:value={editFields[key]}
placeholder="Field Value"
/>
<button
class="btn btn-primary"
on:click={() => handleUpdate(key)}
>
Save
</button>
</div>
{:else}
<div class="flex justify-between items-center">
<span class="text-black font-medium">{label}</span>
<span class="text-sm">{value}</span>
<button class="btn btn-sm btn-outline" on:click={() => toggleEditMode(key, value)}>
Edit
</button>
</div>
{/if}
<div class="bg-ableton-yellow text-black p-6 rounded-lg text-left w-80 font-sans">
<h2 class="text-2xl font-semibold mb-6">Profile</h2>

{#each profileFields as [key, value]}
<div class="mb-4">
<p class="text-sm font-medium mb-1">{key}</p>
{#if !editMode[key]}
<div class="bg-white rounded px-2 py-1 text-sm flex justify-between items-center">
<span>{value}</span>
<button
class="text-blue-500 text-xs"
on:click={() => toggleEditMode(key, value)}
>
Edit
</button>
</div>
{/each}
{:else}
<p class="text-center">No profile information available</p>
{/if}

<div class="form-control gap-2 mt-4">
<label class="label">
<span class="label-text text-black font-medium">Add New Field</span>
</label>
<div class="flex flex-col gap-2">
<input
type="text"
class="input input-bordered flex-grow "
placeholder="Field Name"
bind:value={newFieldName}
/>
<input
type="text"
class="input input-bordered flex-grow"
placeholder="Field Value"
bind:value={newFieldValue}
/>
<button class="btn btn-primary" on:click={handleAddField}>Add</button>
</div>
{:else}
<div class="flex">
<input
class="bg-white rounded-l px-2 py-1 text-sm flex-grow"
bind:value={editFields[key]}
/>
<button
class="bg-green-500 text-white rounded-r px-2 py-1 text-xs"
on:click={() => handleUpdate(key)}
>
Save
</button>
</div>
{/if}
</div>
{/each}

<div class="mt-6">
<input
class="bg-white rounded px-2 py-1 text-sm w-full mb-2"
bind:value={newFieldName}
placeholder="New field name"
/>
<input
class="bg-white rounded px-2 py-1 text-sm w-full mb-2"
bind:value={newFieldValue}
placeholder="New field value"
/>
<button
class="bg-blue-500 text-white rounded px-2 py-1 text-sm"
on:click={handleAddField}
>
Add Field
</button>
</div>
</div>

<style>
/* Puoi aggiungere stili personalizzati qui se necessario */
:global(.bg-ableton-yellow) {
background-color: #FBFFA7;
}
</style>
Loading

0 comments on commit a57ce7a

Please sign in to comment.