From eb5bf6d94d4a3a07e42b2eed3ccdf28305ab7f44 Mon Sep 17 00:00:00 2001
From: familyfriendlymikey <ozmichaelt@icloud.com>
Date: Thu, 1 Sep 2022 21:49:53 -0400
Subject: [PATCH] refactor, client.imba done

---
 app/api.imba                     |  41 +++++++++-
 app/client.imba                  | 135 +++----------------------------
 app/components/app-edit.imba     |   2 +-
 app/components/app-links.imba    |  49 +++++++++--
 app/components/app-settings.imba |   7 +-
 app/db.imba                      |   2 +-
 app/utils.imba                   |  23 +++---
 7 files changed, 111 insertions(+), 148 deletions(-)

diff --git a/app/api.imba b/app/api.imba
index c91735d..48c4141 100644
--- a/app/api.imba
+++ b/app/api.imba
@@ -1,4 +1,5 @@
 let p = console.log
+import { err } from './utils'
 
 import db from './db'
 import state from './state'
@@ -6,7 +7,6 @@ import state from './state'
 import config from './config'
 p config
 import { omit, orderBy } from 'lodash'
-import { parse_url } from './utils'
 import { nanoid } from 'nanoid'
 import fzi from 'fzi'
 
@@ -78,6 +78,23 @@ export default new class api
 			return state.sorted_links = fzi state.links, state.query
 		state.sorted_links = fzi state.links, state.query, "display_name"
 
+	def add_initial_links
+		let initial_links = [
+			"tutorial github.com/familyfriendlymikey/fuzzyhome"
+			"!brave search `b search.brave.com/search?q="
+			"!youtube youtube.com/results?search_query="
+			"photopea photopea.com"
+			"twitch twitch.tv"
+			"messenger `me messenger.com"
+			"instagram `in instagram.com"
+			"localhost `3000 http://localhost:3000"
+		]
+		for link_text in initial_links
+			try
+				add_link link_text
+			catch e
+				err "adding link", e
+
 	def create_link_from_text text
 		text = text.trim!
 		throw "Text is empty." if text is ''
@@ -124,3 +141,25 @@ export default new class api
 		config.save!
 		sort_links!
 
+	def construct_link_text link
+		let link_text = ""
+		link_text += "!" if link.is_bang
+		link_text += link.display_name
+		link_text += " `{link.name}" if link.name isnt link.display_name
+		link_text += " {link.url}"
+		link_text
+
+	def parse_url url
+		throw "invalid url" if url === null
+		let get_url = do |s|
+			let url = new URL s
+			throw _ unless (url.host and url.href)
+			url
+		try
+			return get_url url
+		try
+			return get_url "https://{url}"
+		throw "invalid url"
+
+	def get_pretty_date
+		Date!.toString!.split(" ").slice(0, 4).join(" ")
diff --git a/app/client.imba b/app/client.imba
index 37dad10..f7910ae 100644
--- a/app/client.imba
+++ b/app/client.imba
@@ -1,8 +1,3 @@
-# TODO, deal with these 3:
-# editing_link = no
-# prior_query = ''
-# viewing_community_links = yes
-
 let p = console.log
 
 # import sw from './sw.imba?serviceworker'
@@ -10,12 +5,14 @@ let p = console.log
 
 import download from 'downloadjs'
 import { nanoid } from 'nanoid'
-import { evaluate as eval_math } from 'mathjs'
+import { err } from './utils'
 
 import pkg from '../package.json'
 let version = pkg.version
+p "fuzzyhome version {version}"
 import db from './db'
 import state from './state'
+let refs = {}
 import api from './api'
 import config from './config'
 
@@ -28,17 +25,6 @@ import app-link from './components/app-link'
 import app-bang from './components/app-bang'
 import './styles'
 
-p "fuzzyhome version {version}"
-
-global._fuzzyhome_delete_everything = do |prompt=yes|
-	return if prompt and window.confirm "This will delete everything. Are you sure?"
-	indexedDB.deleteDatabase("fuzzyhome")
-	delete localStorage.fuzzyhome_config
-	delete localStorage.fuzzyhome_visited
-	location.reload!
-
-let refs = {}
-
 extend tag element
 	get state
 		state
@@ -50,6 +36,8 @@ extend tag element
 		console.log
 	get refs
 		refs
+	get err
+		err
 
 tag app
 
@@ -58,12 +46,15 @@ tag app
 	get render? do mounted?
 
 	def mount
+
 		refs.settings = $as
 		refs.edit = $ae
 		refs.community-links = $acl
+
 		unless global.localStorage.fuzzyhome_visited
-			await add_initial_links!
+			await api.add_initial_links!
 			global.localStorage.fuzzyhome_visited = yes
+
 		try
 			await api.reload_db!
 			p "links:", state.links
@@ -72,111 +63,6 @@ tag app
 			fatal_error = yes
 			return
 
-	def add_initial_links
-		let initial_links = [
-			"tutorial github.com/familyfriendlymikey/fuzzyhome"
-			"!brave search `b search.brave.com/search?q="
-			"!youtube youtube.com/results?search_query="
-			"photopea photopea.com"
-			"twitch twitch.tv"
-			"messenger `me messenger.com"
-			"instagram `in instagram.com"
-			"localhost `3000 http://localhost:3000"
-		]
-		for link_text in initial_links
-			try
-				api.add_link link_text
-			catch e
-				err "adding link", e
-
-	def err s, e
-		p "error:"
-		p e
-		window.alert("Error {s}:\n\n{e}")
-
-	get selected_link
-		state.sorted_links[selection_index]
-
-	get tip_url
-		let split_query = state.query.trim!.split /\s+/
-		if split_query.length >= 2
-			return ' https://' + split_query.pop!
-		else
-			return ''
-
-	get tip_name
-		let split_query = state.query.trim!.split /\s+/
-		let name = split_query.join ' '
-		if split_query.length >= 2
-			split_query.pop!
-			if split_query[-1].startsWith '~'
-				split_query.pop!
-			name = split_query.join ' '
-			if name.startsWith '!'
-				name = name.slice(1)
-		name
-
-	get can_add
-		return no if state.loading
-		return no if settings_active
-		let query = state.query.trim!
-		return no if query is ''
-		let split_query = query.split /\s+/
-		return no if split_query.length < 2
-		yes
-
-	def handle_add
-		state.loading = yes
-		try
-			await api.add_link state.query
-			state.query = ''
-			sort_links!
-		catch e
-			err "adding link", e
-		state.loading = no
-
-	def construct_link_text link
-		let link_text = ""
-		link_text += "!" if link.is_bang
-		link_text += link.display_name
-		link_text += " `{link.name}" if link.name isnt link.display_name
-		link_text += " {link.url}"
-		link_text
-
-	def handle_esc
-		if editing_link
-			editing_link = no
-			state.query = prior_query
-			prior_query = ''
-		elif viewing_community_links
-			viewing_community_links = no
-		sort_links!
-
-	def name_exists new_name
-		state.links.some! do |{name}| new_name is name
-
-	def handle_paste e
-		return unless config.data.enable_search_on_paste
-		return if state.query.length > 0
-		global.setTimeout(&, 0) do
-			return if math_result isnt no
-			bang ||= config.data.default_bang
-			handle_bang!
-
-	def handle_click_copy s
-		try
-			await window.navigator.clipboard.writeText(s)
-			state.query = ''
-			sort_links!
-
-	def handle_cut e
-		return unless e.target.selectionStart == e.target.selectionEnd
-		let s = math_result
-		s ||= state.query
-		await window.navigator.clipboard.writeText(s)
-		state.query = ''
-		sort_links!
-
 	def render
 
 		<self.disabled=state.loading>
@@ -186,7 +72,8 @@ tag app
 				box-sizing:border-box p:30px rd:10px mt:10vh
 
 			if fatal_error
-				<[c:blue2]>
+				<.fatal>
+					css c:blue2
 					"""
 						There was an error state.loading the database.
 						This could be due to a user setting
diff --git a/app/components/app-edit.imba b/app/components/app-edit.imba
index ba28213..887f1c5 100644
--- a/app/components/app-edit.imba
+++ b/app/components/app-edit.imba
@@ -14,7 +14,7 @@ tag app-edit < app-prompt
 		<self>
 			<.tips>
 				<.tip
-					@click=handle_esc
+					@click=(active = no)
 				>
 					<.tip-hotkey> "Esc"
 					<.tip-content> "Cancel Edits"
diff --git a/app/components/app-links.imba b/app/components/app-links.imba
index 9be9fb4..879d92a 100644
--- a/app/components/app-links.imba
+++ b/app/components/app-links.imba
@@ -1,3 +1,5 @@
+import { evaluate as eval_math } from 'mathjs'
+
 tag app-links
 
 	selection_index = 0
@@ -7,12 +9,44 @@ tag app-links
 		p document
 		$links-input.focus!
 
+	def handle_paste e
+		return unless config.data.enable_search_on_paste
+		return if state.query.length > 0
+		global.setTimeout(&, 0) do
+			return if math_result isnt no
+			bang ||= config.data.default_bang
+			handle_bang!
+
+	def handle_click_copy s
+		try
+			await window.navigator.clipboard.writeText(s)
+			state.query = ''
+			sort_links!
+
+	def handle_cut e
+		return unless e.target.selectionStart == e.target.selectionEnd
+		let s = math_result
+		s ||= state.query
+		await window.navigator.clipboard.writeText(s)
+		state.query = ''
+		sort_links!
+
 	def increment_selection_index
 		selection_index = Math.min(state.sorted_links.length - 1, selection_index + 1)
 
 	def decrement_selection_index
 		selection_index = Math.max(0, selection_index - 1)
 
+	def handle_add
+		state.loading = yes
+		try
+			await api.add_link state.query
+			state.query = ''
+			sort_links!
+		catch e
+			err "adding link", e
+		state.loading = no
+
 	get math_result
 		try
 			let result = Number(eval_math state.query)
@@ -29,7 +63,7 @@ tag app-links
 	def handle_edit link
 		prior_query = state.query
 		editing_link = link
-		state.query = construct_link_text(link)
+		state.query = api.construct_link_text(link)
 
 	def make_edit link, new_link_text
 		def edit_link
@@ -117,10 +151,11 @@ tag app-links
 				<.header>
 					css d:flex fld:row w:100%
 					css .side c:purple3/90 fs:15px d:flex ja:center w:30px cursor:pointer
-					css .side svg w:15px
+					css .side svg w:15px d:flex
+					css .left jc:left
+					css .right jc:right
 
-					<.side@click=api.toggle_effective_names>
-						css d:flex jc:left
+					<.side.left@click=api.toggle_effective_names>
 
 						if config.data.enable_effective_names
 							<svg src="../assets/eye.svg">
@@ -147,9 +182,9 @@ tag app-links
 					>
 
 					if (let m = math_result) isnt no
-						<.side @click=handle_click_copy(m)>
+						<.side.right@click=handle_click_copy(m)>
 							"= {Math.round(m * 100)/100}"
-							css d:flex jc:right c:blue3 fs:20px ml:10px w:unset
+							css c:blue3 fs:20px ml:10px w:unset
 
 					else
 						<.side.right @click.if(!state.loading)=toggle_settings>
@@ -214,7 +249,7 @@ tag app-links
 										<span[c:blue3 ws:pre]> " {url}"
 										<span> '"'
 									else
-										<span> '"{sq.join " "}"'
+										<span> "\"{sq.join " "}\""
 
 							<.tip @click=handle_shift_backspace>
 								<.tip-hotkey> "Shift + Backspace"
diff --git a/app/components/app-settings.imba b/app/components/app-settings.imba
index d965fb2..bc72d7e 100644
--- a/app/components/app-settings.imba
+++ b/app/components/app-settings.imba
@@ -32,6 +32,10 @@ tag app-settings
 		settings_active = no
 
 	def handle_click_import e
+
+		def name_exists new_name
+			state.links.some! do |{name}| new_name is name
+
 		def handle_import
 			let errors = []
 			try
@@ -49,6 +53,7 @@ tag app-settings
 					errors.push "{link_text}\n{e}"
 			if errors.length > 0
 				err "importing some links", errors.join("\n\n")
+
 		loading = yes
 		await handle_import!
 		settings_active = no
@@ -58,7 +63,7 @@ tag app-settings
 		loading = yes
 		await reload_db!
 		let links = state.links.map do |link|
-			construct_link_text link
+			api.construct_link_text link
 		let datetime = new Date!.toString!.split(" ")
 		let date = datetime.slice(1, 4).join("-").toLowerCase!
 		let time = datetime[4].split(":").join("-")
diff --git a/app/db.imba b/app/db.imba
index 42ad57d..35a406b 100644
--- a/app/db.imba
+++ b/app/db.imba
@@ -2,6 +2,7 @@ let p = console.log
 import Dexie from 'dexie'
 import 'dexie-export-import'
 import { nanoid } from 'nanoid'
+import api from './api'
 
 let db = new Dexie 'fuzzyhome'
 
@@ -21,7 +22,6 @@ db.version(2).stores({
 		let img = link.img
 		this.value = { id, name, url, frequency, img }
 
-import { parse_url } from './utils'
 db.version(3).stores({
 	links: "++id,name,url,frequency,img"
 }).upgrade! do |trans|
diff --git a/app/utils.imba b/app/utils.imba
index a7c6e3b..e125ed1 100644
--- a/app/utils.imba
+++ b/app/utils.imba
@@ -1,14 +1,11 @@
-export def parse_url url
-	throw "invalid url" if url === null
-	let get_url = do |s|
-		let url = new URL s
-		throw _ unless (url.host and url.href)
-		url
-	try
-		return get_url url
-	try
-		return get_url "https://{url}"
-	throw "invalid url"
+export def err s, e
+	p "error:"
+	p e
+	window.alert("Error {s}:\n\n{e}")
 
-def get_pretty_date
-	Date!.toString!.split(" ").slice(0, 4).join(" ")
+global._fuzzyhome_delete_everything = do |prompt=yes|
+	return if prompt and window.confirm "This will delete everything. Are you sure?"
+	indexedDB.deleteDatabase("fuzzyhome")
+	delete localStorage.fuzzyhome_config
+	delete localStorage.fuzzyhome_visited
+	location.reload!