diff --git a/src/composables/useSearchHistory.ts b/src/composables/useSearchHistory.ts new file mode 100644 index 0000000..6e10fcc --- /dev/null +++ b/src/composables/useSearchHistory.ts @@ -0,0 +1,48 @@ +import { ref, readonly } from 'vue' + +const STORAGE_KEY = 'polyclient_search_history' +const MAX_HISTORY = 10 + +const history = ref([]) + +function loadHistory() { + try { + const raw = localStorage.getItem(STORAGE_KEY) + history.value = raw ? JSON.parse(raw) : [] + } catch { + history.value = [] + } +} + +function saveHistory() { + try { + localStorage.setItem(STORAGE_KEY, JSON.stringify(history.value)) + } catch { + // ignore + } +} + +export function useSearchHistory() { + const list = readonly(history) + + function add(keyword: string) { + const k = keyword.trim() + if (!k) return + history.value = [k, ...history.value.filter((h) => h !== k)].slice(0, MAX_HISTORY) + saveHistory() + } + + function remove(index: number) { + history.value = history.value.filter((_, i) => i !== index) + saveHistory() + } + + function clearAll() { + history.value = [] + saveHistory() + } + + loadHistory() + + return { list, add, remove, clearAll } +} diff --git a/src/views/Home.vue b/src/views/Home.vue index 443e3cb..932374a 100644 --- a/src/views/Home.vue +++ b/src/views/Home.vue @@ -1,18 +1,90 @@