UNPKG

6.87 kBJavaScriptView Raw
1"use strict";
2var __extends = (this && this.__extends) || (function () {
3 var extendStatics = function (d, b) {
4 extendStatics = Object.setPrototypeOf ||
5 ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6 function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7 return extendStatics(d, b);
8 };
9 return function (d, b) {
10 extendStatics(d, b);
11 function __() { this.constructor = d; }
12 d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
13 };
14})();
15var __assign = (this && this.__assign) || function () {
16 __assign = Object.assign || function(t) {
17 for (var s, i = 1, n = arguments.length; i < n; i++) {
18 s = arguments[i];
19 for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
20 t[p] = s[p];
21 }
22 return t;
23 };
24 return __assign.apply(this, arguments);
25};
26var __read = (this && this.__read) || function (o, n) {
27 var m = typeof Symbol === "function" && o[Symbol.iterator];
28 if (!m) return o;
29 var i = m.call(o), r, ar = [], e;
30 try {
31 while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
32 }
33 catch (error) { e = { error: error }; }
34 finally {
35 try {
36 if (r && !r.done && (m = i["return"])) m.call(i);
37 }
38 finally { if (e) throw e.error; }
39 }
40 return ar;
41};
42var __spread = (this && this.__spread) || function () {
43 for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i]));
44 return ar;
45};
46var __importDefault = (this && this.__importDefault) || function (mod) {
47 return (mod && mod.__esModule) ? mod : { "default": mod };
48};
49Object.defineProperty(exports, "__esModule", { value: true });
50exports.QueryHistory = void 0;
51var graphql_1 = require("graphql");
52var react_1 = __importDefault(require("react"));
53var QueryStore_1 = __importDefault(require("../utility/QueryStore"));
54var HistoryQuery_1 = __importDefault(require("./HistoryQuery"));
55var MAX_QUERY_SIZE = 100000;
56var MAX_HISTORY_LENGTH = 20;
57var shouldSaveQuery = function (query, variables, headers, lastQuerySaved) {
58 if (!query) {
59 return false;
60 }
61 try {
62 graphql_1.parse(query);
63 }
64 catch (e) {
65 return false;
66 }
67 if (query.length > MAX_QUERY_SIZE) {
68 return false;
69 }
70 if (!lastQuerySaved) {
71 return true;
72 }
73 if (JSON.stringify(query) === JSON.stringify(lastQuerySaved.query)) {
74 if (JSON.stringify(variables) === JSON.stringify(lastQuerySaved.variables)) {
75 if (JSON.stringify(headers) === JSON.stringify(lastQuerySaved.headers)) {
76 return false;
77 }
78 if (headers && !lastQuerySaved.headers) {
79 return false;
80 }
81 }
82 if (variables && !lastQuerySaved.variables) {
83 return false;
84 }
85 }
86 return true;
87};
88var QueryHistory = (function (_super) {
89 __extends(QueryHistory, _super);
90 function QueryHistory(props) {
91 var _this = _super.call(this, props) || this;
92 _this.updateHistory = function (query, variables, headers, operationName) {
93 if (shouldSaveQuery(query, variables, headers, _this.historyStore.fetchRecent())) {
94 _this.historyStore.push({
95 query: query,
96 variables: variables,
97 headers: headers,
98 operationName: operationName,
99 });
100 var historyQueries = _this.historyStore.items;
101 var favoriteQueries = _this.favoriteStore.items;
102 var queries = historyQueries.concat(favoriteQueries);
103 _this.setState({
104 queries: queries,
105 });
106 }
107 };
108 _this.toggleFavorite = function (query, variables, headers, operationName, label, favorite) {
109 var item = {
110 query: query,
111 variables: variables,
112 headers: headers,
113 operationName: operationName,
114 label: label,
115 };
116 if (!_this.favoriteStore.contains(item)) {
117 item.favorite = true;
118 _this.favoriteStore.push(item);
119 }
120 else if (favorite) {
121 item.favorite = false;
122 _this.favoriteStore.delete(item);
123 }
124 _this.setState({
125 queries: __spread(_this.historyStore.items, _this.favoriteStore.items),
126 });
127 };
128 _this.editLabel = function (query, variables, headers, operationName, label, favorite) {
129 var item = {
130 query: query,
131 variables: variables,
132 headers: headers,
133 operationName: operationName,
134 label: label,
135 };
136 if (favorite) {
137 _this.favoriteStore.edit(__assign(__assign({}, item), { favorite: favorite }));
138 }
139 else {
140 _this.historyStore.edit(item);
141 }
142 _this.setState({
143 queries: __spread(_this.historyStore.items, _this.favoriteStore.items),
144 });
145 };
146 _this.historyStore = new QueryStore_1.default('queries', props.storage, MAX_HISTORY_LENGTH);
147 _this.favoriteStore = new QueryStore_1.default('favorites', props.storage, null);
148 var historyQueries = _this.historyStore.fetchAll();
149 var favoriteQueries = _this.favoriteStore.fetchAll();
150 var queries = historyQueries.concat(favoriteQueries);
151 _this.state = { queries: queries };
152 return _this;
153 }
154 QueryHistory.prototype.render = function () {
155 var _this = this;
156 var queries = this.state.queries.slice().reverse();
157 var queryNodes = queries.map(function (query, i) {
158 return (react_1.default.createElement(HistoryQuery_1.default, __assign({ handleEditLabel: _this.editLabel, handleToggleFavorite: _this.toggleFavorite, key: i + ":" + (query.label || query.query), onSelect: _this.props.onSelectQuery }, query)));
159 });
160 return (react_1.default.createElement("section", { "aria-label": "History" },
161 react_1.default.createElement("div", { className: "history-title-bar" },
162 react_1.default.createElement("div", { className: "history-title" }, 'History'),
163 react_1.default.createElement("div", { className: "doc-explorer-rhs" }, this.props.children)),
164 react_1.default.createElement("ul", { className: "history-contents" }, queryNodes)));
165 };
166 return QueryHistory;
167}(react_1.default.Component));
168exports.QueryHistory = QueryHistory;
169//# sourceMappingURL=QueryHistory.js.map
\No newline at end of file