-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implemented: Gitbook lens search feature #24
Closed
Closed
Changes from 8 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
5270ae0
Implemented: gitbook search modal in the product store page
amansinghbais 0af56e9
Implemented: feature to fetch and redirect to source pages
amansinghbais e467fa4
Improved: empty state handling on search
amansinghbais 40ba00c
Improved: resetting sources fetch result on query change
amansinghbais d6a2c5d
Implemented: markdown parser to parse a string
amansinghbais 2065cb5
Improved: logic for searching and asking query
amansinghbais dd3436b
Improved: entry in the env.example (#23)
amansinghbais f902dd5
Improved: positioning of the searchbar in lens search modal (#23)
amansinghbais b912832
Improrved: added entry for the gitbook api baseUrl, improved method n…
amansinghbais ad19a30
Improved: added slots in icons, method name for loading (#23)
amansinghbais 3643e6a
Improved: axios request intercept to avoid sending api_key in case of…
amansinghbais File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,254 @@ | ||
<template> | ||
<ion-header> | ||
<ion-toolbar> | ||
<ion-buttons slot="start"> | ||
<ion-button @click="closeModal"> | ||
<ion-icon :icon="close" /> | ||
</ion-button> | ||
</ion-buttons> | ||
<ion-title>{{ translate("Ask me anything?") }}</ion-title> | ||
</ion-toolbar> | ||
</ion-header> | ||
|
||
<ion-content> | ||
<ion-searchbar :placeholder="translate(selectedSegment === 'search' ? 'Search...': 'Ask...')" :value="queryString" @keyup.enter="queryString = $event.target.value; selectedSegment === 'search' ? searchQuery() : askQuery()" /> | ||
|
||
<ion-segment v-model="selectedSegment" @ionChange="updateSegment()"> | ||
<ion-segment-button value="search"> | ||
<ion-label>{{ translate("Search") }}</ion-label> | ||
</ion-segment-button> | ||
<ion-segment-button value="ask"> | ||
<ion-label>{{ translate("Ask") }}</ion-label> | ||
</ion-segment-button> | ||
</ion-segment> | ||
|
||
<div class="empty-state" v-if="isQueryFetching"> | ||
<ion-item lines="none"> | ||
<ion-spinner name="crescent" slot="start" /> | ||
{{ translate(selectedSegment === 'search' ? "Searching your query." : "Analyzing the question to answer your question.") }} | ||
</ion-item> | ||
</div> | ||
|
||
<template v-else> | ||
<template v-if="selectedSegment === 'search'"> | ||
<template v-if="searchedItems.length"> | ||
<div v-for="(item, index) in searchedItems" :key="index"> | ||
<ion-item> | ||
<ion-icon :icon="documentOutline" slot="start" /> | ||
<ion-label>{{ item.title }}</ion-label> | ||
<ion-button fill="clear" color="medium" slot="end" @click="redirectToDoc(item)"> | ||
<ion-icon :icon="returnDownBackOutline" slot="start" /> | ||
{{ translate("go to page") }} | ||
</ion-button> | ||
</ion-item> | ||
|
||
<template v-if="item.sections.length"> | ||
<ion-item v-for="(section, index) in item.sections" :key="index" class="ion-padding-left"> | ||
<ion-icon :icon="returnDownForwardOutline" slot="start" /> | ||
<ion-label> | ||
<div class="item-header"> | ||
<vue-markdown :source="section.title ? section.title : item.title" /> | ||
<ion-button fill="clear" color="medium" slot="end" @click="redirectToDoc(section)"> | ||
<ion-icon :icon="returnDownBackOutline" slot="start" /> | ||
{{ translate("go to section") }} | ||
</ion-button> | ||
</div> | ||
<p class="truncate"><vue-markdown :source="section.body" /></p> | ||
</ion-label> | ||
</ion-item> | ||
</template> | ||
</div> | ||
</template> | ||
<div class="empty-state" v-else-if="queryString"> | ||
<ion-item lines="none"> | ||
{{ translate("No page found.") }} | ||
</ion-item> | ||
</div> | ||
</template> | ||
|
||
<template v-else-if="selectedSegment === 'ask'"> | ||
<template v-if="answer && Object.keys(answer).length"> | ||
<ion-item lines="none"> | ||
<ion-label>{{ queryString }}</ion-label> | ||
</ion-item> | ||
<ion-item> | ||
<ion-label> | ||
<p><vue-markdown :source="answer.text" /></p> | ||
</ion-label> | ||
</ion-item> | ||
|
||
<ion-item v-if="answer.sources.length" button @click="!isResourceFetched ? fetchSources() : ''"> | ||
<ion-label> | ||
<p>{{ translate("Answer based on resources", { count: answer.sources.length }) }}</p> | ||
</ion-label> | ||
<ion-icon :icon="sources.length ? caretDownOutline : caretForwardOutline" /> | ||
</ion-item> | ||
|
||
<div class="empty-state" v-if="isResourceLoading"> | ||
<ion-item lines="none"> | ||
<ion-spinner name="crescent" slot="start" /> | ||
{{ translate("Fetching resources...") }} | ||
</ion-item> | ||
</div> | ||
|
||
<ion-list v-else-if="sources.length"> | ||
<ion-item v-for="source in sources" :key="source.title"> | ||
<ion-label>{{ source.title }}</ion-label> | ||
<ion-button fill="clear" color="medium" slot="end" @click="redirectToDoc(source)"> | ||
<ion-icon :icon="returnDownBackOutline" slot="start" /> | ||
{{ translate("go to page") }} | ||
</ion-button> | ||
</ion-item> | ||
</ion-list> | ||
|
||
<ion-item v-else-if="isResourceFetched"> | ||
<ion-label> | ||
<p>{{ translate("No resource found.") }}</p> | ||
</ion-label> | ||
</ion-item> | ||
|
||
<template v-if="answer.followupQuestions.length"> | ||
<ion-item lines="none"> | ||
<ion-label> | ||
<p>{{ translate("Related Queries") }}</p> | ||
</ion-label> | ||
</ion-item> | ||
|
||
<ion-item lines="none" v-for="(question, index) in answer.followupQuestions" :key="index" @click="searchRelatedQuestion(question)"> | ||
<ion-chip> | ||
<ion-icon :icon="searchOutline" /> | ||
<ion-label>{{ question }}</ion-label> | ||
</ion-chip> | ||
</ion-item> | ||
</template> | ||
</template> | ||
<div class="empty-state" v-else-if="queryString"> | ||
<ion-item lines="none"> | ||
{{ translate("No answer found.") }} | ||
</ion-item> | ||
</div> | ||
</template> | ||
</template> | ||
|
||
|
||
</ion-content> | ||
</template> | ||
|
||
<script setup lang="ts"> | ||
import { IonButton, IonButtons, IonChip, IonContent, IonHeader, IonIcon, IonItem, IonLabel, IonList, IonSearchbar, IonSegment, IonSegmentButton, IonSpinner, IonTitle, IonToolbar, modalController } from "@ionic/vue"; | ||
import { ref } from "vue"; | ||
import { caretForwardOutline, caretDownOutline, close, documentOutline, returnDownBackOutline, returnDownForwardOutline, searchOutline } from "ionicons/icons"; | ||
import { translate } from "@/i18n" | ||
import { UtilService } from "@/services/UtilService"; | ||
import { hasError } from "@/utils"; | ||
import VueMarkdown from 'vue-markdown-render' | ||
import logger from "@/logger"; | ||
|
||
const selectedSegment = ref("search"); | ||
let queryString = ref("") | ||
let searchedItems = ref([]) as any; | ||
const answer = ref({}) as any; | ||
|
||
const isQueryFetching = ref(false); | ||
|
||
const isResourceLoading = ref(false); | ||
const sources = ref([]) as any; | ||
const isResourceFetched = ref(false); | ||
|
||
function closeModal() { | ||
modalController.dismiss({ dismissed: true }); | ||
} | ||
|
||
function updateSegment() { | ||
queryString.value = ""; | ||
searchedItems.value = []; | ||
answer.value = {}; | ||
} | ||
|
||
async function askQuery() { | ||
isQueryFetching.value = true; | ||
let response = {} as any; | ||
|
||
try { | ||
const resp = await UtilService.askQuery({ queryString: queryString.value }); | ||
|
||
if(!hasError(resp)) { | ||
response = resp.data.answer; | ||
sources.value = [] | ||
isResourceFetched.value = false | ||
} else { | ||
throw resp.data; | ||
} | ||
} catch(error: any) { | ||
logger.error(error); | ||
} | ||
|
||
answer.value = response; | ||
isQueryFetching.value = false; | ||
} | ||
|
||
async function searchQuery() { | ||
isQueryFetching.value = true; | ||
ymaheshwari1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let items = [] as any; | ||
|
||
try { | ||
const resp = await UtilService.searchQuery({ queryString: queryString.value }); | ||
if(!hasError(resp)) { | ||
items = resp.data.items; | ||
} else { | ||
throw resp.data; | ||
} | ||
} catch(error: any) { | ||
logger.error(error); | ||
} | ||
|
||
searchedItems.value = items | ||
isQueryFetching.value = false; | ||
} | ||
|
||
async function fetchSources() { | ||
isResourceLoading.value = true; | ||
const list = [] as any; | ||
|
||
const responses = await Promise.allSettled(answer.value.sources.map((source: any) => { | ||
if(source.type === "page") { | ||
return UtilService.getGitboookPage(source.page); | ||
} | ||
})) | ||
|
||
responses.map((response: any) => { | ||
if(response.status === "fulfilled") { | ||
list.push(response.value.data) | ||
} | ||
}) | ||
sources.value = list | ||
isResourceLoading.value = false | ||
isResourceFetched.value = true | ||
} | ||
|
||
function redirectToDoc(item: any) { | ||
window.open(`https://docs.hotwax.co/user-guides/${item.path}`, "_blank") | ||
} | ||
|
||
function searchRelatedQuestion(question: string) { | ||
queryString.value = question; | ||
askQuery() | ||
} | ||
|
||
</script> | ||
|
||
<style scoped> | ||
.item-header { | ||
display: flex; | ||
justify-content: space-between | ||
} | ||
|
||
.truncate { | ||
overflow: hidden; | ||
text-overflow: ellipsis; | ||
display: -webkit-box; | ||
-webkit-line-clamp: 2; /* number of lines to show */ | ||
line-clamp: 2; | ||
-webkit-box-orient: vertical; | ||
} | ||
</style> |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add slot icon-only