File size: 3,198 Bytes
19918ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import server from './server.js'
import Status from './Status.js'

/* global alert $settings $cssSelect $timeline $login $header $server */

function settings (shown) {
  if (shown) {
    $timeline.classList.add('hidden')
    $settings.classList.remove('hidden')
  } else {
    $timeline.classList.remove('hidden')
    $settings.classList.add('hidden')
  }
}

// const accountFromUsername = username => server.

async function showStatusList (header, statuses) {
  if (statuses.error) {
    alert(statuses.error)
    return
  }
  $header.innerHTML = header
  $timeline.replaceChildren()
  for (const statusJson of statuses) {
    const status = Status(statusJson)
    const $article = document.createElement('article')
    $timeline.append($article)
    await status.thread($article)
  }
}

async function showTimeline (header, querySuffix) {
  await showStatusList(header, await server.timeline(querySuffix))
}

async function showAccountTimeline (accountId, querySuffix) {
  await showStatusList(
    'πŸ§‘',
    await server.accountTimeline(accountId, querySuffix)
  )
}

async function hasServer () {
  server.setAuthorizeHref($login)
  server.updateNavButtons()
  settings(false)
  if (!document.location.hash || document.location.hash === '#') {
    document.location.hash = server.isLoggedIn() ? '#home' : '#public'
  } else {
    app()
  }
  settings(false)
}

async function noServer () {
  server.updateNavButtons()
  settings(true)
  $header.innerHTML = ''
}

let appRunning = false
async function app () {
  if (appRunning) {
    return
  }
  appRunning = true

  const codeMatch = document.location.search.match(/code=(.+)$/)
  if (codeMatch) {
    await server.login(codeMatch[1])
    document.location.search = ''
    document.location.hash = '#home'
    appRunning = false
    return
  }
  switch (document.location.hash) {
    case '#home':
      await showTimeline('🏠', 'home?limit=40')
      break
    case '#public':
      await showTimeline('🌐', 'public?limit=40')
      break
    case '#public/local':
      await showTimeline('πŸ§‘πŸ½β€πŸ€β€πŸ§‘πŸ½', 'public?limit=40&local=true')
      break
    case '#settings':
      settings(true)
      break
    default: {
      const hashtagMatch = document.location.hash.match(/#tags\/(.+)$/)
      if (hashtagMatch) {
        const hashtag = hashtagMatch[1]
        await showTimeline(`#${hashtag}`, `tag/${hashtag}?limit=40`)
        break
      }
      const accountMatch = document.location.hash.match(/#accounts\/(.+)$/)
      if (accountMatch) {
        const accountId = accountMatch[1]
        await showAccountTimeline(accountId, 'limit=40')
        break
      }
      console.warn(`Unexpected hash ${document.location.hash}`)
    }
  }
  appRunning = false
}

window.onhashchange = app

if (server.hasHostname()) {
  hasServer()
} else {
  noServer()
}

$server.addEventListener('keyup', async (event) => {
  if (event.key === 'Enter') {
    const hostname = $server.value.trim()
    if (hostname && hostname.match(/[a-z]+\.[a-z]+/)) {
      server.setHostname(hostname)
      await hasServer()
    }
  }
})

$cssSelect.addEventListener('change', (event) => {
  server.setCssTheme()
  settings(false)
})

app()