From e3aa00cabcef81ae9a4ad76a49ef26451c731d64 Mon Sep 17 00:00:00 2001 From: Liam Fitzgerald Date: Tue, 24 Aug 2021 16:42:30 +1000 Subject: [PATCH 1/4] metadata-store: remove dangling entries upon %initial-group --- pkg/arvo/app/metadata-store.hoon | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/pkg/arvo/app/metadata-store.hoon b/pkg/arvo/app/metadata-store.hoon index 3b9393c276..4e70e9cf07 100644 --- a/pkg/arvo/app/metadata-store.hoon +++ b/pkg/arvo/app/metadata-store.hoon @@ -246,15 +246,32 @@ |= [group=resource =associations:store] =/ assocs=(list [=md-resource:store grp=resource =metadatum:store]) ~(tap by associations) - :- (send-diff:mc %initial-group group associations) + =/ old-resources=(set md-resource:store) (~(get ju group-indices) group) + =/ cur-resources=(set md-resource:store) ~(key by assocs) + =. state (remove-gone group (~(dif in old-resource) cur-resource)) |- ?~ assocs - state + :_ state + (send-diff:mc %initial-group group associations) =, assocs ?> =(group grp.i) =^ cards state (handle-add group [md-resource metadatum]:i) $(assocs t) + :: + ++ remove-gone + |= [group=resource gone=(set md-resource:store) + ^+ state + %+ roll ~(tap in gone) + |= [=md-resource:store out=_state] + %_ out + associations (~(del by associations.out) md-resource) + group-indices (~(del ju group-indices.out) group md-resource) + resource-indices (~(del ju resource-indices.out) md-resource group) + :: + app-indices + (~(del ju app-indices.out) app.md-resource group resource.md-resource) + == -- :: ++ poke-import From 041cd8e95db8eb0148f56a35272610d80b102f01 Mon Sep 17 00:00:00 2001 From: Liam Fitzgerald Date: Tue, 24 Aug 2021 16:57:28 +1000 Subject: [PATCH 2/4] interface: reduce %initial-group correctly --- .../src/logic/reducers/metadata-update.ts | 45 ++++++++++++------- pkg/interface/src/logic/state/metadata.ts | 2 +- pkg/npm/api/metadata/types.ts | 4 +- 3 files changed, 31 insertions(+), 20 deletions(-) diff --git a/pkg/interface/src/logic/reducers/metadata-update.ts b/pkg/interface/src/logic/reducers/metadata-update.ts index 5cf6671afb..958c3e57f3 100644 --- a/pkg/interface/src/logic/reducers/metadata-update.ts +++ b/pkg/interface/src/logic/reducers/metadata-update.ts @@ -1,4 +1,4 @@ -import { MetadataUpdate } from '@urbit/api/metadata'; +import { MetadataUpdate, Associations, ResourceAssociations } from '@urbit/api/metadata'; import _ from 'lodash'; import { Cage } from '~/types/cage'; import { BaseState } from '../state/base'; @@ -12,24 +12,32 @@ export default class MetadataReducer { } } +function normalizeAssociations(assocs: ResourceAssociations): Associations { + return _.reduce(assocs, (acc, val) => { + const appName = val['app-name']; + const rid = val.resource; + const old = acc[appName]; + return { + ...acc, + [appName]: { + ...old, + [rid]: val + } + }; + }, { + groups: {}, + graph: {} + } as Associations); +} + +function removeGroup(group: string, assocs: Associations): Associations { + return _.mapValues(assocs, (val): any => _.omitBy(val, assoc => assoc.group === group)); +} + const associations = (json: MetadataUpdate, state: MetadataState): MetadataState => { const data = _.get(json, 'associations', false); if (data) { - const metadata = state.associations; - Object.keys(data).forEach((key) => { - const val = data[key]; - const appName = val['app-name']; - const rid = val.resource; - if (!(appName in metadata)) { - metadata[appName] = {}; - } - if (!(rid in metadata[appName])) { - metadata[appName][rid] = {}; - } - metadata[appName][rid] = val; - }); - - state.associations = metadata; + state.associations = normalizeAssociations(data); } return state; }; @@ -57,7 +65,10 @@ const add = (json: MetadataUpdate, state: MetadataState): MetadataState => { const groupInitial = (json: MetadataUpdate, state: MetadataState): MetadataState => { const data = _.get(json, 'initial-group', false); if(data) { - associations(data, state); + state.associations = removeGroup(data.group, state.associations); + const { groups, graph } = normalizeAssociations(data.associations); + state.associations.graph = { ...state.associations.graph, ...graph }; + state.associations.groups = { ...state.associations.groups, ...groups }; } return state; }; diff --git a/pkg/interface/src/logic/state/metadata.ts b/pkg/interface/src/logic/state/metadata.ts index 096c2a7919..fc1524bf6d 100644 --- a/pkg/interface/src/logic/state/metadata.ts +++ b/pkg/interface/src/logic/state/metadata.ts @@ -1,4 +1,4 @@ -import { Association, Associations, MetadataUpdatePreview } from '@urbit/api'; +import { Association, Associations, MetadataUpdatePreview } from '@urbit/api/metadata'; import _ from 'lodash'; import { useCallback, useEffect, useState } from 'react'; import { diff --git a/pkg/npm/api/metadata/types.ts b/pkg/npm/api/metadata/types.ts index b2a162aef3..b024bef724 100644 --- a/pkg/npm/api/metadata/types.ts +++ b/pkg/npm/api/metadata/types.ts @@ -1,4 +1,4 @@ -import { AppName, Path, Patp } from '../lib'; +import { Path, Patp } from '../lib'; export type MetadataUpdate = MetadataUpdateInitial @@ -41,7 +41,7 @@ export type MetadataUpdateRemove = { export interface MdResource { resource: string; - 'app-name': AppName; + 'app-name': 'groups' | 'graph'; } export interface MetadataUpdatePreview { From 95b24c8a34ae16909ea7488fddd4d3bb309937dd Mon Sep 17 00:00:00 2001 From: Liam Fitzgerald Date: Fri, 27 Aug 2021 13:16:31 +1000 Subject: [PATCH 3/4] metadata-store: fix compile errors --- pkg/arvo/app/metadata-store.hoon | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/pkg/arvo/app/metadata-store.hoon b/pkg/arvo/app/metadata-store.hoon index 4e70e9cf07..2051d553e5 100644 --- a/pkg/arvo/app/metadata-store.hoon +++ b/pkg/arvo/app/metadata-store.hoon @@ -247,8 +247,8 @@ =/ assocs=(list [=md-resource:store grp=resource =metadatum:store]) ~(tap by associations) =/ old-resources=(set md-resource:store) (~(get ju group-indices) group) - =/ cur-resources=(set md-resource:store) ~(key by assocs) - =. state (remove-gone group (~(dif in old-resource) cur-resource)) + =/ cur-resources=(set md-resource:store) ~(key by associations) + =. state (remove-gone group (~(dif in old-resources) cur-resources)) |- ?~ assocs :_ state @@ -260,17 +260,17 @@ $(assocs t) :: ++ remove-gone - |= [group=resource gone=(set md-resource:store) + |= [group=resource gone=(set md-resource:store)] ^+ state %+ roll ~(tap in gone) |= [=md-resource:store out=_state] %_ out associations (~(del by associations.out) md-resource) group-indices (~(del ju group-indices.out) group md-resource) - resource-indices (~(del ju resource-indices.out) md-resource group) + resource-indices (~(del by resource-indices.out) md-resource) :: app-indices - (~(del ju app-indices.out) app.md-resource group resource.md-resource) + (~(del ju app-indices.out) app-name.md-resource group resource.md-resource) == -- :: From 83f0b4c7942342d4d6f942571ec9b89e5e119d72 Mon Sep 17 00:00:00 2001 From: Liam Fitzgerald Date: Mon, 30 Aug 2021 10:54:09 +1000 Subject: [PATCH 4/4] interface: fix tsc errors --- .../views/landscape/components/GroupifyForm.tsx | 2 +- .../src/views/landscape/components/Resource.tsx | 2 +- .../landscape/components/ResourceSkeleton.tsx | 2 +- .../landscape/components/Sidebar/SidebarItem.tsx | 6 +++--- pkg/npm/api/metadata/lib.ts | 14 +++++++------- pkg/npm/api/metadata/types.ts | 5 ++++- 6 files changed, 17 insertions(+), 14 deletions(-) diff --git a/pkg/interface/src/views/landscape/components/GroupifyForm.tsx b/pkg/interface/src/views/landscape/components/GroupifyForm.tsx index e7769b9209..9690fd20cc 100644 --- a/pkg/interface/src/views/landscape/components/GroupifyForm.tsx +++ b/pkg/interface/src/views/landscape/components/GroupifyForm.tsx @@ -36,7 +36,7 @@ export function GroupifyForm(props: GroupifyFormProps) { name, values.group?.toString() || undefined )); - let mod = association['app-name']; + let mod: string = association['app-name']; if (association?.metadata?.config && 'graph' in association.metadata.config) { mod = association.metadata.config.graph as AppName; } diff --git a/pkg/interface/src/views/landscape/components/Resource.tsx b/pkg/interface/src/views/landscape/components/Resource.tsx index 59f3149c72..eb32cb6740 100644 --- a/pkg/interface/src/views/landscape/components/Resource.tsx +++ b/pkg/interface/src/views/landscape/components/Resource.tsx @@ -26,7 +26,7 @@ export function Resource(props: ResourceProps): ReactElement { const notificationsCount = useHarkState(state => state.notificationsCount); const associations = useMetadataState(state => state.associations); const contacts = useContactState(state => state.contacts); - let app = association['app-name']; + let app: string = association['app-name']; if (association?.metadata?.config && 'graph' in association.metadata.config) { app = association.metadata.config.graph as AppName; } diff --git a/pkg/interface/src/views/landscape/components/ResourceSkeleton.tsx b/pkg/interface/src/views/landscape/components/ResourceSkeleton.tsx index 1cfc82eb2b..1429e02bc4 100644 --- a/pkg/interface/src/views/landscape/components/ResourceSkeleton.tsx +++ b/pkg/interface/src/views/landscape/components/ResourceSkeleton.tsx @@ -65,7 +65,7 @@ type ResourceSkeletonProps = { export function ResourceSkeleton(props: ResourceSkeletonProps): ReactElement { const { association, baseUrl, children } = props; - let app = association['app-name']; + let app: string = association['app-name']; if (association?.metadata?.config && 'graph' in association.metadata.config) { app = association.metadata.config.graph as AppName; } diff --git a/pkg/interface/src/views/landscape/components/Sidebar/SidebarItem.tsx b/pkg/interface/src/views/landscape/components/Sidebar/SidebarItem.tsx index 91be6a0648..d50417d6e6 100644 --- a/pkg/interface/src/views/landscape/components/Sidebar/SidebarItem.tsx +++ b/pkg/interface/src/views/landscape/components/Sidebar/SidebarItem.tsx @@ -2,7 +2,7 @@ import _ from 'lodash'; import React, { useRef, ReactNode } from 'react'; import urbitOb from 'urbit-ob'; import { Icon, Row, Box, Text, BaseImage } from '@tlon/indigo-react'; -import { Association, cite, AppName } from '@urbit/api'; +import { Association, cite } from '@urbit/api'; import { HoverBoxLink } from '~/views/components/HoverBox'; import { Sigil } from '~/logic/lib/sigil'; import { useTutorialModal } from '~/views/components/useTutorialModal'; @@ -176,9 +176,9 @@ export const SidebarAssociationItem = React.memo((props: { const { association, selected } = props; const title = getItemTitle(association) || ''; const appName = association?.['app-name']; - let mod = appName; + let mod: string = appName; if (association?.metadata?.config && 'graph' in association.metadata.config) { - mod = association.metadata.config.graph as AppName; + mod = association.metadata.config.graph ; } const rid = association?.resource; const groupPath = association?.group; diff --git a/pkg/npm/api/metadata/lib.ts b/pkg/npm/api/metadata/lib.ts index 2d98a76af9..ace81e8a31 100644 --- a/pkg/npm/api/metadata/lib.ts +++ b/pkg/npm/api/metadata/lib.ts @@ -1,5 +1,5 @@ -import { AppName, Path, Poke, uxToHex, PatpNoSig } from '../lib'; -import { Association, Metadata, MetadataUpdate, MetadataUpdateAdd, MetadataUpdateRemove, MetadataEditField, MetadataUpdateEdit } from './types'; +import { Path, Poke, uxToHex, PatpNoSig } from '../lib'; +import { MdAppName, Association, Metadata, MetadataUpdate, MetadataUpdateAdd, MetadataUpdateRemove, MetadataEditField, MetadataUpdateEdit } from './types'; export const METADATA_UPDATE_VERSION = 2; @@ -11,7 +11,7 @@ export const metadataAction = (data: T, version: numbe export const add = ( ship: PatpNoSig, - appName: AppName, + appName: MdAppName, resource: Path, group: Path, title: string, @@ -44,10 +44,10 @@ export const add = ( export { add as metadataAdd }; export const remove = ( - appName: AppName, + appName: MdAppName, resource: string, group: string -): Poke => metadataAction({ +): Poke => metadataAction({ remove: { group, resource: { @@ -62,7 +62,7 @@ export { remove as metadataRemove }; export const edit = ( association: Association, edit: MetadataEditField -): Poke => metadataAction({ +): Poke => metadataAction({ edit: { group: association.group, resource: { @@ -84,7 +84,7 @@ export const update = ( ): Poke => { const metadata = { ...association.metadata, ...newMetadata }; metadata.color = uxToHex(metadata.color); - return metadataAction({ + return metadataAction({ add: { group: association.group, resource: { diff --git a/pkg/npm/api/metadata/types.ts b/pkg/npm/api/metadata/types.ts index b024bef724..828c83b1f6 100644 --- a/pkg/npm/api/metadata/types.ts +++ b/pkg/npm/api/metadata/types.ts @@ -1,5 +1,7 @@ import { Path, Patp } from '../lib'; +export type MdAppName = 'groups' | 'graph'; + export type MetadataUpdate = MetadataUpdateInitial | MetadataUpdateAdd @@ -26,6 +28,7 @@ export type MetadataUpdateUpdate = { export interface MetadataUpdateEdit { edit: { resource: MdResource; + group: string; edit: MetadataEditField; } } @@ -41,7 +44,7 @@ export type MetadataUpdateRemove = { export interface MdResource { resource: string; - 'app-name': 'groups' | 'graph'; + 'app-name': MdAppName; } export interface MetadataUpdatePreview {