git ssb

16+

Dominic / patchbay



Commit 7a1861362de764504e02614b8c8bc5837fdc089d

/calendar: refactor to use live queries, have collected state

mixmix committed on 7/22/2018, 5:39:14 AM
Parent: fa78173db5d80d49155705dbb8f60d90da768df7

Files changed

app/page/calendar.jschanged
app/page/calendar.mcsschanged
app/page/notifications.jschanged
app/page/calendar.jsView
@@ -1,6 +1,6 @@
11 const nest = require('depnest')
2-const { h, Value, computed, Dict, throttle, dictToCollection } = require('mutant')
2 +const { h, Array: MutantArray, Struct, computed, watch, throttle } = require('mutant')
33 const pull = require('pull-stream')
44 const { isMsg } = require('ssb-ref')
55
66 exports.gives = nest('app.page.calendar')
@@ -12,35 +12,23 @@
1212 exports.create = (api) => {
1313 return nest('app.page.calendar', calendarPage)
1414
1515 function calendarPage (location) {
16- const store = Dict({})
17- pull(
18- pullGatherings(2018),
19- pull.drain(
20- ({ key, date }) => store.put(key, date),
21- (err) => {
22- if (err) console.error(err)
23- else console.log('DONE')
24- }
25- )
26- )
16 + const d = new Date()
17 + const state = Struct({
18 + today: new Date(d.getFullYear(), d.getMonth(), d.getDate()),
19 + year: d.getFullYear(),
20 + events: MutantArray([])
21 + })
2722
23 + watch(state.year, year => getEvents(year, state.events))
24 +
2825 return h('CalendarPage', { title: '/calendar' }, [
29- computed(throttle(dictToCollection(store), 150), collection => {
30- const events = collection.map(item => {
31- return {
32- date: item.value,
33- data: { key: item.key }
34- }
35- })
36-
37- return Calendar(events)
38- })
26 + Calendar(state)
3927 ])
4028 }
4129
42- function pullGatherings (year) {
30 + function getEvents (year, events) {
4331 const query = [{
4432 $filter: {
4533 value: {
4634 timestamp: {$gt: Number(new Date(year, 0, 1))}, // ordered by published time
@@ -60,45 +48,49 @@
6048 }]
6149
6250 const opts = {
6351 reverse: false,
52 + live: true,
6453 query
6554 }
6655
67- return pull(
56 + pull(
6857 api.sbot.pull.stream(server => server.query.read(opts)),
58 + pull.filter(m => !m.sync),
6959 pull.filter(r => isMsg(r.key) && Number.isInteger(r.date)),
7060 pull.map(r => {
7161 return { key: r.key, date: new Date(r.date) }
62 + }),
63 + pull.drain(({ key, date }) => {
64 + var target = events.find(ev => ev.data.key === key)
65 + if (target && target.date <= date) events.delete(target)
66 +
67 + events.push({ date, data: { key } })
7268 })
7369 )
7470 }
7571 }
7672
7773 // ////////////////// extract below into a module ///////////////////////
7874
7975 // Thanks to nomand for the inspiration and code (https://github.com/nomand/Letnice),
80-// they formed the foundationf of this work
76 +// they formed the foundation of this work
8177
8278 const MONTHS = [ 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December' ]
8379 const DAYS = [ 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday' ]
8480
85-function Calendar (events) {
81 +function Calendar (state) {
8682 // TODO assert events is an Array of object
8783 // of form { date, data }
8884
89- const year = Value(new Date().getFullYear())
90- const today = new Date(new Date().getFullYear(), new Date().getMonth(), new Date().getDate())
91- // const today = new Date(new Date().getFullYear(), new Date().getMonth(), new Date().getDate() - 1, 0)
92-
93- const root = h('Calendar', [
94- Header(year),
95- h('div.months', computed(year, year => {
96- return MONTHS.map((month, monthIndex) => Month({ month, monthIndex, year, today, events }))
85 + return h('Calendar', [
86 + Header(state.year),
87 + h('div.months', computed(throttle(state, 100), ({ today, year, events }) => {
88 + return MONTHS.map((month, monthIndex) => {
89 + return Month({ month, monthIndex, today, year, events })
90 + })
9791 }))
9892 ])
99-
100- return root
10193 }
10294
10395 function Header (year) {
10496 return h('div.header', [
@@ -106,13 +98,12 @@
10698 year,
10799 h('a', { 'ev-click': () => year.set(year() - 1) }, '-'),
108100 h('a', { 'ev-click': () => year.set(year() + 1) }, '+')
109101 ])
110- // h('p.percentage', yearProgress(year))
111102 ])
112103 }
113104
114-function Month ({ month, monthIndex, year, today, events }) {
105 +function Month ({ month, monthIndex, today, year, events }) {
115106 const monthLength = new Date(year, monthIndex + 1, 0).getDate()
116107 // NOTE Date takes month as a monthIndex i.e. april = 3
117108 // and day = 0 goes back a day
118109 const days = Array(monthLength).fill().map((_, i) => i + 1)
@@ -140,10 +131,13 @@
140131 const eventsOnDay = events.filter(e => {
141132 return e.date >= date && e.date < dateEnd
142133 })
143134
144- return h('CalendarDay', {
145- attributes: { 'data-date': `${year}-${monthIndex + 1}-${day}` },
135 + const opts = {
136 + attributes: {
137 + 'title': `${year}-${monthIndex + 1}-${day}`,
138 + 'data-date': `${year}-${monthIndex + 1}-${day}`
139 + },
146140 style: {
147141 'grid-row': `${weekday} / ${weekday + 1}`,
148142 'grid-column': `${week + 1} / ${week + 2}`
149143 // column moved by 1 to make space for labels
@@ -151,9 +145,18 @@
151145 classList: [
152146 date < today ? '-past' : '-future',
153147 eventsOnDay.length ? '-events' : ''
154148 ]
155- })
149 + }
150 +
151 + if (!eventsOnDay.length) return h('CalendarDay', opts)
152 +
153 + return h('CalendarDay', opts, [
154 + // TODO try a FontAwesome circle
155 + h('div', [
156 + // Math.random() > 0.3 ? h('div') : ''
157 + ])
158 + ])
156159 }
157160 }
158161
159162 function DayName (day, index) {
app/page/calendar.mcssView
@@ -75,13 +75,13 @@
7575 }
7676
7777
7878 CalendarMonth {
79- --day-width: 12px
79 + --day-radius: 6px
8080 --day-border-radius: 2px
81- --day-gap: 2px
81 + --day-gap: 1px
8282
83- width: calc(7 * var(--day-width) + 6 * var(--day-gap))
83 + width: calc(7 * 2 * var(--day-radius) + 6 * var(--day-gap))
8484 /* max of 6 weeks + day labels, with gaps between each */
8585
8686 div.month-name {
8787 font-size: 20px
@@ -99,13 +99,17 @@
9999 }
100100 }
101101
102102 CalendarDay {
103- width: var(--day-width)
104- height: var(--day-width)
103 + width: calc(2 * var(--day-radius))
104 + height: calc(2 * var(--day-radius))
105105
106106 border-radius: var(--day-border-radius)
107107
108 + display: flex
109 + justify-content: center
110 + align-items: center
111 +
108112 -past {
109113 background: hsl(0, 0%, 20%)
110114 }
111115
@@ -113,24 +117,42 @@
113117 background: hsl(0, 0%, 80%)
114118 }
115119
116120 -events {
117- border-radius: var(--day-width)
121 + /* border-radius: var(--day-radius) */
118122 /* background: hsla(277, 57%, 45%, 1) */
119123
124 + div {
125 + background: #fff
126 + width: 6px
127 + height: 6px
128 + border-radius: 8px
129 +
130 + display: flex
131 + justify-content: center
132 + align-items: center
133 +
134 + div {
135 + background: #000
136 + width: 4px
137 + height: 4px
138 + border-radius: 4px
139 + }
140 + }
141 +
120142 -past {
121143 }
122144 }
123145 }
124146
125147 CalendarDayName {
126148 color: hsl(0, 0%, 40%)
127149 font-family: arial
128- font-size: calc(var(--day-width) - 3px)
129- /* line-height: calc(var(--day-width) - 2px) */
150 + font-size: calc(2 * var(--day-radius) - 3px)
151 + /* line-height: calc(2 * var(--day-radius) - 2px) */
130152
131- width: var(--day-width)
132- height: var(--day-width)
153 + width: calc(2 * var(--day-radius))
154 + height: calc(2 * var(--day-radius))
133155
134156 display: flex
135157 align-items: center
136158 justify-content: center
app/page/notifications.jsView
@@ -43,17 +43,17 @@
4343 function draw () {
4444 resetFeed({ container, content })
4545
4646 pull(
47- next(api.feed.pull.mentions(id), {old: false, limit: 100}),
47 + next(api.feed.pull.mentions(id), {old: false, limit: 100, property: ['timestamp']}),
4848 removeMyMessages(),
4949 removePrivateMessages(),
5050 filterDownThrough(),
5151 Scroller(container, content, api.message.html.render, true, false)
5252 )
5353
5454 pull(
55- next(api.feed.pull.mentions(id), {reverse: true, limit: 100, live: false}),
55 + next(api.feed.pull.mentions(id), {reverse: true, limit: 100, live: false, property: ['timestamp']}),
5656 removeMyMessages(),
5757 removePrivateMessages(),
5858 filterUpThrough(),
5959 Scroller(container, content, api.message.html.render, false, false)

Built with git-ssb-web