Skip to content

Commit 3411bc2

Browse files
committed
fix: load all gists
1 parent f12d65a commit 3411bc2

1 file changed

Lines changed: 57 additions & 33 deletions

File tree

src/app/blog/page.tsx

Lines changed: 57 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -104,42 +104,66 @@ export default function Blog() {
104104
const [error, setError] = useState<string | null>(null);
105105
const [searchTerm, setSearchTerm] = useState('');
106106

107-
useEffect(() => {
108-
const fetchGists = async () => {
109-
try {
110-
const response = await fetch('https://api.github.com/users/gbzarelli/gists');
111-
if (!response.ok) {
112-
throw new Error('Falha ao carregar os gists');
113-
}
114-
const data = await response.json();
107+
const fetchGists = async (pageNumber: number): Promise<{ gists: Gist[], hasMore: boolean }> => {
108+
try {
109+
const response = await fetch(`https://api.github.com/users/gbzarelli/gists?page=${pageNumber}&per_page=30`);
110+
if (!response.ok) {
111+
throw new Error('Falha ao carregar os gists');
112+
}
113+
114+
const data = await response.json();
115+
116+
// Filtra e processa os gists
117+
const helpdevGists = data
118+
.filter((gist: Gist) => gist.description?.includes('#helpdev-blog'))
119+
.map((gist: Gist) => {
120+
const rawTitle = gist.description?.replace('#helpdev-blog', '').trim() || 'Sem título';
121+
const firstFile = Object.values(gist.files)[0];
122+
const language = firstFile?.language || 'default';
123+
const fileName = firstFile?.filename.split('.')[0].replace(/-/g, ' ');
124+
const description = `${fileName} - ${language}`;
125+
126+
return {
127+
...gist,
128+
cleanTitle: rawTitle,
129+
cleanDescription: description,
130+
imageUrl: getImageForTitle(rawTitle)
131+
};
132+
});
133+
134+
// Se a página retornou menos que 30 gists, é a última página
135+
const hasMore = data.length === 30;
136+
137+
return { gists: helpdevGists, hasMore };
138+
} catch (err) {
139+
throw new Error(err instanceof Error ? err.message : 'Erro ao carregar os gists');
140+
}
141+
};
142+
143+
const loadAllGists = async () => {
144+
try {
145+
let currentPage = 1;
146+
let allGists: Gist[] = [];
147+
let hasMore = true;
148+
149+
while (hasMore) {
150+
const { gists: pageGists, hasMore: hasMoreGists } = await fetchGists(currentPage);
115151

116-
// Filtra e processa os gists
117-
const helpdevGists = data
118-
.filter((gist: Gist) => gist.description?.includes('#helpdev-blog'))
119-
.map((gist: Gist) => {
120-
const rawTitle = gist.description?.replace('#helpdev-blog', '').trim() || 'Sem título';
121-
const firstFile = Object.values(gist.files)[0];
122-
const language = firstFile?.language || 'default';
123-
const fileName = firstFile?.filename.split('.')[0].replace(/-/g, ' ');
124-
const description = `${fileName} - ${language}`;
125-
126-
return {
127-
...gist,
128-
cleanTitle: rawTitle,
129-
cleanDescription: description,
130-
imageUrl: getImageForTitle(rawTitle)
131-
};
132-
});
133-
134-
setGists(helpdevGists);
135-
setLoading(false);
136-
} catch (err) {
137-
setError(err instanceof Error ? err.message : 'Erro ao carregar os gists');
138-
setLoading(false);
152+
allGists = [...allGists, ...pageGists];
153+
hasMore = hasMoreGists;
154+
currentPage++;
139155
}
140-
};
141156

142-
fetchGists();
157+
setGists(allGists);
158+
setLoading(false);
159+
} catch (err) {
160+
setError(err instanceof Error ? err.message : 'Erro ao carregar os gists');
161+
setLoading(false);
162+
}
163+
};
164+
165+
useEffect(() => {
166+
loadAllGists();
143167
}, []);
144168

145169
const filteredGists = useMemo(() => {

0 commit comments

Comments
 (0)