From 3fdc014367375b914a3e483de957e98e75e4b466 Mon Sep 17 00:00:00 2001 From: Jacky Lai Date: Sun, 23 Feb 2025 13:25:10 -0500 Subject: [PATCH] Use kv instead of db to store username --- src/index.ts | 40 +++++----------------------------------- 1 file changed, 5 insertions(+), 35 deletions(-) diff --git a/src/index.ts b/src/index.ts index bfac699..5382ec6 100644 --- a/src/index.ts +++ b/src/index.ts @@ -5,7 +5,7 @@ import { IconEnum, List, Form, - db, + kv, open, fetch, toast, @@ -22,7 +22,7 @@ import { const LETTERBOXD_URL = "https://letterboxd.com" const LETTERBOXD_TMDB_URL = `${LETTERBOXD_URL}/tmdb` -const DB_SEARCH_TEXT = 'letterboxdUser' +const USERNAME_KEY = 'letterboxdUser' function convertActions(actions: any) { return actions.map((action: any) => { @@ -52,48 +52,18 @@ async function getFinalURL(initialURL: string) { } } -async function setLetterboxdUsername(username: string) { - const existingUsername = await db.search({ searchText: DB_SEARCH_TEXT }); - if (!username) { - if (existingUsername.length > 0) { - await db.delete(existingUsername[0].dataId); - } - } else if (existingUsername.length > 0) { - await db.update({ - dataId: existingUsername[0].dataId, - data: JSON.stringify({ letterboxdUser: username }), - }); - } else { - await db.add({ - data: JSON.stringify({ letterboxdUser: username }), - dataType: 'preference', - searchText: DB_SEARCH_TEXT, - }); - } -} - -async function getLetterboxdUsername() { - const existingUsername = await db.search({ searchText: DB_SEARCH_TEXT, fields: ['data', 'search_text'] }); - if (existingUsername.length > 0) { - if (existingUsername[0].data) { - return JSON.parse(existingUsername[0].data).letterboxdUser; - } - } - return null; -} - class LetterboxdCmd extends TemplateUiCommand { highlightedItem: string = "" highlightedMovieId: string = "" currentLetterboxdUsername: string = "" async onFormSubmit(value: Record): Promise { - await setLetterboxdUsername(value['clear-username'] ? '' : value.username) + await kv.set(USERNAME_KEY, value['clear-username'] ? '' : value.username) this.currentLetterboxdUsername = value.username toast.success("Letterboxd username " + value['clear-username'] ? 'removed.' : `set to: ${value.username}`) this.load() } async load() { - this.currentLetterboxdUsername = await getLetterboxdUsername(); + this.currentLetterboxdUsername = await kv.get(USERNAME_KEY) ?? ''; await ui.setSearchBarPlaceholder("Search for a movie..."); await ui.render( new List.List({ @@ -195,7 +165,7 @@ class LetterboxdCmd extends TemplateUiCommand { label: 'Letterboxd Username', description: 'Providing your Letterboxd username will enable additional actions like viewing your friends\' reviews and lists.', optional: true, - placeholder: await getLetterboxdUsername() ?? 'Enter your Letterboxd username' + placeholder: await kv.get(USERNAME_KEY) ?? 'Enter your Letterboxd username' }) ] if (this.currentLetterboxdUsername) {