Second frontend, written in Next.JS + Typescript.
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.

179 lines
3.4 KiB

4 years ago
4 years ago
  1. import { GQLClient, gql } from "../client/graphql";
  2. export async function listLogs(gql: GQLClient, filter?: LogFilter): Promise<LogHeader[]> {
  3. return (await gql.post(LOGS_DOC, {filter}, "ListLogs")).logs;
  4. }
  5. export function logFilterToQueryString(filter: LogFilter): string {
  6. const parts: {k:string, v:string}[] = [];
  7. if (filter.characters != null && filter.characters.length > 0) {
  8. parts.push({k:"characters", v:filter.characters.join(",")})
  9. }
  10. if (filter.channels != null && filter.channels.length > 0) {
  11. parts.push({k:"channels", v:filter.channels.join(",")})
  12. }
  13. if (filter.events != null && filter.events.length > 0) {
  14. parts.push({k:"events", v:filter.events.join(",")})
  15. }
  16. if (filter.search != null && filter.search !== "") {
  17. parts.push({k:"search", v:filter.search})
  18. }
  19. if (filter.open != null) {
  20. parts.push({k:"open", v:filter.open ? "true" : "false"})
  21. }
  22. if (filter.limit != null) {
  23. parts.push({k:"limit", v:filter.limit.toFixed(0)})
  24. } else {
  25. parts.push({k:"limit", v:"100"})
  26. }
  27. if (parts.length === 1 && parts[0].k === "limit" && parts[0].v === "100") {
  28. return "";
  29. }
  30. return "?" + parts.map(p => `${p.k}=${encodeURIComponent(p.v)}`);
  31. }
  32. export function logFilterFromQueryString(qs: string): LogFilter {
  33. const filter: LogFilter = {limit: 100};
  34. const qsParts = (qs.startsWith("?") ? qs.slice(1) : qs).split("&").map(t => t.split("=")).map(kv => ({
  35. k: kv[0],
  36. v: decodeURIComponent(kv[1]),
  37. }));
  38. for (const part of qsParts) {
  39. switch (part.k) {
  40. case "characters": {
  41. filter.characters = part.v.split(",");
  42. break;
  43. }
  44. case "channels": {
  45. filter.channels = part.v.split(",");
  46. break;
  47. }
  48. case "events": {
  49. filter.events = part.v.split(",");
  50. break;
  51. }
  52. case "search": {
  53. filter.search = part.v;
  54. break;
  55. }
  56. case "open": {
  57. filter.open = part.v === "open";
  58. break;
  59. }
  60. case "limit": {
  61. filter.limit = parseInt(part.v) || 0;
  62. break;
  63. }
  64. }
  65. }
  66. return filter;
  67. }
  68. export interface LogFilter {
  69. /** Character IDs */
  70. characters?: string[]
  71. /** Channel names */
  72. channels?: string[]
  73. /** Event names */
  74. events?: string[]
  75. /** Search text */
  76. search?: string
  77. /** Show only open logs */
  78. open?: boolean
  79. /** Limit the result set size. */
  80. limit?: number
  81. }
  82. export interface LogHeader extends LogCommon {
  83. /** Character IDs */
  84. characters: LogHeaderCharacter[]
  85. }
  86. interface LogCommon {
  87. id: string
  88. shortId: string
  89. date: string
  90. channelName: string
  91. title: string
  92. description: string
  93. eventName: string
  94. open: boolean
  95. }
  96. export interface LogHeaderCharacter {
  97. id: string
  98. name: string
  99. shortName: string
  100. author: string
  101. }
  102. const LOGS_DOC = gql`
  103. query ListLogs($filter: LogsFilter) {
  104. logs(filter: $filter) {
  105. ...LogMeta
  106. ...LogMetaCharacters
  107. }
  108. }
  109. query FindLog($id: String!) {
  110. log(id: $id) {
  111. ...LogMeta
  112. ...LogData
  113. }
  114. }
  115. fragment LogMeta on Log {
  116. id
  117. shortId
  118. date
  119. channelName
  120. title
  121. eventName
  122. description
  123. }
  124. fragment LogMetaCharacters on Log {
  125. characters {
  126. id
  127. name
  128. shortName
  129. author
  130. }
  131. }
  132. fragment LogData on Log {
  133. open
  134. channel {
  135. name
  136. logged
  137. hub
  138. eventName
  139. locationName
  140. }
  141. characters {
  142. id
  143. nicks
  144. author
  145. name
  146. shortName
  147. description
  148. }
  149. posts {
  150. id
  151. time
  152. kind
  153. nick
  154. text
  155. position
  156. }
  157. }
  158. `;