The frontend/UI server, written in JS using the MarkoJS library
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

96 lines
3.0 KiB

  1. const {logHeaderApi} = require("../../../../../rpdata/api/LogHeader")
  2. module.exports = class {
  3. onCreate(input) {
  4. this.state = {
  5. id: input.log.id,
  6. channelName: input.log.channelName,
  7. eventName: input.log.eventName,
  8. nexts: [],
  9. prevs: [],
  10. loaded: false,
  11. error: null,
  12. }
  13. }
  14. onMount() {
  15. this.timeout = setTimeout(() => {
  16. logHeaderApi.list().then(logs => {
  17. const index = logs.findIndex(l => l.id === this.state.id);
  18. if (index === -1) {
  19. return Promise.reject("This log not found in list.")
  20. }
  21. const log = logs[index]
  22. const characterIds = log.characters.map(c => c.id);
  23. const nexts = []
  24. const prevs = []
  25. let foundGroups = {}
  26. let foundChannel = false
  27. for (let i = index - 1; i >= 0; --i) {
  28. const log2 = logs[i]
  29. const hasEvent = (log.eventName ? log.eventName === log2.eventName : false)
  30. const hasChannel = (log.channelName === log2.channelName)
  31. const characters = log2.findCharacters(...characterIds)
  32. const characterIdsKey = characters.map(c => c.id).sort().join(",")
  33. const suggestion = {log: log2, characters, hasEvent, hasChannel}
  34. if (hasChannel && !foundChannel) {
  35. foundChannel = true
  36. foundGroups[characterIdsKey] = true
  37. nexts.push(suggestion)
  38. } else if (hasEvent) {
  39. foundGroups[characterIdsKey] = true
  40. nexts.push(suggestion)
  41. } else if (nexts.length < 8 && !hasEvent && characters.length > 1 && !foundGroups[characterIdsKey]) {
  42. foundGroups[characterIdsKey] = true
  43. nexts.push(suggestion)
  44. }
  45. }
  46. foundGroups = {}
  47. foundChannel = false
  48. for (let i = index + 1; i < logs.length; ++i) {
  49. const log2 = logs[i]
  50. const hasEvent = (log.eventName ? log.eventName === log2.eventName : false)
  51. const hasChannel = (log.channelName === log2.channelName)
  52. const characters = log2.findCharacters(...characterIds)
  53. const characterIdsKey = characters.map(c => c.id).sort().join(",")
  54. const suggestion = {log: log2, characters, hasEvent, hasChannel}
  55. if (hasChannel && !foundChannel) {
  56. foundChannel = true
  57. foundGroups[characterIdsKey] = true
  58. prevs.push(suggestion)
  59. } else if (hasEvent) {
  60. foundGroups[characterIdsKey] = true
  61. prevs.push(suggestion)
  62. } else if (prevs.length < 8 && !hasEvent && characters.length > 1 && !foundGroups[characterIdsKey]) {
  63. foundGroups[characterIdsKey] = true
  64. prevs.push(suggestion)
  65. }
  66. }
  67. this.state.nexts = nexts;
  68. this.state.prevs = prevs;
  69. this.state.loaded = true;
  70. }).catch(err => {
  71. console.warn("Suggestions load error:", err)
  72. this.suggestionsError = "Failed to load suggestions: " + err.toString()
  73. })
  74. }, 1000)
  75. }
  76. onUnmount() {
  77. clearTimeout(this.timeout)
  78. }
  79. }