@codual/react-native-ui 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/NOTICE +2 -0
- package/README.md +18 -2
- package/dist/announce.d.ts +2 -0
- package/dist/announce.js +8 -0
- package/dist/charts/data.d.ts +32 -0
- package/dist/charts/data.js +13 -0
- package/dist/charts/labels.d.ts +54 -0
- package/dist/charts/labels.js +91 -0
- package/dist/charts/machine.d.ts +62 -0
- package/dist/charts/machine.js +144 -0
- package/dist/charts/points.d.ts +23 -0
- package/dist/charts/points.js +46 -0
- package/dist/charts/summary.d.ts +29 -0
- package/dist/charts/summary.js +45 -0
- package/dist/charts/use-chart-interaction.d.ts +62 -0
- package/dist/charts/use-chart-interaction.js +79 -0
- package/dist/forms/async-checks.d.ts +34 -0
- package/dist/forms/async-checks.js +77 -0
- package/dist/forms/field-errors.d.ts +8 -0
- package/dist/forms/field-errors.js +16 -0
- package/dist/forms/form-types.d.ts +43 -0
- package/dist/forms/form-types.js +2 -0
- package/dist/forms/machine.d.ts +63 -0
- package/dist/forms/machine.js +251 -0
- package/dist/forms/numbers.d.ts +5 -0
- package/dist/forms/numbers.js +83 -0
- package/dist/forms/rules.d.ts +47 -0
- package/dist/forms/rules.js +72 -0
- package/dist/forms/server-errors.d.ts +17 -0
- package/dist/forms/server-errors.js +22 -0
- package/dist/forms/use-form.d.ts +49 -0
- package/dist/forms/use-form.js +61 -0
- package/dist/index.d.ts +50 -0
- package/dist/index.js +94 -0
- package/dist/lists/PagedList.d.ts +47 -0
- package/dist/lists/PagedList.js +81 -0
- package/dist/lists/controller.d.ts +23 -0
- package/dist/lists/controller.js +20 -0
- package/dist/lists/inverted-list.d.ts +23 -0
- package/dist/lists/inverted-list.js +60 -0
- package/dist/lists/machine.d.ts +81 -0
- package/dist/lists/machine.js +252 -0
- package/dist/lists/newest-end.d.ts +30 -0
- package/dist/lists/newest-end.js +58 -0
- package/dist/lists/paged-content.d.ts +46 -0
- package/dist/lists/paged-content.js +65 -0
- package/dist/lists/paged-list-types.d.ts +56 -0
- package/dist/lists/paged-list-types.js +2 -0
- package/dist/lists/system-footer.d.ts +28 -0
- package/dist/lists/system-footer.js +50 -0
- package/dist/lists/use-paged-list.d.ts +25 -0
- package/dist/lists/use-paged-list.js +34 -0
- package/dist/permissions/access.d.ts +30 -0
- package/dist/permissions/access.js +2 -0
- package/dist/permissions/android-permission.d.ts +52 -0
- package/dist/permissions/android-permission.js +143 -0
- package/dist/permissions/permission.d.ts +69 -0
- package/dist/permissions/permission.js +201 -0
- package/dist/permissions/setup.d.ts +25 -0
- package/dist/permissions/setup.js +41 -0
- package/dist/permissions/status.d.ts +29 -0
- package/dist/permissions/status.js +18 -0
- package/dist/permissions/use-permission.d.ts +23 -0
- package/dist/permissions/use-permission.js +29 -0
- package/dist/purchases/Paywall.d.ts +27 -0
- package/dist/purchases/Paywall.js +28 -0
- package/dist/purchases/paywall-content.d.ts +20 -0
- package/dist/purchases/paywall-content.js +34 -0
- package/dist/purchases/purchases.d.ts +92 -0
- package/dist/purchases/purchases.js +236 -0
- package/dist/purchases/status.d.ts +62 -0
- package/dist/purchases/status.js +25 -0
- package/dist/purchases/store.d.ts +71 -0
- package/dist/purchases/store.js +2 -0
- package/dist/purchases/system-views.d.ts +49 -0
- package/dist/purchases/system-views.js +91 -0
- package/dist/purchases/use-purchases.d.ts +3 -0
- package/dist/purchases/use-purchases.js +8 -0
- package/dist/react-native-iap/iap-purchases.d.ts +24 -0
- package/dist/react-native-iap/iap-purchases.js +90 -0
- package/dist/react-native-iap/iap-store.d.ts +56 -0
- package/dist/react-native-iap/iap-store.js +249 -0
- package/dist/react-native-iap/iap-types.d.ts +12 -0
- package/dist/react-native-iap/iap-types.js +11 -0
- package/dist/react-native-iap/index.d.ts +31 -0
- package/dist/react-native-iap/index.js +24 -0
- package/dist/react-native-permissions/index.d.ts +24 -0
- package/dist/react-native-permissions/index.js +88 -0
- package/dist/screen-states/ScreenStates.d.ts +38 -0
- package/dist/screen-states/ScreenStates.js +69 -0
- package/dist/screen-states/errors.d.ts +9 -0
- package/dist/screen-states/errors.js +24 -0
- package/dist/screen-states/machine.d.ts +97 -0
- package/dist/screen-states/machine.js +172 -0
- package/dist/screen-states/system-views.d.ts +40 -0
- package/dist/screen-states/system-views.js +66 -0
- package/dist/screen-states/use-screen-states.d.ts +23 -0
- package/dist/screen-states/use-screen-states.js +41 -0
- package/dist/search/SearchResults.d.ts +22 -0
- package/dist/search/SearchResults.js +25 -0
- package/dist/search/announcements.d.ts +17 -0
- package/dist/search/announcements.js +31 -0
- package/dist/search/labels.d.ts +15 -0
- package/dist/search/labels.js +14 -0
- package/dist/search/machine.d.ts +65 -0
- package/dist/search/machine.js +241 -0
- package/dist/search/recent-search-sync.d.ts +32 -0
- package/dist/search/recent-search-sync.js +87 -0
- package/dist/search/recent-searches.d.ts +22 -0
- package/dist/search/recent-searches.js +40 -0
- package/dist/search/system-views.d.ts +11 -0
- package/dist/search/system-views.js +16 -0
- package/dist/search/types.d.ts +51 -0
- package/dist/search/types.js +2 -0
- package/dist/search/use-search.d.ts +73 -0
- package/dist/search/use-search.js +123 -0
- package/dist/selection/config.d.ts +34 -0
- package/dist/selection/config.js +13 -0
- package/dist/selection/labels.d.ts +19 -0
- package/dist/selection/labels.js +23 -0
- package/dist/selection/machine.d.ts +80 -0
- package/dist/selection/machine.js +274 -0
- package/dist/selection/snapshot.d.ts +54 -0
- package/dist/selection/snapshot.js +45 -0
- package/dist/selection/state.d.ts +25 -0
- package/dist/selection/state.js +24 -0
- package/dist/selection/system-views.d.ts +11 -0
- package/dist/selection/system-views.js +22 -0
- package/dist/selection/use-selection.d.ts +73 -0
- package/dist/selection/use-selection.js +100 -0
- package/dist/sheets/machine.d.ts +101 -0
- package/dist/sheets/machine.js +213 -0
- package/dist/sheets/use-sheet.d.ts +78 -0
- package/dist/sheets/use-sheet.js +103 -0
- package/dist/victory-native/index.d.ts +37 -0
- package/dist/victory-native/index.js +41 -0
- package/package.json +94 -4
- package/react-native-iap/package.json +4 -0
- package/react-native-permissions/package.json +4 -0
- package/victory-native/package.json +4 -0
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isEmptyCollection = isEmptyCollection;
|
|
4
|
+
exports.useScreenStates = useScreenStates;
|
|
5
|
+
const react_1 = require("react");
|
|
6
|
+
const errors_1 = require("./errors");
|
|
7
|
+
const machine_1 = require("./machine");
|
|
8
|
+
/** Whether `value` is a collection with no items. */
|
|
9
|
+
function isEmptyCollection(value) {
|
|
10
|
+
if (Array.isArray(value))
|
|
11
|
+
return value.length === 0;
|
|
12
|
+
return (value instanceof Map || value instanceof Set) && value.size === 0;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* The state of a screen that loads data, for apps that draw the states themselves. Loads on mount, aborts the load
|
|
16
|
+
* on unmount, and always calls the latest `load` and options. Give the component a new `key` to load again for a
|
|
17
|
+
* new input.
|
|
18
|
+
*/
|
|
19
|
+
function useScreenStates(load, options = {}) {
|
|
20
|
+
const source = {
|
|
21
|
+
load,
|
|
22
|
+
isEmpty: options.isEmpty ?? isEmptyCollection,
|
|
23
|
+
isOffline: options.isOffline ?? errors_1.isOfflineError,
|
|
24
|
+
};
|
|
25
|
+
const latest = (0, react_1.useRef)(source);
|
|
26
|
+
(0, react_1.useLayoutEffect)(() => {
|
|
27
|
+
latest.current = source;
|
|
28
|
+
});
|
|
29
|
+
const [machine] = (0, react_1.useState)(() => new machine_1.ScreenStatesMachine(() => latest.current));
|
|
30
|
+
const snapshot = (0, react_1.useSyncExternalStore)(machine.subscribe, machine.getSnapshot);
|
|
31
|
+
(0, react_1.useEffect)(() => {
|
|
32
|
+
machine.appear();
|
|
33
|
+
return machine.disappear;
|
|
34
|
+
}, [machine]);
|
|
35
|
+
return (0, react_1.useMemo)(() => ({
|
|
36
|
+
...snapshot,
|
|
37
|
+
retry: machine.retry,
|
|
38
|
+
refresh: machine.refresh,
|
|
39
|
+
dismissRefreshError: machine.dismissRefreshError,
|
|
40
|
+
}), [snapshot, machine]);
|
|
41
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { ReactNode } from "react";
|
|
2
|
+
import { type PagedListViewProps } from "../lists/PagedList";
|
|
3
|
+
import type { SearchLabels } from "./labels";
|
|
4
|
+
import type { RecentSearches } from "./recent-searches";
|
|
5
|
+
import type { SearchController } from "./use-search";
|
|
6
|
+
export interface SearchResultsProps<Item, Scope> extends Omit<PagedListViewProps<Item>, "list" | "empty" | "labels" | "focusWhenLoaded" | "pullToRefreshProps" | "keyExtractor"> {
|
|
7
|
+
readonly search: SearchController<Item, Scope>;
|
|
8
|
+
/** By default, the search's own `keyExtractor`. */
|
|
9
|
+
readonly keyExtractor?: (item: Item) => string;
|
|
10
|
+
/** While the query is shorter than the minimum. By default, the recent searches with Remove and Clear. */
|
|
11
|
+
readonly suggestions?: (recent: RecentSearches) => ReactNode;
|
|
12
|
+
/** A query that found nothing, with the query. By default, a system message. */
|
|
13
|
+
readonly noResults?: (query: string) => ReactNode;
|
|
14
|
+
/** Text over `useSearch`'s `labels`, for these views only. */
|
|
15
|
+
readonly labels?: Partial<SearchLabels> & PagedListViewProps<Item>["labels"];
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* The suggestions while the query is shorter than the minimum, then the latest query's results as a paged list in
|
|
19
|
+
* a `FlatList` of the app's rows, with no results, failed and offline views. Scrolling either dismisses the
|
|
20
|
+
* keyboard. Any other prop goes to the `FlatList`.
|
|
21
|
+
*/
|
|
22
|
+
export declare function SearchResults<Item, Scope>(props: SearchResultsProps<Item, Scope>): import("react").JSX.Element;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SearchResults = SearchResults;
|
|
4
|
+
const react_1 = require("react");
|
|
5
|
+
const jsx_runtime_1 = require("react/jsx-runtime");
|
|
6
|
+
const react_native_1 = require("react-native");
|
|
7
|
+
const PagedList_1 = require("../lists/PagedList");
|
|
8
|
+
const system_views_1 = require("./system-views");
|
|
9
|
+
/**
|
|
10
|
+
* The suggestions while the query is shorter than the minimum, then the latest query's results as a paged list in
|
|
11
|
+
* a `FlatList` of the app's rows, with no results, failed and offline views. Scrolling either dismisses the
|
|
12
|
+
* keyboard. Any other prop goes to the `FlatList`.
|
|
13
|
+
*/
|
|
14
|
+
function SearchResults(props) {
|
|
15
|
+
const { search, suggestions, noResults, ...listViewProps } = props;
|
|
16
|
+
const labels = { ...search.labels, ...props.labels };
|
|
17
|
+
const { results } = search;
|
|
18
|
+
if (results === undefined) {
|
|
19
|
+
return ((0, jsx_runtime_1.jsx)(react_native_1.ScrollView, { testID: "search-suggestions", ...search.listProps, children: suggestions ? (suggestions(search.recent)) : ((0, jsx_runtime_1.jsx)(system_views_1.SystemRecentSearches, { recent: search.recent, labels: labels })) }));
|
|
20
|
+
}
|
|
21
|
+
const { query } = results;
|
|
22
|
+
return ((0, react_1.createElement)(PagedList_1.PagedListView, { ...search.listProps, ...listViewProps, key: results.key, list: results.list, keyExtractor: props.keyExtractor ?? search.keyExtractor, labels: labels, pullToRefreshProps: search.listProps,
|
|
23
|
+
// The search field keeps focus while results arrive; the announcement tells the screen reader about them.
|
|
24
|
+
focusWhenLoaded: "stay", empty: () => (noResults ? noResults(query) : (0, jsx_runtime_1.jsx)(system_views_1.SystemNoResultsView, { query: query, labels: labels })) }));
|
|
25
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { ScreenState } from "../screen-states/machine";
|
|
2
|
+
import type { SearchLabels } from "./labels";
|
|
3
|
+
/** What a screen reader hears when the latest query's first page arrives or fails. */
|
|
4
|
+
export type SearchAnnouncement = {
|
|
5
|
+
readonly kind: "results";
|
|
6
|
+
readonly count: number;
|
|
7
|
+
} | {
|
|
8
|
+
readonly kind: "noResults";
|
|
9
|
+
readonly query: string;
|
|
10
|
+
} | {
|
|
11
|
+
readonly kind: "failed";
|
|
12
|
+
} | {
|
|
13
|
+
readonly kind: "offline";
|
|
14
|
+
};
|
|
15
|
+
export declare function searchAnnouncementText(announcement: SearchAnnouncement, labels: SearchLabels): string;
|
|
16
|
+
/** What to announce for a first page that has stopped loading, or `undefined` while it loads. */
|
|
17
|
+
export declare function announcementFor(state: ScreenState<readonly unknown[]>, query: string): SearchAnnouncement | undefined;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.searchAnnouncementText = searchAnnouncementText;
|
|
4
|
+
exports.announcementFor = announcementFor;
|
|
5
|
+
function searchAnnouncementText(announcement, labels) {
|
|
6
|
+
switch (announcement.kind) {
|
|
7
|
+
case "results":
|
|
8
|
+
return labels.showingResults(announcement.count);
|
|
9
|
+
case "noResults":
|
|
10
|
+
return labels.noResultsAnnouncement(announcement.query);
|
|
11
|
+
case "failed":
|
|
12
|
+
return labels.searchFailed;
|
|
13
|
+
case "offline":
|
|
14
|
+
return labels.searchOffline;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
/** What to announce for a first page that has stopped loading, or `undefined` while it loads. */
|
|
18
|
+
function announcementFor(state, query) {
|
|
19
|
+
switch (state.status) {
|
|
20
|
+
case "idle":
|
|
21
|
+
case "loading":
|
|
22
|
+
return undefined;
|
|
23
|
+
case "content":
|
|
24
|
+
return { kind: "results", count: state.value.length };
|
|
25
|
+
case "empty":
|
|
26
|
+
return { kind: "noResults", query };
|
|
27
|
+
case "failed":
|
|
28
|
+
case "offline":
|
|
29
|
+
return { kind: state.status };
|
|
30
|
+
}
|
|
31
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/** The text of the views `SearchResults` shows for the ones an app leaves out, and of the announcements. */
|
|
2
|
+
export interface SearchLabels {
|
|
3
|
+
readonly recentSearches: string;
|
|
4
|
+
readonly clear: string;
|
|
5
|
+
/** The label of a recent search's Remove button: "Remove “cat”". */
|
|
6
|
+
readonly remove: (query: string) => string;
|
|
7
|
+
readonly noResults: (query: string) => string;
|
|
8
|
+
readonly noResultsMessage: string;
|
|
9
|
+
/** "Showing 20 results.", with the plural your language needs. */
|
|
10
|
+
readonly showingResults: (count: number) => string;
|
|
11
|
+
readonly noResultsAnnouncement: (query: string) => string;
|
|
12
|
+
readonly searchFailed: string;
|
|
13
|
+
readonly searchOffline: string;
|
|
14
|
+
}
|
|
15
|
+
export declare const defaultSearchLabels: SearchLabels;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.defaultSearchLabels = void 0;
|
|
4
|
+
exports.defaultSearchLabels = {
|
|
5
|
+
recentSearches: "Recent Searches",
|
|
6
|
+
clear: "Clear",
|
|
7
|
+
remove: (query) => `Remove “${query}”`,
|
|
8
|
+
noResults: (query) => `No Results for “${query}”`,
|
|
9
|
+
noResultsMessage: "Check the spelling or try a new search.",
|
|
10
|
+
showingResults: (count) => (count === 1 ? "Showing 1 result." : `Showing ${count} results.`),
|
|
11
|
+
noResultsAnnouncement: (query) => `No results for “${query}”.`,
|
|
12
|
+
searchFailed: "Couldn't load results.",
|
|
13
|
+
searchOffline: "No internet connection.",
|
|
14
|
+
};
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { type RecentSearchStore } from "./recent-searches";
|
|
2
|
+
import type { SearchSnapshot, SearchSource } from "./types";
|
|
3
|
+
/**
|
|
4
|
+
* The state machine behind `useSearch`. A query is searched once typing stops for the debounce, or at once on
|
|
5
|
+
* submit; each query gets its own paged list, and a change makes the old one go away, so its request aborts and its
|
|
6
|
+
* answer is dropped. `source` is read whenever it is needed, so it can change between renders.
|
|
7
|
+
*/
|
|
8
|
+
export declare class SearchMachine<Item, Token, Scope> {
|
|
9
|
+
private readonly source;
|
|
10
|
+
private state;
|
|
11
|
+
private readonly listeners;
|
|
12
|
+
private waiting;
|
|
13
|
+
private localAnnouncement;
|
|
14
|
+
private isOnScreen;
|
|
15
|
+
private readonly recentSearchSync;
|
|
16
|
+
private lastListKey;
|
|
17
|
+
private derived;
|
|
18
|
+
constructor(source: () => SearchSource<Item, Token, Scope>, store: RecentSearchStore, initial: {
|
|
19
|
+
readonly query: string;
|
|
20
|
+
readonly scope: Scope | undefined;
|
|
21
|
+
});
|
|
22
|
+
readonly subscribe: (listener: () => void) => (() => void);
|
|
23
|
+
readonly getSnapshot: () => SearchSnapshot<Item, Scope>;
|
|
24
|
+
/** Starts searching: a query in the field (an initial one, or one that was waiting) is searched at once. */
|
|
25
|
+
readonly appear: () => void;
|
|
26
|
+
/** Aborts the running search, stops the waiting debounce, and drops their results. */
|
|
27
|
+
readonly disappear: () => void;
|
|
28
|
+
readonly setQuery: (query: string) => void;
|
|
29
|
+
/** Searches the current query again at once with the new scope. */
|
|
30
|
+
readonly setScope: (scope: Scope | undefined) => void;
|
|
31
|
+
/**
|
|
32
|
+
* Searches the query at once, skipping the debounce, and saves it to the recent searches: the keyboard's search
|
|
33
|
+
* button, or, with `query`, a recent search or a suggestion picked. Does nothing for a query that is too short.
|
|
34
|
+
*/
|
|
35
|
+
readonly submit: (query?: string) => void;
|
|
36
|
+
/** Clears the query and cancels the search: the Cancel button. */
|
|
37
|
+
readonly cancel: () => void;
|
|
38
|
+
/** Searches the latest query again from the failed or offline view. */
|
|
39
|
+
readonly retry: () => void;
|
|
40
|
+
readonly removeRecentSearch: (query: string) => void;
|
|
41
|
+
readonly clearRecentSearches: () => void;
|
|
42
|
+
/**
|
|
43
|
+
* In local mode, filters the results shown again for items that changed, in the same list, so its scroll position
|
|
44
|
+
* stays. Only typing is announced, so this is not.
|
|
45
|
+
*/
|
|
46
|
+
readonly itemsChanged: () => void;
|
|
47
|
+
private searchableQuery;
|
|
48
|
+
private isShown;
|
|
49
|
+
private queryChanged;
|
|
50
|
+
private startWaiting;
|
|
51
|
+
private cancelWaiting;
|
|
52
|
+
private cancelLocalAnnouncement;
|
|
53
|
+
private showSuggestions;
|
|
54
|
+
private search;
|
|
55
|
+
private loadPage;
|
|
56
|
+
private filtered;
|
|
57
|
+
/** Passes the list's changes on, and announces its first page (or a retry) once it stops loading. */
|
|
58
|
+
private watch;
|
|
59
|
+
/** Local results show at once, so their announcement waits until typing has stopped for the debounce. */
|
|
60
|
+
private announceAfterTheDebounce;
|
|
61
|
+
private dropList;
|
|
62
|
+
private derive;
|
|
63
|
+
private update;
|
|
64
|
+
private emit;
|
|
65
|
+
}
|
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SearchMachine = void 0;
|
|
4
|
+
const controller_1 = require("../lists/controller");
|
|
5
|
+
const machine_1 = require("../lists/machine");
|
|
6
|
+
const machine_2 = require("../screen-states/machine");
|
|
7
|
+
const announcements_1 = require("./announcements");
|
|
8
|
+
const recent_search_sync_1 = require("./recent-search-sync");
|
|
9
|
+
const recent_searches_1 = require("./recent-searches");
|
|
10
|
+
/**
|
|
11
|
+
* The state machine behind `useSearch`. A query is searched once typing stops for the debounce, or at once on
|
|
12
|
+
* submit; each query gets its own paged list, and a change makes the old one go away, so its request aborts and its
|
|
13
|
+
* answer is dropped. `source` is read whenever it is needed, so it can change between renders.
|
|
14
|
+
*/
|
|
15
|
+
class SearchMachine {
|
|
16
|
+
source;
|
|
17
|
+
state;
|
|
18
|
+
listeners = new Set();
|
|
19
|
+
waiting;
|
|
20
|
+
localAnnouncement;
|
|
21
|
+
isOnScreen = false;
|
|
22
|
+
recentSearchSync;
|
|
23
|
+
lastListKey = 0;
|
|
24
|
+
derived;
|
|
25
|
+
constructor(source, store, initial) {
|
|
26
|
+
this.source = source;
|
|
27
|
+
this.recentSearchSync = new recent_search_sync_1.RecentSearchSync(store, () => this.source().maxRecentSearches, (recentSearches) => this.update({ recentSearches }));
|
|
28
|
+
this.state = { ...initial, recentSearches: this.recentSearchSync.searches, active: undefined };
|
|
29
|
+
this.derived = { from: this.state, list: undefined, to: this.derive(undefined) };
|
|
30
|
+
}
|
|
31
|
+
subscribe = (listener) => {
|
|
32
|
+
this.listeners.add(listener);
|
|
33
|
+
return () => this.listeners.delete(listener);
|
|
34
|
+
};
|
|
35
|
+
getSnapshot = () => {
|
|
36
|
+
const list = this.state.active?.machine.getSnapshot();
|
|
37
|
+
if (this.derived.from !== this.state || this.derived.list !== list) {
|
|
38
|
+
this.derived = { from: this.state, list, to: this.derive(list) };
|
|
39
|
+
}
|
|
40
|
+
return this.derived.to;
|
|
41
|
+
};
|
|
42
|
+
/** Starts searching: a query in the field (an initial one, or one that was waiting) is searched at once. */
|
|
43
|
+
appear = () => {
|
|
44
|
+
this.isOnScreen = true;
|
|
45
|
+
const query = this.searchableQuery();
|
|
46
|
+
if (query === undefined)
|
|
47
|
+
return;
|
|
48
|
+
if (this.isShown(query))
|
|
49
|
+
this.state.active?.machine.appear();
|
|
50
|
+
else
|
|
51
|
+
this.search(query);
|
|
52
|
+
};
|
|
53
|
+
/** Aborts the running search, stops the waiting debounce, and drops their results. */
|
|
54
|
+
disappear = () => {
|
|
55
|
+
this.isOnScreen = false;
|
|
56
|
+
this.cancelWaiting();
|
|
57
|
+
this.cancelLocalAnnouncement();
|
|
58
|
+
this.state.active?.machine.disappear();
|
|
59
|
+
};
|
|
60
|
+
setQuery = (query) => {
|
|
61
|
+
if (query === this.state.query)
|
|
62
|
+
return;
|
|
63
|
+
this.update({ query });
|
|
64
|
+
this.queryChanged();
|
|
65
|
+
};
|
|
66
|
+
/** Searches the current query again at once with the new scope. */
|
|
67
|
+
setScope = (scope) => {
|
|
68
|
+
if (Object.is(scope, this.state.scope))
|
|
69
|
+
return;
|
|
70
|
+
this.update({ scope });
|
|
71
|
+
const query = this.searchableQuery();
|
|
72
|
+
if (query === undefined)
|
|
73
|
+
return;
|
|
74
|
+
this.cancelWaiting();
|
|
75
|
+
this.search(query);
|
|
76
|
+
};
|
|
77
|
+
/**
|
|
78
|
+
* Searches the query at once, skipping the debounce, and saves it to the recent searches: the keyboard's search
|
|
79
|
+
* button, or, with `query`, a recent search or a suggestion picked. Does nothing for a query that is too short.
|
|
80
|
+
*/
|
|
81
|
+
submit = (query) => {
|
|
82
|
+
if (query !== undefined)
|
|
83
|
+
this.setQuery(query);
|
|
84
|
+
const searchable = this.searchableQuery();
|
|
85
|
+
if (searchable === undefined)
|
|
86
|
+
return;
|
|
87
|
+
this.recentSearchSync.change((searches) => (0, recent_searches_1.addingRecentSearch)(searchable, searches, this.source().maxRecentSearches));
|
|
88
|
+
this.cancelWaiting();
|
|
89
|
+
const list = this.state.active?.machine;
|
|
90
|
+
if (list === undefined || !this.isShown(searchable)) {
|
|
91
|
+
this.search(searchable);
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
const { status } = list.getSnapshot().state;
|
|
95
|
+
if (status === "failed" || status === "offline")
|
|
96
|
+
list.retry();
|
|
97
|
+
else if (status === "idle")
|
|
98
|
+
list.appear();
|
|
99
|
+
};
|
|
100
|
+
/** Clears the query and cancels the search: the Cancel button. */
|
|
101
|
+
cancel = () => this.setQuery("");
|
|
102
|
+
/** Searches the latest query again from the failed or offline view. */
|
|
103
|
+
retry = () => this.state.active?.machine.retry();
|
|
104
|
+
removeRecentSearch = (query) => this.recentSearchSync.change((searches) => (0, recent_searches_1.removingRecentSearch)(query, searches));
|
|
105
|
+
clearRecentSearches = () => this.recentSearchSync.change(() => []);
|
|
106
|
+
/**
|
|
107
|
+
* In local mode, filters the results shown again for items that changed, in the same list, so its scroll position
|
|
108
|
+
* stays. Only typing is announced, so this is not.
|
|
109
|
+
*/
|
|
110
|
+
itemsChanged = () => {
|
|
111
|
+
const active = this.state.active;
|
|
112
|
+
if (active !== undefined && this.source().mode.kind === "local") {
|
|
113
|
+
active.machine.replaceFirstPage(this.filtered(active.query));
|
|
114
|
+
}
|
|
115
|
+
};
|
|
116
|
+
searchableQuery() {
|
|
117
|
+
const trimmed = this.state.query.trim();
|
|
118
|
+
return trimmed.length >= this.source().minimumLength ? trimmed : undefined;
|
|
119
|
+
}
|
|
120
|
+
isShown(query) {
|
|
121
|
+
const active = this.state.active;
|
|
122
|
+
return active?.query === query && Object.is(active.scope, this.state.scope);
|
|
123
|
+
}
|
|
124
|
+
queryChanged() {
|
|
125
|
+
const query = this.searchableQuery();
|
|
126
|
+
if (query === undefined) {
|
|
127
|
+
this.showSuggestions();
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
if (this.isShown(query)) {
|
|
131
|
+
this.cancelWaiting();
|
|
132
|
+
if (this.isOnScreen)
|
|
133
|
+
this.state.active?.machine.appear();
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
if (this.source().mode.kind === "local") {
|
|
137
|
+
this.search(query);
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
this.state.active?.machine.disappear();
|
|
141
|
+
this.startWaiting();
|
|
142
|
+
}
|
|
143
|
+
startWaiting() {
|
|
144
|
+
this.cancelWaiting();
|
|
145
|
+
this.waiting = setTimeout(() => {
|
|
146
|
+
this.waiting = undefined;
|
|
147
|
+
const query = this.searchableQuery();
|
|
148
|
+
if (query !== undefined)
|
|
149
|
+
this.search(query);
|
|
150
|
+
}, this.source().debounceMs);
|
|
151
|
+
}
|
|
152
|
+
cancelWaiting() {
|
|
153
|
+
clearTimeout(this.waiting);
|
|
154
|
+
this.waiting = undefined;
|
|
155
|
+
}
|
|
156
|
+
cancelLocalAnnouncement() {
|
|
157
|
+
clearTimeout(this.localAnnouncement);
|
|
158
|
+
this.localAnnouncement = undefined;
|
|
159
|
+
}
|
|
160
|
+
showSuggestions() {
|
|
161
|
+
this.cancelWaiting();
|
|
162
|
+
this.dropList();
|
|
163
|
+
this.update({ active: undefined });
|
|
164
|
+
}
|
|
165
|
+
search(query) {
|
|
166
|
+
this.dropList();
|
|
167
|
+
const { scope } = this.state;
|
|
168
|
+
const machine = new machine_1.PagedListMachine(() => ({
|
|
169
|
+
loadPage: (token, signal) => this.loadPage(query, { token, scope, signal }),
|
|
170
|
+
keyExtractor: this.source().keyExtractor,
|
|
171
|
+
isOffline: this.source().isOffline,
|
|
172
|
+
announce: this.source().announcePage,
|
|
173
|
+
}));
|
|
174
|
+
const isLocal = this.source().mode.kind === "local";
|
|
175
|
+
if (isLocal)
|
|
176
|
+
machine.showLoaded(this.filtered(query));
|
|
177
|
+
this.lastListKey += 1;
|
|
178
|
+
const unsubscribe = this.watch(machine, query);
|
|
179
|
+
this.update({ active: { key: this.lastListKey, query, scope, machine, unsubscribe } });
|
|
180
|
+
if (isLocal)
|
|
181
|
+
this.announceAfterTheDebounce(machine.getSnapshot().state, query);
|
|
182
|
+
else if (this.isOnScreen)
|
|
183
|
+
machine.appear();
|
|
184
|
+
}
|
|
185
|
+
loadPage(query, request) {
|
|
186
|
+
const { mode } = this.source();
|
|
187
|
+
// A local list loads only on pull to refresh, which filters the items again.
|
|
188
|
+
return mode.kind === "remote" ? mode.search(query, request) : Promise.resolve(this.filtered(query));
|
|
189
|
+
}
|
|
190
|
+
filtered(query) {
|
|
191
|
+
const { mode } = this.source();
|
|
192
|
+
return { items: mode.kind === "local" ? mode.items.filter((item) => mode.matches(item, query)) : [] };
|
|
193
|
+
}
|
|
194
|
+
/** Passes the list's changes on, and announces its first page (or a retry) once it stops loading. */
|
|
195
|
+
watch(machine, query) {
|
|
196
|
+
let wasLoading = (0, machine_2.isShowingLoading)(machine.getSnapshot().state);
|
|
197
|
+
return machine.subscribe(() => {
|
|
198
|
+
const { state } = machine.getSnapshot();
|
|
199
|
+
const announcement = wasLoading ? (0, announcements_1.announcementFor)(state, query) : undefined;
|
|
200
|
+
wasLoading = (0, machine_2.isShowingLoading)(state);
|
|
201
|
+
if (announcement !== undefined)
|
|
202
|
+
this.source().announce(announcement);
|
|
203
|
+
this.emit();
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
/** Local results show at once, so their announcement waits until typing has stopped for the debounce. */
|
|
207
|
+
announceAfterTheDebounce(state, query) {
|
|
208
|
+
const announcement = (0, announcements_1.announcementFor)(state, query);
|
|
209
|
+
if (announcement === undefined)
|
|
210
|
+
return;
|
|
211
|
+
this.localAnnouncement = setTimeout(() => this.source().announce(announcement), this.source().debounceMs);
|
|
212
|
+
}
|
|
213
|
+
dropList() {
|
|
214
|
+
this.cancelLocalAnnouncement();
|
|
215
|
+
const active = this.state.active;
|
|
216
|
+
active?.unsubscribe();
|
|
217
|
+
active?.machine.disappear();
|
|
218
|
+
}
|
|
219
|
+
derive(list) {
|
|
220
|
+
const { query, scope, recentSearches, active } = this.state;
|
|
221
|
+
if (active === undefined || list === undefined)
|
|
222
|
+
return { query, scope, recentSearches, results: undefined };
|
|
223
|
+
const { key, machine } = active;
|
|
224
|
+
const results = {
|
|
225
|
+
key,
|
|
226
|
+
query: active.query,
|
|
227
|
+
scope: active.scope,
|
|
228
|
+
list: (0, controller_1.pagedListController)(machine, list),
|
|
229
|
+
};
|
|
230
|
+
return { query, scope, recentSearches, results };
|
|
231
|
+
}
|
|
232
|
+
update(change) {
|
|
233
|
+
this.state = { ...this.state, ...change };
|
|
234
|
+
this.emit();
|
|
235
|
+
}
|
|
236
|
+
emit() {
|
|
237
|
+
for (const listener of this.listeners)
|
|
238
|
+
listener();
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
exports.SearchMachine = SearchMachine;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { type RecentSearchStore } from "./recent-searches";
|
|
2
|
+
/** A change to the recent searches, applied to the list as it stands. */
|
|
3
|
+
type RecentSearchChange = (searches: readonly string[]) => readonly string[];
|
|
4
|
+
/**
|
|
5
|
+
* Keeps the recent searches in step with the app's store: loads them once, cleaned, saves every change in order, and
|
|
6
|
+
* merges changes made before a slow store answered into what it loaded before saving. `show` gets each new list.
|
|
7
|
+
*/
|
|
8
|
+
export declare class RecentSearchSync {
|
|
9
|
+
private readonly store;
|
|
10
|
+
private readonly limit;
|
|
11
|
+
private readonly show;
|
|
12
|
+
private current;
|
|
13
|
+
/** Changes made while the store's list is still loading; `undefined` once it has loaded. */
|
|
14
|
+
private changesBeforeLoad;
|
|
15
|
+
/** The last save, so the next one waits for it and an older list never lands after a newer one. */
|
|
16
|
+
private saving;
|
|
17
|
+
constructor(store: RecentSearchStore, limit: () => number, show: (searches: readonly string[]) => void);
|
|
18
|
+
get searches(): readonly string[];
|
|
19
|
+
/**
|
|
20
|
+
* Applies `change` to the store's list as it stands now, so screens sharing one store never undo each other's
|
|
21
|
+
* changes. A store that answers later is read once; its changes go on the list last loaded or saved.
|
|
22
|
+
*/
|
|
23
|
+
change(change: RecentSearchChange): void;
|
|
24
|
+
private latest;
|
|
25
|
+
private readonly loaded;
|
|
26
|
+
/** Keeps the changes made meanwhile, and reports the error on its own, as a failed save does. */
|
|
27
|
+
private readonly loadFailed;
|
|
28
|
+
private cleaned;
|
|
29
|
+
private set;
|
|
30
|
+
private save;
|
|
31
|
+
}
|
|
32
|
+
export {};
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RecentSearchSync = void 0;
|
|
4
|
+
const recent_searches_1 = require("./recent-searches");
|
|
5
|
+
function isStoredList(value) {
|
|
6
|
+
return Array.isArray(value);
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Keeps the recent searches in step with the app's store: loads them once, cleaned, saves every change in order, and
|
|
10
|
+
* merges changes made before a slow store answered into what it loaded before saving. `show` gets each new list.
|
|
11
|
+
*/
|
|
12
|
+
class RecentSearchSync {
|
|
13
|
+
store;
|
|
14
|
+
limit;
|
|
15
|
+
show;
|
|
16
|
+
current = [];
|
|
17
|
+
/** Changes made while the store's list is still loading; `undefined` once it has loaded. */
|
|
18
|
+
changesBeforeLoad;
|
|
19
|
+
/** The last save, so the next one waits for it and an older list never lands after a newer one. */
|
|
20
|
+
saving = Promise.resolve();
|
|
21
|
+
constructor(store, limit, show) {
|
|
22
|
+
this.store = store;
|
|
23
|
+
this.limit = limit;
|
|
24
|
+
this.show = show;
|
|
25
|
+
const stored = store.load();
|
|
26
|
+
if (isStoredList(stored)) {
|
|
27
|
+
this.current = this.cleaned(stored);
|
|
28
|
+
}
|
|
29
|
+
else {
|
|
30
|
+
this.changesBeforeLoad = [];
|
|
31
|
+
stored.then(this.loaded, this.loadFailed);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
get searches() {
|
|
35
|
+
return this.current;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Applies `change` to the store's list as it stands now, so screens sharing one store never undo each other's
|
|
39
|
+
* changes. A store that answers later is read once; its changes go on the list last loaded or saved.
|
|
40
|
+
*/
|
|
41
|
+
change(change) {
|
|
42
|
+
if (this.changesBeforeLoad !== undefined) {
|
|
43
|
+
// Saving now would overwrite the stored list before it is read; it is saved, merged, once it loads.
|
|
44
|
+
this.changesBeforeLoad.push(change);
|
|
45
|
+
this.set(change(this.current));
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
this.set(change(this.latest()));
|
|
49
|
+
this.save();
|
|
50
|
+
}
|
|
51
|
+
latest() {
|
|
52
|
+
const stored = this.store.load();
|
|
53
|
+
return isStoredList(stored) ? this.cleaned(stored) : this.current;
|
|
54
|
+
}
|
|
55
|
+
loaded = (stored) => {
|
|
56
|
+
const changes = this.changesBeforeLoad ?? [];
|
|
57
|
+
this.changesBeforeLoad = undefined;
|
|
58
|
+
this.set(changes.reduce((searches, change) => change(searches), this.cleaned(stored)));
|
|
59
|
+
if (changes.length > 0)
|
|
60
|
+
this.save();
|
|
61
|
+
};
|
|
62
|
+
/** Keeps the changes made meanwhile, and reports the error on its own, as a failed save does. */
|
|
63
|
+
loadFailed = (error) => {
|
|
64
|
+
this.changesBeforeLoad = undefined;
|
|
65
|
+
reportLater(error);
|
|
66
|
+
};
|
|
67
|
+
cleaned(searches) {
|
|
68
|
+
return (0, recent_searches_1.cleanedRecentSearches)(searches, this.limit());
|
|
69
|
+
}
|
|
70
|
+
set(searches) {
|
|
71
|
+
this.current = searches;
|
|
72
|
+
this.show(searches);
|
|
73
|
+
}
|
|
74
|
+
save() {
|
|
75
|
+
const searches = this.current;
|
|
76
|
+
const saved = this.saving.then(() => this.store.save(searches));
|
|
77
|
+
// A failed save doesn't stop the next one.
|
|
78
|
+
this.saving = saved.catch(reportLater);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
exports.RecentSearchSync = RecentSearchSync;
|
|
82
|
+
/** Throws `error` on its own, so it is reported without stopping what comes after. */
|
|
83
|
+
function reportLater(error) {
|
|
84
|
+
setTimeout(() => {
|
|
85
|
+
throw error;
|
|
86
|
+
});
|
|
87
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Where a search keeps its recent searches, most recent first. `load` may return a promise, so AsyncStorage or MMKV
|
|
3
|
+
* fit. Keep one store for the app's lifetime to share them between screens.
|
|
4
|
+
*/
|
|
5
|
+
export interface RecentSearchStore {
|
|
6
|
+
readonly load: () => readonly string[] | Promise<readonly string[]>;
|
|
7
|
+
readonly save: (searches: readonly string[]) => void | Promise<void>;
|
|
8
|
+
}
|
|
9
|
+
/** Recent searches kept in memory, gone when the app quits: the default store. */
|
|
10
|
+
export declare function createInMemoryRecentSearches(initial?: readonly string[]): RecentSearchStore;
|
|
11
|
+
/** The recent searches, with the actions the suggestions view needs: pick one (it searches at once), remove, clear. */
|
|
12
|
+
export interface RecentSearches {
|
|
13
|
+
readonly searches: readonly string[];
|
|
14
|
+
readonly pick: (query: string) => void;
|
|
15
|
+
readonly remove: (query: string) => void;
|
|
16
|
+
readonly clear: () => void;
|
|
17
|
+
}
|
|
18
|
+
/** `searches` with `query` first, in the spelling just used, and no other copy of it, cut to `limit`. */
|
|
19
|
+
export declare function addingRecentSearch(query: string, searches: readonly string[], limit: number): readonly string[];
|
|
20
|
+
export declare function removingRecentSearch(query: string, searches: readonly string[]): readonly string[];
|
|
21
|
+
/** A stored list with its blank entries and duplicates dropped, cut to `limit`. */
|
|
22
|
+
export declare function cleanedRecentSearches(searches: readonly string[], limit: number): readonly string[];
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createInMemoryRecentSearches = createInMemoryRecentSearches;
|
|
4
|
+
exports.addingRecentSearch = addingRecentSearch;
|
|
5
|
+
exports.removingRecentSearch = removingRecentSearch;
|
|
6
|
+
exports.cleanedRecentSearches = cleanedRecentSearches;
|
|
7
|
+
/** Recent searches kept in memory, gone when the app quits: the default store. */
|
|
8
|
+
function createInMemoryRecentSearches(initial = []) {
|
|
9
|
+
let searches = initial;
|
|
10
|
+
return {
|
|
11
|
+
load: () => searches,
|
|
12
|
+
save: (next) => {
|
|
13
|
+
searches = next;
|
|
14
|
+
},
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
function comparable(search) {
|
|
18
|
+
return search.trim().toLowerCase();
|
|
19
|
+
}
|
|
20
|
+
/** `searches` with `query` first, in the spelling just used, and no other copy of it, cut to `limit`. */
|
|
21
|
+
function addingRecentSearch(query, searches, limit) {
|
|
22
|
+
return [query.trim(), ...removingRecentSearch(query, searches)].slice(0, limit);
|
|
23
|
+
}
|
|
24
|
+
function removingRecentSearch(query, searches) {
|
|
25
|
+
const key = comparable(query);
|
|
26
|
+
return searches.filter((search) => comparable(search) !== key);
|
|
27
|
+
}
|
|
28
|
+
/** A stored list with its blank entries and duplicates dropped, cut to `limit`. */
|
|
29
|
+
function cleanedRecentSearches(searches, limit) {
|
|
30
|
+
const keys = new Set();
|
|
31
|
+
const kept = [];
|
|
32
|
+
for (const search of searches) {
|
|
33
|
+
const key = comparable(search);
|
|
34
|
+
if (key === "" || keys.has(key))
|
|
35
|
+
continue;
|
|
36
|
+
keys.add(key);
|
|
37
|
+
kept.push(search.trim());
|
|
38
|
+
}
|
|
39
|
+
return kept.slice(0, limit);
|
|
40
|
+
}
|