| 45 | } |
| 46 | |
| 47 | constructor(options = {}) { |
| 48 | this.defaultTheme = defaultTheme |
| 49 | this.useTheme(defaultTheme) |
| 50 | |
| 51 | Object.assign(this, { |
| 52 | id: window.location.href, |
| 53 | title: window.document.title, |
| 54 | link: window.location.href, |
| 55 | desc: '', |
| 56 | labels: [], |
| 57 | theme: defaultTheme, |
| 58 | oauth: {}, |
| 59 | perPage: 20, |
| 60 | maxCommentHeight: 250, |
| 61 | }, options) |
| 62 | |
| 63 | this.useTheme(this.theme) |
| 64 | |
| 65 | const user = {} |
| 66 | try { |
| 67 | const userInfo = localStorage.getItem(LS_USER_KEY) |
| 68 | if (this.accessToken && userInfo) { |
| 69 | Object.assign(user, JSON.parse(userInfo), { |
| 70 | fromCache: true, |
| 71 | }) |
| 72 | } |
| 73 | } catch (e) { |
| 74 | localStorage.removeItem(LS_USER_KEY) |
| 75 | } |
| 76 | |
| 77 | this.state = observable({ |
| 78 | user, |
| 79 | error: null, |
| 80 | meta: {}, |
| 81 | comments: undefined, |
| 82 | reactions: [], |
| 83 | commentReactions: {}, |
| 84 | currentPage: 1, |
| 85 | }) |
| 86 | |
| 87 | const query = Query.parse() |
| 88 | if (query.code) { |
| 89 | const { client_id, client_secret } = this.oauth |
| 90 | const code = query.code |
| 91 | delete query.code |
| 92 | const search = Query.stringify(query) |
| 93 | const replacedUrl = `${window.location.origin}${window.location.pathname}${search}${window.location.hash}` |
| 94 | history.replaceState({}, '', replacedUrl) |
| 95 | |
| 96 | Object.assign(this, { |
| 97 | id: replacedUrl, |
| 98 | link: replacedUrl, |
| 99 | }, options) |
| 100 | |
| 101 | this.state.user.isLoggingIn = true |
| 102 | http.post('https://gh-oauth.imsun.net', { |
| 103 | code, |
| 104 | client_id, |