-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathwidgets.ts
36 lines (33 loc) · 1.16 KB
/
widgets.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import { DaoWidget, WidgetId } from '@dao-dao/types'
import { DAO_WIDGET_ITEM_NAMESPACE } from './constants'
import { getFilteredDaoItemsByPrefix } from './dao'
/**
* Get the key in the DAO storage items map for a widget item.
*
* @param id `WidgetId` of the widget item.
* @returns The key in the DAO storage items map for the widget item.
*/
export const getWidgetStorageItemKey = (id: WidgetId | string): string =>
DAO_WIDGET_ITEM_NAMESPACE + id
/**
* Get the DAO widgets from the DAO storage items.
*
* @param items The DAO storage items.
* @returns Parsed DAO widgets.
*/
export const getDaoWidgets = (items: Record<string, string>): DaoWidget[] =>
getFilteredDaoItemsByPrefix(items, getWidgetStorageItemKey(''))
.map(([id, widgetJson]): DaoWidget | undefined => {
try {
return {
id,
values: (widgetJson && JSON.parse(widgetJson)) || {},
}
} catch (err) {
// Ignore widget format error but log to console for debugging.
console.error(`Invalid widget JSON: ${widgetJson}`, err)
return
}
})
// Validate widget structure.
.filter((widget): widget is DaoWidget => !!widget)