Skip to content

Commit

Permalink
feat: refresh results cache after changing config
Browse files Browse the repository at this point in the history
  • Loading branch information
matiasdaloia committed Feb 18, 2025
1 parent 9de6921 commit 57842a2
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 16 deletions.
11 changes: 11 additions & 0 deletions backend/repository/result_repository_json_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,22 @@ func NewResultRepositoryJsonImpl(fr utils.FileReader) *ResultRepositoryJsonImpl
log.Error().Err(err).Msg("Error loading initial cache")
}

config.GetInstance().RegisterListener(repo.onConfigChange)

go repo.watchForChanges()

return repo
}

func (r *ResultRepositoryJsonImpl) onConfigChange(newCfg *config.Config) {
r.mutex.Lock()
defer r.mutex.Unlock()

if err := r.refreshCache(); err != nil {
log.Error().Err(err).Msg("Error refreshing results cache after config change")
}
}

func (r *ResultRepositoryJsonImpl) watchForChanges() {
for {
select {
Expand Down
5 changes: 1 addition & 4 deletions frontend/src/components/CodeViewer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,8 @@ export default function CodeViewer({

useEffect(() => {
updateContent();
}, [content]);

useEffect(() => {
updateHighlight();
}, []);
}, [content]);

if (isError) {
return (
Expand Down
5 changes: 3 additions & 2 deletions frontend/src/components/ScanDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { Button } from '@/components/ui/button';
import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle } from '@/components/ui/dialog';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import { useResults } from '@/hooks/useResults';
import { withErrorHandling } from '@/lib/errors';
import useResultsStore from '@/modules/results/stores/useResultsStore';
import useConfigStore from '@/stores/useConfigStore';
Expand Down Expand Up @@ -67,7 +68,7 @@ export default function ScanDialog({ onOpenChange }: ScanDialogProps) {
const [advancedScanArgs, setAdvancedScanArgs] = useState<string[]>([]);
const [options, setOptions] = useState<Record<string, string | number | boolean | string[]>>({});

const fetchResults = useResultsStore((state) => state.fetchResults);
const { reset: resetResults } = useResults();
const setSelectedResults = useResultsStore((state) => state.setSelectedResults);

const handleSelectDirectory = withErrorHandling({
Expand Down Expand Up @@ -116,7 +117,7 @@ export default function ScanDialog({ onOpenChange }: ScanDialogProps) {
await ScanStream(cmdArgs);
await setScanRoot(directory);
setSelectedResults([]);
await fetchResults();
resetResults();
},
onError: () => {
toast({
Expand Down
8 changes: 4 additions & 4 deletions frontend/src/components/SelectResultsFile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,28 +23,28 @@

import { File } from 'lucide-react';

import { useResults } from '@/hooks/useResults';
import { withErrorHandling } from '@/lib/errors';
import { truncatePath } from '@/lib/utils';
import useResultsStore from '@/modules/results/stores/useResultsStore';
import useConfigStore from '@/stores/useConfigStore';

import { SelectFile } from '../../wailsjs/go/main/App';
import { Tooltip, TooltipContent, TooltipTrigger } from './ui/tooltip';
import { toast } from './ui/use-toast';

export default function SelectResultsFile() {
const fetchResults = useResultsStore((state) => state.fetchResults);

const scanRoot = useConfigStore((state) => state.scanRoot);
const resultsFile = useConfigStore((state) => state.resultsFile);
const setResultsFile = useConfigStore((state) => state.setResultsFile);

const { reset: resetResults } = useResults();

const handleSelectResultsFile = withErrorHandling({
asyncFn: async () => {
const file = await SelectFile(scanRoot ?? '.');
if (file) {
await setResultsFile(file);
await fetchResults();
resetResults();
}
},
onError: () => {
Expand Down
6 changes: 3 additions & 3 deletions frontend/src/components/SelectScanRoot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
import { useQueryClient } from '@tanstack/react-query';
import { Folder } from 'lucide-react';

import { useResults } from '@/hooks/useResults';
import useSelectedResult from '@/hooks/useSelectedResult';
import { withErrorHandling } from '@/lib/errors';
import { truncatePath } from '@/lib/utils';
import useResultsStore from '@/modules/results/stores/useResultsStore';
import useConfigStore from '@/stores/useConfigStore';

import { SelectDirectory } from '../../wailsjs/go/main/App';
Expand All @@ -42,14 +42,14 @@ export default function SelectScanRoot() {
const scanRoot = useConfigStore((state) => state.scanRoot);
const setScanRoot = useConfigStore((state) => state.setScanRoot);

const fetchResults = useResultsStore((state) => state.fetchResults);
const { reset: resetResults } = useResults();

const handleSelectScanRoot = withErrorHandling({
asyncFn: async () => {
const selectedDir = await SelectDirectory();
if (selectedDir) {
await setScanRoot(selectedDir);
await fetchResults();
resetResults();
await queryClient.invalidateQueries({
queryKey: ['localFileContent', selectedResult?.path],
});
Expand Down
6 changes: 3 additions & 3 deletions frontend/src/components/SelectSettingsFile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,17 @@

import { File } from 'lucide-react';

import { useResults } from '@/hooks/useResults';
import { withErrorHandling } from '@/lib/errors';
import { truncatePath } from '@/lib/utils';
import useResultsStore from '@/modules/results/stores/useResultsStore';
import useConfigStore from '@/stores/useConfigStore';

import { SelectFile } from '../../wailsjs/go/main/App';
import { Tooltip, TooltipContent, TooltipTrigger } from './ui/tooltip';
import { toast } from './ui/use-toast';

export default function SelectSettingsFile() {
const fetchResults = useResultsStore((state) => state.fetchResults);
const { reset: resetResults } = useResults();

const scanRoot = useConfigStore((state) => state.scanRoot);
const settingsFile = useConfigStore((state) => state.settingsFile);
Expand All @@ -44,7 +44,7 @@ export default function SelectSettingsFile() {
const file = await SelectFile(scanRoot ?? '.');
if (file) {
await setSettingsFile(file);
await fetchResults();
resetResults();
}
},
onError: () => {
Expand Down
1 change: 1 addition & 0 deletions frontend/src/hooks/useResults.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export const useResults = () => {
const query = useResultsStore((state) => state.query);
const sort = useResultsStore((state) => state.sort);
const fetchResults = useResultsStore((state) => state.fetchResults);

const setQuery = useResultsStore((state) => state.setQuery);

const debouncedQuery = useDebounce<string>(query, DEBOUNCE_QUERY_MS);
Expand Down

0 comments on commit 57842a2

Please sign in to comment.