/src/autocomplete/DuckDuckGoProvider.tsx

https://github.com/matrix-org/matrix-react-sdk · TypeScript · 109 lines · 83 code · 9 blank · 17 comment · 6 complexity · 9468ff51a43a30fca8fc66e8290369ef MD5 · raw file

  1. /*
  2. Copyright 2016 Aviral Dasgupta
  3. Copyright 2017 Vector Creations Ltd
  4. Copyright 2017, 2018 New Vector Ltd
  5. Licensed under the Apache License, Version 2.0 (the "License");
  6. you may not use this file except in compliance with the License.
  7. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. */
  15. import React from 'react';
  16. import { _t } from '../languageHandler';
  17. import AutocompleteProvider from './AutocompleteProvider';
  18. import {TextualCompletion} from './Components';
  19. import {ICompletion, ISelectionRange} from "./Autocompleter";
  20. const DDG_REGEX = /\/ddg\s+(.+)$/g;
  21. const REFERRER = 'vector';
  22. export default class DuckDuckGoProvider extends AutocompleteProvider {
  23. constructor() {
  24. super(DDG_REGEX);
  25. }
  26. static getQueryUri(query: string) {
  27. return `https://api.duckduckgo.com/?q=${encodeURIComponent(query)}`
  28. + `&format=json&no_redirect=1&no_html=1&t=${encodeURIComponent(REFERRER)}`;
  29. }
  30. async getCompletions(query: string, selection: ISelectionRange, force= false): Promise<ICompletion[]> {
  31. const {command, range} = this.getCurrentCommand(query, selection);
  32. if (!query || !command) {
  33. return [];
  34. }
  35. const response = await fetch(DuckDuckGoProvider.getQueryUri(command[1]), {
  36. method: 'GET',
  37. });
  38. const json = await response.json();
  39. const results = json.Results.map((result) => {
  40. return {
  41. completion: result.Text,
  42. component: (
  43. <TextualCompletion
  44. title={result.Text}
  45. description={result.Result} />
  46. ),
  47. range,
  48. };
  49. });
  50. if (json.Answer) {
  51. results.unshift({
  52. completion: json.Answer,
  53. component: (
  54. <TextualCompletion
  55. title={json.Answer}
  56. description={json.AnswerType} />
  57. ),
  58. range,
  59. });
  60. }
  61. if (json.RelatedTopics && json.RelatedTopics.length > 0) {
  62. results.unshift({
  63. completion: json.RelatedTopics[0].Text,
  64. component: (
  65. <TextualCompletion
  66. title={json.RelatedTopics[0].Text} />
  67. ),
  68. range,
  69. });
  70. }
  71. if (json.AbstractText) {
  72. results.unshift({
  73. completion: json.AbstractText,
  74. component: (
  75. <TextualCompletion
  76. title={json.AbstractText} />
  77. ),
  78. range,
  79. });
  80. }
  81. return results;
  82. }
  83. getName() {
  84. return '🔍 ' + _t('Results from DuckDuckGo');
  85. }
  86. renderCompletions(completions: React.ReactNode[]): React.ReactNode {
  87. return (
  88. <div
  89. className="mx_Autocomplete_Completion_container_block"
  90. role="listbox"
  91. aria-label={_t("DuckDuckGo Results")}
  92. >
  93. { completions }
  94. </div>
  95. );
  96. }
  97. }