+
From 70ad39597f5fbd1389bf558477900d913df2da53 Mon Sep 17 00:00:00 2001
From: Wan Qi Chen <495709+wa0x6e@users.noreply.github.com>
Date: Wed, 1 May 2024 00:00:22 +0400
Subject: [PATCH 080/104] refactor: code improvement
---
apps/ui/src/networks/offchain/api/index.ts | 5 +-
apps/ui/src/stores/bookmarks.ts | 68 +++++++++++-----------
2 files changed, 39 insertions(+), 34 deletions(-)
diff --git a/apps/ui/src/networks/offchain/api/index.ts b/apps/ui/src/networks/offchain/api/index.ts
index a55466cf8..80013a5f7 100644
--- a/apps/ui/src/networks/offchain/api/index.ts
+++ b/apps/ui/src/networks/offchain/api/index.ts
@@ -360,7 +360,10 @@ export function createApi(uri: string, networkId: NetworkID): NetworkApi {
}
});
- return follows;
+ return follows.map(follow => {
+ follow.space.network = networkId;
+ return follow;
+ });
}
};
}
diff --git a/apps/ui/src/stores/bookmarks.ts b/apps/ui/src/stores/bookmarks.ts
index d57868242..c1109025b 100644
--- a/apps/ui/src/stores/bookmarks.ts
+++ b/apps/ui/src/stores/bookmarks.ts
@@ -5,6 +5,10 @@ import { Space } from '@/types';
const offchainNetworkId = offchainNetworks.filter(network => enabledNetworks.includes(network))[0];
+function compositeSpaceId(space: Space) {
+ return `${space.network}:${space.id}`;
+}
+
export const useBookmarksStore = defineStore('bookmarks', () => {
const { web3, authInitiated } = useWeb3();
const spacesStore = useSpacesStore();
@@ -29,7 +33,7 @@ export const useBookmarksStore = defineStore('bookmarks', () => {
);
const bookmarkedSpacesMap = computed(
- () => new Map(spacesData.value.map(space => [`${space.network}:${space.id}`, space]))
+ () => new Map(spacesData.value.map(space => [compositeSpaceId(space), space]))
);
const bookmarkedSpaces = computed({
@@ -41,18 +45,42 @@ export const useBookmarksStore = defineStore('bookmarks', () => {
set(spaces: Space[]) {
starredSpacesIds.value = spaces
.filter(space => space.network !== offchainNetworkId)
- .map(space => `${space.network}:${space.id}`);
+ .map(compositeSpaceId);
- accountsBookmarkedSpacesIds.value[web3.value.account] = spaces.map(
- space => `${space.network}:${space.id}`
- );
+ accountsBookmarkedSpacesIds.value[web3.value.account] = spaces.map(compositeSpaceId);
}
});
+ function syncBookmarkedSpacesIds(spaceIds: string[], type: 'starred' | 'followed') {
+ accountsBookmarkedSpacesIds.value[web3.value.account] = Array.from(
+ new Set(
+ [...bookmarkedSpacesIds.value, ...spaceIds].filter(
+ id =>
+ id.startsWith(`${offchainNetworkId}:`) !== (type === 'followed') ||
+ spaceIds.includes(id)
+ )
+ )
+ );
+ }
+
+ async function fetchSpacesData(ids: string[]) {
+ if (!ids.length) return;
+
+ const spaces = await getSpaces({
+ id_in: ids.filter(id => !spacesMap.has(id))
+ });
+
+ spacesData.value = [
+ ...spacesData.value,
+ ...spaces,
+ ...(ids.map(id => spacesMap.get(id)).filter(Boolean) as Space[])
+ ];
+ }
+
async function loadFollowedSpaces() {
const network = getNetwork(offchainNetworkId);
- const followedIds = (await network.api.loadFollows(web3.value.account)).map(
- follow => `${offchainNetworkId}:${follow.space.id}`
+ const followedIds = (await network.api.loadFollows(web3.value.account)).map(follow =>
+ compositeSpaceId(follow.space)
);
const newIds = followedIds.filter(id => !followedSpacesIds.value.includes(id));
followedSpacesIds.value = followedIds;
@@ -80,32 +108,6 @@ export const useBookmarksStore = defineStore('bookmarks', () => {
});
}
- async function fetchSpacesData(ids: string[]) {
- if (!ids.length) return;
-
- const spaces = await getSpaces({
- id_in: ids.filter(id => !spacesMap.has(id))
- });
-
- spacesData.value = [
- ...spacesData.value,
- ...spaces,
- ...(ids.map(id => spacesMap.get(id)).filter(Boolean) as Space[])
- ];
- }
-
- function syncBookmarkedSpacesIds(spaceIds: string[], type: 'starred' | 'followed') {
- accountsBookmarkedSpacesIds.value[web3.value.account] = Array.from(
- new Set(
- [...bookmarkedSpacesIds.value, ...spaceIds].filter(
- id =>
- id.startsWith(`${offchainNetworkId}:`) !== (type === 'followed') ||
- spaceIds.includes(id)
- )
- )
- );
- }
-
watch(
starredSpacesIds,
async (currentIds, previousIds) => {
From 23c8246008f08bec1feb57768ee8d7fca1d7a27b Mon Sep 17 00:00:00 2001
From: Wan Qi Chen <495709+wa0x6e@users.noreply.github.com>
Date: Wed, 1 May 2024 00:43:07 +0400
Subject: [PATCH 081/104] refactor: code DRYing
---
apps/ui/src/components/SpacesListItem.vue | 5 +----
apps/ui/src/stores/bookmarks.ts | 15 ++++++++++++---
apps/ui/src/views/Space/Overview.vue | 4 ++--
3 files changed, 15 insertions(+), 9 deletions(-)
diff --git a/apps/ui/src/components/SpacesListItem.vue b/apps/ui/src/components/SpacesListItem.vue
index ec9d69f7e..d5b094b82 100644
--- a/apps/ui/src/components/SpacesListItem.vue
+++ b/apps/ui/src/components/SpacesListItem.vue
@@ -28,10 +28,7 @@ const spaceIdComposite = `${props.space.network}:${props.space.id}`;
class="hidden group-hover:block absolute top-3 right-3 hover:text-skin-link"
@click.prevent="bookmarksStore.toggleSpaceStar(spaceIdComposite)"
>
-
+
diff --git a/apps/ui/src/stores/bookmarks.ts b/apps/ui/src/stores/bookmarks.ts
index c1109025b..1e0124d7c 100644
--- a/apps/ui/src/stores/bookmarks.ts
+++ b/apps/ui/src/stores/bookmarks.ts
@@ -82,7 +82,7 @@ export const useBookmarksStore = defineStore('bookmarks', () => {
const followedIds = (await network.api.loadFollows(web3.value.account)).map(follow =>
compositeSpaceId(follow.space)
);
- const newIds = followedIds.filter(id => !followedSpacesIds.value.includes(id));
+ const newIds = followedIds.filter(id => !isFollowed(id));
followedSpacesIds.value = followedIds;
syncBookmarkedSpacesIds(followedIds, 'followed');
@@ -90,7 +90,7 @@ export const useBookmarksStore = defineStore('bookmarks', () => {
}
function toggleSpaceStar(id: string) {
- const alreadyStarred = starredSpacesIds.value.includes(id);
+ const alreadyStarred = isStarred(id);
if (alreadyStarred) {
starredSpacesIds.value = starredSpacesIds.value.filter((spaceId: string) => spaceId !== id);
@@ -108,6 +108,14 @@ export const useBookmarksStore = defineStore('bookmarks', () => {
});
}
+ function isStarred(spaceId: string) {
+ return starredSpacesIds.value.includes(spaceId);
+ }
+
+ function isFollowed(spaceId: string) {
+ return followedSpacesIds.value.includes(spaceId);
+ }
+
watch(
starredSpacesIds,
async (currentIds, previousIds) => {
@@ -147,10 +155,11 @@ export const useBookmarksStore = defineStore('bookmarks', () => {
return {
bookmarkedSpaces,
bookmarksLoaded,
- starredSpacesIds,
starredSpacesLoaded,
followedSpacesIds,
followedSpacesLoaded,
+ isStarred,
+ isFollowed,
toggleSpaceStar
};
});
diff --git a/apps/ui/src/views/Space/Overview.vue b/apps/ui/src/views/Space/Overview.vue
index 34c57d01b..abbfe167e 100644
--- a/apps/ui/src/views/Space/Overview.vue
+++ b/apps/ui/src/views/Space/Overview.vue
@@ -27,8 +27,8 @@ onMounted(() => {
const spaceIdComposite = `${props.space.network}:${props.space.id}`;
const isOffchainSpace = offchainNetworks.includes(props.space.network);
-const spaceStarred = computed(() => bookmarksStore.starredSpacesIds.includes(spaceIdComposite));
-const spaceFollowed = computed(() => bookmarksStore.followedSpacesIds.includes(spaceIdComposite));
+const spaceStarred = computed(() => bookmarksStore.isStarred(spaceIdComposite));
+const spaceFollowed = computed(() => bookmarksStore.isFollowed(spaceIdComposite));
const isController = computed(() => compareAddresses(props.space.controller, web3.value.account));
const socials = computed(() =>
From 6e0b892e7630beb0d49c621982ad7ed35c8e7915 Mon Sep 17 00:00:00 2001
From: Wan Qi Chen <495709+wa0x6e@users.noreply.github.com>
Date: Wed, 1 May 2024 01:20:34 +0400
Subject: [PATCH 082/104] fix: populate spaces store
---
apps/ui/src/stores/bookmarks.ts | 11 +++++-----
apps/ui/src/stores/spaces.ts | 38 ++++++++++++++++++++++-----------
2 files changed, 31 insertions(+), 18 deletions(-)
diff --git a/apps/ui/src/stores/bookmarks.ts b/apps/ui/src/stores/bookmarks.ts
index 1e0124d7c..3fede06c1 100644
--- a/apps/ui/src/stores/bookmarks.ts
+++ b/apps/ui/src/stores/bookmarks.ts
@@ -12,7 +12,6 @@ function compositeSpaceId(space: Space) {
export const useBookmarksStore = defineStore('bookmarks', () => {
const { web3, authInitiated } = useWeb3();
const spacesStore = useSpacesStore();
- const { spacesMap, getSpaces } = spacesStore;
const { mixpanel } = useMixpanel();
const spacesData = ref([]);
@@ -66,14 +65,14 @@ export const useBookmarksStore = defineStore('bookmarks', () => {
async function fetchSpacesData(ids: string[]) {
if (!ids.length) return;
- const spaces = await getSpaces({
- id_in: ids.filter(id => !spacesMap.has(id))
- });
+ await spacesStore.fetchSpaces(
+ ids.filter(id => !spacesStore.spacesMap.has(id)).map(id => id.split(':')[1]),
+ offchainNetworkId
+ );
spacesData.value = [
...spacesData.value,
- ...spaces,
- ...(ids.map(id => spacesMap.get(id)).filter(Boolean) as Space[])
+ ...(ids.map(id => spacesStore.spacesMap.get(id)).filter(Boolean) as Space[])
];
}
diff --git a/apps/ui/src/stores/spaces.ts b/apps/ui/src/stores/spaces.ts
index 7ce7c4c39..0aad02bae 100644
--- a/apps/ui/src/stores/spaces.ts
+++ b/apps/ui/src/stores/spaces.ts
@@ -5,17 +5,8 @@ import { NetworkID } from '@/types';
export const useSpacesStore = defineStore('spaces', () => {
const metaStore = useMetaStore();
- const {
- loading,
- loaded,
- networksMap,
- spaces,
- spacesMap,
- hasMoreSpaces,
- fetch,
- fetchMore,
- getSpaces
- } = useSpaces();
+ const { loading, loaded, networksMap, spaces, spacesMap, hasMoreSpaces, fetch, fetchMore } =
+ useSpaces();
async function fetchSpace(spaceId: string, networkId: NetworkID) {
await metaStore.fetchBlock(networkId);
@@ -31,6 +22,29 @@ export const useSpacesStore = defineStore('spaces', () => {
};
}
+ async function fetchSpaces(spaceIds: string[], networkId: NetworkID) {
+ await metaStore.fetchBlock(networkId);
+
+ const network = getNetwork(networkId);
+
+ const spaces = await network.api.loadSpaces(
+ {
+ skip: 0,
+ limit: 100
+ },
+ {
+ id_in: spaceIds
+ }
+ );
+
+ if (!spaces.length) return;
+
+ networksMap.value[networkId].spaces = {
+ ...networksMap.value[networkId].spaces,
+ ...Object.fromEntries(spaces.map(space => [space.id, space]))
+ };
+ }
+
return {
loading,
loaded,
@@ -41,6 +55,6 @@ export const useSpacesStore = defineStore('spaces', () => {
fetch,
fetchMore,
fetchSpace,
- getSpaces
+ fetchSpaces
};
});
From f6ca94ba383d29af9e710ba4af18ec04c3b08efa Mon Sep 17 00:00:00 2001
From: Wan Qi Chen <495709+wa0x6e@users.noreply.github.com>
Date: Wed, 1 May 2024 01:41:57 +0400
Subject: [PATCH 083/104] fix: fix spaces fetching for multiple network type
---
apps/ui/src/stores/bookmarks.ts | 17 +++++++-------
apps/ui/src/stores/spaces.ts | 41 +++++++++++++++++----------------
2 files changed, 30 insertions(+), 28 deletions(-)
diff --git a/apps/ui/src/stores/bookmarks.ts b/apps/ui/src/stores/bookmarks.ts
index 3fede06c1..f84ecfc08 100644
--- a/apps/ui/src/stores/bookmarks.ts
+++ b/apps/ui/src/stores/bookmarks.ts
@@ -65,10 +65,7 @@ export const useBookmarksStore = defineStore('bookmarks', () => {
async function fetchSpacesData(ids: string[]) {
if (!ids.length) return;
- await spacesStore.fetchSpaces(
- ids.filter(id => !spacesStore.spacesMap.has(id)).map(id => id.split(':')[1]),
- offchainNetworkId
- );
+ await spacesStore.fetchSpaces(ids.filter(id => !spacesStore.spacesMap.has(id)));
spacesData.value = [
...spacesData.value,
@@ -93,12 +90,16 @@ export const useBookmarksStore = defineStore('bookmarks', () => {
if (alreadyStarred) {
starredSpacesIds.value = starredSpacesIds.value.filter((spaceId: string) => spaceId !== id);
- accountsBookmarkedSpacesIds.value[web3.value.account] = accountsBookmarkedSpacesIds.value[
- web3.value.account
- ].filter((spaceId: string) => spaceId !== id);
+
+ if (web3.value.account)
+ accountsBookmarkedSpacesIds.value[web3.value.account] = accountsBookmarkedSpacesIds.value[
+ web3.value.account
+ ].filter((spaceId: string) => spaceId !== id);
} else {
starredSpacesIds.value = [id, ...starredSpacesIds.value];
- accountsBookmarkedSpacesIds.value[web3.value.account] = [id, ...bookmarkedSpacesIds.value];
+
+ if (web3.value.account)
+ accountsBookmarkedSpacesIds.value[web3.value.account] = [id, ...bookmarkedSpacesIds.value];
}
mixpanel.track('Set space favorite', {
diff --git a/apps/ui/src/stores/spaces.ts b/apps/ui/src/stores/spaces.ts
index 0aad02bae..94420d050 100644
--- a/apps/ui/src/stores/spaces.ts
+++ b/apps/ui/src/stores/spaces.ts
@@ -5,8 +5,17 @@ import { NetworkID } from '@/types';
export const useSpacesStore = defineStore('spaces', () => {
const metaStore = useMetaStore();
- const { loading, loaded, networksMap, spaces, spacesMap, hasMoreSpaces, fetch, fetchMore } =
- useSpaces();
+ const {
+ loading,
+ loaded,
+ networksMap,
+ spaces,
+ spacesMap,
+ hasMoreSpaces,
+ fetch,
+ fetchMore,
+ getSpaces
+ } = useSpaces();
async function fetchSpace(spaceId: string, networkId: NetworkID) {
await metaStore.fetchBlock(networkId);
@@ -22,27 +31,19 @@ export const useSpacesStore = defineStore('spaces', () => {
};
}
- async function fetchSpaces(spaceIds: string[], networkId: NetworkID) {
- await metaStore.fetchBlock(networkId);
-
- const network = getNetwork(networkId);
-
- const spaces = await network.api.loadSpaces(
- {
- skip: 0,
- limit: 100
- },
- {
- id_in: spaceIds
- }
- );
+ async function fetchSpaces(spaceIds: string[]) {
+ const spaces = await getSpaces({
+ id_in: spaceIds
+ });
if (!spaces.length) return;
- networksMap.value[networkId].spaces = {
- ...networksMap.value[networkId].spaces,
- ...Object.fromEntries(spaces.map(space => [space.id, space]))
- };
+ for (const space of spaces) {
+ networksMap.value[space.network].spaces = {
+ ...networksMap.value[space.network].spaces,
+ [space.id]: space
+ };
+ }
}
return {
From 6c6c3c3f1746e814a3776cd151d5dc7983f324d6 Mon Sep 17 00:00:00 2001
From: Wan Qi Chen <495709+wa0x6e@users.noreply.github.com>
Date: Wed, 1 May 2024 01:46:36 +0400
Subject: [PATCH 084/104] fix: fix variable name
---
apps/ui/src/components/SpacesListItem.vue | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/apps/ui/src/components/SpacesListItem.vue b/apps/ui/src/components/SpacesListItem.vue
index d5b094b82..4bf54b1e3 100644
--- a/apps/ui/src/components/SpacesListItem.vue
+++ b/apps/ui/src/components/SpacesListItem.vue
@@ -6,12 +6,12 @@ const props = defineProps<{ space: Space }>();
const bookmarksStore = useBookmarksStore();
-const spaceIdComposite = `${props.space.network}:${props.space.id}`;
+const compositeSpaceId = `${props.space.network}:${props.space.id}`;
@@ -26,9 +26,9 @@ const spaceIdComposite = `${props.space.network}:${props.space.id}`;
From 108cbd8b12f3df0ba574785307b828fd0903f7db Mon Sep 17 00:00:00 2001
From: Wan Qi Chen <495709+wa0x6e@users.noreply.github.com>
Date: Wed, 1 May 2024 02:01:56 +0400
Subject: [PATCH 085/104] chore: code cleaning
---
apps/ui/src/stores/bookmarks.ts | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/apps/ui/src/stores/bookmarks.ts b/apps/ui/src/stores/bookmarks.ts
index f84ecfc08..94cb2eec5 100644
--- a/apps/ui/src/stores/bookmarks.ts
+++ b/apps/ui/src/stores/bookmarks.ts
@@ -10,8 +10,8 @@ function compositeSpaceId(space: Space) {
}
export const useBookmarksStore = defineStore('bookmarks', () => {
- const { web3, authInitiated } = useWeb3();
const spacesStore = useSpacesStore();
+ const { web3, authInitiated } = useWeb3();
const { mixpanel } = useMixpanel();
const spacesData = ref
([]);
@@ -155,7 +155,6 @@ export const useBookmarksStore = defineStore('bookmarks', () => {
return {
bookmarkedSpaces,
bookmarksLoaded,
- starredSpacesLoaded,
followedSpacesIds,
followedSpacesLoaded,
isStarred,
From bdad47357217524b5cf6221a91947631b512b87e Mon Sep 17 00:00:00 2001
From: Wan Qi Chen <495709+wa0x6e@users.noreply.github.com>
Date: Wed, 1 May 2024 23:19:47 +0400
Subject: [PATCH 086/104] fix(ux): add missing loading icon while loading
follow list in sidebar
---
apps/ui/src/stores/bookmarks.ts | 15 ++++++++++++---
1 file changed, 12 insertions(+), 3 deletions(-)
diff --git a/apps/ui/src/stores/bookmarks.ts b/apps/ui/src/stores/bookmarks.ts
index 94cb2eec5..704c5a53f 100644
--- a/apps/ui/src/stores/bookmarks.ts
+++ b/apps/ui/src/stores/bookmarks.ts
@@ -83,6 +83,8 @@ export const useBookmarksStore = defineStore('bookmarks', () => {
syncBookmarkedSpacesIds(followedIds, 'followed');
fetchSpacesData(newIds);
+
+ console.log('fetch end');
}
function toggleSpaceStar(id: string) {
@@ -135,9 +137,14 @@ export const useBookmarksStore = defineStore('bookmarks', () => {
);
watch(
- [() => web3.value.account, () => web3.value.type, () => authInitiated.value],
- async ([web3, type, authInitiated]) => {
- if (!authInitiated) return;
+ [
+ () => web3.value.account,
+ () => web3.value.type,
+ () => web3.value.authLoading,
+ () => authInitiated.value
+ ],
+ async ([web3, type, authLoading, authInitiated]) => {
+ if (!authInitiated || authLoading) return;
if (!web3) {
followedSpacesIds.value = [];
@@ -147,6 +154,7 @@ export const useBookmarksStore = defineStore('bookmarks', () => {
if (type !== 'argentx') await loadFollowedSpaces();
+ console.log('ready');
followedSpacesLoaded.value = true;
},
{ immediate: true }
@@ -155,6 +163,7 @@ export const useBookmarksStore = defineStore('bookmarks', () => {
return {
bookmarkedSpaces,
bookmarksLoaded,
+ starredSpacesLoaded,
followedSpacesIds,
followedSpacesLoaded,
isStarred,
From b3841350840dc99d5752db5dc99d81f15b164442 Mon Sep 17 00:00:00 2001
From: Wan Qi Chen <495709+wa0x6e@users.noreply.github.com>
Date: Wed, 1 May 2024 23:20:44 +0400
Subject: [PATCH 087/104] chore: remove debug output
---
apps/ui/src/stores/bookmarks.ts | 3 ---
1 file changed, 3 deletions(-)
diff --git a/apps/ui/src/stores/bookmarks.ts b/apps/ui/src/stores/bookmarks.ts
index 704c5a53f..d86b5b1b1 100644
--- a/apps/ui/src/stores/bookmarks.ts
+++ b/apps/ui/src/stores/bookmarks.ts
@@ -83,8 +83,6 @@ export const useBookmarksStore = defineStore('bookmarks', () => {
syncBookmarkedSpacesIds(followedIds, 'followed');
fetchSpacesData(newIds);
-
- console.log('fetch end');
}
function toggleSpaceStar(id: string) {
@@ -154,7 +152,6 @@ export const useBookmarksStore = defineStore('bookmarks', () => {
if (type !== 'argentx') await loadFollowedSpaces();
- console.log('ready');
followedSpacesLoaded.value = true;
},
{ immediate: true }
From 474d1b72ce4ddcf2c9741e0e449a13f31d9e9c76 Mon Sep 17 00:00:00 2001
From: Wan Qi Chen <495709+wa0x6e@users.noreply.github.com>
Date: Mon, 6 May 2024 23:31:23 +0400
Subject: [PATCH 088/104] fix: finish merge conflict
---
apps/ui/src/stores/bookmarks.ts | 39 +++++++++++++++++++++++++++++++--
1 file changed, 37 insertions(+), 2 deletions(-)
diff --git a/apps/ui/src/stores/bookmarks.ts b/apps/ui/src/stores/bookmarks.ts
index d86b5b1b1..11313897d 100644
--- a/apps/ui/src/stores/bookmarks.ts
+++ b/apps/ui/src/stores/bookmarks.ts
@@ -1,7 +1,7 @@
import { defineStore } from 'pinia';
import { enabledNetworks, getNetwork, offchainNetworks } from '@/networks';
import pkg from '../../package.json';
-import { Space } from '@/types';
+import { NetworkID, Space } from '@/types';
const offchainNetworkId = offchainNetworks.filter(network => enabledNetworks.includes(network))[0];
@@ -11,12 +11,14 @@ function compositeSpaceId(space: Space) {
export const useBookmarksStore = defineStore('bookmarks', () => {
const spacesStore = useSpacesStore();
+ const actions = useActions();
const { web3, authInitiated } = useWeb3();
const { mixpanel } = useMixpanel();
const spacesData = ref([]);
const followedSpacesIds = ref([]);
const followedSpacesLoaded = ref(false);
+ const followSpaceLoading = ref(false);
const starredSpacesIds = useStorage(`${pkg.name}.spaces-starred`, [] as string[]);
const starredSpacesLoaded = ref(false);
// Combined list of starred and followed spaces by account, to keep sort order
@@ -108,6 +110,37 @@ export const useBookmarksStore = defineStore('bookmarks', () => {
});
}
+ async function toggleSpaceFollow(id: string) {
+ const alreadyFollowed = followedSpacesIds.value.includes(id);
+ const [spaceNetwork, spaceId] = id.split(':');
+ followSpaceLoading.value = true;
+
+ try {
+ if (alreadyFollowed) {
+ const result = await actions.unfollowSpace(spaceNetwork as NetworkID, spaceId);
+ if (!result) return;
+
+ followedSpacesIds.value = followedSpacesIds.value.filter(
+ (spaceId: string) => spaceId !== id
+ );
+ accountsBookmarkedSpacesIds.value[web3.value.account] = accountsBookmarkedSpacesIds.value[
+ web3.value.account
+ ].filter((spaceId: string) => spaceId !== id);
+ } else {
+ const result = await actions.followSpace(spaceNetwork as NetworkID, spaceId);
+ if (!result) return;
+
+ fetchSpacesData([id]);
+
+ followedSpacesIds.value = [id, ...followedSpacesIds.value];
+ accountsBookmarkedSpacesIds.value[web3.value.account] = [id, ...bookmarkedSpacesIds.value];
+ }
+ } catch (e) {
+ } finally {
+ followSpaceLoading.value = false;
+ }
+ }
+
function isStarred(spaceId: string) {
return starredSpacesIds.value.includes(spaceId);
}
@@ -163,8 +196,10 @@ export const useBookmarksStore = defineStore('bookmarks', () => {
starredSpacesLoaded,
followedSpacesIds,
followedSpacesLoaded,
+ followSpaceLoading,
isStarred,
isFollowed,
- toggleSpaceStar
+ toggleSpaceStar,
+ toggleSpaceFollow
};
});
From ace84d2c50bb3b669e18790331aa3d44f24e04ce Mon Sep 17 00:00:00 2001
From: Wan Qi Chen <495709+wa0x6e@users.noreply.github.com>
Date: Sat, 25 May 2024 18:20:10 +0400
Subject: [PATCH 089/104] fix: fix wrong network for follow/unfollow actions
---
apps/ui/src/composables/useActions.ts | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/apps/ui/src/composables/useActions.ts b/apps/ui/src/composables/useActions.ts
index f4f390f27..9bec69e01 100644
--- a/apps/ui/src/composables/useActions.ts
+++ b/apps/ui/src/composables/useActions.ts
@@ -519,7 +519,7 @@ export function useActions() {
try {
await wrapPromise(
- networkId,
+ offchainNetworkId,
network.actions.followSpace(
await aliasableSigner(networkId),
networkId,
@@ -545,7 +545,7 @@ export function useActions() {
try {
await wrapPromise(
- networkId,
+ offchainNetworkId,
network.actions.unfollowSpace(
await aliasableSigner(networkId),
networkId,
From d5b3da7f1c4ecf58a8429459d0e1687b9039290a Mon Sep 17 00:00:00 2001
From: Wan Qi Chen <495709+wa0x6e@users.noreply.github.com>
Date: Sat, 25 May 2024 18:22:40 +0400
Subject: [PATCH 090/104] chore: revert unrelated changes
---
apps/ui/src/components/SpacesListItem.vue | 1 -
1 file changed, 1 deletion(-)
diff --git a/apps/ui/src/components/SpacesListItem.vue b/apps/ui/src/components/SpacesListItem.vue
index 46145cddd..edfb4c04f 100644
--- a/apps/ui/src/components/SpacesListItem.vue
+++ b/apps/ui/src/components/SpacesListItem.vue
@@ -4,7 +4,6 @@ import { offchainNetworks } from '@/networks';
import { Space } from '@/types';
const props = defineProps<{ space: Space }>();
-
const compositeSpaceId = `${props.space.network}:${props.space.id}`;
From 8854ce012fe80788e8b4fb00ba1594c72ad880fb Mon Sep 17 00:00:00 2001
From: Wan Qi Chen <495709+wa0x6e@users.noreply.github.com>
Date: Sat, 25 May 2024 18:23:22 +0400
Subject: [PATCH 091/104] chore: revert unrelated changes
---
apps/ui/src/stores/spaces.ts | 1 +
1 file changed, 1 insertion(+)
diff --git a/apps/ui/src/stores/spaces.ts b/apps/ui/src/stores/spaces.ts
index 81b011753..bf2a3ccff 100644
--- a/apps/ui/src/stores/spaces.ts
+++ b/apps/ui/src/stores/spaces.ts
@@ -27,6 +27,7 @@ export const useSpacesStore = defineStore('spaces', () => {
const space = await network.api.loadSpace(spaceId);
if (!space) return;
+
networksMap.value[networkId].spaces = {
...networksMap.value[networkId].spaces,
[spaceId]: space
From 6583931fcd211842493a34e83bbee2eb21b51ad6 Mon Sep 17 00:00:00 2001
From: Wan Qi Chen <495709+wa0x6e@users.noreply.github.com>
Date: Sat, 25 May 2024 18:26:50 +0400
Subject: [PATCH 092/104] chore: keep same properties order
---
packages/sx.js/src/clients/offchain/types.ts | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/packages/sx.js/src/clients/offchain/types.ts b/packages/sx.js/src/clients/offchain/types.ts
index 6d22e1aec..8b6927628 100644
--- a/packages/sx.js/src/clients/offchain/types.ts
+++ b/packages/sx.js/src/clients/offchain/types.ts
@@ -109,8 +109,8 @@ export type EIP712UnfollowSpaceMessage = {
};
export type EIP712SetAliasMessage = {
- alias: string;
from?: string;
+ alias: string;
timestamp?: number;
};
From 23754078f51bcb1bb234e69b4d0d9676a4ad4754 Mon Sep 17 00:00:00 2001
From: Wan Qi Chen <495709+wa0x6e@users.noreply.github.com>
Date: Sat, 25 May 2024 20:03:56 +0400
Subject: [PATCH 093/104] fix: make `from` arg optional
---
apps/ui/src/networks/offchain/actions.ts | 10 +++++-----
apps/ui/src/networks/types.ts | 4 ++--
2 files changed, 7 insertions(+), 7 deletions(-)
diff --git a/apps/ui/src/networks/offchain/actions.ts b/apps/ui/src/networks/offchain/actions.ts
index 7673d7578..184a68b76 100644
--- a/apps/ui/src/networks/offchain/actions.ts
+++ b/apps/ui/src/networks/offchain/actions.ts
@@ -1,4 +1,4 @@
-import { getAddress, isAddress } from '@ethersproject/address';
+import { isAddress } from '@ethersproject/address';
import {
OffchainNetworkConfig,
clients,
@@ -203,21 +203,21 @@ export function createActions(
};
});
},
- followSpace(web3: Web3Provider | Wallet, networkId: NetworkID, spaceId: string, from: string) {
+ followSpace(web3: Web3Provider | Wallet, networkId: NetworkID, spaceId: string, from?: string) {
return client.followSpace({
signer: web3 instanceof Web3Provider ? web3.getSigner() : web3,
- data: { network: networkId, space: spaceId, from: from || getAddress(from) }
+ data: { network: networkId, space: spaceId, ...(from ? { from } : {}) }
});
},
unfollowSpace(
web3: Web3Provider | Wallet,
networkId: NetworkID,
spaceId: string,
- from: string
+ from?: string
) {
return client.unfollowSpace({
signer: web3 instanceof Web3Provider ? web3.getSigner() : web3,
- data: { network: networkId, space: spaceId, from: from || getAddress(from) }
+ data: { network: networkId, space: spaceId, ...(from ? { from } : {}) }
});
},
setAlias(web3: Web3Provider, alias: string) {
diff --git a/apps/ui/src/networks/types.ts b/apps/ui/src/networks/types.ts
index caf2a54b1..0b0bd12bf 100644
--- a/apps/ui/src/networks/types.ts
+++ b/apps/ui/src/networks/types.ts
@@ -127,8 +127,8 @@ export type ReadOnlyNetworkActions = {
proposal: Proposal,
choice: Choice
): Promise;
- followSpace(web3: Web3Provider | Wallet, networkId: NetworkID, spaceId: string, from: string);
- unfollowSpace(web3: Web3Provider | Wallet, networkId: NetworkID, spaceId: string, from: string);
+ followSpace(web3: Web3Provider | Wallet, networkId: NetworkID, spaceId: string, from?: string);
+ unfollowSpace(web3: Web3Provider | Wallet, networkId: NetworkID, spaceId: string, from?: string);
setAlias(web3: Web3Provider, alias: string);
send(envelope: any): Promise;
};
From 969731ee37a8583b280ca5853352f12f197b6325 Mon Sep 17 00:00:00 2001
From: Wan Qi Chen <495709+wa0x6e@users.noreply.github.com>
Date: Sat, 25 May 2024 20:05:37 +0400
Subject: [PATCH 094/104] fix: remove leftover file from conflict merge
---
apps/ui/src/stores/bookmarks.ts | 205 --------------------------------
1 file changed, 205 deletions(-)
delete mode 100644 apps/ui/src/stores/bookmarks.ts
diff --git a/apps/ui/src/stores/bookmarks.ts b/apps/ui/src/stores/bookmarks.ts
deleted file mode 100644
index 11313897d..000000000
--- a/apps/ui/src/stores/bookmarks.ts
+++ /dev/null
@@ -1,205 +0,0 @@
-import { defineStore } from 'pinia';
-import { enabledNetworks, getNetwork, offchainNetworks } from '@/networks';
-import pkg from '../../package.json';
-import { NetworkID, Space } from '@/types';
-
-const offchainNetworkId = offchainNetworks.filter(network => enabledNetworks.includes(network))[0];
-
-function compositeSpaceId(space: Space) {
- return `${space.network}:${space.id}`;
-}
-
-export const useBookmarksStore = defineStore('bookmarks', () => {
- const spacesStore = useSpacesStore();
- const actions = useActions();
- const { web3, authInitiated } = useWeb3();
- const { mixpanel } = useMixpanel();
-
- const spacesData = ref([]);
- const followedSpacesIds = ref([]);
- const followedSpacesLoaded = ref(false);
- const followSpaceLoading = ref(false);
- const starredSpacesIds = useStorage(`${pkg.name}.spaces-starred`, [] as string[]);
- const starredSpacesLoaded = ref(false);
- // Combined list of starred and followed spaces by account, to keep sort order
- const accountsBookmarkedSpacesIds = useStorage(
- `${pkg.name}.spaces-bookmarked`,
- {} as Record
- );
-
- const bookmarksLoaded = computed(() => starredSpacesLoaded.value && followedSpacesLoaded.value);
-
- const bookmarkedSpacesIds = computed(
- () => accountsBookmarkedSpacesIds.value[web3.value.account] || []
- );
-
- const bookmarkedSpacesMap = computed(
- () => new Map(spacesData.value.map(space => [compositeSpaceId(space), space]))
- );
-
- const bookmarkedSpaces = computed({
- get() {
- return (web3.value.account ? bookmarkedSpacesIds.value : starredSpacesIds.value)
- .map(id => bookmarkedSpacesMap.value.get(id))
- .filter(Boolean) as Space[];
- },
- set(spaces: Space[]) {
- starredSpacesIds.value = spaces
- .filter(space => space.network !== offchainNetworkId)
- .map(compositeSpaceId);
-
- accountsBookmarkedSpacesIds.value[web3.value.account] = spaces.map(compositeSpaceId);
- }
- });
-
- function syncBookmarkedSpacesIds(spaceIds: string[], type: 'starred' | 'followed') {
- accountsBookmarkedSpacesIds.value[web3.value.account] = Array.from(
- new Set(
- [...bookmarkedSpacesIds.value, ...spaceIds].filter(
- id =>
- id.startsWith(`${offchainNetworkId}:`) !== (type === 'followed') ||
- spaceIds.includes(id)
- )
- )
- );
- }
-
- async function fetchSpacesData(ids: string[]) {
- if (!ids.length) return;
-
- await spacesStore.fetchSpaces(ids.filter(id => !spacesStore.spacesMap.has(id)));
-
- spacesData.value = [
- ...spacesData.value,
- ...(ids.map(id => spacesStore.spacesMap.get(id)).filter(Boolean) as Space[])
- ];
- }
-
- async function loadFollowedSpaces() {
- const network = getNetwork(offchainNetworkId);
- const followedIds = (await network.api.loadFollows(web3.value.account)).map(follow =>
- compositeSpaceId(follow.space)
- );
- const newIds = followedIds.filter(id => !isFollowed(id));
- followedSpacesIds.value = followedIds;
-
- syncBookmarkedSpacesIds(followedIds, 'followed');
- fetchSpacesData(newIds);
- }
-
- function toggleSpaceStar(id: string) {
- const alreadyStarred = isStarred(id);
-
- if (alreadyStarred) {
- starredSpacesIds.value = starredSpacesIds.value.filter((spaceId: string) => spaceId !== id);
-
- if (web3.value.account)
- accountsBookmarkedSpacesIds.value[web3.value.account] = accountsBookmarkedSpacesIds.value[
- web3.value.account
- ].filter((spaceId: string) => spaceId !== id);
- } else {
- starredSpacesIds.value = [id, ...starredSpacesIds.value];
-
- if (web3.value.account)
- accountsBookmarkedSpacesIds.value[web3.value.account] = [id, ...bookmarkedSpacesIds.value];
- }
-
- mixpanel.track('Set space favorite', {
- space: id,
- favorite: !alreadyStarred
- });
- }
-
- async function toggleSpaceFollow(id: string) {
- const alreadyFollowed = followedSpacesIds.value.includes(id);
- const [spaceNetwork, spaceId] = id.split(':');
- followSpaceLoading.value = true;
-
- try {
- if (alreadyFollowed) {
- const result = await actions.unfollowSpace(spaceNetwork as NetworkID, spaceId);
- if (!result) return;
-
- followedSpacesIds.value = followedSpacesIds.value.filter(
- (spaceId: string) => spaceId !== id
- );
- accountsBookmarkedSpacesIds.value[web3.value.account] = accountsBookmarkedSpacesIds.value[
- web3.value.account
- ].filter((spaceId: string) => spaceId !== id);
- } else {
- const result = await actions.followSpace(spaceNetwork as NetworkID, spaceId);
- if (!result) return;
-
- fetchSpacesData([id]);
-
- followedSpacesIds.value = [id, ...followedSpacesIds.value];
- accountsBookmarkedSpacesIds.value[web3.value.account] = [id, ...bookmarkedSpacesIds.value];
- }
- } catch (e) {
- } finally {
- followSpaceLoading.value = false;
- }
- }
-
- function isStarred(spaceId: string) {
- return starredSpacesIds.value.includes(spaceId);
- }
-
- function isFollowed(spaceId: string) {
- return followedSpacesIds.value.includes(spaceId);
- }
-
- watch(
- starredSpacesIds,
- async (currentIds, previousIds) => {
- if (web3.value.account) syncBookmarkedSpacesIds(currentIds, 'starred');
-
- const newIds = !previousIds
- ? currentIds
- : currentIds.filter(
- (id: string) => !previousIds.includes(id) && !bookmarkedSpacesMap.value.has(id)
- );
-
- await fetchSpacesData(newIds);
-
- starredSpacesLoaded.value = true;
- },
- { immediate: true }
- );
-
- watch(
- [
- () => web3.value.account,
- () => web3.value.type,
- () => web3.value.authLoading,
- () => authInitiated.value
- ],
- async ([web3, type, authLoading, authInitiated]) => {
- if (!authInitiated || authLoading) return;
-
- if (!web3) {
- followedSpacesIds.value = [];
- followedSpacesLoaded.value = true;
- return;
- }
-
- if (type !== 'argentx') await loadFollowedSpaces();
-
- followedSpacesLoaded.value = true;
- },
- { immediate: true }
- );
-
- return {
- bookmarkedSpaces,
- bookmarksLoaded,
- starredSpacesLoaded,
- followedSpacesIds,
- followedSpacesLoaded,
- followSpaceLoading,
- isStarred,
- isFollowed,
- toggleSpaceStar,
- toggleSpaceFollow
- };
-});
From 202019082b5d4066c36e528dc703873568c105b2 Mon Sep 17 00:00:00 2001
From: Wan Qi Chen <495709+wa0x6e@users.noreply.github.com>
Date: Sat, 25 May 2024 20:11:34 +0400
Subject: [PATCH 095/104] refactor: improve naming
---
apps/ui/src/composables/useAlias.ts | 12 ++++++------
1 file changed, 6 insertions(+), 6 deletions(-)
diff --git a/apps/ui/src/composables/useAlias.ts b/apps/ui/src/composables/useAlias.ts
index 666467465..29214a1ed 100644
--- a/apps/ui/src/composables/useAlias.ts
+++ b/apps/ui/src/composables/useAlias.ts
@@ -1,7 +1,7 @@
import { Wallet } from '@ethersproject/wallet';
import { getDefaultProvider } from '@ethersproject/providers';
-import pkg from '../../package.json';
import { enabledNetworks, getNetwork, offchainNetworks } from '@/networks';
+import pkg from '../../package.json';
const aliases = useStorage(`${pkg.name}.aliases`, {} as Record);
@@ -13,17 +13,17 @@ export function useAlias() {
const wallet = computed(() => {
const provider = getDefaultProvider();
- const pk = aliases.value[web3.value.account];
+ const privateKey = aliases.value[web3.value.account];
- if (!pk) return null;
+ if (!privateKey) return null;
- return new Wallet(pk, provider);
+ return new Wallet(privateKey, provider);
});
- async function create(actionFn: (address: string) => Promise) {
+ async function create(networkCreateActionFn: (address: string) => Promise) {
const newAliasWallet = Wallet.createRandom();
- await actionFn(newAliasWallet.address);
+ await networkCreateActionFn(newAliasWallet.address);
aliases.value = {
...aliases.value,
From b7530e50bfc03815c3ac41ac1cee505a90c20d3e Mon Sep 17 00:00:00 2001
From: Wan Qi Chen <495709+wa0x6e@users.noreply.github.com>
Date: Sat, 25 May 2024 20:22:15 +0400
Subject: [PATCH 096/104] refactor: hard code alias to offchain network
---
apps/ui/src/composables/useActions.ts | 15 +++++----------
1 file changed, 5 insertions(+), 10 deletions(-)
diff --git a/apps/ui/src/composables/useActions.ts b/apps/ui/src/composables/useActions.ts
index 9bec69e01..5b1eea9c7 100644
--- a/apps/ui/src/composables/useActions.ts
+++ b/apps/ui/src/composables/useActions.ts
@@ -108,12 +108,12 @@ export function useActions() {
modalAccountOpen.value = true;
}
- async function aliasableSigner(networkId: NetworkID): Promise {
- const network = getNetwork(networkId);
+ async function aliasableSigner(): Promise {
+ const network = getNetwork(offchainNetworkId);
if (!(await alias.isValid())) {
await alias.create(address =>
- wrapPromise(networkId, network.actions.setAlias(auth.web3, address))
+ wrapPromise(offchainNetworkId, network.actions.setAlias(auth.web3, address))
);
}
@@ -520,12 +520,7 @@ export function useActions() {
try {
await wrapPromise(
offchainNetworkId,
- network.actions.followSpace(
- await aliasableSigner(networkId),
- networkId,
- spaceId,
- web3.value.account
- )
+ network.actions.followSpace(await aliasableSigner(), networkId, spaceId, web3.value.account)
);
} catch (e) {
uiStore.addNotification('error', e.message);
@@ -547,7 +542,7 @@ export function useActions() {
await wrapPromise(
offchainNetworkId,
network.actions.unfollowSpace(
- await aliasableSigner(networkId),
+ await aliasableSigner(),
networkId,
spaceId,
web3.value.account
From 1c099aaee5456804338ffc07704c91d8f18b2496 Mon Sep 17 00:00:00 2001
From: Wan Qi Chen <495709+wa0x6e@users.noreply.github.com>
Date: Sun, 26 May 2024 03:12:07 +0400
Subject: [PATCH 097/104] fix: use only recently created alias
---
apps/ui/src/composables/useAlias.ts | 6 +++++-
apps/ui/src/networks/offchain/api/index.ts | 9 +++++++--
apps/ui/src/networks/offchain/api/queries.ts | 4 ++--
apps/ui/src/networks/types.ts | 2 +-
4 files changed, 15 insertions(+), 6 deletions(-)
diff --git a/apps/ui/src/composables/useAlias.ts b/apps/ui/src/composables/useAlias.ts
index 29214a1ed..88c57dfa8 100644
--- a/apps/ui/src/composables/useAlias.ts
+++ b/apps/ui/src/composables/useAlias.ts
@@ -36,7 +36,11 @@ export function useAlias() {
async function isValid() {
if (!wallet.value) return false;
- const registeredAlias = await network.api.loadAlias(web3.value.account, wallet.value.address);
+ const registeredAlias = await network.api.loadAlias(
+ web3.value.account,
+ wallet.value.address,
+ Math.floor(Date.now() / 1000) - 60 * 60 * 24 * 30
+ );
if (!registeredAlias) return false;
diff --git a/apps/ui/src/networks/offchain/api/index.ts b/apps/ui/src/networks/offchain/api/index.ts
index 131c0edae..d40216d2c 100644
--- a/apps/ui/src/networks/offchain/api/index.ts
+++ b/apps/ui/src/networks/offchain/api/index.ts
@@ -373,14 +373,19 @@ export function createApi(uri: string, networkId: NetworkID): NetworkApi {
return follows.map(follow => ({ ...follow, space: { ...follow.space, network: networkId } }));
},
- loadAlias: async (address: string, aliasAddress: string): Promise => {
+ loadAlias: async (
+ address: string,
+ aliasAddress: string,
+ created_gt: number
+ ): Promise => {
const {
data: { aliases }
}: { data: { aliases: Alias[] } } = await apollo.query({
query: ALIASES_QUERY,
variables: {
address,
- alias: aliasAddress
+ alias: aliasAddress,
+ created_gt
}
});
diff --git a/apps/ui/src/networks/offchain/api/queries.ts b/apps/ui/src/networks/offchain/api/queries.ts
index f777ab07a..5a9030a47 100644
--- a/apps/ui/src/networks/offchain/api/queries.ts
+++ b/apps/ui/src/networks/offchain/api/queries.ts
@@ -185,8 +185,8 @@ export const VOTES_QUERY = gql`
`;
export const ALIASES_QUERY = gql`
- query Aliases($address: String!, $alias: String!) {
- aliases(where: { address: $address, alias: $alias }) {
+ query Aliases($address: String!, $alias: String!, $created_gt: Int) {
+ aliases(where: { address: $address, alias: $alias, created_gt: $created_gt }) {
address
alias
}
diff --git a/apps/ui/src/networks/types.ts b/apps/ui/src/networks/types.ts
index 0b0bd12bf..78ff2f60a 100644
--- a/apps/ui/src/networks/types.ts
+++ b/apps/ui/src/networks/types.ts
@@ -215,7 +215,7 @@ export type NetworkApi = {
sortBy?: 'vote_count-desc' | 'vote_count-asc' | 'proposal_count-desc' | 'proposal_count-asc'
): Promise;
loadFollows(userId?: string, spaceId?: string): Promise;
- loadAlias(address: string, alias: string): Promise;
+ loadAlias(address: string, alias: string, created_gt: number): Promise;
};
export type NetworkConstants = {
From e3be6b238817cc547984d18beaf4b68d2f615dff Mon Sep 17 00:00:00 2001
From: Wan <495709+wa0x6e@users.noreply.github.com>
Date: Mon, 27 May 2024 20:47:33 +0400
Subject: [PATCH 098/104] Update apps/ui/src/networks/offchain/api/index.ts
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Co-authored-by: Wiktor Tkaczyński
---
apps/ui/src/networks/offchain/api/index.ts | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/apps/ui/src/networks/offchain/api/index.ts b/apps/ui/src/networks/offchain/api/index.ts
index d40216d2c..426857e25 100644
--- a/apps/ui/src/networks/offchain/api/index.ts
+++ b/apps/ui/src/networks/offchain/api/index.ts
@@ -389,7 +389,7 @@ export function createApi(uri: string, networkId: NetworkID): NetworkApi {
}
});
- return aliases?.[0];
+ return aliases?.[0] ?? null;
}
};
}
From 975c755056aaac31c34d277fc0c1e2bec8a97d19 Mon Sep 17 00:00:00 2001
From: Wan Qi Chen <495709+wa0x6e@users.noreply.github.com>
Date: Mon, 27 May 2024 20:49:22 +0400
Subject: [PATCH 099/104] refactor: remove redundant check
---
apps/ui/src/composables/useAlias.ts | 7 +------
1 file changed, 1 insertion(+), 6 deletions(-)
diff --git a/apps/ui/src/composables/useAlias.ts b/apps/ui/src/composables/useAlias.ts
index 88c57dfa8..6686bd127 100644
--- a/apps/ui/src/composables/useAlias.ts
+++ b/apps/ui/src/composables/useAlias.ts
@@ -42,12 +42,7 @@ export function useAlias() {
Math.floor(Date.now() / 1000) - 60 * 60 * 24 * 30
);
- if (!registeredAlias) return false;
-
- return (
- registeredAlias.address === web3.value.account &&
- registeredAlias.alias === wallet.value.address
- );
+ return !!registeredAlias;
}
return { wallet, isValid, create };
From f0349eb31f8db3761f994a80258e65758978c88f Mon Sep 17 00:00:00 2001
From: Wan Qi Chen <495709+wa0x6e@users.noreply.github.com>
Date: Tue, 28 May 2024 01:17:05 +0400
Subject: [PATCH 100/104] refactor: use single public function to handle
handling alias
---
apps/ui/package.json | 1 +
apps/ui/src/composables/useActions.ts | 10 +++------
apps/ui/src/composables/useAlias.ts | 30 ++++++++++++++-------------
3 files changed, 20 insertions(+), 21 deletions(-)
diff --git a/apps/ui/package.json b/apps/ui/package.json
index 173fa9c14..b55820f63 100644
--- a/apps/ui/package.json
+++ b/apps/ui/package.json
@@ -34,6 +34,7 @@
"@ethersproject/abstract-signer": "^5.7.0",
"@ethersproject/address": "^5.7.0",
"@ethersproject/bignumber": "^5.7.0",
+ "@ethersproject/bytes": "^5.7.0",
"@ethersproject/constants": "^5.7.0",
"@ethersproject/contracts": "^5.7.0",
"@ethersproject/hash": "^5.7.0",
diff --git a/apps/ui/src/composables/useActions.ts b/apps/ui/src/composables/useActions.ts
index 5b1eea9c7..c5e070ccf 100644
--- a/apps/ui/src/composables/useActions.ts
+++ b/apps/ui/src/composables/useActions.ts
@@ -111,13 +111,9 @@ export function useActions() {
async function aliasableSigner(): Promise {
const network = getNetwork(offchainNetworkId);
- if (!(await alias.isValid())) {
- await alias.create(address =>
- wrapPromise(offchainNetworkId, network.actions.setAlias(auth.web3, address))
- );
- }
-
- return alias.wallet.value || auth.web3;
+ return alias.getAliasWallet(address =>
+ wrapPromise(offchainNetworkId, network.actions.setAlias(auth.web3, address))
+ );
}
async function predictSpaceAddress(networkId: NetworkID, salt: string): Promise {
diff --git a/apps/ui/src/composables/useAlias.ts b/apps/ui/src/composables/useAlias.ts
index 6686bd127..97a9c8387 100644
--- a/apps/ui/src/composables/useAlias.ts
+++ b/apps/ui/src/composables/useAlias.ts
@@ -1,5 +1,6 @@
import { Wallet } from '@ethersproject/wallet';
import { getDefaultProvider } from '@ethersproject/providers';
+import { isHexString } from '@ethersproject/bytes';
import { enabledNetworks, getNetwork, offchainNetworks } from '@/networks';
import pkg from '../../package.json';
@@ -9,17 +10,9 @@ const networkId = offchainNetworks.filter(network => enabledNetworks.includes(ne
const network = getNetwork(networkId);
export function useAlias() {
+ const provider = getDefaultProvider();
const { web3 } = useWeb3();
- const wallet = computed(() => {
- const provider = getDefaultProvider();
- const privateKey = aliases.value[web3.value.account];
-
- if (!privateKey) return null;
-
- return new Wallet(privateKey, provider);
- });
-
async function create(networkCreateActionFn: (address: string) => Promise) {
const newAliasWallet = Wallet.createRandom();
@@ -31,19 +24,28 @@ export function useAlias() {
[web3.value.account]: newAliasWallet.privateKey
}
};
+
+ return new Wallet(newAliasWallet.privateKey, provider);
}
- async function isValid() {
- if (!wallet.value) return false;
+ async function existingAliasWallet(privateKey: string) {
+ if (!isHexString(privateKey)) return null;
const registeredAlias = await network.api.loadAlias(
web3.value.account,
- wallet.value.address,
+ new Wallet(privateKey, provider).address,
Math.floor(Date.now() / 1000) - 60 * 60 * 24 * 30
);
- return !!registeredAlias;
+ return registeredAlias ? new Wallet(privateKey, provider) : null;
+ }
+
+ async function getAliasWallet(networkCreateActionFn: (address: string) => Promise) {
+ return (
+ (await existingAliasWallet(aliases.value[web3.value.account])) ||
+ (await create(networkCreateActionFn))
+ );
}
- return { wallet, isValid, create };
+ return { getAliasWallet };
}
From 9da1199c1360d9eb5c29632fc0df4bc808305df7 Mon Sep 17 00:00:00 2001
From: Wan Qi Chen <495709+wa0x6e@users.noreply.github.com>
Date: Tue, 28 May 2024 01:20:34 +0400
Subject: [PATCH 101/104] refactor: add a buffer to alias expiration time
---
apps/ui/src/composables/useAlias.ts | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)
diff --git a/apps/ui/src/composables/useAlias.ts b/apps/ui/src/composables/useAlias.ts
index 97a9c8387..c14ebd604 100644
--- a/apps/ui/src/composables/useAlias.ts
+++ b/apps/ui/src/composables/useAlias.ts
@@ -4,6 +4,9 @@ import { isHexString } from '@ethersproject/bytes';
import { enabledNetworks, getNetwork, offchainNetworks } from '@/networks';
import pkg from '../../package.json';
+const ALIAS_AVAILABILITY_PERIOD = 60 * 60 * 24 * 30; // 30 days
+const ALIAS_AVAILABILITY_BUFFER = 60 * 5; // 5 minutes
+
const aliases = useStorage(`${pkg.name}.aliases`, {} as Record);
const networkId = offchainNetworks.filter(network => enabledNetworks.includes(network))[0];
@@ -34,7 +37,7 @@ export function useAlias() {
const registeredAlias = await network.api.loadAlias(
web3.value.account,
new Wallet(privateKey, provider).address,
- Math.floor(Date.now() / 1000) - 60 * 60 * 24 * 30
+ Math.floor(Date.now() / 1000) - ALIAS_AVAILABILITY_PERIOD - ALIAS_AVAILABILITY_BUFFER
);
return registeredAlias ? new Wallet(privateKey, provider) : null;
From ca2c6db9330866d6a444ac6cbba506fa400601da Mon Sep 17 00:00:00 2001
From: Wan Qi Chen <495709+wa0x6e@users.noreply.github.com>
Date: Tue, 28 May 2024 01:23:00 +0400
Subject: [PATCH 102/104] fix: fix function signature
---
apps/ui/src/composables/useActions.ts | 13 +++----------
1 file changed, 3 insertions(+), 10 deletions(-)
diff --git a/apps/ui/src/composables/useActions.ts b/apps/ui/src/composables/useActions.ts
index c5e070ccf..784d4c888 100644
--- a/apps/ui/src/composables/useActions.ts
+++ b/apps/ui/src/composables/useActions.ts
@@ -12,9 +12,7 @@ import type {
NetworkID,
VoteType
} from '@/types';
-import type { Web3Provider } from '@ethersproject/providers';
import type { Connector, StrategyConfig } from '@/networks/types';
-import type { Wallet } from '@ethersproject/wallet';
const offchainNetworkId = offchainNetworks.filter(network => enabledNetworks.includes(network))[0];
@@ -108,7 +106,7 @@ export function useActions() {
modalAccountOpen.value = true;
}
- async function aliasableSigner(): Promise {
+ async function aliasSigner() {
const network = getNetwork(offchainNetworkId);
return alias.getAliasWallet(address =>
@@ -516,7 +514,7 @@ export function useActions() {
try {
await wrapPromise(
offchainNetworkId,
- network.actions.followSpace(await aliasableSigner(), networkId, spaceId, web3.value.account)
+ network.actions.followSpace(await aliasSigner(), networkId, spaceId, web3.value.account)
);
} catch (e) {
uiStore.addNotification('error', e.message);
@@ -537,12 +535,7 @@ export function useActions() {
try {
await wrapPromise(
offchainNetworkId,
- network.actions.unfollowSpace(
- await aliasableSigner(),
- networkId,
- spaceId,
- web3.value.account
- )
+ network.actions.unfollowSpace(await aliasSigner(), networkId, spaceId, web3.value.account)
);
} catch (e) {
uiStore.addNotification('error', e.message);
From 9668fb33684c69f9849ad5886d3ecfb1667dddd8 Mon Sep 17 00:00:00 2001
From: Wan Qi Chen <495709+wa0x6e@users.noreply.github.com>
Date: Tue, 28 May 2024 01:25:51 +0400
Subject: [PATCH 103/104] fix: fix buffer computation
---
apps/ui/src/composables/useAlias.ts | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/apps/ui/src/composables/useAlias.ts b/apps/ui/src/composables/useAlias.ts
index c14ebd604..43feb7312 100644
--- a/apps/ui/src/composables/useAlias.ts
+++ b/apps/ui/src/composables/useAlias.ts
@@ -37,7 +37,7 @@ export function useAlias() {
const registeredAlias = await network.api.loadAlias(
web3.value.account,
new Wallet(privateKey, provider).address,
- Math.floor(Date.now() / 1000) - ALIAS_AVAILABILITY_PERIOD - ALIAS_AVAILABILITY_BUFFER
+ Math.floor(Date.now() / 1000) - ALIAS_AVAILABILITY_PERIOD + ALIAS_AVAILABILITY_BUFFER
);
return registeredAlias ? new Wallet(privateKey, provider) : null;
From 882bfd5eca4a5b93677221c97e1f17cf41a9fa77 Mon Sep 17 00:00:00 2001
From: Wan Qi Chen <495709+wa0x6e@users.noreply.github.com>
Date: Tue, 28 May 2024 21:15:58 +0400
Subject: [PATCH 104/104] refactor: variable renaming
---
apps/ui/src/composables/useActions.ts | 11 ++++++++---
apps/ui/src/composables/useAlias.ts | 4 ++--
2 files changed, 10 insertions(+), 5 deletions(-)
diff --git a/apps/ui/src/composables/useActions.ts b/apps/ui/src/composables/useActions.ts
index 784d4c888..337d919d8 100644
--- a/apps/ui/src/composables/useActions.ts
+++ b/apps/ui/src/composables/useActions.ts
@@ -106,7 +106,7 @@ export function useActions() {
modalAccountOpen.value = true;
}
- async function aliasSigner() {
+ async function getAliasSigner() {
const network = getNetwork(offchainNetworkId);
return alias.getAliasWallet(address =>
@@ -514,7 +514,7 @@ export function useActions() {
try {
await wrapPromise(
offchainNetworkId,
- network.actions.followSpace(await aliasSigner(), networkId, spaceId, web3.value.account)
+ network.actions.followSpace(await getAliasSigner(), networkId, spaceId, web3.value.account)
);
} catch (e) {
uiStore.addNotification('error', e.message);
@@ -535,7 +535,12 @@ export function useActions() {
try {
await wrapPromise(
offchainNetworkId,
- network.actions.unfollowSpace(await aliasSigner(), networkId, spaceId, web3.value.account)
+ network.actions.unfollowSpace(
+ await getAliasSigner(),
+ networkId,
+ spaceId,
+ web3.value.account
+ )
);
} catch (e) {
uiStore.addNotification('error', e.message);
diff --git a/apps/ui/src/composables/useAlias.ts b/apps/ui/src/composables/useAlias.ts
index 43feb7312..5d8a30046 100644
--- a/apps/ui/src/composables/useAlias.ts
+++ b/apps/ui/src/composables/useAlias.ts
@@ -31,7 +31,7 @@ export function useAlias() {
return new Wallet(newAliasWallet.privateKey, provider);
}
- async function existingAliasWallet(privateKey: string) {
+ async function getExistingAliasWallet(privateKey: string) {
if (!isHexString(privateKey)) return null;
const registeredAlias = await network.api.loadAlias(
@@ -45,7 +45,7 @@ export function useAlias() {
async function getAliasWallet(networkCreateActionFn: (address: string) => Promise) {
return (
- (await existingAliasWallet(aliases.value[web3.value.account])) ||
+ (await getExistingAliasWallet(aliases.value[web3.value.account])) ||
(await create(networkCreateActionFn))
);
}