@@ -20,14 +20,14 @@ export class LocalStoredShortcuts implements ShortcutApi {
|
||||
// (undocumented)
|
||||
add(shortcut: Omit<Shortcut, 'id'>): Promise<void>;
|
||||
// (undocumented)
|
||||
get(): Shortcut[];
|
||||
// (undocumented)
|
||||
getColor(url: string): string;
|
||||
// (undocumented)
|
||||
remove(id: string): Promise<void>;
|
||||
// (undocumented)
|
||||
shortcut$(): Observable_2<Shortcut[]>;
|
||||
// (undocumented)
|
||||
snapshot(): Shortcut[];
|
||||
// (undocumented)
|
||||
update(shortcut: Shortcut): Promise<void>;
|
||||
}
|
||||
|
||||
@@ -45,10 +45,10 @@ export type Shortcut = {
|
||||
// @public (undocumented)
|
||||
export interface ShortcutApi {
|
||||
add(shortcut: Omit<Shortcut, 'id'>): Promise<void>;
|
||||
get(): Shortcut[];
|
||||
getColor(url: string): string;
|
||||
remove(id: string): Promise<void>;
|
||||
shortcut$(): Observable<Shortcut[]>;
|
||||
snapshot(): Shortcut[];
|
||||
update(shortcut: Shortcut): Promise<void>;
|
||||
}
|
||||
|
||||
|
||||
@@ -39,10 +39,7 @@ export interface ShortcutsProps {
|
||||
|
||||
export const Shortcuts = (props: ShortcutsProps) => {
|
||||
const shortcutApi = useApi(shortcutsApiRef);
|
||||
const shortcuts = useObservable(
|
||||
shortcutApi.shortcut$(),
|
||||
shortcutApi.snapshot(),
|
||||
);
|
||||
const shortcuts = useObservable(shortcutApi.shortcut$(), shortcutApi.get());
|
||||
const [anchorEl, setAnchorEl] = React.useState<Element | undefined>();
|
||||
const loading = Boolean(!shortcuts);
|
||||
|
||||
|
||||
@@ -33,27 +33,27 @@ export class LocalStoredShortcuts implements ShortcutApi {
|
||||
);
|
||||
}
|
||||
|
||||
snapshot() {
|
||||
get() {
|
||||
return Array.from(
|
||||
this.storageApi.snapshot<Shortcut[]>('items').value ?? [],
|
||||
).sort((a, b) => (a.title >= b.title ? 1 : -1));
|
||||
}
|
||||
|
||||
async add(shortcut: Omit<Shortcut, 'id'>) {
|
||||
const shortcuts = this.snapshot();
|
||||
const shortcuts = this.get();
|
||||
shortcuts.push({ ...shortcut, id: uuid() });
|
||||
|
||||
await this.storageApi.set('items', shortcuts);
|
||||
}
|
||||
|
||||
async remove(id: string) {
|
||||
const shortcuts = this.snapshot().filter(s => s.id !== id);
|
||||
const shortcuts = this.get().filter(s => s.id !== id);
|
||||
|
||||
await this.storageApi.set('items', shortcuts);
|
||||
}
|
||||
|
||||
async update(shortcut: Shortcut) {
|
||||
const shortcuts = this.snapshot().filter(s => s.id !== shortcut.id);
|
||||
const shortcuts = this.get().filter(s => s.id !== shortcut.id);
|
||||
shortcuts.push(shortcut);
|
||||
|
||||
await this.storageApi.set('items', shortcuts);
|
||||
|
||||
@@ -31,7 +31,7 @@ export interface ShortcutApi {
|
||||
/**
|
||||
* Returns an immediate snapshot of shortcuts, sorted by title
|
||||
*/
|
||||
snapshot(): Shortcut[];
|
||||
get(): Shortcut[];
|
||||
|
||||
/**
|
||||
* Generates a unique id for the shortcut and then saves it.
|
||||
|
||||
Reference in New Issue
Block a user