Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 28 additions & 3 deletions custom/UniversalSearchInput.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
v-model="localValue"
type="text"
:placeholder="props.meta?.placeholder ?? ''"
class="border rounded px-2 py-1 text-sm w-64 dark:bg-gray-800 dark:border-gray-600"
class="border rounded px-2 py-1 text-sm dark:bg-gray-800 dark:border-gray-600"
:class="localValue ? 'w-[222px]' : 'w-64'"
@keyup.enter="applyImmediate"
/>
<button
Expand All @@ -20,21 +21,40 @@
</div>
</template>
<script setup lang="ts">
import { ref, watch } from 'vue';
import { ref, watch, onMounted } from 'vue';
import adminforth from '@/adminforth';
import { AdminForthFilterOperators } from '@/types/Common';
import { useRoute } from 'vue-router';

const route = useRoute();

const props = defineProps<{ meta?: any; resource?: any; adminUser?: any }>();
const localValue = ref('');
let t: any = null;
let blockFilterUpdate = false;
Copy link

Copilot AI Sep 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The blockFilterUpdate variable uses let but could be a ref() for better Vue 3 reactivity consistency with the rest of the component's reactive state.

Copilot uses AI. Check for mistakes.

onMounted(() => {
const filters = Object.keys(route.query).filter(k => k.startsWith('filter__')).map(k => {
const [_, field, operator] = k.split('__');
return {
field,
operator,
value: JSON.parse(decodeURIComponent(route.query[k] as string))
Comment on lines +39 to +42
Copy link

Copilot AI Sep 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The JSON.parse operation could throw an error if the query parameter contains invalid JSON. Consider wrapping it in a try-catch block or validating the input before parsing.

Suggested change
return {
field,
operator,
value: JSON.parse(decodeURIComponent(route.query[k] as string))
let value;
try {
value = JSON.parse(decodeURIComponent(route.query[k] as string));
} catch (e) {
value = undefined; // or '' if you prefer empty string
}
return {
field,
operator,
value

Copilot uses AI. Check for mistakes.
}
});
const isUniversalSearchFilterApplied = filters.find(f => f.field === '_universal_search');
if (isUniversalSearchFilterApplied) {
localValue.value = isUniversalSearchFilterApplied.value;
blockFilterUpdate = true;
};
});

function send(term?: string) {
adminforth?.list?.updateFilter?.({
field: '_universal_search',
operator: AdminForthFilterOperators.EQ,
value: term || '',
});
adminforth?.list?.refresh?.();
}

function apply() {
Expand All @@ -49,12 +69,17 @@ function applyImmediate() {
}

watch(localValue, () => {
if (blockFilterUpdate) {
blockFilterUpdate = false;
return;
}
const delay = props.meta?.debounceMs ?? 500;
if (t) clearTimeout(t);
t = setTimeout(apply, delay);
});

function clear() {
blockFilterUpdate = true;
localValue.value = '';
applyImmediate();
}
Expand Down